Search in sources :

Example 1 with MockResponse

use of okhttp3.mockwebserver.MockResponse in project feign by OpenFeign.

the class AbstractClientTest method postWithSpacesInPath.

@Test
public void postWithSpacesInPath() throws IOException, InterruptedException {
    server.enqueue(new MockResponse().setBody("foo"));
    TestInterface api = newBuilder().target(TestInterface.class, "http://localhost:" + server.getPort());
    Response response = api.post("current documents", "foo");
    MockWebServerAssertions.assertThat(server.takeRequest()).hasMethod("POST").hasPath("/path/current%20documents/resource").hasBody("foo");
}
Also used : Response(feign.Response) MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 2 with MockResponse

use of okhttp3.mockwebserver.MockResponse in project feign by OpenFeign.

the class FeignTest method responseCoercesToStringBody.

@Test
public void responseCoercesToStringBody() throws Exception {
    server.enqueue(new MockResponse().setBody("foo"));
    TestInterface api = new TestInterfaceBuilder().target("http://localhost:" + server.getPort());
    Response response = api.response();
    assertTrue(response.body().isRepeatable());
    assertEquals("foo", response.body().toString());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 3 with MockResponse

use of okhttp3.mockwebserver.MockResponse in project Parse-SDK-Android by ParsePlatform.

the class ParseOkHttpClientTest method testParseOkHttpClientExecuteWithExternalInterceptorAndGZIPResponse.

@Test
public void testParseOkHttpClientExecuteWithExternalInterceptorAndGZIPResponse() throws Exception {
    // Make mock response
    Buffer buffer = new Buffer();
    final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    GZIPOutputStream gzipOut = new GZIPOutputStream(byteOut);
    gzipOut.write("content".getBytes());
    gzipOut.close();
    buffer.write(byteOut.toByteArray());
    MockResponse mockResponse = new MockResponse().setStatus("HTTP/1.1 " + 201 + " " + "OK").setBody(buffer).setHeader("Content-Encoding", "gzip");
    // Start mock server
    server.enqueue(mockResponse);
    server.start();
    ParseHttpClient client = new ParseOkHttpClient(10000, null);
    final Semaphore done = new Semaphore(0);
    // Add plain interceptor to disable decompress response stream
    client.addExternalInterceptor(new ParseNetworkInterceptor() {

        @Override
        public ParseHttpResponse intercept(Chain chain) throws IOException {
            done.release();
            ParseHttpResponse parseResponse = chain.proceed(chain.getRequest());
            // Make sure the response we get from the interceptor is the raw gzip stream
            byte[] content = ParseIOUtils.toByteArray(parseResponse.getContent());
            assertArrayEquals(byteOut.toByteArray(), content);
            // We need to set a new stream since we have read it
            return new ParseHttpResponse.Builder().setContent(new ByteArrayInputStream(byteOut.toByteArray())).build();
        }
    });
    // We do not need to add Accept-Encoding header manually, httpClient library should do that.
    String requestUrl = server.url("/").toString();
    ParseHttpRequest parseRequest = new ParseHttpRequest.Builder().setUrl(requestUrl).setMethod(ParseHttpRequest.Method.GET).build();
    // Execute request
    ParseHttpResponse parseResponse = client.execute(parseRequest);
    // Make sure the response we get is ungziped by OkHttp library
    byte[] content = ParseIOUtils.toByteArray(parseResponse.getContent());
    assertArrayEquals("content".getBytes(), content);
    // Make sure interceptor is called
    assertTrue(done.tryAcquire(10, TimeUnit.SECONDS));
    server.shutdown();
}
Also used : Buffer(okio.Buffer) MockResponse(okhttp3.mockwebserver.MockResponse) ParseHttpRequest(com.parse.http.ParseHttpRequest) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Semaphore(java.util.concurrent.Semaphore) IOException(java.io.IOException) GZIPOutputStream(java.util.zip.GZIPOutputStream) ParseNetworkInterceptor(com.parse.http.ParseNetworkInterceptor) ByteArrayInputStream(java.io.ByteArrayInputStream) ParseHttpResponse(com.parse.http.ParseHttpResponse) Test(org.junit.Test)

Example 4 with MockResponse

use of okhttp3.mockwebserver.MockResponse in project sonarqube by SonarSource.

the class WebhookCallerImplTest method send_posts_payload_to_http_server.

@Test
public void send_posts_payload_to_http_server() throws Exception {
    Webhook webhook = new Webhook(PROJECT_UUID, CE_TASK_UUID, "my-webhook", server.url("/ping").toString());
    server.enqueue(new MockResponse().setBody("pong").setResponseCode(201));
    WebhookDelivery delivery = newSender().call(webhook, PAYLOAD);
    assertThat(delivery.getHttpStatus().get()).isEqualTo(201);
    assertThat(delivery.getDurationInMs().get()).isGreaterThanOrEqualTo(0);
    assertThat(delivery.getError()).isEmpty();
    assertThat(delivery.getAt()).isEqualTo(NOW);
    assertThat(delivery.getWebhook()).isSameAs(webhook);
    assertThat(delivery.getPayload()).isSameAs(PAYLOAD);
    RecordedRequest recordedRequest = server.takeRequest();
    assertThat(recordedRequest.getMethod()).isEqualTo("POST");
    assertThat(recordedRequest.getPath()).isEqualTo("/ping");
    assertThat(recordedRequest.getBody().readUtf8()).isEqualTo(PAYLOAD.getJson());
    assertThat(recordedRequest.getHeader("User-Agent")).isEqualTo("SonarQube/6.2");
    assertThat(recordedRequest.getHeader("Content-Type")).isEqualTo("application/json; charset=utf-8");
    assertThat(recordedRequest.getHeader("X-SonarQube-Project")).isEqualTo(PAYLOAD.getProjectKey());
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 5 with MockResponse

use of okhttp3.mockwebserver.MockResponse in project sonarqube by SonarSource.

the class WebhookCallerImplTest method redirects_throws_ISE_if_header_Location_does_not_relate_to_a_supported_protocol.

@Test
public void redirects_throws_ISE_if_header_Location_does_not_relate_to_a_supported_protocol() throws Exception {
    HttpUrl url = server.url("/redirect");
    Webhook webhook = new Webhook(PROJECT_UUID, CE_TASK_UUID, "my-webhook", url.toString());
    server.enqueue(new MockResponse().setResponseCode(307).setHeader("Location", "ftp://foo"));
    WebhookDelivery delivery = newSender().call(webhook, PAYLOAD);
    Throwable error = delivery.getError().get();
    assertThat(error).isInstanceOf(IllegalStateException.class).hasMessage("Unsupported protocol in redirect of " + url + " to ftp://foo");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) HttpUrl(okhttp3.HttpUrl) Test(org.junit.Test)

Aggregations

MockResponse (okhttp3.mockwebserver.MockResponse)1753 Test (org.junit.Test)1284 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)482 AtomicReference (java.util.concurrent.atomic.AtomicReference)258 Test (org.junit.jupiter.api.Test)216 MockWebServer (okhttp3.mockwebserver.MockWebServer)200 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