Search in sources :

Example 21 with MockResponse

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

the class WebClientIntegrationTests method postJsonPojo.

@Test
public void postJsonPojo() throws Exception {
    this.server.enqueue(new MockResponse().setHeader("Content-Type", "application/json").setBody("{\"bar\":\"BARBAR\",\"foo\":\"FOOFOO\"}"));
    Mono<Pojo> result = this.webClient.post().uri("/pojo/capitalize").accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON).exchange(fromObject(new Pojo("foofoo", "barbar"))).then(response -> response.bodyToMono(Pojo.class));
    StepVerifier.create(result).consumeNextWith(p -> assertEquals("BARBAR", p.getBar())).expectComplete().verify(Duration.ofSeconds(3));
    RecordedRequest recordedRequest = server.takeRequest();
    Assert.assertEquals(1, server.getRequestCount());
    Assert.assertEquals("/pojo/capitalize", recordedRequest.getPath());
    Assert.assertEquals("{\"foo\":\"foofoo\",\"bar\":\"barbar\"}", recordedRequest.getBody().readUtf8());
    Assert.assertEquals("chunked", recordedRequest.getHeader(HttpHeaders.TRANSFER_ENCODING));
    Assert.assertEquals("application/json", recordedRequest.getHeader(HttpHeaders.ACCEPT));
    Assert.assertEquals("application/json", recordedRequest.getHeader(HttpHeaders.CONTENT_TYPE));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Pojo(org.springframework.http.codec.Pojo) Test(org.junit.Test)

Example 22 with MockResponse

use of okhttp3.mockwebserver.MockResponse in project glide by bumptech.

the class VolleyStreamFetcherServerTest method testCallsLoadFailedIfStatusCodeIs500.

@Test
public void testCallsLoadFailedIfStatusCodeIs500() throws Exception {
    mockWebServer.enqueue(new MockResponse().setResponseCode(500).setBody("error"));
    getFetcher().loadData(Priority.NORMAL, callback);
    waitForResponseLatch.await();
    verify(callback).onLoadFailed(isA(VolleyError.class));
}
Also used : VolleyError(com.android.volley.VolleyError) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 23 with MockResponse

use of okhttp3.mockwebserver.MockResponse in project glide by bumptech.

the class VolleyStreamFetcherServerTest method testHandlesRedirect302s.

@Test
public void testHandlesRedirect302s() throws Exception {
    String expected = "fakedata";
    mockWebServer.enqueue(new MockResponse().setResponseCode(302).setHeader("Location", mockWebServer.url("/redirect").toString()));
    mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(expected));
    getFetcher().loadData(Priority.LOW, callback);
    waitForResponseLatch.await();
    verify(callback).onDataReady(streamCaptor.capture());
    assertStreamOf(expected, streamCaptor.getValue());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 24 with MockResponse

use of okhttp3.mockwebserver.MockResponse in project glide by bumptech.

the class VolleyStreamFetcherServerTest method testCallsLoadFailedIfStatusCodeIs400.

@Test
public void testCallsLoadFailedIfStatusCodeIs400() throws Exception {
    mockWebServer.enqueue(new MockResponse().setResponseCode(400).setBody("error"));
    getFetcher().loadData(Priority.LOW, callback);
    waitForResponseLatch.await();
    verify(callback).onLoadFailed(isA(VolleyError.class));
}
Also used : VolleyError(com.android.volley.VolleyError) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 25 with MockResponse

use of okhttp3.mockwebserver.MockResponse in project zipkin by openzipkin.

the class ZipkinServerTest method connectsToConfiguredBackend.

@Test
public void connectsToConfiguredBackend() throws Exception {
    try (MockWebServer es = new MockWebServer()) {
        es.start(elasticsearchPort);
        es.enqueue(new MockResponse().setBody("{\"version\":{\"number\":\"2.4.0\"}}"));
        // template
        es.enqueue(new MockResponse());
        // search (will fail because no content, but that's ok)
        es.enqueue(new MockResponse());
        client.newCall(new Request.Builder().url(HttpUrl.parse("http://localhost:" + zipkinPort + "/api/v1/services")).get().build()).execute();
        // version
        assertEquals("/", es.takeRequest().getPath());
        assertEquals("/_template/zipkin_template", es.takeRequest().getPath());
        assertTrue(es.takeRequest().getPath().replaceAll("\\?.*", "").endsWith("/span/_search"));
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockWebServer(okhttp3.mockwebserver.MockWebServer) Request(okhttp3.Request) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

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