use of cn.taketoday.http.client.ClientHttpRequestInterceptor in project today-infrastructure by TAKETODAY.
the class RestTemplateTests method requestInterceptorCanAddExistingHeaderValueWithoutBody.
@Test
// SPR-15066
void requestInterceptorCanAddExistingHeaderValueWithoutBody() throws Exception {
ClientHttpRequestInterceptor interceptor = (request, body, execution) -> {
request.getHeaders().add("MyHeader", "MyInterceptorValue");
return execution.execute(request, body);
};
template.setInterceptors(interceptor);
HttpHeaders requestHeaders = HttpHeaders.create();
mockSentRequest(POST, "https://example.com", requestHeaders);
mockResponseStatus(HttpStatus.OK);
HttpHeaders entityHeaders = HttpHeaders.create();
entityHeaders.add("MyHeader", "MyEntityValue");
HttpEntity<Void> entity = new HttpEntity<>(null, entityHeaders);
template.exchange("https://example.com", POST, entity, Void.class);
assertThat(requestHeaders.get("MyHeader")).contains("MyEntityValue", "MyInterceptorValue");
verify(response).close();
}
use of cn.taketoday.http.client.ClientHttpRequestInterceptor in project today-infrastructure by TAKETODAY.
the class RestTemplateTests method requestInterceptorCanAddExistingHeaderValueWithBody.
@Test
// SPR-15066
void requestInterceptorCanAddExistingHeaderValueWithBody() throws Exception {
ClientHttpRequestInterceptor interceptor = (request, body, execution) -> {
request.getHeaders().add("MyHeader", "MyInterceptorValue");
return execution.execute(request, body);
};
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);
entityHeaders.add("MyHeader", "MyEntityValue");
HttpEntity<String> entity = new HttpEntity<>("Hello World", entityHeaders);
template.exchange("https://example.com", POST, entity, Void.class);
assertThat(requestHeaders.get("MyHeader")).contains("MyEntityValue", "MyInterceptorValue");
verify(response).close();
}
use of cn.taketoday.http.client.ClientHttpRequestInterceptor in project today-framework by TAKETODAY.
the class RestTemplateTests method requestInterceptorCanAddExistingHeaderValueWithBody.
@Test
// SPR-15066
void requestInterceptorCanAddExistingHeaderValueWithBody() throws Exception {
ClientHttpRequestInterceptor interceptor = (request, body, execution) -> {
request.getHeaders().add("MyHeader", "MyInterceptorValue");
return execution.execute(request, body);
};
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);
entityHeaders.add("MyHeader", "MyEntityValue");
HttpEntity<String> entity = new HttpEntity<>("Hello World", entityHeaders);
template.exchange("https://example.com", POST, entity, Void.class);
assertThat(requestHeaders.get("MyHeader")).contains("MyEntityValue", "MyInterceptorValue");
verify(response).close();
}
use of cn.taketoday.http.client.ClientHttpRequestInterceptor in project today-framework by TAKETODAY.
the class RestTemplateTests method requestInterceptorCanAddExistingHeaderValueWithoutBody.
@Test
// SPR-15066
void requestInterceptorCanAddExistingHeaderValueWithoutBody() throws Exception {
ClientHttpRequestInterceptor interceptor = (request, body, execution) -> {
request.getHeaders().add("MyHeader", "MyInterceptorValue");
return execution.execute(request, body);
};
template.setInterceptors(interceptor);
HttpHeaders requestHeaders = HttpHeaders.create();
mockSentRequest(POST, "https://example.com", requestHeaders);
mockResponseStatus(HttpStatus.OK);
HttpHeaders entityHeaders = HttpHeaders.create();
entityHeaders.add("MyHeader", "MyEntityValue");
HttpEntity<Void> entity = new HttpEntity<>(null, entityHeaders);
template.exchange("https://example.com", POST, entity, Void.class);
assertThat(requestHeaders.get("MyHeader")).contains("MyEntityValue", "MyInterceptorValue");
verify(response).close();
}
use of cn.taketoday.http.client.ClientHttpRequestInterceptor 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();
}
Aggregations