Search in sources :

Example 1 with PushPromise

use of okhttp3.mockwebserver.PushPromise in project okhttp by square.

the class HttpOverHttp2Test method serverSendsPushPromise_GET.

@Test
public void serverSendsPushPromise_GET() throws Exception {
    PushPromise pushPromise = new PushPromise("GET", "/foo/bar", Headers.of("foo", "bar"), new MockResponse().setBody("bar").setStatus("HTTP/1.1 200 Sweet"));
    server.enqueue(new MockResponse().setBody("ABCDE").setStatus("HTTP/1.1 200 Sweet").withPush(pushPromise));
    Call call = client.newCall(new Request.Builder().url(server.url("/foo")).build());
    Response response = call.execute();
    assertEquals("ABCDE", response.body().string());
    assertEquals(200, response.code());
    assertEquals("Sweet", response.message());
    RecordedRequest request = server.takeRequest();
    assertEquals("GET /foo HTTP/1.1", request.getRequestLine());
    assertEquals("https", request.getHeader(":scheme"));
    assertEquals(server.getHostName() + ":" + server.getPort(), request.getHeader(":authority"));
    RecordedRequest pushedRequest = server.takeRequest();
    assertEquals("GET /foo/bar HTTP/1.1", pushedRequest.getRequestLine());
    assertEquals("bar", pushedRequest.getHeader("foo"));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Response(okhttp3.Response) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Call(okhttp3.Call) Request(okhttp3.Request) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) PushPromise(okhttp3.mockwebserver.PushPromise) Test(org.junit.Test)

Example 2 with PushPromise

use of okhttp3.mockwebserver.PushPromise in project okhttp by square.

the class HttpOverHttp2Test method serverSendsPushPromise_HEAD.

@Test
public void serverSendsPushPromise_HEAD() throws Exception {
    PushPromise pushPromise = new PushPromise("HEAD", "/foo/bar", Headers.of("foo", "bar"), new MockResponse().setStatus("HTTP/1.1 204 Sweet"));
    server.enqueue(new MockResponse().setBody("ABCDE").setStatus("HTTP/1.1 200 Sweet").withPush(pushPromise));
    Call call = client.newCall(new Request.Builder().url(server.url("/foo")).build());
    Response response = call.execute();
    assertEquals("ABCDE", response.body().string());
    assertEquals(200, response.code());
    assertEquals("Sweet", response.message());
    RecordedRequest request = server.takeRequest();
    assertEquals("GET /foo HTTP/1.1", request.getRequestLine());
    assertEquals("https", request.getHeader(":scheme"));
    assertEquals(server.getHostName() + ":" + server.getPort(), request.getHeader(":authority"));
    RecordedRequest pushedRequest = server.takeRequest();
    assertEquals("HEAD /foo/bar HTTP/1.1", pushedRequest.getRequestLine());
    assertEquals("bar", pushedRequest.getHeader("foo"));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Response(okhttp3.Response) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Call(okhttp3.Call) Request(okhttp3.Request) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) PushPromise(okhttp3.mockwebserver.PushPromise) Test(org.junit.Test)

Example 3 with PushPromise

use of okhttp3.mockwebserver.PushPromise 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)

Aggregations

Test (org.junit.Test)3 Call (okhttp3.Call)2 Request (okhttp3.Request)2 Response (okhttp3.Response)2 MockResponse (okhttp3.mockwebserver.MockResponse)2 PushPromise (okhttp3.mockwebserver.PushPromise)2 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)2 InFrame (okhttp3.internal.http2.MockHttp2Peer.InFrame)1