Search in sources :

Example 66 with Connection

use of okhttp3.Connection in project okhttp by square.

the class Http2ConnectionTest method sendGoAway.

@Test
public void sendGoAway() throws Exception {
    // write the mocking script
    peer.sendFrame().settings(new Settings());
    // ACK
    peer.acceptFrame();
    // SYN_STREAM 1
    peer.acceptFrame();
    // GOAWAY
    peer.acceptFrame();
    // PING
    peer.acceptFrame();
    // Should be ignored!
    peer.sendFrame().synStream(false, 2, 0, headerEntries("b", "b"));
    peer.sendFrame().ping(true, 1, 0);
    peer.play();
    // play it back
    Http2Connection connection = connect(peer);
    connection.newStream(headerEntries("a", "android"), false);
    Ping ping = connection.ping();
    connection.shutdown(ErrorCode.PROTOCOL_ERROR);
    assertEquals(1, connection.openStreamCount());
    // Prevent the peer from exiting prematurely.
    ping.roundTripTime();
    // verify the peer received what was expected
    InFrame synStream1 = peer.takeFrame();
    assertEquals(Http2.TYPE_HEADERS, synStream1.type);
    InFrame pingFrame = peer.takeFrame();
    assertEquals(Http2.TYPE_PING, pingFrame.type);
    InFrame goaway = peer.takeFrame();
    assertEquals(Http2.TYPE_GOAWAY, goaway.type);
    assertEquals(0, goaway.streamId);
    assertEquals(ErrorCode.PROTOCOL_ERROR, goaway.errorCode);
}
Also used : InFrame(okhttp3.internal.http2.MockHttp2Peer.InFrame) Test(org.junit.Test)

Example 67 with Connection

use of okhttp3.Connection in project okhttp by square.

the class URLConnectionTest method testResponseRedirectedWithPost.

private void testResponseRedirectedWithPost(int redirectCode, TransferKind transferKind) throws Exception {
    server.enqueue(new MockResponse().setResponseCode(redirectCode).addHeader("Location: /page2").setBody("This page has moved!"));
    server.enqueue(new MockResponse().setBody("Page 2"));
    connection = urlFactory.open(server.url("/page1").url());
    connection.setDoOutput(true);
    transferKind.setForRequest(connection, 4);
    byte[] requestBody = { 'A', 'B', 'C', 'D' };
    OutputStream outputStream = connection.getOutputStream();
    outputStream.write(requestBody);
    outputStream.close();
    assertEquals("Page 2", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
    assertTrue(connection.getDoOutput());
    RecordedRequest page1 = server.takeRequest();
    assertEquals("POST /page1 HTTP/1.1", page1.getRequestLine());
    assertEquals("ABCD", page1.getBody().readUtf8());
    RecordedRequest page2 = server.takeRequest();
    assertEquals("GET /page2 HTTP/1.1", page2.getRequestLine());
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) OutputStream(java.io.OutputStream)

Example 68 with Connection

use of okhttp3.Connection in project okhttp by square.

the class URLConnectionTest method connectionCloseInRequest.

@Test
public void connectionCloseInRequest() throws IOException, InterruptedException {
    // server doesn't honor the connection: close header!
    server.enqueue(new MockResponse());
    server.enqueue(new MockResponse());
    HttpURLConnection a = urlFactory.open(server.url("/").url());
    a.setRequestProperty("Connection", "close");
    assertEquals(200, a.getResponseCode());
    HttpURLConnection b = urlFactory.open(server.url("/").url());
    assertEquals(200, b.getResponseCode());
    assertEquals(0, server.takeRequest().getSequenceNumber());
    assertEquals("When connection: close is used, each request should get its own connection", 0, server.takeRequest().getSequenceNumber());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection) OkHttpURLConnection(okhttp3.internal.huc.OkHttpURLConnection) Test(org.junit.Test)

Example 69 with Connection

use of okhttp3.Connection in project okhttp by square.

the class URLConnectionTest method redirectedPostStripsRequestBodyHeaders.

@Test
public void redirectedPostStripsRequestBodyHeaders() throws Exception {
    server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP).addHeader("Location: /page2"));
    server.enqueue(new MockResponse().setBody("Page 2"));
    connection = urlFactory.open(server.url("/page1").url());
    connection.setDoOutput(true);
    connection.addRequestProperty("Content-Length", "4");
    connection.addRequestProperty("Content-Type", "text/plain; charset=utf-8");
    connection.addRequestProperty("Transfer-Encoding", "identity");
    OutputStream outputStream = connection.getOutputStream();
    outputStream.write("ABCD".getBytes("UTF-8"));
    outputStream.close();
    assertEquals("Page 2", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
    assertEquals("POST /page1 HTTP/1.1", server.takeRequest().getRequestLine());
    RecordedRequest page2 = server.takeRequest();
    assertEquals("GET /page2 HTTP/1.1", page2.getRequestLine());
    assertNull(page2.getHeader("Content-Length"));
    assertNull(page2.getHeader("Content-Type"));
    assertNull(page2.getHeader("Transfer-Encoding"));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) OutputStream(java.io.OutputStream) Test(org.junit.Test)

