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));
}
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());
}
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());
}
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());
}
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());
}
Aggregations