Search in sources :

Example 11 with MockResponse

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

the class AbstractMockWebServerTestCase method getRequest.

private MockResponse getRequest(RecordedRequest request, byte[] body, String contentType) {
    if (request.getMethod().equals("OPTIONS")) {
        return new MockResponse().setResponseCode(200).setHeader("Allow", "GET, OPTIONS, HEAD, TRACE");
    }
    Buffer buf = new Buffer();
    buf.write(body);
    MockResponse response = new MockResponse().setHeader("Content-Length", body.length).setBody(buf).setResponseCode(200);
    if (contentType != null) {
        response = response.setHeader("Content-Type", contentType);
    }
    return response;
}
Also used : Buffer(okio.Buffer) MockResponse(okhttp3.mockwebserver.MockResponse)

Example 12 with MockResponse

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

the class AbstractMockWebServerTestCase method putRequest.

private MockResponse putRequest(RecordedRequest request, String expectedRequestContent) {
    assertTrue("Invalid request content-length", Integer.parseInt(request.getHeader("Content-Length")) > 0);
    String requestContentType = request.getHeader("Content-Type");
    assertNotNull("No content-type", requestContentType);
    Charset charset = StandardCharsets.ISO_8859_1;
    if (requestContentType.contains("charset=")) {
        String charsetName = requestContentType.split("charset=")[1];
        charset = Charset.forName(charsetName);
    }
    assertEquals("Invalid request body", expectedRequestContent, request.getBody().readString(charset));
    return new MockResponse().setResponseCode(202);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Charset(java.nio.charset.Charset)

Example 13 with MockResponse

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

the class AbstractMockWebServerTestCase method postRequest.

private MockResponse postRequest(RecordedRequest request, String expectedRequestContent, String location, String contentType, byte[] responseBody) {
    assertEquals(1, request.getHeaders().values("Content-Length").size());
    assertTrue("Invalid request content-length", Integer.parseInt(request.getHeader("Content-Length")) > 0);
    String requestContentType = request.getHeader("Content-Type");
    assertNotNull("No content-type", requestContentType);
    Charset charset = StandardCharsets.ISO_8859_1;
    if (requestContentType.contains("charset=")) {
        String charsetName = requestContentType.split("charset=")[1];
        charset = Charset.forName(charsetName);
    }
    assertEquals("Invalid request body", expectedRequestContent, request.getBody().readString(charset));
    Buffer buf = new Buffer();
    buf.write(responseBody);
    return new MockResponse().setHeader("Location", baseUrl + location).setHeader("Content-Type", contentType).setHeader("Content-Length", responseBody.length).setBody(buf).setResponseCode(201);
}
Also used : Buffer(okio.Buffer) MockResponse(okhttp3.mockwebserver.MockResponse) Charset(java.nio.charset.Charset)

Example 14 with MockResponse

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

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

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