Example 70 with Connection

use of okhttp3.Connection in project okhttp by square.

the class URLConnectionTest method connectViaHttpsReusingConnectionsDifferentFactories.

@Test
public void connectViaHttpsReusingConnectionsDifferentFactories() throws Exception {
    server.useHttps(sslClient.socketFactory, false);
    server.enqueue(new MockResponse().setBody("this response comes via HTTPS"));
    server.enqueue(new MockResponse().setBody("another response via HTTPS"));
    // install a custom SSL socket factory so the server can be authorized
    urlFactory.setClient(urlFactory.client().newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(new RecordingHostnameVerifier()).build());
    HttpURLConnection connection1 = urlFactory.open(server.url("/").url());
    assertContent("this response comes via HTTPS", connection1);
    SSLContext sslContext2 = SSLContext.getInstance("TLS");
    sslContext2.init(null, null, null);
    SSLSocketFactory sslSocketFactory2 = sslContext2.getSocketFactory();
    TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    trustManagerFactory.init((KeyStore) null);
    X509TrustManager trustManager = (X509TrustManager) trustManagerFactory.getTrustManagers()[0];
    urlFactory.setClient(urlFactory.client().newBuilder().sslSocketFactory(sslSocketFactory2, trustManager).build());
    HttpURLConnection connection2 = urlFactory.open(server.url("/").url());
    try {
        readAscii(connection2.getInputStream(), Integer.MAX_VALUE);
        fail("without an SSL socket factory, the connection should fail");
    } catch (SSLException expected) {
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection) OkHttpURLConnection(okhttp3.internal.huc.OkHttpURLConnection) X509TrustManager(javax.net.ssl.X509TrustManager) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) SSLContext(javax.net.ssl.SSLContext) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) SSLException(javax.net.ssl.SSLException) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)226 MockResponse (okhttp3.mockwebserver.MockResponse)215 HttpURLConnection (java.net.HttpURLConnection)106 IOException (java.io.IOException)79 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)67 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)59 URLConnection (java.net.URLConnection)53 Response (okhttp3.Response)43 Connection (com.trilead.ssh2.Connection)40 InputStream (java.io.InputStream)40 Request (okhttp3.Request)38 OkHttpURLConnection (okhttp3.internal.huc.OkHttpURLConnection)36 URL (java.net.URL)32 Session (com.trilead.ssh2.Session)31 InFrame (okhttp3.internal.http2.MockHttp2Peer.InFrame)28 Buffer (okio.Buffer)26 OutputStream (java.io.OutputStream)19 InterruptedIOException (java.io.InterruptedIOException)15 Call (okhttp3.Call)14 ResponseBody (okhttp3.ResponseBody)14