Search in sources :

Example 51 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 52 with MockResponse

use of okhttp3.mockwebserver.MockResponse in project Fast-Android-Networking by amitshekhariitbhu.

the class ApiTest method testSynchronousGetRequest.

@SuppressWarnings("unchecked")
public void testSynchronousGetRequest() throws InterruptedException {
    server.enqueue(new MockResponse().setBody("getResponse"));
    ANRequest request = AndroidNetworking.get(server.url("/").toString()).build();
    ANResponse<String> response = request.executeForString();
    assertEquals("getResponse", response.getResult());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) ANRequest(com.androidnetworking.common.ANRequest)

Example 53 with MockResponse

use of okhttp3.mockwebserver.MockResponse in project Fast-Android-Networking by amitshekhariitbhu.

the class ApiTest method testPostRequest404.

public void testPostRequest404() throws InterruptedException {
    server.enqueue(new MockResponse().setResponseCode(404).setBody("postResponse"));
    final AtomicReference<String> errorDetailRef = new AtomicReference<>();
    final AtomicReference<String> errorBodyRef = new AtomicReference<>();
    final AtomicReference<Integer> errorCodeRef = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    AndroidNetworking.post(server.url("/").toString()).addBodyParameter("fistName", "Amit").addBodyParameter("lastName", "Shekhar").build().getAsString(new StringRequestListener() {

        @Override
        public void onResponse(String response) {
            assertTrue(false);
        }

        @Override
        public void onError(ANError anError) {
            errorBodyRef.set(anError.getErrorBody());
            errorDetailRef.set(anError.getErrorDetail());
            errorCodeRef.set(anError.getErrorCode());
            latch.countDown();
        }
    });
    assertTrue(latch.await(2, SECONDS));
    assertEquals(ANConstants.RESPONSE_FROM_SERVER_ERROR, errorDetailRef.get());
    assertEquals("postResponse", errorBodyRef.get());
    assertEquals(404, errorCodeRef.get().intValue());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) StringRequestListener(com.androidnetworking.interfaces.StringRequestListener) AtomicReference(java.util.concurrent.atomic.AtomicReference) ANError(com.androidnetworking.error.ANError) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 54 with MockResponse

use of okhttp3.mockwebserver.MockResponse in project Fast-Android-Networking by amitshekhariitbhu.

the class ApiTest method testUploadRequest.

public void testUploadRequest() throws InterruptedException {
    server.enqueue(new MockResponse().setBody("uploadTestResponse"));
    final AtomicReference<String> responseRef = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    AndroidNetworking.upload(server.url("/").toString()).addMultipartParameter("key", "value").build().getAsString(new StringRequestListener() {

        @Override
        public void onResponse(String response) {
            responseRef.set(response);
            latch.countDown();
        }

        @Override
        public void onError(ANError anError) {
            assertTrue(false);
        }
    });
    assertTrue(latch.await(2, SECONDS));
    assertEquals("uploadTestResponse", responseRef.get());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) StringRequestListener(com.androidnetworking.interfaces.StringRequestListener) AtomicReference(java.util.concurrent.atomic.AtomicReference) ANError(com.androidnetworking.error.ANError) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 55 with MockResponse

use of okhttp3.mockwebserver.MockResponse in project Fast-Android-Networking by amitshekhariitbhu.

the class ApiTest method testSynchronousPostRequest.

@SuppressWarnings("unchecked")
public void testSynchronousPostRequest() throws InterruptedException {
    server.enqueue(new MockResponse().setBody("postResponse"));
    ANRequest request = AndroidNetworking.post(server.url("/").toString()).addBodyParameter("fistName", "Amit").addBodyParameter("lastName", "Shekhar").build();
    ANResponse<String> response = request.executeForString();
    assertEquals("postResponse", response.getResult());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) ANRequest(com.androidnetworking.common.ANRequest)

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