Search in sources :

Example 1 with ClientHttpRequestInitializer

use of org.springframework.http.client.ClientHttpRequestInitializer in project spring-framework by spring-projects.

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.setClientHttpRequestInitializers(Collections.singletonList(initializer));
    template.setInterceptors(Collections.singletonList(interceptor));
    MediaType contentType = MediaType.TEXT_PLAIN;
    given(converter.canWrite(String.class, contentType)).willReturn(true);
    HttpHeaders requestHeaders = new HttpHeaders();
    mockSentRequest(POST, "https://example.com", requestHeaders);
    mockResponseStatus(HttpStatus.OK);
    HttpHeaders entityHeaders = new HttpHeaders();
    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();
}
Also used : ClientHttpRequestInitializer(org.springframework.http.client.ClientHttpRequestInitializer) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) GET(org.springframework.http.HttpMethod.GET) PATCH(org.springframework.http.HttpMethod.PATCH) ByteArrayInputStream(java.io.ByteArrayInputStream) BDDMockito.given(org.mockito.BDDMockito.given) Map(java.util.Map) MockWebServer(okhttp3.mockwebserver.MockWebServer) HEAD(org.springframework.http.HttpMethod.HEAD) URI(java.net.URI) OPTIONS(org.springframework.http.HttpMethod.OPTIONS) BDDMockito.willThrow(org.mockito.BDDMockito.willThrow) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) POST(org.springframework.http.HttpMethod.POST) HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) Set(java.util.Set) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) List(java.util.List) HttpEntity(org.springframework.http.HttpEntity) ClientHttpRequestInterceptor(org.springframework.http.client.ClientHttpRequestInterceptor) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) MediaType.parseMediaType(org.springframework.http.MediaType.parseMediaType) Entry(java.util.Map.Entry) MockResponse(okhttp3.mockwebserver.MockResponse) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) ClientHttpRequestFactory(org.springframework.http.client.ClientHttpRequestFactory) HashMap(java.util.HashMap) DELETE(org.springframework.http.HttpMethod.DELETE) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest) ClientHttpRequestInitializer(org.springframework.http.client.ClientHttpRequestInitializer) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) DefaultUriBuilderFactory(org.springframework.web.util.DefaultUriBuilderFactory) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) PUT(org.springframework.http.HttpMethod.PUT) StreamUtils(org.springframework.util.StreamUtils) SimpleClientHttpRequestFactory(org.springframework.http.client.SimpleClientHttpRequestFactory) HttpMethod(org.springframework.http.HttpMethod) IOException(java.io.IOException) Mockito.verify(org.mockito.Mockito.verify) HttpStatus(org.springframework.http.HttpStatus) HttpInputMessage(org.springframework.http.HttpInputMessage) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) ResponseEntity(org.springframework.http.ResponseEntity) Collections(java.util.Collections) GenericHttpMessageConverter(org.springframework.http.converter.GenericHttpMessageConverter) HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) MediaType(org.springframework.http.MediaType) MediaType.parseMediaType(org.springframework.http.MediaType.parseMediaType) ClientHttpRequestInterceptor(org.springframework.http.client.ClientHttpRequestInterceptor) Test(org.junit.jupiter.api.Test)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 URI (java.net.URI)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 MockResponse (okhttp3.mockwebserver.MockResponse)1 MockWebServer (okhttp3.mockwebserver.MockWebServer)1 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)1 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 Test (org.junit.jupiter.api.Test)1 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)1