Search in sources :

Example 46 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 47 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 48 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 49 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 50 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)839 Test (org.junit.Test)749 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)287 HttpURLConnection (java.net.HttpURLConnection)156 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)85 IOException (java.io.IOException)76 URLConnection (java.net.URLConnection)76 URL (java.net.URL)74 Response (okhttp3.Response)61 Request (okhttp3.Request)52 AtomicReference (java.util.concurrent.atomic.AtomicReference)46 OkHttpURLConnection (okhttp3.internal.huc.OkHttpURLConnection)43 Call (okhttp3.Call)42 Buffer (okio.Buffer)42 MockWebServer (okhttp3.mockwebserver.MockWebServer)32 CompositeException (io.reactivex.exceptions.CompositeException)28 InputStream (java.io.InputStream)26 CountDownLatch (java.util.concurrent.CountDownLatch)26 ToStringConverterFactory (retrofit2.helpers.ToStringConverterFactory)25 OutputStream (java.io.OutputStream)19