Search in sources :

Example 16 with InFrame

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

the class Http2ConnectionTest method pushPromiseStreamsAutomaticallyCancel.

@Test
public void pushPromiseStreamsAutomaticallyCancel() throws Exception {
    // write the mocking script
    peer.sendFrame().settings(new Settings());
    // ACK
    peer.acceptFrame();
    peer.sendFrame().pushPromise(3, 2, Arrays.asList(new Header(Header.TARGET_METHOD, "GET"), new Header(Header.TARGET_SCHEME, "https"), new Header(Header.TARGET_AUTHORITY, "squareup.com"), new Header(Header.TARGET_PATH, "/cached")));
    peer.sendFrame().synReply(true, 2, Arrays.asList(new Header(Header.RESPONSE_STATUS, "200")));
    // RST_STREAM
    peer.acceptFrame();
    peer.play();
    // play it back
    connect(peer, PushObserver.CANCEL, REFUSE_INCOMING_STREAMS);
    // verify the peer received what was expected
    InFrame rstStream = peer.takeFrame();
    assertEquals(Http2.TYPE_RST_STREAM, rstStream.type);
    assertEquals(2, rstStream.streamId);
    assertEquals(ErrorCode.CANCEL, rstStream.errorCode);
}
Also used : InFrame(okhttp3.internal.http2.MockHttp2Peer.InFrame) Test(org.junit.Test)

Example 17 with InFrame

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

the class Http2ConnectionTest method serverClosesClientOutputStream.

@Test
public void serverClosesClientOutputStream() throws Exception {
    // write the mocking script
    peer.sendFrame().settings(new Settings());
    // ACK
    peer.acceptFrame();
    // SYN_STREAM
    peer.acceptFrame();
    peer.sendFrame().rstStream(3, ErrorCode.CANCEL);
    // 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"), true);
    BufferedSink out = Okio.buffer(stream.getSink());
    // Ensure that the RST_CANCEL has been received.
    connection.ping().roundTripTime();
    try {
        out.writeUtf8("square");
        out.flush();
        fail();
    } catch (IOException expected) {
        assertEquals("stream was reset: CANCEL", expected.getMessage());
    }
    try {
        out.close();
        fail();
    } catch (IOException expected) {
    // Close throws because buffered data wasn't flushed.
    }
    assertEquals(0, connection.openStreamCount());
    // verify the peer received what was expected
    InFrame synStream = peer.takeFrame();
    assertEquals(Http2.TYPE_HEADERS, synStream.type);
    assertFalse(synStream.inFinished);
    assertFalse(synStream.outFinished);
    InFrame ping = peer.takeFrame();
    assertEquals(Http2.TYPE_PING, ping.type);
    assertEquals(1, ping.payload1);
}
Also used : InFrame(okhttp3.internal.http2.MockHttp2Peer.InFrame) BufferedSink(okio.BufferedSink) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) Test(org.junit.Test)

Example 18 with InFrame

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

the class Http2ConnectionTest method clientPingsServer.

@Test
public void clientPingsServer() throws Exception {
    // write the mocking script
    peer.sendFrame().settings(new Settings());
    // ACK
    peer.acceptFrame();
    // PING
    peer.acceptFrame();
    peer.sendFrame().ping(true, 1, 5);
    peer.play();
    // play it back
    Http2Connection connection = connect(peer);
    Ping ping = connection.ping();
    assertTrue(ping.roundTripTime() > 0);
    assertTrue(ping.roundTripTime() < TimeUnit.SECONDS.toNanos(1));
    // verify the peer received what was expected
    InFrame pingFrame = peer.takeFrame();
    assertEquals(Http2.TYPE_PING, pingFrame.type);
    assertEquals(1, pingFrame.payload1);
    assertEquals(new Buffer().writeUtf8("OKok").readInt(), pingFrame.payload2);
    assertFalse(pingFrame.ack);
}
Also used : Buffer(okio.Buffer) InFrame(okhttp3.internal.http2.MockHttp2Peer.InFrame) Test(org.junit.Test)

Example 19 with InFrame

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

the class Http2ConnectionTest method receiveGoAway.

@Test
public void receiveGoAway() throws Exception {
    // write the mocking script
    peer.sendFrame().settings(new Settings());
    // ACK
    peer.acceptFrame();
    // SYN_STREAM 1
    peer.acceptFrame();
    // SYN_STREAM 3
    peer.acceptFrame();
    // PING.
    peer.acceptFrame();
    peer.sendFrame().goAway(3, ErrorCode.PROTOCOL_ERROR, Util.EMPTY_BYTE_ARRAY);
    peer.sendFrame().ping(true, 1, 0);
    // DATA STREAM 1
    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"), false);
        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 20 with InFrame

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

the class Http2ConnectionTest method serverPingsClientHttp2.

@Test
public void serverPingsClientHttp2() throws Exception {
    // write the mocking script
    peer.sendFrame().settings(new Settings());
    // ACK
    peer.acceptFrame();
    peer.sendFrame().ping(false, 2, 3);
    // PING
    peer.acceptFrame();
    peer.play();
    // play it back
    connect(peer);
    // verify the peer received what was expected
    InFrame ping = peer.takeFrame();
    assertEquals(Http2.TYPE_PING, ping.type);
    assertEquals(0, ping.streamId);
    assertEquals(2, ping.payload1);
    assertEquals(3, ping.payload2);
    assertTrue(ping.ack);
}
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