Search in sources :

Example 81 with RecordedRequest

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

the class CallTest method expect100ContinueTimesOutWithoutContinue.

@Test
public void expect100ContinueTimesOutWithoutContinue() throws Exception {
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.NO_RESPONSE));
    client = client.newBuilder().readTimeout(500, TimeUnit.MILLISECONDS).build();
    Request request = new Request.Builder().url(server.url("/")).header("Expect", "100-continue").post(RequestBody.create(MediaType.parse("text/plain"), "abc")).build();
    Call call = client.newCall(request);
    try {
        call.execute();
        fail();
    } catch (SocketTimeoutException expected) {
    }
    RecordedRequest recordedRequest = server.takeRequest();
    assertEquals("", recordedRequest.getBody().readUtf8());
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) SocketTimeoutException(java.net.SocketTimeoutException) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 82 with RecordedRequest

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

the class CallTest method postBodyRetransmittedAfterAuthorizationFail.

private void postBodyRetransmittedAfterAuthorizationFail(String body) throws Exception {
    server.enqueue(new MockResponse().setResponseCode(401));
    server.enqueue(new MockResponse());
    Request request = new Request.Builder().url(server.url("/")).method("POST", RequestBody.create(null, body)).build();
    String credential = Credentials.basic("jesse", "secret");
    client = client.newBuilder().authenticator(new RecordingOkAuthenticator(credential)).build();
    Response response = client.newCall(request).execute();
    assertEquals(200, response.code());
    response.body().close();
    RecordedRequest recordedRequest1 = server.takeRequest();
    assertEquals("POST", recordedRequest1.getMethod());
    assertEquals(body, recordedRequest1.getBody().readUtf8());
    assertNull(recordedRequest1.getHeader("Authorization"));
    RecordedRequest recordedRequest2 = server.takeRequest();
    assertEquals("POST", recordedRequest2.getMethod());
    assertEquals(body, recordedRequest2.getBody().readUtf8());
    assertEquals(credential, recordedRequest2.getHeader("Authorization"));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) RecordingOkAuthenticator(okhttp3.internal.RecordingOkAuthenticator) RecordedRequest(okhttp3.mockwebserver.RecordedRequest)

Example 83 with RecordedRequest

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

the class CallTest method unspecifiedRequestBodyContentTypeDoesNotGetDefault.

@Test
public void unspecifiedRequestBodyContentTypeDoesNotGetDefault() throws Exception {
    server.enqueue(new MockResponse());
    Request request = new Request.Builder().url(server.url("/")).method("POST", RequestBody.create(null, "abc")).build();
    executeSynchronously(request).assertCode(200);
    RecordedRequest recordedRequest = server.takeRequest();
    assertEquals(null, recordedRequest.getHeader("Content-Type"));
    assertEquals("3", recordedRequest.getHeader("Content-Length"));
    assertEquals("abc", recordedRequest.getBody().readUtf8());
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 84 with RecordedRequest

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

the class CacheTest method etag.

@Test
public void etag() throws Exception {
    RecordedRequest conditionalRequest = assertConditionallyCached(new MockResponse().addHeader("ETag: v1"));
    assertEquals("v1", conditionalRequest.getHeader("If-None-Match"));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 85 with RecordedRequest

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

the class CallTest method httpsWithIpAddress.

@Test
public void httpsWithIpAddress() throws Exception {
    String localIpAddress = InetAddress.getLoopbackAddress().getHostAddress();
    // Create a certificate with an IP address in the subject alt name.
    HeldCertificate heldCertificate = new HeldCertificate.Builder().commonName("example.com").subjectAlternativeName(localIpAddress).build();
    SslClient sslClient = new SslClient.Builder().certificateChain(heldCertificate.keyPair, heldCertificate.certificate).addTrustedCertificate(heldCertificate.certificate).build();
    // Use that certificate on the server and trust it on the client.
    server.useHttps(sslClient.socketFactory, false);
    client = client.newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(new RecordingHostnameVerifier()).protocols(Collections.singletonList(Protocol.HTTP_1_1)).build();
    // Make a request.
    server.enqueue(new MockResponse());
    HttpUrl url = server.url("/").newBuilder().host(localIpAddress).build();
    Request request = new Request.Builder().url(url).build();
    executeSynchronously(request).assertCode(200);
    // Confirm that the IP address was used in the host header.
    RecordedRequest recordedRequest = server.takeRequest();
    assertEquals(localIpAddress + ":" + server.getPort(), recordedRequest.getHeader("Host"));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) SslClient(okhttp3.internal.tls.SslClient) HeldCertificate(okhttp3.internal.tls.HeldCertificate) 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