Search in sources :

Example 26 with InFrame

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

the class Http2ConnectionTest method remoteDoubleSynReply.

@Test
public void remoteDoubleSynReply() 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"));
    // PING
    peer.acceptFrame();
    peer.sendFrame().synReply(false, 3, headerEntries("b", "banana"));
    peer.sendFrame().ping(true, 1, 0);
    peer.play();
    // play it back
    Http2Connection connection = connect(peer);
    Http2Stream stream = connection.newStream(headerEntries("c", "cola"), false);
    assertEquals(headerEntries("a", "android"), stream.takeResponseHeaders());
    // Ensure that the 2nd SYN REPLY has been received.
    connection.ping().roundTripTime();
    // 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 27 with InFrame

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

the class Http2ConnectionTest method connect.

/** Builds a new connection to {@code peer} with settings acked. */
private Http2Connection connect(MockHttp2Peer peer, PushObserver pushObserver, Http2Connection.Listener listener) throws Exception {
    Http2Connection connection = new Http2Connection.Builder(true).socket(peer.openSocket()).pushObserver(pushObserver).listener(listener).build();
    connection.start(false);
    // verify the peer received the ACK
    InFrame ackFrame = peer.takeFrame();
    assertEquals(Http2.TYPE_SETTINGS, ackFrame.type);
    assertEquals(0, ackFrame.streamId);
    assertTrue(ackFrame.ack);
    return connection;
}
Also used : InFrame(okhttp3.internal.http2.MockHttp2Peer.InFrame)

Example 28 with InFrame

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

the class Http2ConnectionTest method close.

@Test
public void close() throws Exception {
    // write the mocking script
    peer.sendFrame().settings(new Settings());
    // ACK
    peer.acceptFrame();
    // SYN_STREAM
    peer.acceptFrame();
    // GOAWAY
    peer.acceptFrame();
    // RST_STREAM
    peer.acceptFrame();
    peer.play();
    // play it back
    Http2Connection connection = connect(peer);
    Http2Stream stream = connection.newStream(headerEntries("a", "android"), false);
    assertEquals(1, connection.openStreamCount());
    connection.close();
    assertEquals(0, connection.openStreamCount());
    try {
        connection.newStream(headerEntries("b", "banana"), false);
        fail();
    } catch (ConnectionShutdownException expected) {
    }
    BufferedSink sink = Okio.buffer(stream.getSink());
    try {
        sink.writeByte(0);
        sink.flush();
        fail();
    } catch (IOException expected) {
        assertEquals("stream finished", expected.getMessage());
    }
    try {
        stream.getSource().read(new Buffer(), 1);
        fail();
    } catch (IOException expected) {
        assertEquals("stream was reset: CANCEL", 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);
    InFrame rstStream = peer.takeFrame();
    assertEquals(Http2.TYPE_RST_STREAM, rstStream.type);
    assertEquals(3, rstStream.streamId);
}
Also used : Buffer(okio.Buffer) InFrame(okhttp3.internal.http2.MockHttp2Peer.InFrame) BufferedSink(okio.BufferedSink) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) Test(org.junit.Test)

Example 29 with InFrame

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

the class Http2ConnectionTest method clientPingsServerHttp2.

@Test
public void clientPingsServerHttp2() 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(0, pingFrame.streamId);
    assertEquals(1, pingFrame.payload1);
    // connection.ping() sets this.
    assertEquals(0x4f4b6f6b, pingFrame.payload2);
    assertFalse(pingFrame.ack);
}
Also used : InFrame(okhttp3.internal.http2.MockHttp2Peer.InFrame) Test(org.junit.Test)

Example 30 with InFrame

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

the class Http2ConnectionTest method clientClosesClientInputStreamIfOutputStreamIsClosed.

/**
   * Test that the client doesn't send a RST_STREAM if doing so will disrupt the output stream.
   */
@Test
public void clientClosesClientInputStreamIfOutputStreamIsClosed() throws Exception {
    // write the mocking script
    peer.sendFrame().settings(new Settings());
    // ACK
    peer.acceptFrame();
    // SYN_STREAM
    peer.acceptFrame();
    // DATA
    peer.acceptFrame();
    // DATA with FLAG_FIN
    peer.acceptFrame();
    // RST_STREAM
    peer.acceptFrame();
    peer.play();
    // play it back
    Http2Connection connection = connect(peer);
    Http2Stream stream = connection.newStream(headerEntries("a", "android"), true);
    Source source = stream.getSource();
    BufferedSink out = Okio.buffer(stream.getSink());
    source.close();
    try {
        source.read(new Buffer(), 1);
        fail();
    } catch (IOException expected) {
        assertEquals("stream closed", expected.getMessage());
    }
    out.writeUtf8("square");
    out.flush();
    out.close();
    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 data = peer.takeFrame();
    assertEquals(Http2.TYPE_DATA, data.type);
    assertTrue(Arrays.equals("square".getBytes("UTF-8"), data.data));
    InFrame fin = peer.takeFrame();
    assertEquals(Http2.TYPE_DATA, fin.type);
    assertTrue(fin.inFinished);
    assertFalse(fin.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)

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