Search in sources :

Example 21 with Connection

use of okhttp3.Connection in project okhttp by square.

the class HttpLoggingInterceptorTest method headersPostNoContentType.

@Test
public void headersPostNoContentType() throws IOException {
    setLevel(Level.HEADERS);
    server.enqueue(new MockResponse());
    Request request = request().post(RequestBody.create(null, "Hi?")).build();
    Response response = client.newCall(request).execute();
    response.body().close();
    applicationLogs.assertLogEqual("--> POST " + url + " http/1.1").assertLogEqual("Content-Length: 3").assertLogEqual("--> END POST").assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)").assertLogEqual("Content-Length: 0").assertLogEqual("<-- END HTTP").assertNoMoreLogs();
    networkLogs.assertLogEqual("--> POST " + url + " http/1.1").assertLogEqual("Content-Length: 3").assertLogEqual("Host: " + host).assertLogEqual("Connection: Keep-Alive").assertLogEqual("Accept-Encoding: gzip").assertLogMatch("User-Agent: okhttp/.+").assertLogEqual("--> END POST").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) Request(okhttp3.Request) Test(org.junit.Test)

Example 22 with Connection

use of okhttp3.Connection in project okhttp by square.

the class HttpLoggingInterceptorTest method bodyResponseNotIdentityEncoded.

@Test
public void bodyResponseNotIdentityEncoded() throws IOException {
    setLevel(Level.BODY);
    server.enqueue(new MockResponse().setHeader("Content-Encoding", "gzip").setHeader("Content-Type", PLAIN).setBody(new Buffer().write(ByteString.decodeBase64("H4sIAAAAAAAAAPNIzcnJ11HwQKIAdyO+9hMAAAA="))));
    Response response = client.newCall(request().build()).execute();
    response.body().close();
    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-Encoding: gzip").assertLogEqual("Content-Type: text/plain; charset=utf-8").assertLogMatch("Content-Length: \\d+").assertLogEqual("<-- END HTTP (encoded body omitted)").assertNoMoreLogs();
    applicationLogs.assertLogEqual("--> GET " + url + " http/1.1").assertLogEqual("--> END GET").assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)").assertLogEqual("Content-Type: text/plain; charset=utf-8").assertLogEqual("").assertLogEqual("Hello, Hello, Hello").assertLogEqual("<-- END HTTP (19-byte body)").assertNoMoreLogs();
}
Also used : Buffer(okio.Buffer) Response(okhttp3.Response) MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 23 with Connection

use of okhttp3.Connection in project okhttp by square.

the class SocksProxyTest method proxy.

@Test
public void proxy() throws Exception {
    server.enqueue(new MockResponse().setBody("abc"));
    server.enqueue(new MockResponse().setBody("def"));
    OkHttpClient client = defaultClient().newBuilder().proxy(socksProxy.proxy()).build();
    Request request1 = new Request.Builder().url(server.url("/")).build();
    Response response1 = client.newCall(request1).execute();
    assertEquals("abc", response1.body().string());
    Request request2 = new Request.Builder().url(server.url("/")).build();
    Response response2 = client.newCall(request2).execute();
    assertEquals("def", response2.body().string());
    // The HTTP calls should share a single connection.
    assertEquals(1, socksProxy.connectionCount());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 24 with Connection

use of okhttp3.Connection in project okhttp by square.

the class InterceptorTest method networkInterceptorReturnsConnectionOnEmptyBody.

@Test
public void networkInterceptorReturnsConnectionOnEmptyBody() throws Exception {
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.DISCONNECT_AT_END).addHeader("Connection", "Close"));
    Interceptor interceptor = new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            Response response = chain.proceed(chain.request());
            assertNotNull(chain.connection());
            return response;
        }
    };
    client = client.newBuilder().addNetworkInterceptor(interceptor).build();
    Request request = new Request.Builder().url(server.url("/")).build();
    Response response = client.newCall(request).execute();
    response.body().close();
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 25 with Connection

use of okhttp3.Connection in project okhttp by square.

the class CallTest method reusedSinksGetIndependentTimeoutInstances.

@Test
public void reusedSinksGetIndependentTimeoutInstances() throws Exception {
    server.enqueue(new MockResponse());
    server.enqueue(new MockResponse());
    // Call 1: set a deadline on the request body.
    RequestBody requestBody1 = new RequestBody() {

        @Override
        public MediaType contentType() {
            return MediaType.parse("text/plain");
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            sink.writeUtf8("abc");
            sink.timeout().deadline(5, TimeUnit.SECONDS);
        }
    };
    Request request1 = new Request.Builder().url(server.url("/")).method("POST", requestBody1).build();
    Response response1 = client.newCall(request1).execute();
    assertEquals(200, response1.code());
    // Call 2: check for the absence of a deadline on the request body.
    RequestBody requestBody2 = new RequestBody() {

        @Override
        public MediaType contentType() {
            return MediaType.parse("text/plain");
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            assertFalse(sink.timeout().hasDeadline());
            sink.writeUtf8("def");
        }
    };
    Request request2 = new Request.Builder().url(server.url("/")).method("POST", requestBody2).build();
    Response response2 = client.newCall(request2).execute();
    assertEquals(200, response2.code());
    // Use sequence numbers to confirm the connection was pooled.
    assertEquals(0, server.takeRequest().getSequenceNumber());
    assertEquals(1, server.takeRequest().getSequenceNumber());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) BufferedSink(okio.BufferedSink) Test(org.junit.Test)

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