Search in sources :

Example 31 with HttpUrl

use of okhttp3.HttpUrl 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 32 with HttpUrl

use of okhttp3.HttpUrl 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 33 with HttpUrl

use of okhttp3.HttpUrl 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)

Example 34 with HttpUrl

use of okhttp3.HttpUrl in project okhttp by square.

the class CacheTest method varyFieldsAreCaseInsensitive.

@Test
public void varyFieldsAreCaseInsensitive() 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 request = new Request.Builder().url(url).header("Accept-Language", "fr-CA").build();
    Response response1 = client.newCall(request).execute();
    assertEquals("A", response1.body().string());
    Request request1 = new Request.Builder().url(url).header("accept-language", "fr-CA").build();
    Response response2 = client.newCall(request1).execute();
    assertEquals("A", response2.body().string());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 35 with HttpUrl

use of okhttp3.HttpUrl in project okhttp by square.

the class CacheTest method postInvalidatesCacheWithUncacheableResponse.

@Test
public void postInvalidatesCacheWithUncacheableResponse() throws Exception {
    // 1. seed the cache
    // 2. invalidate it with uncacheable response
    // 3. expect a cache miss
    server.enqueue(new MockResponse().setBody("A").addHeader("Expires: " + formatDate(1, TimeUnit.HOURS)));
    server.enqueue(new MockResponse().setBody("B").setResponseCode(500));
    server.enqueue(new MockResponse().setBody("C"));
    HttpUrl url = server.url("/");
    assertEquals("A", get(url).body().string());
    Request request = new Request.Builder().url(url).method("POST", requestBodyOrNull("POST")).build();
    Response invalidate = client.newCall(request).execute();
    assertEquals("B", invalidate.body().string());
    assertEquals("C", get(url).body().string());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Aggregations

HttpUrl (okhttp3.HttpUrl)72 Test (org.junit.Test)55 MockResponse (okhttp3.mockwebserver.MockResponse)53 Request (okhttp3.Request)28 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)25 IOException (java.io.IOException)20 Response (okhttp3.Response)17 MockWebServer (okhttp3.mockwebserver.MockWebServer)17 RequestBody (okhttp3.RequestBody)12 Moshi (com.squareup.moshi.Moshi)6 CookieManager (java.net.CookieManager)6 Call (retrofit2.Call)6 Callback (retrofit2.Callback)6 Response (retrofit2.Response)6 LoginFailedException (com.pokegoapi.exceptions.request.LoginFailedException)5 HttpCookie (java.net.HttpCookie)5 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 QueryRequest (zipkin.storage.QueryRequest)5 HashMap (java.util.HashMap)4