Search in sources :

Example 21 with InFrame

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

the class Http2ConnectionTest method readSendsWindowUpdateHttp2.

@Test
public void readSendsWindowUpdateHttp2() 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 22 with InFrame

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

the class Http2ConnectionTest method bogusReplySilentlyIgnored.

@Test
public void bogusReplySilentlyIgnored() throws Exception {
    // write the mocking script
    peer.sendFrame().settings(new Settings());
    // ACK
    peer.acceptFrame();
    peer.sendFrame().synReply(false, 41, headerEntries("a", "android"));
    peer.sendFrame().ping(false, 2, 0);
    // PING
    peer.acceptFrame();
    peer.play();
    // play it back
    connect(peer);
    // verify the peer received what was expected
    InFrame ping = peer.takeFrame();
    assertEquals(2, ping.payload1);
}
Also used : InFrame(okhttp3.internal.http2.MockHttp2Peer.InFrame) Test(org.junit.Test)

Example 23 with InFrame

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

the class Http2ConnectionTest method serverClosesClientInputStream.

@Test
public void serverClosesClientInputStream() throws Exception {
    // write the mocking script
    peer.sendFrame().settings(new Settings());
    // ACK
    peer.acceptFrame();
    // SYN_STREAM
    peer.acceptFrame();
    peer.sendFrame().synReply(false, 3, headerEntries("b", "banana"));
    peer.sendFrame().data(true, 3, new Buffer().writeUtf8("square"), 6);
    // PING
    peer.acceptFrame();
    peer.sendFrame().ping(true, 1, 0);
    peer.play();
    // play it back
    Http2Connection connection = connect(peer);
    Http2Stream stream = connection.newStream(headerEntries("a", "android"), false);
    Source source = stream.getSource();
    assertStreamData("square", source);
    // Ensure that inFinished has been received.
    connection.ping().roundTripTime();
    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);
}
Also used : Buffer(okio.Buffer) InFrame(okhttp3.internal.http2.MockHttp2Peer.InFrame) Source(okio.Source) BufferedSource(okio.BufferedSource) Test(org.junit.Test)

Example 24 with InFrame

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

the class Http2ConnectionTest method remoteOmitsInitialSettings.

@Test
public void remoteOmitsInitialSettings() throws Exception {
    // Write the mocking script. Note no SETTINGS frame is sent or acknowledged.
    // SYN_STREAM
    peer.acceptFrame();
    peer.sendFrame().synReply(false, 3, headerEntries("a", "android"));
    // GOAWAY
    peer.acceptFrame();
    peer.play();
    Http2Connection connection = new Http2Connection.Builder(true).socket(peer.openSocket()).build();
    connection.start(false);
    Http2Stream stream = connection.newStream(headerEntries("b", "banana"), false);
    try {
        stream.takeResponseHeaders();
        fail();
    } catch (IOException expected) {
        assertEquals("stream was reset: PROTOCOL_ERROR", expected.getMessage());
    }
    // verify the peer received what was expected
    InFrame synStream = peer.takeFrame();
    assertEquals(Http2.TYPE_HEADERS, synStream.type);
    InFrame goaway = peer.takeFrame();
    assertEquals(Http2.TYPE_GOAWAY, goaway.type);
    assertEquals(ErrorCode.PROTOCOL_ERROR, goaway.errorCode);
}
Also used : InFrame(okhttp3.internal.http2.MockHttp2Peer.InFrame) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) Test(org.junit.Test)

Example 25 with InFrame

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

the class Http2ConnectionTest method outgoingWritesAreBatched.

@Test
public void outgoingWritesAreBatched() 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"));
    // DATA
    peer.acceptFrame();
    peer.play();
    // play it back
    Http2Connection connection = connect(peer);
    Http2Stream stream = connection.newStream(headerEntries("b", "banana"), true);
    // two outgoing writes
    Sink sink = stream.getSink();
    sink.write(new Buffer().writeUtf8("abcde"), 5);
    sink.write(new Buffer().writeUtf8("fghij"), 5);
    sink.close();
    // verify the peer received one incoming frame
    assertEquals(Http2.TYPE_HEADERS, peer.takeFrame().type);
    InFrame data = peer.takeFrame();
    assertEquals(Http2.TYPE_DATA, data.type);
    assertTrue(Arrays.equals("abcdefghij".getBytes("UTF-8"), data.data));
    assertTrue(data.inFinished);
}
Also used : Buffer(okio.Buffer) InFrame(okhttp3.internal.http2.MockHttp2Peer.InFrame) Sink(okio.Sink) BufferedSink(okio.BufferedSink) 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