Search in sources :

Example 1 with SimpleClientHttpRequestFactory

use of cn.taketoday.http.client.SimpleClientHttpRequestFactory in project today-infrastructure by TAKETODAY.

the class RestTemplateBuilderTests method bufferRequestBodyCanBeConfiguredOnAWrappedRequestFactory.

@Test
void bufferRequestBodyCanBeConfiguredOnAWrappedRequestFactory() {
    SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
    this.builder.requestFactory(() -> new BufferingClientHttpRequestFactory(requestFactory)).setBufferRequestBody(false).build();
    assertThat(requestFactory).hasFieldOrPropertyWithValue("bufferRequestBody", false);
    this.builder.requestFactory(() -> new BufferingClientHttpRequestFactory(requestFactory)).setBufferRequestBody(true).build();
    assertThat(requestFactory).hasFieldOrPropertyWithValue("bufferRequestBody", true);
    this.builder.requestFactory(() -> new BufferingClientHttpRequestFactory(requestFactory)).build();
    assertThat(requestFactory).hasFieldOrPropertyWithValue("bufferRequestBody", true);
}
Also used : SimpleClientHttpRequestFactory(cn.taketoday.http.client.SimpleClientHttpRequestFactory) BufferingClientHttpRequestFactory(cn.taketoday.http.client.BufferingClientHttpRequestFactory) Test(org.junit.jupiter.api.Test)

Example 2 with SimpleClientHttpRequestFactory

use of cn.taketoday.http.client.SimpleClientHttpRequestFactory in project today-infrastructure by TAKETODAY.

the class RestTemplateTests method keepGivenAcceptHeaderOnPut.

@Test
// gh-23740
void keepGivenAcceptHeaderOnPut() throws Exception {
    try (MockWebServer server = new MockWebServer()) {
        server.enqueue(new MockResponse().setResponseCode(500).setBody("internal server error"));
        server.start();
        HttpHeaders headers = HttpHeaders.create();
        headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
        HttpEntity<String> entity = new HttpEntity<>(headers);
        template.setRequestFactory(new SimpleClientHttpRequestFactory());
        template.exchange(server.url("/internal/server/error").uri(), PUT, entity, Void.class);
        RecordedRequest request = server.takeRequest();
        final List<List<String>> accepts = request.getHeaders().toMultimap().entrySet().stream().filter(entry -> entry.getKey().equalsIgnoreCase("accept")).map(Entry::getValue).collect(Collectors.toList());
        assertThat(accepts).hasSize(1);
        assertThat(accepts.get(0)).hasSize(1);
        assertThat(accepts.get(0).get(0)).isEqualTo("application/json");
    }
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) HttpHeaders(cn.taketoday.http.HttpHeaders) SimpleClientHttpRequestFactory(cn.taketoday.http.client.SimpleClientHttpRequestFactory) HttpEntity(cn.taketoday.http.HttpEntity) MockWebServer(okhttp3.mockwebserver.MockWebServer) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 3 with SimpleClientHttpRequestFactory

use of cn.taketoday.http.client.SimpleClientHttpRequestFactory in project today-framework by TAKETODAY.

the class RestTemplateTests method keepGivenAcceptHeaderOnPut.

@Test
// gh-23740
void keepGivenAcceptHeaderOnPut() throws Exception {
    try (MockWebServer server = new MockWebServer()) {
        server.enqueue(new MockResponse().setResponseCode(500).setBody("internal server error"));
        server.start();
        HttpHeaders headers = HttpHeaders.create();
        headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
        HttpEntity<String> entity = new HttpEntity<>(headers);
        template.setRequestFactory(new SimpleClientHttpRequestFactory());
        template.exchange(server.url("/internal/server/error").uri(), PUT, entity, Void.class);
        RecordedRequest request = server.takeRequest();
        final List<List<String>> accepts = request.getHeaders().toMultimap().entrySet().stream().filter(entry -> entry.getKey().equalsIgnoreCase("accept")).map(Entry::getValue).collect(Collectors.toList());
        assertThat(accepts).hasSize(1);
        assertThat(accepts.get(0)).hasSize(1);
        assertThat(accepts.get(0).get(0)).isEqualTo("application/json");
    }
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) HttpHeaders(cn.taketoday.http.HttpHeaders) SimpleClientHttpRequestFactory(cn.taketoday.http.client.SimpleClientHttpRequestFactory) HttpEntity(cn.taketoday.http.HttpEntity) MockWebServer(okhttp3.mockwebserver.MockWebServer) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 4 with SimpleClientHttpRequestFactory

use of cn.taketoday.http.client.SimpleClientHttpRequestFactory in project today-framework by TAKETODAY.

the class RestTemplateTests method headerAcceptAllOnDelete.

@Test
// gh-23740
void headerAcceptAllOnDelete() throws Exception {
    try (MockWebServer server = new MockWebServer()) {
        server.enqueue(new MockResponse().setResponseCode(500).setBody("internal server error"));
        server.start();
        template.setRequestFactory(new SimpleClientHttpRequestFactory());
        template.delete(server.url("/internal/server/error").uri());
        assertThat(server.takeRequest().getHeader("Accept")).isEqualTo("*/*");
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) SimpleClientHttpRequestFactory(cn.taketoday.http.client.SimpleClientHttpRequestFactory) MockWebServer(okhttp3.mockwebserver.MockWebServer) Test(org.junit.jupiter.api.Test)

Example 5 with SimpleClientHttpRequestFactory

use of cn.taketoday.http.client.SimpleClientHttpRequestFactory in project today-framework by TAKETODAY.

the class RestTemplateBuilderTests method connectTimeoutCanBeConfiguredOnAWrappedRequestFactory.

@Test
void connectTimeoutCanBeConfiguredOnAWrappedRequestFactory() {
    SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
    this.builder.requestFactory(() -> new BufferingClientHttpRequestFactory(requestFactory)).setConnectTimeout(Duration.ofMillis(1234)).build();
    assertThat(requestFactory).hasFieldOrPropertyWithValue("connectTimeout", 1234);
}
Also used : SimpleClientHttpRequestFactory(cn.taketoday.http.client.SimpleClientHttpRequestFactory) BufferingClientHttpRequestFactory(cn.taketoday.http.client.BufferingClientHttpRequestFactory) Test(org.junit.jupiter.api.Test)

Aggregations

SimpleClientHttpRequestFactory (cn.taketoday.http.client.SimpleClientHttpRequestFactory)16 Test (org.junit.jupiter.api.Test)14 BufferingClientHttpRequestFactory (cn.taketoday.http.client.BufferingClientHttpRequestFactory)8 MockResponse (okhttp3.mockwebserver.MockResponse)6 MockWebServer (okhttp3.mockwebserver.MockWebServer)6 HttpEntity (cn.taketoday.http.HttpEntity)2 HttpHeaders (cn.taketoday.http.HttpHeaders)2 ClientHttpRequest (cn.taketoday.http.client.ClientHttpRequest)2 ClientHttpResponse (cn.taketoday.http.client.ClientHttpResponse)2 RestTemplate (cn.taketoday.web.client.RestTemplate)2 URI (java.net.URI)2 List (java.util.List)2 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)2