Search in sources :

Example 1 with SocketRecorder

use of okhttp3.internal.SocketRecorder in project okhttp by square.

the class HttpOverHttp2Test method emptyDataFrameSentWithEmptyBody.

@Test
public void emptyDataFrameSentWithEmptyBody() throws Exception {
    server.enqueue(new MockResponse().setBody("ABC"));
    SocketRecorder socketRecorder = new SocketRecorder();
    client = client.newBuilder().sslSocketFactory(socketRecorder.sslSocketFactory(sslClient.socketFactory), sslClient.trustManager).build();
    Call call = client.newCall(new Request.Builder().url(server.url("/")).method("DELETE", Util.EMPTY_REQUEST).build());
    Response response = call.execute();
    assertEquals("ABC", response.body().string());
    // Replay the bytes written by the client to confirm an empty data frame was sent.
    SocketRecorder.RecordedSocket recordedSocket = socketRecorder.takeSocket();
    Buffer buffer = new Buffer();
    buffer.write(recordedSocket.bytesWritten());
    RecordingHandler handler = new RecordingHandler();
    Http2Reader reader = new Http2Reader(buffer, false);
    reader.readConnectionPreface(null);
    while (reader.nextFrame(false, handler)) {
    }
    assertEquals(1, handler.headerFrameCount);
    assertEquals(Collections.singletonList(0), handler.dataFrames);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Response(okhttp3.Response) Buffer(okio.Buffer) MockResponse(okhttp3.mockwebserver.MockResponse) Call(okhttp3.Call) SocketRecorder(okhttp3.internal.SocketRecorder) Test(org.junit.Test)

Example 2 with SocketRecorder

use of okhttp3.internal.SocketRecorder in project okhttp by square.

the class HttpOverHttp2Test method noDataFramesSentWithNullRequestBody.

@Test
public void noDataFramesSentWithNullRequestBody() throws Exception {
    server.enqueue(new MockResponse().setBody("ABC"));
    SocketRecorder socketRecorder = new SocketRecorder();
    client = client.newBuilder().sslSocketFactory(socketRecorder.sslSocketFactory(sslClient.socketFactory), sslClient.trustManager).build();
    Call call = client.newCall(new Request.Builder().url(server.url("/")).method("DELETE", null).build());
    Response response = call.execute();
    assertEquals("ABC", response.body().string());
    // Replay the bytes written by the client to confirm no data frames were sent.
    SocketRecorder.RecordedSocket recordedSocket = socketRecorder.takeSocket();
    Buffer buffer = new Buffer();
    buffer.write(recordedSocket.bytesWritten());
    RecordingHandler handler = new RecordingHandler();
    Http2Reader reader = new Http2Reader(buffer, false);
    reader.readConnectionPreface(null);
    while (reader.nextFrame(false, handler)) {
    }
    assertEquals(1, handler.headerFrameCount);
    assertTrue(handler.dataFrames.isEmpty());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Response(okhttp3.Response) Buffer(okio.Buffer) MockResponse(okhttp3.mockwebserver.MockResponse) Call(okhttp3.Call) SocketRecorder(okhttp3.internal.SocketRecorder) Test(org.junit.Test)

Aggregations

Call (okhttp3.Call)2 Response (okhttp3.Response)2 SocketRecorder (okhttp3.internal.SocketRecorder)2 MockResponse (okhttp3.mockwebserver.MockResponse)2 Buffer (okio.Buffer)2 Test (org.junit.Test)2