Search in sources :

Example 1 with InFrame

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

the class Http2ConnectionTest method readSendsWindowUpdate.

@Test
public void readSendsWindowUpdate() throws Exception {
    int windowSize = 100;
    int windowUpdateThreshold = 50;
    // Write the mocking script.
    peer.sendFrame().settings(new Settings());
    // ACK
    peer.acceptFrame();
    // SYN_STREAM
    peer.acceptFrame();
    peer.sendFrame().synReply(false, 3, headerEntries("a", "android"));
    for (int i = 0; i < 3; i++) {
        // Send frames of summing to size 50, which is windowUpdateThreshold.
        peer.sendFrame().data(false, 3, data(24), 24);
        peer.sendFrame().data(false, 3, data(25), 25);
        peer.sendFrame().data(false, 3, data(1), 1);
        // connection WINDOW UPDATE
        peer.acceptFrame();
        // stream WINDOW UPDATE
        peer.acceptFrame();
    }
    peer.sendFrame().data(true, 3, data(0), 0);
    peer.play();
    // Play it back.
    Http2Connection connection = connect(peer);
    connection.okHttpSettings.set(INITIAL_WINDOW_SIZE, windowSize);
    Http2Stream stream = connection.newStream(headerEntries("b", "banana"), false);
    assertEquals(0, stream.unacknowledgedBytesRead);
    assertEquals(headerEntries("a", "android"), stream.takeResponseHeaders());
    Source in = stream.getSource();
    Buffer buffer = new Buffer();
    buffer.writeAll(in);
    assertEquals(-1, in.read(buffer, 1));
    assertEquals(150, buffer.size());
    InFrame synStream = peer.takeFrame();
    assertEquals(Http2.TYPE_HEADERS, synStream.type);
    for (int i = 0; i < 3; i++) {
        List<Integer> windowUpdateStreamIds = new ArrayList<>(2);
        for (int j = 0; j < 2; j++) {
            InFrame windowUpdate = peer.takeFrame();
            assertEquals(Http2.TYPE_WINDOW_UPDATE, windowUpdate.type);
            windowUpdateStreamIds.add(windowUpdate.streamId);
            assertEquals(windowUpdateThreshold, windowUpdate.windowSizeIncrement);
        }
        // connection
        assertTrue(windowUpdateStreamIds.contains(0));
        // stream
        assertTrue(windowUpdateStreamIds.contains(3));
    }
}
Also used : Buffer(okio.Buffer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InFrame(okhttp3.internal.http2.MockHttp2Peer.InFrame) ArrayList(java.util.ArrayList) Source(okio.Source) BufferedSource(okio.BufferedSource) Test(org.junit.Test)

Example 2 with InFrame

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

the class Http2ConnectionTest method receiveGoAwayHttp2.

@Test
public void receiveGoAwayHttp2() throws Exception {
    // write the mocking script
    peer.sendFrame().settings(new Settings());
    // ACK
    peer.acceptFrame();
    // SYN_STREAM 3
    peer.acceptFrame();
    // SYN_STREAM 5
    peer.acceptFrame();
    peer.sendFrame().goAway(3, ErrorCode.PROTOCOL_ERROR, EMPTY_BYTE_ARRAY);
    // PING
    peer.acceptFrame();
    peer.sendFrame().ping(true, 1, 0);
    // DATA STREAM 3
    peer.acceptFrame();
    peer.play();
    // play it back
    Http2Connection connection = connect(peer);
    Http2Stream stream1 = connection.newStream(headerEntries("a", "android"), true);
    Http2Stream stream2 = connection.newStream(headerEntries("b", "banana"), true);
    // Ensure the GO_AWAY that resets stream2 has been received.
    connection.ping().roundTripTime();
    BufferedSink sink1 = Okio.buffer(stream1.getSink());
    BufferedSink sink2 = Okio.buffer(stream2.getSink());
    sink1.writeUtf8("abc");
    try {
        sink2.writeUtf8("abc");
        sink2.flush();
        fail();
    } catch (IOException expected) {
        assertEquals("stream was reset: REFUSED_STREAM", expected.getMessage());
    }
    sink1.writeUtf8("def");
    sink1.close();
    try {
        connection.newStream(headerEntries("c", "cola"), true);
        fail();
    } catch (ConnectionShutdownException expected) {
    }
    assertTrue(stream1.isOpen());
    assertFalse(stream2.isOpen());
    assertEquals(1, connection.openStreamCount());
    // verify the peer received what was expected
    InFrame synStream1 = peer.takeFrame();
    assertEquals(Http2.TYPE_HEADERS, synStream1.type);
    InFrame synStream2 = peer.takeFrame();
    assertEquals(Http2.TYPE_HEADERS, synStream2.type);
    InFrame ping = peer.takeFrame();
    assertEquals(Http2.TYPE_PING, ping.type);
    InFrame data1 = peer.takeFrame();
    assertEquals(Http2.TYPE_DATA, data1.type);
    assertEquals(3, data1.streamId);
    assertTrue(Arrays.equals("abcdef".getBytes("UTF-8"), data1.data));
}
Also used : InFrame(okhttp3.internal.http2.MockHttp2Peer.InFrame) BufferedSink(okio.BufferedSink) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) Test(org.junit.Test)

