Search in sources :

Example 61 with Connection

use of okhttp3.Connection in project okhttp by square.

the class Http2ConnectionTest method noPingsAfterShutdown.

@Test
public void noPingsAfterShutdown() throws Exception {
    // write the mocking script
    peer.sendFrame().settings(new Settings());
    // ACK
    peer.acceptFrame();
    // GOAWAY
    peer.acceptFrame();
    peer.play();
    // play it back
    Http2Connection connection = connect(peer);
    connection.shutdown(ErrorCode.INTERNAL_ERROR);
    try {
        connection.ping();
        fail();
    } catch (ConnectionShutdownException expected) {
    }
    // verify the peer received what was expected
    InFrame goaway = peer.takeFrame();
    assertEquals(Http2.TYPE_GOAWAY, goaway.type);
    assertEquals(ErrorCode.INTERNAL_ERROR, goaway.errorCode);
}
Also used : InFrame(okhttp3.internal.http2.MockHttp2Peer.InFrame) Test(org.junit.Test)

Example 62 with Connection

use of okhttp3.Connection in project okhttp by square.

the class Http2ConnectionTest method remoteSendsRefusedStreamBeforeReplyHeaders.

@Test
public void remoteSendsRefusedStreamBeforeReplyHeaders() throws Exception {
    // write the mocking script
    peer.sendFrame().settings(new Settings());
    // ACK
    peer.acceptFrame();
    // SYN_STREAM
    peer.acceptFrame();
    peer.sendFrame().rstStream(3, ErrorCode.REFUSED_STREAM);
    peer.sendFrame().ping(false, 2, 0);
    // PING
    peer.acceptFrame();
    peer.play();
    // play it back
    Http2Connection connection = connect(peer);
    Http2Stream stream = connection.newStream(headerEntries("a", "android"), false);
    try {
        stream.takeResponseHeaders();
        fail();
    } catch (IOException expected) {
        assertEquals("stream was reset: REFUSED_STREAM", expected.getMessage());
    }
    assertEquals(0, connection.openStreamCount());
    // verify the peer received what was expected
    InFrame synStream = peer.takeFrame();
    assertEquals(Http2.TYPE_HEADERS, synStream.type);
    InFrame ping = peer.takeFrame();
    assertEquals(Http2.TYPE_PING, ping.type);
    assertEquals(2, ping.payload1);
}
Also used : InFrame(okhttp3.internal.http2.MockHttp2Peer.InFrame) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) Test(org.junit.Test)

Example 63 with Connection

use of okhttp3.Connection in project okhttp by square.

the class Http2ConnectionTest method clientClosesClientInputStream.

/**
   * Test that the client sends a RST_STREAM if doing so won't disrupt the output stream.
   */
@Test
public void clientClosesClientInputStream() throws Exception {
    // write the mocking script
    peer.sendFrame().settings(new Settings());
    // ACK
    peer.acceptFrame();
    // SYN_STREAM
    peer.acceptFrame();
    // RST_STREAM
    peer.acceptFrame();
    peer.play();
    // play it back
    Http2Connection connection = connect(peer);
    Http2Stream stream = connection.newStream(headerEntries("a", "android"), false);
    Source in = stream.getSource();
    BufferedSink out = Okio.buffer(stream.getSink());
    in.close();
    try {
        in.read(new Buffer(), 1);
        fail();
    } catch (IOException expected) {
        assertEquals("stream closed", expected.getMessage());
    }
    try {
        out.writeUtf8("a");
        out.flush();
        fail();
    } catch (IOException expected) {
        assertEquals("stream finished", expected.getMessage());
    }
    assertEquals(0, connection.openStreamCount());
    // verify the peer received what was expected
    InFrame synStream = peer.takeFrame();
    assertEquals(Http2.TYPE_HEADERS, synStream.type);
    assertTrue(synStream.inFinished);
    assertFalse(synStream.outFinished);
    InFrame rstStream = peer.takeFrame();
    assertEquals(Http2.TYPE_RST_STREAM, rstStream.type);
    assertEquals(ErrorCode.CANCEL, rstStream.errorCode);
}
Also used : Buffer(okio.Buffer) InFrame(okhttp3.internal.http2.MockHttp2Peer.InFrame) BufferedSink(okio.BufferedSink) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) Source(okio.Source) BufferedSource(okio.BufferedSource) Test(org.junit.Test)

