Search in sources :

Example 16 with Connection

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());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection) Test(org.junit.Test)

Example 17 with Connection

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();
}
Also used : Response(okhttp3.Response) MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 18 with Connection

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();
}
Also used : Response(okhttp3.Response) MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 19 with Connection

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();
}
Also used : Response(okhttp3.Response) MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) Request(okhttp3.Request) Test(org.junit.Test)

Example 20 with Connection

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();
}
Also used : Response(okhttp3.Response) MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse)

Aggregations

Test (org.junit.Test)226 MockResponse (okhttp3.mockwebserver.MockResponse)215 HttpURLConnection (java.net.HttpURLConnection)106 IOException (java.io.IOException)73 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)67 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)59 URLConnection (java.net.URLConnection)53 Response (okhttp3.Response)41 InputStream (java.io.InputStream)39 Connection (com.trilead.ssh2.Connection)36 Request (okhttp3.Request)36 OkHttpURLConnection (okhttp3.internal.huc.OkHttpURLConnection)36 URL (java.net.URL)32 Session (com.trilead.ssh2.Session)31 InFrame (okhttp3.internal.http2.MockHttp2Peer.InFrame)28 Buffer (okio.Buffer)25 OutputStream (java.io.OutputStream)19 InterruptedIOException (java.io.InterruptedIOException)15 Call (okhttp3.Call)14 BufferedSink (okio.BufferedSink)14