Search in sources :

Example 11 with RecordedRequest

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

the class AbstractMockWebServerTestCase method jsonPostRequest.

private MockResponse jsonPostRequest(RecordedRequest request, String location, String contentType) {
    if (request.getBodySize() > 0) {
        assertTrue("Invalid request content-length", Integer.parseInt(request.getHeader("Content-Length")) > 0);
        assertNotNull("No content-type", request.getHeader("Content-Type"));
    }
    return new MockResponse().setHeader("Location", baseUrl + location).setHeader("Content-Type", contentType).setHeader("Content-Length", request.getBody().size()).setBody(request.getBody()).setResponseCode(201);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse)

Example 12 with RecordedRequest

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

the class WebClientIntegrationTests method plainText.

@Test
public void plainText() throws Exception {
    this.server.enqueue(new MockResponse().setBody("Hello Spring!"));
    Mono<String> result = this.webClient.get().uri("/greeting?name=Spring").header("X-Test-Header", "testvalue").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("testvalue", recordedRequest.getHeader("X-Test-Header"));
    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 13 with RecordedRequest

use of okhttp3.mockwebserver.RecordedRequest 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 14 with RecordedRequest

use of okhttp3.mockwebserver.RecordedRequest 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 15 with RecordedRequest

use of okhttp3.mockwebserver.RecordedRequest 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)

Aggregations

RecordedRequest (okhttp3.mockwebserver.RecordedRequest)206 MockResponse (okhttp3.mockwebserver.MockResponse)188 Test (org.junit.Test)168 HttpURLConnection (java.net.HttpURLConnection)18 MockWebServer (okhttp3.mockwebserver.MockWebServer)14 IOException (java.io.IOException)13 URL (java.net.URL)13 OutputStream (java.io.OutputStream)12 Response (okhttp3.Response)12 OkHttpURLConnection (okhttp3.internal.huc.OkHttpURLConnection)12 AbstractTest (org.openstack4j.api.AbstractTest)12 Test (org.testng.annotations.Test)12 Call (okhttp3.Call)11 Buffer (okio.Buffer)11 Request (okhttp3.Request)8 RecordingOkAuthenticator (okhttp3.internal.RecordingOkAuthenticator)7 Dispatcher (okhttp3.mockwebserver.Dispatcher)6 ByteString (okio.ByteString)6 CookieManager (java.net.CookieManager)5 URLConnection (java.net.URLConnection)5