Search in sources :

Example 46 with Headers

use of okhttp3.Headers in project okhttp by square.

the class MockWebServerTest method headersToList.

private List<String> headersToList(MockResponse response) {
    Headers headers = response.getHeaders();
    int size = headers.size();
    List<String> headerList = new ArrayList<>(size);
    for (int i = 0; i < size; i++) {
        headerList.add(headers.name(i) + ": " + headers.value(i));
    }
    return headerList;
}
Also used : Headers(okhttp3.Headers) ArrayList(java.util.ArrayList)

Example 47 with Headers

use of okhttp3.Headers in project okhttp by square.

the class Http2ConnectionTest method peerHttp2ServerLowersInitialWindowSize.

@Test
public void peerHttp2ServerLowersInitialWindowSize() throws Exception {
    Settings initial = new Settings();
    initial.set(INITIAL_WINDOW_SIZE, 1684);
    Settings shouldntImpactConnection = new Settings();
    shouldntImpactConnection.set(INITIAL_WINDOW_SIZE, 3368);
    peer.sendFrame().settings(initial);
    // ACK
    peer.acceptFrame();
    peer.sendFrame().settings(shouldntImpactConnection);
    // ACK 2
    peer.acceptFrame();
    // HEADERS
    peer.acceptFrame();
    peer.play();
    Http2Connection connection = connect(peer);
    // Verify the peer received the second ACK.
    InFrame ackFrame = peer.takeFrame();
    assertEquals(Http2.TYPE_SETTINGS, ackFrame.type);
    assertEquals(0, ackFrame.streamId);
    assertTrue(ackFrame.ack);
    // This stream was created *after* the connection settings were adjusted.
    Http2Stream stream = connection.newStream(headerEntries("a", "android"), false);
    assertEquals(3368, connection.peerSettings.getInitialWindowSize());
    // initial wasn't affected.
    assertEquals(1684, connection.bytesLeftInWriteWindow);
    // New Stream is has the most recent initial window size.
    assertEquals(3368, stream.bytesLeftInWriteWindow);
}
Also used : InFrame(okhttp3.internal.http2.MockHttp2Peer.InFrame) Test(org.junit.Test)

Example 48 with Headers

use of okhttp3.Headers in project okhttp by square.

the class HttpOverHttp2Test method readResponseHeaderTimeout.

@Test
public void readResponseHeaderTimeout() throws Exception {
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.NO_RESPONSE));
    server.enqueue(new MockResponse().setBody("A"));
    client = client.newBuilder().readTimeout(1000, MILLISECONDS).build();
    // Make a call expecting a timeout reading the response headers.
    Call call1 = client.newCall(new Request.Builder().url(server.url("/")).build());
    try {
        call1.execute();
        fail("Should have timed out!");
    } catch (SocketTimeoutException expected) {
        assertEquals("timeout", expected.getMessage());
    }
    // Confirm that a subsequent request on the same connection is not impacted.
    Call call2 = client.newCall(new Request.Builder().url(server.url("/")).build());
    Response response2 = call2.execute();
    assertEquals("A", response2.body().string());
    // Confirm that the connection was reused.
    assertEquals(0, server.takeRequest().getSequenceNumber());
    assertEquals(1, server.takeRequest().getSequenceNumber());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Response(okhttp3.Response) MockResponse(okhttp3.mockwebserver.MockResponse) Call(okhttp3.Call) SocketTimeoutException(java.net.SocketTimeoutException) Request(okhttp3.Request) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 49 with Headers

use of okhttp3.Headers in project okhttp by square.

the class Http2ConnectionTest method headers.

@Test
public void headers() throws Exception {
    // write the mocking script
    peer.sendFrame().settings(new Settings());
    // ACK
    peer.acceptFrame();
    // SYN_STREAM
    peer.acceptFrame();
    // PING
    peer.acceptFrame();
    peer.sendFrame().synReply(false, 3, headerEntries("a", "android"));
    peer.sendFrame().headers(3, headerEntries("c", "c3po"));
    peer.sendFrame().ping(true, 1, 0);
    peer.play();
    // play it back
    Http2Connection connection = connect(peer);
    Http2Stream stream = connection.newStream(headerEntries("b", "banana"), true);
    // Ensure that the HEADERS has been received.
    connection.ping().roundTripTime();
    assertEquals(Arrays.asList(new Header("a", "android"), null, new Header("c", "c3po")), stream.takeResponseHeaders());
    // 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 50 with Headers

use of okhttp3.Headers in project okhttp by square.

the class Http2ConnectionTest method peerSetsZeroFlowControl.

/**
   * Webservers may set the initial window size to zero, which is a special case because it means
   * that we have to flush headers immediately before any request body can be sent.
   * https://github.com/square/okhttp/issues/2543
   */
@Test
public void peerSetsZeroFlowControl() throws Exception {
    peer.setClient(true);
    // Write the mocking script.
    peer.sendFrame().settings(new Settings().set(INITIAL_WINDOW_SIZE, 0));
    // ACK
    peer.acceptFrame();
    // Increase the connection window size.
    peer.sendFrame().windowUpdate(0, 10);
    // PING
    peer.acceptFrame();
    peer.sendFrame().ping(true, 1, 0);
    // HEADERS STREAM 3
    peer.acceptFrame();
    peer.sendFrame().windowUpdate(3, 5);
    // DATA STREAM 3 "abcde"
    peer.acceptFrame();
    peer.sendFrame().windowUpdate(3, 5);
    // DATA STREAM 3 "fghi"
    peer.acceptFrame();
    peer.play();
    // Play it back.
    Http2Connection connection = connect(peer);
    // Ensure the SETTINGS have been received.
    connection.ping().roundTripTime();
    Http2Stream stream = connection.newStream(headerEntries("a", "android"), true);
    BufferedSink sink = Okio.buffer(stream.getSink());
    sink.writeUtf8("abcdefghi");
    sink.flush();
    // Verify the peer received what was expected.
    // PING
    peer.takeFrame();
    InFrame headers = peer.takeFrame();
    assertEquals(Http2.TYPE_HEADERS, headers.type);
    InFrame data1 = peer.takeFrame();
    assertEquals(Http2.TYPE_DATA, data1.type);
    assertEquals(3, data1.streamId);
    assertTrue(Arrays.equals("abcde".getBytes("UTF-8"), data1.data));
    InFrame data2 = peer.takeFrame();
    assertEquals(Http2.TYPE_DATA, data2.type);
    assertEquals(3, data2.streamId);
    assertTrue(Arrays.equals("fghi".getBytes("UTF-8"), data2.data));
}
Also used : InFrame(okhttp3.internal.http2.MockHttp2Peer.InFrame) BufferedSink(okio.BufferedSink) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)69 Request (okhttp3.Request)58 Response (okhttp3.Response)53 Headers (okhttp3.Headers)46 IOException (java.io.IOException)34 MockResponse (okhttp3.mockwebserver.MockResponse)33 HttpHeaders (okhttp3.internal.http.HttpHeaders)29 ResponseBody (okhttp3.ResponseBody)27 RequestBody (okhttp3.RequestBody)21 List (java.util.List)19 MediaType (okhttp3.MediaType)16 HashMap (java.util.HashMap)15 Map (java.util.Map)15 ANResponse (com.androidnetworking.common.ANResponse)13 AnalyticsListener (com.androidnetworking.interfaces.AnalyticsListener)13 LinkedHashMap (java.util.LinkedHashMap)13 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)13 Buffer (okio.Buffer)12 ANError (com.androidnetworking.error.ANError)11 HttpURLConnection (java.net.HttpURLConnection)11