Search in sources :

Example 56 with Connection

use of okhttp3.Connection in project okhttp by square.

the class Http2ConnectionTest method maxFrameSizeHonored.

@Test
public void maxFrameSizeHonored() throws Exception {
    byte[] buff = new byte[peer.maxOutboundDataLength() + 1];
    Arrays.fill(buff, (byte) '*');
    // write the mocking script
    peer.sendFrame().settings(new Settings());
    // ACK
    peer.acceptFrame();
    // SYN_STREAM
    peer.acceptFrame();
    peer.sendFrame().synReply(false, 3, headerEntries("a", "android"));
    // DATA
    peer.acceptFrame();
    // DATA
    peer.acceptFrame();
    peer.play();
    // play it back
    Http2Connection connection = connect(peer);
    Http2Stream stream = connection.newStream(headerEntries("b", "banana"), true);
    BufferedSink out = Okio.buffer(stream.getSink());
    out.write(buff);
    out.flush();
    out.close();
    InFrame synStream = peer.takeFrame();
    assertEquals(Http2.TYPE_HEADERS, synStream.type);
    InFrame data = peer.takeFrame();
    assertEquals(peer.maxOutboundDataLength(), data.data.length);
    data = peer.takeFrame();
    assertEquals(1, data.data.length);
}
Also used : InFrame(okhttp3.internal.http2.MockHttp2Peer.InFrame) BufferedSink(okio.BufferedSink) Test(org.junit.Test)

Example 57 with Connection

use of okhttp3.Connection in project okhttp by square.

the class HttpOverHttp2Test method cancelWithStreamNotCompleted.

/** https://github.com/square/okhttp/issues/1191 */
// TODO: recover gracefully when a connection is shutdown.
@Ignore
@Test
public void cancelWithStreamNotCompleted() throws Exception {
    // Ensure that the (shared) connection pool is in a consistent state.
    client.connectionPool().evictAll();
    assertEquals(0, client.connectionPool().connectionCount());
    server.enqueue(new MockResponse().setBody("abc"));
    server.enqueue(new MockResponse().setBody("def"));
    // Disconnect before the stream is created. A connection is still established!
    Call call1 = client.newCall(new Request.Builder().url(server.url("/")).build());
    Response response = call1.execute();
    call1.cancel();
    // That connection is pooled, and it works.
    assertEquals(1, client.connectionPool().connectionCount());
    Call call2 = client.newCall(new Request.Builder().url(server.url("/")).build());
    Response response2 = call2.execute();
    assertEquals("def", response2.body().string());
    assertEquals(0, server.takeRequest().getSequenceNumber());
    // Clean up the connection.
    response.close();
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Response(okhttp3.Response) MockResponse(okhttp3.mockwebserver.MockResponse) Call(okhttp3.Call) Request(okhttp3.Request) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 58 with Connection

use of okhttp3.Connection in project okhttp by square.

the class HttpOverHttp2Test method readResponseHeaderTimeout.

@Test
public void readResponseHeaderTimeout() throws Exception {
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.NO_RESPONSE));
    server.enqueue(new MockResponse().setBody("A"));
    client = client.newBuilder().readTimeout(1000, MILLISECONDS).build();
    // Make a call expecting a timeout reading the response headers.
    Call call1 = client.newCall(new Request.Builder().url(server.url("/")).build());
    try {
        call1.execute();
        fail("Should have timed out!");
    } catch (SocketTimeoutException expected) {
        assertEquals("timeout", expected.getMessage());
    }
    // Confirm that a subsequent request on the same connection is not impacted.
    Call call2 = client.newCall(new Request.Builder().url(server.url("/")).build());
    Response response2 = call2.execute();
    assertEquals("A", response2.body().string());
    // Confirm that the connection was reused.
    assertEquals(0, server.takeRequest().getSequenceNumber());
    assertEquals(1, server.takeRequest().getSequenceNumber());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Response(okhttp3.Response) MockResponse(okhttp3.mockwebserver.MockResponse) Call(okhttp3.Call) SocketTimeoutException(java.net.SocketTimeoutException) Request(okhttp3.Request) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 59 with Connection

use of okhttp3.Connection in project okhttp by square.

the class HttpOverHttp2Test method recoverFromOneRefusedStreamReusesConnection.

@Test
public void recoverFromOneRefusedStreamReusesConnection() throws Exception {
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.RESET_STREAM_AT_START).setHttp2ErrorCode(ErrorCode.REFUSED_STREAM.httpCode));
    server.enqueue(new MockResponse().setBody("abc"));
    Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
    Response response = call.execute();
    assertEquals("abc", response.body().string());
    // New connection.
    assertEquals(0, server.takeRequest().getSequenceNumber());
    // Reused connection.
    assertEquals(1, server.takeRequest().getSequenceNumber());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Response(okhttp3.Response) MockResponse(okhttp3.mockwebserver.MockResponse) Call(okhttp3.Call) Request(okhttp3.Request) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 60 with Connection

use of okhttp3.Connection in project okhttp by square.

the class HttpOverHttp2Test method settingsLimitsMaxConcurrentStreams.

/**
   * Push a setting that permits up to 2 concurrent streams, then make 3 concurrent requests and
   * confirm that the third concurrent request prepared a new connection.
   */
@Test
public void settingsLimitsMaxConcurrentStreams() throws Exception {
    Settings settings = new Settings();
    settings.set(Settings.MAX_CONCURRENT_STREAMS, 2);
    // Read & write a full request to confirm settings are accepted.
    server.enqueue(new MockResponse().withSettings(settings));
    Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
    Response response = call.execute();
    assertEquals("", response.body().string());
    server.enqueue(new MockResponse().setBody("ABC"));
    server.enqueue(new MockResponse().setBody("DEF"));
    server.enqueue(new MockResponse().setBody("GHI"));
    Call call1 = client.newCall(new Request.Builder().url(server.url("/")).build());
    Response response1 = call1.execute();
    Call call2 = client.newCall(new Request.Builder().url(server.url("/")).build());
    Response response2 = call2.execute();
    Call call3 = client.newCall(new Request.Builder().url(server.url("/")).build());
    Response response3 = call3.execute();
    assertEquals("ABC", response1.body().string());
    assertEquals("DEF", response2.body().string());
    assertEquals("GHI", response3.body().string());
    // Settings connection.
    assertEquals(0, server.takeRequest().getSequenceNumber());
    // Reuse settings connection.
    assertEquals(1, server.takeRequest().getSequenceNumber());
    // Reuse settings connection.
    assertEquals(2, server.takeRequest().getSequenceNumber());
    // New connection!
    assertEquals(0, server.takeRequest().getSequenceNumber());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Response(okhttp3.Response) MockResponse(okhttp3.mockwebserver.MockResponse) Call(okhttp3.Call) Request(okhttp3.Request) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) 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