Search in sources :

Example 16 with MockResponse

use of okhttp3.mockwebserver.MockResponse in project spring-framework by spring-projects.

the class WebClientIntegrationTests method headers.

@Test
public void headers() throws Exception {
    this.server.enqueue(new MockResponse().setHeader("Content-Type", "text/plain").setBody("Hello Spring!"));
    Mono<HttpHeaders> result = this.webClient.get().uri("/greeting?name=Spring").exchange().map(response -> response.headers().asHttpHeaders());
    StepVerifier.create(result).consumeNextWith(httpHeaders -> {
        assertEquals(MediaType.TEXT_PLAIN, httpHeaders.getContentType());
        assertEquals(13L, httpHeaders.getContentLength());
    }).expectComplete().verify(Duration.ofSeconds(3));
    RecordedRequest recordedRequest = server.takeRequest();
    Assert.assertEquals(1, server.getRequestCount());
    Assert.assertEquals("*/*", recordedRequest.getHeader(HttpHeaders.ACCEPT));
    Assert.assertEquals("/greeting?name=Spring", recordedRequest.getPath());
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) HttpHeaders(org.springframework.http.HttpHeaders) Test(org.junit.Test)

Example 17 with MockResponse

use of okhttp3.mockwebserver.MockResponse in project spring-framework by spring-projects.

the class WebClientIntegrationTests method jsonString.

@Test
public void jsonString() throws Exception {
    String content = "{\"bar\":\"barbar\",\"foo\":\"foofoo\"}";
    this.server.enqueue(new MockResponse().setHeader("Content-Type", "application/json").setBody(content));
    Mono<String> result = this.webClient.get().uri("/json").accept(MediaType.APPLICATION_JSON).exchange().then(response -> response.bodyToMono(String.class));
    StepVerifier.create(result).expectNext(content).expectComplete().verify(Duration.ofSeconds(3));
    RecordedRequest recordedRequest = server.takeRequest();
    Assert.assertEquals(1, server.getRequestCount());
    Assert.assertEquals("/json", recordedRequest.getPath());
    Assert.assertEquals("application/json", recordedRequest.getHeader(HttpHeaders.ACCEPT));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 18 with MockResponse

use of okhttp3.mockwebserver.MockResponse in project spring-framework by spring-projects.

the class WebClientIntegrationTests method buildFilter.

@Test
public void buildFilter() throws Exception {
    this.server.enqueue(new MockResponse().setHeader("Content-Type", "text/plain").setBody("Hello Spring!"));
    WebClient filteredClient = this.webClient.filter((request, next) -> {
        ClientRequest filteredRequest = ClientRequest.from(request).header("foo", "bar").build();
        return next.exchange(filteredRequest);
    });
    Mono<String> result = filteredClient.get().uri("/greeting?name=Spring").exchange().then(response -> response.bodyToMono(String.class));
    StepVerifier.create(result).expectNext("Hello Spring!").expectComplete().verify(Duration.ofSeconds(3));
    RecordedRequest recordedRequest = server.takeRequest();
    Assert.assertEquals(1, server.getRequestCount());
    Assert.assertEquals("bar", recordedRequest.getHeader("foo"));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 19 with MockResponse

use of okhttp3.mockwebserver.MockResponse in project spring-framework by spring-projects.

the class WebClientIntegrationTests method notFound.

@Test
public void notFound() throws Exception {
    this.server.enqueue(new MockResponse().setResponseCode(404).setHeader("Content-Type", "text/plain").setBody("Not Found"));
    Mono<ClientResponse> result = this.webClient.get().uri("/greeting?name=Spring").exchange();
    StepVerifier.create(result).consumeNextWith(response -> assertEquals(HttpStatus.NOT_FOUND, response.statusCode())).expectComplete().verify(Duration.ofSeconds(3));
    RecordedRequest recordedRequest = server.takeRequest();
    Assert.assertEquals(1, server.getRequestCount());
    Assert.assertEquals("*/*", recordedRequest.getHeader(HttpHeaders.ACCEPT));
    Assert.assertEquals("/greeting?name=Spring", recordedRequest.getPath());
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 20 with MockResponse

use of okhttp3.mockwebserver.MockResponse in project spring-framework by spring-projects.

the class WebClientIntegrationTests method filter.

@Test
public void filter() throws Exception {
    this.server.enqueue(new MockResponse().setHeader("Content-Type", "text/plain").setBody("Hello Spring!"));
    WebClient filteredClient = this.webClient.filter((request, next) -> {
        ClientRequest filteredRequest = ClientRequest.from(request).header("foo", "bar").build();
        return next.exchange(filteredRequest);
    });
    Mono<String> result = filteredClient.get().uri("/greeting?name=Spring").exchange().then(response -> response.bodyToMono(String.class));
    StepVerifier.create(result).expectNext("Hello Spring!").expectComplete().verify(Duration.ofSeconds(3));
    RecordedRequest recordedRequest = server.takeRequest();
    Assert.assertEquals(1, server.getRequestCount());
    Assert.assertEquals("bar", recordedRequest.getHeader("foo"));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Aggregations

MockResponse (okhttp3.mockwebserver.MockResponse)1754 Test (org.junit.Test)1284 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)483 AtomicReference (java.util.concurrent.atomic.AtomicReference)258 Test (org.junit.jupiter.api.Test)216 MockWebServer (okhttp3.mockwebserver.MockWebServer)201 CountDownLatch (java.util.concurrent.CountDownLatch)196 IOException (java.io.IOException)158 HttpURLConnection (java.net.HttpURLConnection)157 ANError (com.androidnetworking.error.ANError)148 Response (okhttp3.Response)147 MockResponse (mockwebserver3.MockResponse)115 Buffer (okio.Buffer)104 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)89 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)84 List (java.util.List)84 URL (java.net.URL)78 URLConnection (java.net.URLConnection)76 Request (okhttp3.Request)70 ANResponse (com.androidnetworking.common.ANResponse)61