use of cn.taketoday.http.client.ClientHttpRequestInitializer in project today-infrastructure by TAKETODAY.
the class RestTemplateBuilderTests method customizerShouldBeAppliedAtTheEnd.
@Test
void customizerShouldBeAppliedAtTheEnd() {
ResponseErrorHandler errorHandler = mock(ResponseErrorHandler.class);
ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
this.builder.interceptors(this.interceptor).messageConverters(this.messageConverter).rootUri("http://localhost:8080").errorHandler(errorHandler).basicAuthentication("spring", "boot").requestFactory(() -> requestFactory).customizers((restTemplate) -> {
assertThat(restTemplate.getInterceptors()).hasSize(1);
assertThat(restTemplate.getMessageConverters()).contains(this.messageConverter);
assertThat(restTemplate.getUriTemplateHandler()).isInstanceOf(RootUriTemplateHandler.class);
assertThat(restTemplate.getErrorHandler()).isEqualTo(errorHandler);
ClientHttpRequestFactory actualRequestFactory = restTemplate.getRequestFactory();
assertThat(actualRequestFactory).isInstanceOf(InterceptingClientHttpRequestFactory.class);
ClientHttpRequestInitializer initializer = restTemplate.getHttpRequestInitializers().get(0);
assertThat(initializer).isInstanceOf(RestTemplateBuilderClientHttpRequestInitializer.class);
}).build();
}
use of cn.taketoday.http.client.ClientHttpRequestInitializer in project today-framework by TAKETODAY.
the class HttpAccessor method createRequest.
/**
* Create a new {@link ClientHttpRequest} via this template's {@link ClientHttpRequestFactory}.
*
* @param url the URL to connect to
* @param method the HTTP method to execute (GET, POST, etc)
* @return the created request
* @throws IOException in case of I/O errors
* @see #getRequestFactory()
* @see ClientHttpRequestFactory#createRequest(URI, HttpMethod)
*/
protected ClientHttpRequest createRequest(URI url, HttpMethod method) throws IOException {
ClientHttpRequest request = getRequestFactory().createRequest(url, method);
List<ClientHttpRequestInitializer> requestInitializers = getHttpRequestInitializers();
if (!requestInitializers.isEmpty()) {
for (ClientHttpRequestInitializer initializer : requestInitializers) {
initializer.initialize(request);
}
}
if (isDebugEnabled) {
logger.debug("HTTP {} {}", method.name(), url);
}
return request;
}
use of cn.taketoday.http.client.ClientHttpRequestInitializer in project today-framework by TAKETODAY.
the class RestTemplateTests method clientHttpRequestInitializerAndRequestInterceptorAreBothApplied.
@Test
void clientHttpRequestInitializerAndRequestInterceptorAreBothApplied() throws Exception {
ClientHttpRequestInitializer initializer = request -> request.getHeaders().add("MyHeader", "MyInitializerValue");
ClientHttpRequestInterceptor interceptor = (request, body, execution) -> {
request.getHeaders().add("MyHeader", "MyInterceptorValue");
return execution.execute(request, body);
};
template.setHttpRequestInitializers(Collections.singletonList(initializer));
template.setInterceptors(interceptor);
MediaType contentType = MediaType.TEXT_PLAIN;
given(converter.canWrite(String.class, contentType)).willReturn(true);
HttpHeaders requestHeaders = HttpHeaders.create();
mockSentRequest(POST, "https://example.com", requestHeaders);
mockResponseStatus(HttpStatus.OK);
HttpHeaders entityHeaders = HttpHeaders.create();
entityHeaders.setContentType(contentType);
HttpEntity<String> entity = new HttpEntity<>("Hello World", entityHeaders);
template.exchange("https://example.com", POST, entity, Void.class);
assertThat(requestHeaders.get("MyHeader")).contains("MyInterceptorValue", "MyInitializerValue");
verify(response).close();
}
use of cn.taketoday.http.client.ClientHttpRequestInitializer in project today-framework by TAKETODAY.
the class RestTemplateBuilderTests method customizerShouldBeAppliedAtTheEnd.
@Test
void customizerShouldBeAppliedAtTheEnd() {
ResponseErrorHandler errorHandler = mock(ResponseErrorHandler.class);
ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
this.builder.interceptors(this.interceptor).messageConverters(this.messageConverter).rootUri("http://localhost:8080").errorHandler(errorHandler).basicAuthentication("spring", "boot").requestFactory(() -> requestFactory).customizers((restTemplate) -> {
assertThat(restTemplate.getInterceptors()).hasSize(1);
assertThat(restTemplate.getMessageConverters()).contains(this.messageConverter);
assertThat(restTemplate.getUriTemplateHandler()).isInstanceOf(RootUriTemplateHandler.class);
assertThat(restTemplate.getErrorHandler()).isEqualTo(errorHandler);
ClientHttpRequestFactory actualRequestFactory = restTemplate.getRequestFactory();
assertThat(actualRequestFactory).isInstanceOf(InterceptingClientHttpRequestFactory.class);
ClientHttpRequestInitializer initializer = restTemplate.getHttpRequestInitializers().get(0);
assertThat(initializer).isInstanceOf(RestTemplateBuilderClientHttpRequestInitializer.class);
}).build();
}
use of cn.taketoday.http.client.ClientHttpRequestInitializer in project today-infrastructure by TAKETODAY.
the class HttpAccessor method createRequest.
/**
* Create a new {@link ClientHttpRequest} via this template's {@link ClientHttpRequestFactory}.
*
* @param url the URL to connect to
* @param method the HTTP method to execute (GET, POST, etc)
* @return the created request
* @throws IOException in case of I/O errors
* @see #getRequestFactory()
* @see ClientHttpRequestFactory#createRequest(URI, HttpMethod)
*/
protected ClientHttpRequest createRequest(URI url, HttpMethod method) throws IOException {
ClientHttpRequest request = getRequestFactory().createRequest(url, method);
List<ClientHttpRequestInitializer> requestInitializers = getHttpRequestInitializers();
if (!requestInitializers.isEmpty()) {
for (ClientHttpRequestInitializer initializer : requestInitializers) {
initializer.initialize(request);
}
}
if (logger.isDebugEnabled()) {
logger.debug("HTTP {} {}", method.name(), url);
}
return request;
}
Aggregations