Search in sources :

Example 56 with Header

use of okhttp3.internal.http2.Header in project okhttp by square.

the class CacheTest method varyMultipleFieldsWithNoMatch.

@Test
public void varyMultipleFieldsWithNoMatch() throws Exception {
    server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").addHeader("Vary: Accept-Language, Accept-Charset").addHeader("Vary: Accept-Encoding").setBody("A"));
    server.enqueue(new MockResponse().setBody("B"));
    HttpUrl url = server.url("/");
    Request frRequest = new Request.Builder().url(url).header("Accept-Language", "fr-CA").header("Accept-Charset", "UTF-8").header("Accept-Encoding", "identity").build();
    Response frResponse = client.newCall(frRequest).execute();
    assertEquals("A", frResponse.body().string());
    Request enRequest = new Request.Builder().url(url).header("Accept-Language", "en-CA").header("Accept-Charset", "UTF-8").header("Accept-Encoding", "identity").build();
    Response enResponse = client.newCall(enRequest).execute();
    assertEquals("B", enResponse.body().string());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 57 with Header

use of okhttp3.internal.http2.Header in project okhttp by square.

the class CacheTest method authorizationRequestFullyCached.

@Test
public void authorizationRequestFullyCached() throws Exception {
    server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").setBody("A"));
    server.enqueue(new MockResponse().setBody("B"));
    HttpUrl url = server.url("/");
    Request request = new Request.Builder().url(url).header("Authorization", "password").build();
    Response response = client.newCall(request).execute();
    assertEquals("A", response.body().string());
    assertEquals("A", get(url).body().string());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 58 with Header

use of okhttp3.internal.http2.Header 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)

Example 59 with Header

use of okhttp3.internal.http2.Header in project okhttp by square.

the class CallTest method illegalToExecuteTwice.

@Test
public void illegalToExecuteTwice() throws Exception {
    server.enqueue(new MockResponse().setBody("abc").addHeader("Content-Type: text/plain"));
    Request request = new Request.Builder().url(server.url("/")).header("User-Agent", "SyncApiTest").build();
    Call call = client.newCall(request);
    Response response = call.execute();
    response.body().close();
    try {
        call.execute();
        fail();
    } catch (IllegalStateException e) {
        assertEquals("Already Executed", e.getMessage());
    }
    try {
        call.enqueue(callback);
        fail();
    } catch (IllegalStateException e) {
        assertEquals("Already Executed", e.getMessage());
    }
    assertEquals("SyncApiTest", server.takeRequest().getHeader("User-Agent"));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 60 with Header

use of okhttp3.internal.http2.Header in project okhttp by square.

the class CacheTest method varyMatchesChangedRequestHeaderField.

@Test
public void varyMatchesChangedRequestHeaderField() throws Exception {
    server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").addHeader("Vary: Accept-Language").setBody("A"));
    server.enqueue(new MockResponse().setBody("B"));
    HttpUrl url = server.url("/");
    Request frRequest = new Request.Builder().url(url).header("Accept-Language", "fr-CA").build();
    Response frResponse = client.newCall(frRequest).execute();
    assertEquals("A", frResponse.body().string());
    Request enRequest = new Request.Builder().url(url).header("Accept-Language", "en-US").build();
    Response enResponse = client.newCall(enRequest).execute();
    assertEquals("B", enResponse.body().string());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)88 MockResponse (okhttp3.mockwebserver.MockResponse)82 Request (okhttp3.Request)70 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)56 Response (okhttp3.Response)52 IOException (java.io.IOException)50 RequestBody (okhttp3.RequestBody)24 Call (okhttp3.Call)20 OkHttpClient (okhttp3.OkHttpClient)20 Callback (okhttp3.Callback)19 Interceptor (okhttp3.Interceptor)12 FormBody (okhttp3.FormBody)11 ResponseBody (okhttp3.ResponseBody)11 ArrayList (java.util.ArrayList)10 Map (java.util.Map)10 Headers (okhttp3.Headers)10 List (java.util.List)9 JSONObject (org.json.JSONObject)7 File (java.io.File)6 HashMap (java.util.HashMap)6