Search in sources :

Example 76 with RecordedRequest

use of okhttp3.mockwebserver.RecordedRequest in project okhttp by square.

the class CallTest method rangeHeaderPreventsAutomaticGzip.

@Test
public void rangeHeaderPreventsAutomaticGzip() throws Exception {
    Buffer gzippedBody = gzip("abcabcabc");
    // Enqueue a gzipped response. Our request isn't expecting it, but that's okay.
    server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_PARTIAL).setBody(gzippedBody).addHeader("Content-Encoding: gzip").addHeader("Content-Range: bytes 0-" + (gzippedBody.size() - 1)));
    // Make a range request.
    Request request = new Request.Builder().url(server.url("/")).header("Range", "bytes=0-").build();
    Call call = client.newCall(request);
    // The response is not decompressed.
    Response response = call.execute();
    assertEquals("gzip", response.header("Content-Encoding"));
    assertEquals(gzippedBody.snapshot(), response.body().source().readByteString());
    // The request did not offer gzip support.
    RecordedRequest recordedRequest = server.takeRequest();
    assertNull(recordedRequest.getHeader("Accept-Encoding"));
}
Also used : Buffer(okio.Buffer) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 77 with RecordedRequest

use of okhttp3.mockwebserver.RecordedRequest in project okhttp by square.

the class CallTest method canceledBeforeResponseReadSignalsOnFailure.

@Test
public void canceledBeforeResponseReadSignalsOnFailure() throws Exception {
    Request requestA = new Request.Builder().url(server.url("/a")).build();
    final Call call = client.newCall(requestA);
    server.setDispatcher(new Dispatcher() {

        @Override
        public MockResponse dispatch(RecordedRequest request) {
            call.cancel();
            return new MockResponse().setBody("A");
        }
    });
    call.enqueue(callback);
    assertEquals("/a", server.takeRequest().getPath());
    callback.await(requestA.url()).assertFailure("Canceled", "stream was reset: CANCEL", "Socket closed");
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Dispatcher(okhttp3.mockwebserver.Dispatcher) Test(org.junit.Test)

Example 78 with RecordedRequest

use of okhttp3.mockwebserver.RecordedRequest in project okhttp by square.

the class CallTest method proxyAuthenticateOnConnect.

/** Respond to a proxy authorization challenge. */
@Test
public void proxyAuthenticateOnConnect() throws Exception {
    server.useHttps(sslClient.socketFactory, true);
    server.enqueue(new MockResponse().setResponseCode(407).addHeader("Proxy-Authenticate: Basic realm=\"localhost\""));
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END).clearHeaders());
    server.enqueue(new MockResponse().setBody("response body"));
    client = client.newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).proxy(server.toProxyAddress()).proxyAuthenticator(new RecordingOkAuthenticator("password")).hostnameVerifier(new RecordingHostnameVerifier()).build();
    Request request = new Request.Builder().url("https://android.com/foo").build();
    Response response = client.newCall(request).execute();
    assertEquals("response body", response.body().string());
    RecordedRequest connect1 = server.takeRequest();
    assertEquals("CONNECT android.com:443 HTTP/1.1", connect1.getRequestLine());
    assertNull(connect1.getHeader("Proxy-Authorization"));
    RecordedRequest connect2 = server.takeRequest();
    assertEquals("CONNECT android.com:443 HTTP/1.1", connect2.getRequestLine());
    assertEquals("password", connect2.getHeader("Proxy-Authorization"));
    RecordedRequest get = server.takeRequest();
    assertEquals("GET /foo HTTP/1.1", get.getRequestLine());
    assertNull(get.getHeader("Proxy-Authorization"));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) RecordingOkAuthenticator(okhttp3.internal.RecordingOkAuthenticator) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 79 with RecordedRequest

use of okhttp3.mockwebserver.RecordedRequest in project okhttp by square.

the class CallTest method put.

@Test
public void put() throws Exception {
    server.enqueue(new MockResponse().setBody("abc"));
    Request request = new Request.Builder().url(server.url("/")).put(RequestBody.create(MediaType.parse("text/plain"), "def")).build();
    executeSynchronously(request).assertCode(200).assertBody("abc");
    RecordedRequest recordedRequest = server.takeRequest();
    assertEquals("PUT", recordedRequest.getMethod());
    assertEquals("def", recordedRequest.getBody().readUtf8());
    assertEquals("3", recordedRequest.getHeader("Content-Length"));
    assertEquals("text/plain; charset=utf-8", recordedRequest.getHeader("Content-Type"));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 80 with RecordedRequest

use of okhttp3.mockwebserver.RecordedRequest in project okhttp by square.

the class CallTest method postBodyRetransmittedOnFailureRecovery.

@Test
public void postBodyRetransmittedOnFailureRecovery() throws Exception {
    server.enqueue(new MockResponse().setBody("abc"));
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.DISCONNECT_AFTER_REQUEST));
    server.enqueue(new MockResponse().setBody("def"));
    // Seed the connection pool so we have something that can fail.
    Request request1 = new Request.Builder().url(server.url("/")).build();
    Response response1 = client.newCall(request1).execute();
    assertEquals("abc", response1.body().string());
    Request request2 = new Request.Builder().url(server.url("/")).post(RequestBody.create(MediaType.parse("text/plain"), "body!")).build();
    Response response2 = client.newCall(request2).execute();
    assertEquals("def", response2.body().string());
    RecordedRequest get = server.takeRequest();
    assertEquals(0, get.getSequenceNumber());
    RecordedRequest post1 = server.takeRequest();
    assertEquals("body!", post1.getBody().readUtf8());
    assertEquals(1, post1.getSequenceNumber());
    RecordedRequest post2 = server.takeRequest();
    assertEquals("body!", post2.getBody().readUtf8());
    assertEquals(0, post2.getSequenceNumber());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Aggregations

RecordedRequest (okhttp3.mockwebserver.RecordedRequest)206 MockResponse (okhttp3.mockwebserver.MockResponse)188 Test (org.junit.Test)168 HttpURLConnection (java.net.HttpURLConnection)18 MockWebServer (okhttp3.mockwebserver.MockWebServer)14 IOException (java.io.IOException)13 URL (java.net.URL)13 OutputStream (java.io.OutputStream)12 Response (okhttp3.Response)12 OkHttpURLConnection (okhttp3.internal.huc.OkHttpURLConnection)12 AbstractTest (org.openstack4j.api.AbstractTest)12 Test (org.testng.annotations.Test)12 Call (okhttp3.Call)11 Buffer (okio.Buffer)11 Request (okhttp3.Request)8 RecordingOkAuthenticator (okhttp3.internal.RecordingOkAuthenticator)7 Dispatcher (okhttp3.mockwebserver.Dispatcher)6 ByteString (okio.ByteString)6 CookieManager (java.net.CookieManager)5 URLConnection (java.net.URLConnection)5