Search in sources :

Example 51 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)

Example 52 with Header

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

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

the class CacheTest method combinedCacheHeadersCanBeNonAscii.

@Test
public void combinedCacheHeadersCanBeNonAscii() throws Exception {
    server.enqueue(new MockResponse().addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS)).addHeader("Cache-Control: max-age=0").addHeaderLenient("Alpha", "α").addHeaderLenient("β", "Beta").setBody("abcd"));
    server.enqueue(new MockResponse().addHeader("Transfer-Encoding: none").addHeaderLenient("Gamma", "Γ").addHeaderLenient("Δ", "Delta").setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
    Response response1 = get(server.url("/"));
    assertEquals("α", response1.header("Alpha"));
    assertEquals("Beta", response1.header("β"));
    assertEquals("abcd", response1.body().string());
    Response response2 = get(server.url("/"));
    assertEquals("α", response2.header("Alpha"));
    assertEquals("Beta", response2.header("β"));
    assertEquals("Γ", response2.header("Gamma"));
    assertEquals("Delta", response2.header("Δ"));
    assertEquals("abcd", response2.body().string());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 54 with Header

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

the class CacheTest method requestMaxStale.

@Test
public void requestMaxStale() throws IOException {
    server.enqueue(new MockResponse().setBody("A").addHeader("Cache-Control: max-age=120").addHeader("Date: " + formatDate(-4, TimeUnit.MINUTES)));
    server.enqueue(new MockResponse().setBody("B"));
    assertEquals("A", get(server.url("/")).body().string());
    Request request = new Request.Builder().url(server.url("/")).header("Cache-Control", "max-stale=180").build();
    Response response = client.newCall(request).execute();
    assertEquals("A", response.body().string());
    assertEquals("110 HttpURLConnection \"Response is stale\"", response.header("Warning"));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 55 with Header

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

the class CacheTest method varyMultipleFieldsWithMatch.

@Test
public void varyMultipleFieldsWithMatch() 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 request = new Request.Builder().url(url).header("Accept-Language", "fr-CA").header("Accept-Charset", "UTF-8").header("Accept-Encoding", "identity").build();
    Response response1 = client.newCall(request).execute();
    assertEquals("A", response1.body().string());
    Request request1 = new Request.Builder().url(url).header("Accept-Language", "fr-CA").header("Accept-Charset", "UTF-8").header("Accept-Encoding", "identity").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)

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