Search in sources :

Example 66 with BufferedSink

use of okio.BufferedSink in project okhttp by square.

the class DiskLruCacheTest method createJournalWithHeader.

private void createJournalWithHeader(String magic, String version, String appVersion, String valueCount, String blank, String... bodyLines) throws Exception {
    BufferedSink sink = Okio.buffer(fileSystem.sink(journalFile));
    sink.writeUtf8(magic + "\n");
    sink.writeUtf8(version + "\n");
    sink.writeUtf8(appVersion + "\n");
    sink.writeUtf8(valueCount + "\n");
    sink.writeUtf8(blank + "\n");
    for (String line : bodyLines) {
        sink.writeUtf8(line);
        sink.writeUtf8("\n");
    }
    sink.close();
}
Also used : BufferedSink(okio.BufferedSink)

Example 67 with BufferedSink

use of okio.BufferedSink in project okhttp by square.

the class DiskLruCacheTest method openWithTruncatedLineDiscardsThatLine.

@Test
public void openWithTruncatedLineDiscardsThatLine() throws Exception {
    cache.close();
    writeFile(getCleanFile("k1", 0), "A");
    writeFile(getCleanFile("k1", 1), "B");
    BufferedSink sink = Okio.buffer(fileSystem.sink(journalFile));
    // no trailing newline
    sink.writeUtf8(MAGIC + "\n" + VERSION_1 + "\n100\n2\n\nCLEAN k1 1 1");
    sink.close();
    createNewCache();
    assertNull(cache.get("k1"));
    // The journal is not corrupt when editing after a truncated line.
    set("k1", "C", "D");
    cache.close();
    createNewCache();
    assertValue("k1", "C", "D");
}
Also used : BufferedSink(okio.BufferedSink) Test(org.junit.Test)

Example 68 with BufferedSink

use of okio.BufferedSink 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 69 with BufferedSink

use of okio.BufferedSink 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 70 with BufferedSink

use of okio.BufferedSink in project okhttp by square.

the class Http2ConnectionTest method clientSendsEmptyDataServerDoesntSendWindowUpdateHttp2.

@Test
public void clientSendsEmptyDataServerDoesntSendWindowUpdateHttp2() 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(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());
}
Also used : BufferedSink(okio.BufferedSink) Test(org.junit.Test)

Aggregations

BufferedSink (okio.BufferedSink)84 Test (org.junit.Test)31 IOException (java.io.IOException)27 Buffer (okio.Buffer)18 File (java.io.File)13 RequestBody (okhttp3.RequestBody)13 Source (okio.Source)11 Response (okhttp3.Response)10 GzipSink (okio.GzipSink)10 Request (okhttp3.Request)9 InFrame (okhttp3.internal.http2.MockHttp2Peer.InFrame)9 BufferedSource (okio.BufferedSource)9 MediaType (okhttp3.MediaType)7 InterruptedIOException (java.io.InterruptedIOException)6 MockResponse (okhttp3.mockwebserver.MockResponse)5 Sink (okio.Sink)5 InputStream (java.io.InputStream)4 OkHttpClient (okhttp3.OkHttpClient)4 Request (com.squareup.okhttp.Request)3 Socket (java.net.Socket)3