Example 64 with Connection

use of okhttp3.Connection in project okhttp by square.

the class Http2ConnectionTest method headers.

@Test
public void headers() throws Exception {
    // write the mocking script
    peer.sendFrame().settings(new Settings());
    // ACK
    peer.acceptFrame();
    // SYN_STREAM
    peer.acceptFrame();
    // PING
    peer.acceptFrame();
    peer.sendFrame().synReply(false, 3, headerEntries("a", "android"));
    peer.sendFrame().headers(3, headerEntries("c", "c3po"));
    peer.sendFrame().ping(true, 1, 0);
    peer.play();
    // play it back
    Http2Connection connection = connect(peer);
    Http2Stream stream = connection.newStream(headerEntries("b", "banana"), true);
    // Ensure that the HEADERS has been received.
    connection.ping().roundTripTime();
    assertEquals(Arrays.asList(new Header("a", "android"), null, new Header("c", "c3po")), stream.takeResponseHeaders());
    // verify the peer received what was expected
    InFrame synStream = peer.takeFrame();
    assertEquals(Http2.TYPE_HEADERS, synStream.type);
    InFrame ping = peer.takeFrame();
    assertEquals(Http2.TYPE_PING, ping.type);
}
Also used : InFrame(okhttp3.internal.http2.MockHttp2Peer.InFrame) Test(org.junit.Test)

Example 65 with Connection

use of okhttp3.Connection in project okhttp by square.

the class Http2ConnectionTest method peerSetsZeroFlowControl.

/**
   * Webservers may set the initial window size to zero, which is a special case because it means
   * that we have to flush headers immediately before any request body can be sent.
   * https://github.com/square/okhttp/issues/2543
   */
@Test
public void peerSetsZeroFlowControl() throws Exception {
    peer.setClient(true);
    // Write the mocking script.
    peer.sendFrame().settings(new Settings().set(INITIAL_WINDOW_SIZE, 0));
    // ACK
    peer.acceptFrame();
    // Increase the connection window size.
    peer.sendFrame().windowUpdate(0, 10);
    // PING
    peer.acceptFrame();
    peer.sendFrame().ping(true, 1, 0);
    // HEADERS STREAM 3
    peer.acceptFrame();
    peer.sendFrame().windowUpdate(3, 5);
    // DATA STREAM 3 "abcde"
    peer.acceptFrame();
    peer.sendFrame().windowUpdate(3, 5);
    // DATA STREAM 3 "fghi"
    peer.acceptFrame();
    peer.play();
    // Play it back.
    Http2Connection connection = connect(peer);
    // Ensure the SETTINGS have been received.
    connection.ping().roundTripTime();
    Http2Stream stream = connection.newStream(headerEntries("a", "android"), true);
    BufferedSink sink = Okio.buffer(stream.getSink());
    sink.writeUtf8("abcdefghi");
    sink.flush();
    // Verify the peer received what was expected.
    // PING
    peer.takeFrame();
    InFrame headers = peer.takeFrame();
    assertEquals(Http2.TYPE_HEADERS, headers.type);
    InFrame data1 = peer.takeFrame();
    assertEquals(Http2.TYPE_DATA, data1.type);
    assertEquals(3, data1.streamId);
    assertTrue(Arrays.equals("abcde".getBytes("UTF-8"), data1.data));
    InFrame data2 = peer.takeFrame();
    assertEquals(Http2.TYPE_DATA, data2.type);
    assertEquals(3, data2.streamId);
    assertTrue(Arrays.equals("fghi".getBytes("UTF-8"), data2.data));
}
Also used : InFrame(okhttp3.internal.http2.MockHttp2Peer.InFrame) 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)79 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)67 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)59 URLConnection (java.net.URLConnection)53 Response (okhttp3.Response)43 Connection (com.trilead.ssh2.Connection)40 InputStream (java.io.InputStream)40 Request (okhttp3.Request)38 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)26 OutputStream (java.io.OutputStream)19 InterruptedIOException (java.io.InterruptedIOException)15 Call (okhttp3.Call)14 ResponseBody (okhttp3.ResponseBody)14