Search in sources :

Example 46 with Header

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

the class CacheTest method getHeadersDeletesCached100LevelWarnings.

@Test
public void getHeadersDeletesCached100LevelWarnings() throws Exception {
    server.enqueue(new MockResponse().addHeader("Warning: 199 test danger").addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS)).addHeader("Cache-Control: max-age=0").setBody("A"));
    server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
    Response response1 = get(server.url("/"));
    assertEquals("A", response1.body().string());
    assertEquals("199 test danger", response1.header("Warning"));
    Response response2 = get(server.url("/"));
    assertEquals("A", response2.body().string());
    assertEquals(null, response2.header("Warning"));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 47 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 48 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 49 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 50 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)

Aggregations

Test (org.junit.Test)89 MockResponse (okhttp3.mockwebserver.MockResponse)82 Request (okhttp3.Request)75 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)56 Response (okhttp3.Response)54 IOException (java.io.IOException)53 RequestBody (okhttp3.RequestBody)24 OkHttpClient (okhttp3.OkHttpClient)22 Call (okhttp3.Call)20 Callback (okhttp3.Callback)19 Interceptor (okhttp3.Interceptor)14 ResponseBody (okhttp3.ResponseBody)12 FormBody (okhttp3.FormBody)11 Headers (okhttp3.Headers)11 ArrayList (java.util.ArrayList)10 Map (java.util.Map)10 List (java.util.List)9 JSONObject (org.json.JSONObject)8 File (java.io.File)6 HashMap (java.util.HashMap)6