Search in sources :

Example 11 with InFrame

use of okhttp3.internal.http2.MockHttp2Peer.InFrame 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 12 with InFrame

use of okhttp3.internal.http2.MockHttp2Peer.InFrame 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 13 with InFrame

use of okhttp3.internal.http2.MockHttp2Peer.InFrame 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)

Example 14 with InFrame

use of okhttp3.internal.http2.MockHttp2Peer.InFrame in project okhttp by square.

the class Http2ConnectionTest method sendGoAway.

@Test
public void sendGoAway() throws Exception {
    // write the mocking script
    peer.sendFrame().settings(new Settings());
    // ACK
    peer.acceptFrame();
    // SYN_STREAM 1
    peer.acceptFrame();
    // GOAWAY
    peer.acceptFrame();
    // PING
    peer.acceptFrame();
    // Should be ignored!
    peer.sendFrame().synStream(false, 2, 0, headerEntries("b", "b"));
    peer.sendFrame().ping(true, 1, 0);
    peer.play();
    // play it back
    Http2Connection connection = connect(peer);
    connection.newStream(headerEntries("a", "android"), false);
    Ping ping = connection.ping();
    connection.shutdown(ErrorCode.PROTOCOL_ERROR);
    assertEquals(1, connection.openStreamCount());
    // Prevent the peer from exiting prematurely.
    ping.roundTripTime();
    // verify the peer received what was expected
    InFrame synStream1 = peer.takeFrame();
    assertEquals(Http2.TYPE_HEADERS, synStream1.type);
    InFrame pingFrame = peer.takeFrame();
    assertEquals(Http2.TYPE_PING, pingFrame.type);
    InFrame goaway = peer.takeFrame();
    assertEquals(Http2.TYPE_GOAWAY, goaway.type);
    assertEquals(0, goaway.streamId);
    assertEquals(ErrorCode.PROTOCOL_ERROR, goaway.errorCode);
}
Also used : InFrame(okhttp3.internal.http2.MockHttp2Peer.InFrame) Test(org.junit.Test)

Example 15 with InFrame

use of okhttp3.internal.http2.MockHttp2Peer.InFrame in project okhttp by square.

the class Http2ConnectionTest method unexpectedPingIsNotReturned.

@Test
public void unexpectedPingIsNotReturned() throws Exception {
    // write the mocking script
    peer.sendFrame().settings(new Settings());
    // ACK
    peer.acceptFrame();
    peer.sendFrame().ping(false, 2, 0);
    // PING
    peer.acceptFrame();
    // This ping will not be returned.
    peer.sendFrame().ping(true, 3, 0);
    peer.sendFrame().ping(false, 4, 0);
    // PING
    peer.acceptFrame();
    peer.play();
    // play it back
    connect(peer);
    // verify the peer received what was expected
    InFrame ping2 = peer.takeFrame();
    assertEquals(2, ping2.payload1);
    InFrame ping4 = peer.takeFrame();
    assertEquals(4, ping4.payload1);
}
Also used : InFrame(okhttp3.internal.http2.MockHttp2Peer.InFrame) Test(org.junit.Test)

Aggregations

InFrame (okhttp3.internal.http2.MockHttp2Peer.InFrame)34 Test (org.junit.Test)33 Buffer (okio.Buffer)14 BufferedSink (okio.BufferedSink)10 IOException (java.io.IOException)8 InterruptedIOException (java.io.InterruptedIOException)8 BufferedSource (okio.BufferedSource)5 Source (okio.Source)5 ArrayList (java.util.ArrayList)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Sink (okio.Sink)1