Example 3 with InFrame

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

the class Http2ConnectionTest method serverSendsEmptyDataClientDoesntSendWindowUpdateHttp2.

@Test
public void serverSendsEmptyDataClientDoesntSendWindowUpdateHttp2() throws Exception {
    // Write the mocking script.
    peer.sendFrame().settings(new Settings());
    // ACK
    peer.acceptFrame();
    // SYN_STREAM
    peer.acceptFrame();
    peer.sendFrame().synReply(false, 3, headerEntries("a", "android"));
    peer.sendFrame().data(true, 3, data(0), 0);
    peer.play();
    // Play it back.
    Http2Connection connection = connect(peer);
    Http2Stream client = connection.newStream(headerEntries("b", "banana"), false);
    assertEquals(-1, client.getSource().read(new Buffer(), 1));
    // Verify the peer received what was expected.
    InFrame synStream = peer.takeFrame();
    assertEquals(Http2.TYPE_HEADERS, synStream.type);
    assertEquals(5, peer.frameCount());
}
Also used : Buffer(okio.Buffer) InFrame(okhttp3.internal.http2.MockHttp2Peer.InFrame) Test(org.junit.Test)

Example 4 with InFrame

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

the class Http2ConnectionTest method peerHttp2ServerLowersInitialWindowSize.

@Test
public void peerHttp2ServerLowersInitialWindowSize() throws Exception {
    Settings initial = new Settings();
    initial.set(INITIAL_WINDOW_SIZE, 1684);
    Settings shouldntImpactConnection = new Settings();
    shouldntImpactConnection.set(INITIAL_WINDOW_SIZE, 3368);
    peer.sendFrame().settings(initial);
    // ACK
    peer.acceptFrame();
    peer.sendFrame().settings(shouldntImpactConnection);
    // ACK 2
    peer.acceptFrame();
    // HEADERS
    peer.acceptFrame();
    peer.play();
    Http2Connection connection = connect(peer);
    // Verify the peer received the second ACK.
    InFrame ackFrame = peer.takeFrame();
    assertEquals(Http2.TYPE_SETTINGS, ackFrame.type);
    assertEquals(0, ackFrame.streamId);
    assertTrue(ackFrame.ack);
    // This stream was created *after* the connection settings were adjusted.
    Http2Stream stream = connection.newStream(headerEntries("a", "android"), false);
    assertEquals(3368, connection.peerSettings.getInitialWindowSize());
    // initial wasn't affected.
    assertEquals(1684, connection.bytesLeftInWriteWindow);
    // New Stream is has the most recent initial window size.
    assertEquals(3368, stream.bytesLeftInWriteWindow);
}
Also used : InFrame(okhttp3.internal.http2.MockHttp2Peer.InFrame) Test(org.junit.Test)

Example 5 with InFrame

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

the class Http2ConnectionTest method bogusDataFrameDoesNotDisruptConnection.

@Test
public void bogusDataFrameDoesNotDisruptConnection() throws Exception {
    // write the mocking script
    peer.sendFrame().settings(new Settings());
    // ACK
    peer.acceptFrame();
    peer.sendFrame().data(true, 41, new Buffer().writeUtf8("bogus"), 5);
    // RST_STREAM
    peer.acceptFrame();
    peer.sendFrame().ping(false, 2, 0);
    // PING
    peer.acceptFrame();
    peer.play();
    // play it back
    connect(peer);
    // verify the peer received what was expected
    InFrame rstStream = peer.takeFrame();
    assertEquals(Http2.TYPE_RST_STREAM, rstStream.type);
    assertEquals(41, rstStream.streamId);
    assertEquals(ErrorCode.PROTOCOL_ERROR, rstStream.errorCode);
    InFrame ping = peer.takeFrame();
    assertEquals(2, ping.payload1);
}
Also used : Buffer(okio.Buffer) 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