use of okio.BufferedSink 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));
}
use of okio.BufferedSink in project okhttp by square.
the class Http2ConnectionTest method clientSendsEmptyDataServerDoesntSendWindowUpdate.
@Test
public void clientSendsEmptyDataServerDoesntSendWindowUpdate() throws Exception {
// Write the mocking script.
peer.sendFrame().settings(new Settings());
// ACK
peer.acceptFrame();
// SYN_STREAM
peer.acceptFrame();
// DATA
peer.acceptFrame();
peer.sendFrame().synReply(false, 3, headerEntries("a", "android"));
peer.play();
// Play it back.
Http2Connection connection = connect(peer);
Http2Stream client = connection.newStream(headerEntries("b", "banana"), true);
BufferedSink out = Okio.buffer(client.getSink());
out.write(Util.EMPTY_BYTE_ARRAY);
out.flush();
out.close();
// Verify the peer received what was expected.
assertEquals(Http2.TYPE_HEADERS, peer.takeFrame().type);
assertEquals(Http2.TYPE_DATA, peer.takeFrame().type);
assertEquals(5, peer.frameCount());
}
use of okio.BufferedSink in project okhttp by square.
the class Http2ConnectionTest method maxFrameSizeHonored.
@Test
public void maxFrameSizeHonored() throws Exception {
byte[] buff = new byte[peer.maxOutboundDataLength() + 1];
Arrays.fill(buff, (byte) '*');
// 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();
// DATA
peer.acceptFrame();
peer.play();
// play it back
Http2Connection connection = connect(peer);
Http2Stream stream = connection.newStream(headerEntries("b", "banana"), true);
BufferedSink out = Okio.buffer(stream.getSink());
out.write(buff);
out.flush();
out.close();
InFrame synStream = peer.takeFrame();
assertEquals(Http2.TYPE_HEADERS, synStream.type);
InFrame data = peer.takeFrame();
assertEquals(peer.maxOutboundDataLength(), data.data.length);
data = peer.takeFrame();
assertEquals(1, data.data.length);
}
use of okio.BufferedSink in project okhttp by square.
the class RelayTest method racingReaders.
@Test
public void racingReaders() throws Exception {
Pipe pipe = new Pipe(1024);
BufferedSink sink = Okio.buffer(pipe.sink());
Relay relay = Relay.edit(file, pipe.source(), metadata, 5);
Future<ByteString> future1 = executor.submit(sourceReader(relay.newSource()));
Future<ByteString> future2 = executor.submit(sourceReader(relay.newSource()));
Thread.sleep(500);
sink.writeUtf8("abcdefghij");
Thread.sleep(500);
sink.writeUtf8("klmnopqrst");
sink.close();
assertEquals(ByteString.encodeUtf8("abcdefghijklmnopqrst"), future1.get());
assertEquals(ByteString.encodeUtf8("abcdefghijklmnopqrst"), future2.get());
assertTrue(relay.isClosed());
assertFile(Relay.PREFIX_CLEAN, 20L, metadata.size(), "abcdefghijklmnopqrst", metadata);
}
use of okio.BufferedSink 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);
}
Aggregations