use of okhttp3.Connection in project okhttp by square.
the class ResponseCacheTest method conditionalHitDoesNotUpdateCache.
/**
* Equivalent to {@code CacheTest.conditionalHitUpdatesCache()}, except a Java standard cache has
* no means to update the headers for an existing entry so the behavior is different.
*/
@Test
public void conditionalHitDoesNotUpdateCache() throws Exception {
// A response that is cacheable, but with a short life.
server.enqueue(new MockResponse().addHeader("Last-Modified: " + formatDate(0, TimeUnit.SECONDS)).addHeader("Cache-Control: max-age=0").setBody("A"));
// A response that refers to the previous response, but is cacheable with a long life.
// Contains a header we can recognize as having come from the server.
server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=30").addHeader("Allow: GET, HEAD").setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
// A response that is cacheable with a long life.
server.enqueue(new MockResponse().setBody("B").addHeader("Cache-Control: max-age=30"));
// A response that should never be requested.
server.enqueue(new MockResponse().setBody("C"));
// cache miss; seed the cache with an entry that will require a network hit to be sure it is
// still valid
HttpURLConnection connection1 = openConnection(server.url("/a").url());
assertEquals("A", readAscii(connection1));
assertEquals(null, connection1.getHeaderField("Allow"));
// conditional cache hit; The cached data should be returned, but the cache is not updated.
HttpURLConnection connection2 = openConnection(server.url("/a").url());
assertEquals(HttpURLConnection.HTTP_OK, connection2.getResponseCode());
assertEquals("A", readAscii(connection2));
assertEquals("GET, HEAD", connection2.getHeaderField("Allow"));
// conditional cache hit; The server responds with new data. The cache is updated.
HttpURLConnection connection3 = openConnection(server.url("/a").url());
assertEquals("B", readAscii(connection3));
// full cache hit; The data from connection3 has now replaced that from connection 1.
HttpURLConnection connection4 = openConnection(server.url("/a").url());
assertEquals("B", readAscii(connection4));
assertEquals(3, server.getRequestCount());
}
use of okhttp3.Connection in project okhttp by square.
the class HttpLoggingInterceptorTest method headersResponseBody.
@Test
public void headersResponseBody() throws IOException {
setLevel(Level.HEADERS);
server.enqueue(new MockResponse().setBody("Hello!").setHeader("Content-Type", PLAIN));
Response response = client.newCall(request().build()).execute();
response.body().close();
applicationLogs.assertLogEqual("--> GET " + url + " http/1.1").assertLogEqual("--> END GET").assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)").assertLogEqual("Content-Length: 6").assertLogEqual("Content-Type: text/plain; charset=utf-8").assertLogEqual("<-- END HTTP").assertNoMoreLogs();
networkLogs.assertLogEqual("--> GET " + url + " http/1.1").assertLogEqual("Host: " + host).assertLogEqual("Connection: Keep-Alive").assertLogEqual("Accept-Encoding: gzip").assertLogMatch("User-Agent: okhttp/.+").assertLogEqual("--> END GET").assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)").assertLogEqual("Content-Length: 6").assertLogEqual("Content-Type: text/plain; charset=utf-8").assertLogEqual("<-- END HTTP").assertNoMoreLogs();
}
use of okhttp3.Connection in project okhttp by square.
the class HttpLoggingInterceptorTest method headersGet.
@Test
public void headersGet() throws IOException {
setLevel(Level.HEADERS);
server.enqueue(new MockResponse());
Response response = client.newCall(request().build()).execute();
response.body().close();
applicationLogs.assertLogEqual("--> GET " + url + " http/1.1").assertLogEqual("--> END GET").assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)").assertLogEqual("Content-Length: 0").assertLogEqual("<-- END HTTP").assertNoMoreLogs();
networkLogs.assertLogEqual("--> GET " + url + " http/1.1").assertLogEqual("Host: " + host).assertLogEqual("Connection: Keep-Alive").assertLogEqual("Accept-Encoding: gzip").assertLogMatch("User-Agent: okhttp/.+").assertLogEqual("--> END GET").assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)").assertLogEqual("Content-Length: 0").assertLogEqual("<-- END HTTP").assertNoMoreLogs();
}
use of okhttp3.Connection in project okhttp by square.
the class HttpLoggingInterceptorTest method bodyPost.
@Test
public void bodyPost() throws IOException {
setLevel(Level.BODY);
server.enqueue(new MockResponse());
Request request = request().post(RequestBody.create(PLAIN, "Hi?")).build();
Response response = client.newCall(request).execute();
response.body().close();
applicationLogs.assertLogEqual("--> POST " + url + " http/1.1").assertLogEqual("Content-Type: text/plain; charset=utf-8").assertLogEqual("Content-Length: 3").assertLogEqual("").assertLogEqual("Hi?").assertLogEqual("--> END POST (3-byte body)").assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)").assertLogEqual("Content-Length: 0").assertLogEqual("<-- END HTTP (0-byte body)").assertNoMoreLogs();
networkLogs.assertLogEqual("--> POST " + url + " http/1.1").assertLogEqual("Content-Type: text/plain; charset=utf-8").assertLogEqual("Content-Length: 3").assertLogEqual("Host: " + host).assertLogEqual("Connection: Keep-Alive").assertLogEqual("Accept-Encoding: gzip").assertLogMatch("User-Agent: okhttp/.+").assertLogEqual("").assertLogEqual("Hi?").assertLogEqual("--> END POST (3-byte body)").assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)").assertLogEqual("Content-Length: 0").assertLogEqual("<-- END HTTP (0-byte body)").assertNoMoreLogs();
}
use of okhttp3.Connection in project okhttp by square.
the class HttpLoggingInterceptorTest method bodyGetNoBody.
private void bodyGetNoBody(int code) throws IOException {
server.enqueue(new MockResponse().setStatus("HTTP/1.1 " + code + " No Content"));
Response response = client.newCall(request().build()).execute();
response.body().close();
applicationLogs.assertLogEqual("--> GET " + url + " http/1.1").assertLogEqual("--> END GET").assertLogMatch("<-- " + code + " No Content " + url + " \\(\\d+ms\\)").assertLogEqual("Content-Length: 0").assertLogEqual("<-- END HTTP (0-byte body)").assertNoMoreLogs();
networkLogs.assertLogEqual("--> GET " + url + " http/1.1").assertLogEqual("Host: " + host).assertLogEqual("Connection: Keep-Alive").assertLogEqual("Accept-Encoding: gzip").assertLogMatch("User-Agent: okhttp/.+").assertLogEqual("--> END GET").assertLogMatch("<-- " + code + " No Content " + url + " \\(\\d+ms\\)").assertLogEqual("Content-Length: 0").assertLogEqual("<-- END HTTP (0-byte body)").assertNoMoreLogs();
}
Aggregations