Search in sources :

Example 96 with Connection

use of okhttp3.Connection in project okhttp by square.

the class URLConnectionTest method clientConfiguredCustomContentEncoding.

@Test
public void clientConfiguredCustomContentEncoding() throws Exception {
    server.enqueue(new MockResponse().setBody("ABCDE").addHeader("Content-Encoding: custom"));
    URLConnection connection = urlFactory.open(server.url("/").url());
    connection.addRequestProperty("Accept-Encoding", "custom");
    assertEquals("ABCDE", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
    RecordedRequest request = server.takeRequest();
    assertEquals("custom", request.getHeader("Accept-Encoding"));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection) OkHttpURLConnection(okhttp3.internal.huc.OkHttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Example 97 with Connection

use of okhttp3.Connection in project okhttp by square.

the class URLConnectionTest method authCallsForHeader.

private List<String> authCallsForHeader(String authHeader) throws IOException {
    boolean proxy = authHeader.startsWith("Proxy-");
    int responseCode = proxy ? 407 : 401;
    RecordingAuthenticator authenticator = new RecordingAuthenticator(null);
    Authenticator.setDefault(authenticator);
    MockResponse pleaseAuthenticate = new MockResponse().setResponseCode(responseCode).addHeader(authHeader).setBody("Please authenticate.");
    server.enqueue(pleaseAuthenticate);
    if (proxy) {
        urlFactory.setClient(urlFactory.client().newBuilder().proxy(server.toProxyAddress()).proxyAuthenticator(new JavaNetAuthenticator()).build());
        connection = urlFactory.open(new URL("http://android.com/"));
    } else {
        urlFactory.setClient(urlFactory.client().newBuilder().authenticator(new JavaNetAuthenticator()).build());
        connection = urlFactory.open(server.url("/").url());
    }
    assertEquals(responseCode, connection.getResponseCode());
    connection.getErrorStream().close();
    return authenticator.calls;
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) RecordingAuthenticator(okhttp3.internal.RecordingAuthenticator) URL(java.net.URL)

Example 98 with Connection

use of okhttp3.Connection in project okhttp by square.

the class URLConnectionTest method emptyResponseHeaderValueIsAllowed.

@Test
public void emptyResponseHeaderValueIsAllowed() throws Exception {
    server.enqueue(new MockResponse().addHeader("A:").setBody("body"));
    connection = urlFactory.open(server.url("/").url());
    assertContent("body", connection);
    assertEquals("", connection.getHeaderField("A"));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 99 with Connection

use of okhttp3.Connection in project okhttp by square.

the class URLConnectionTest method streamedBodyWithClientRequestTimeout.

@Test
public void streamedBodyWithClientRequestTimeout() throws Exception {
    enqueueClientRequestTimeoutResponses();
    HttpURLConnection connection = urlFactory.open(server.url("/").url());
    connection.setRequestMethod("POST");
    connection.setChunkedStreamingMode(0);
    connection.getOutputStream().write("Hello".getBytes("UTF-8"));
    assertEquals(408, connection.getResponseCode());
    assertEquals(1, server.getRequestCount());
    connection.getErrorStream().close();
}
Also used : HttpURLConnection(java.net.HttpURLConnection) OkHttpURLConnection(okhttp3.internal.huc.OkHttpURLConnection) Test(org.junit.Test)

Example 100 with Connection

use of okhttp3.Connection in project okhttp by square.

the class URLConnectionTest method disconnectDuringConnect_cookieJar.

@Test
public void disconnectDuringConnect_cookieJar() throws Exception {
    final AtomicReference<HttpURLConnection> connectionHolder = new AtomicReference<>();
    class DisconnectingCookieJar implements CookieJar {

        @Override
        public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
        }

        @Override
        public List<Cookie> loadForRequest(HttpUrl url) {
            connectionHolder.get().disconnect();
            return Collections.emptyList();
        }
    }
    OkHttpClient client = new okhttp3.OkHttpClient.Builder().cookieJar(new DisconnectingCookieJar()).build();
    URL url = server.url("path that should never be accessed").url();
    HttpURLConnection connection = new OkHttpURLConnection(url, client);
    connectionHolder.set(connection);
    try {
        connection.getInputStream();
        fail("Connection should not be established");
    } catch (IOException expected) {
        assertEquals("Canceled", expected.getMessage());
    } finally {
        connection.disconnect();
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) OkHttpURLConnection(okhttp3.internal.huc.OkHttpURLConnection) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) OkHttpURLConnection(okhttp3.internal.huc.OkHttpURLConnection) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)226 MockResponse (okhttp3.mockwebserver.MockResponse)215 HttpURLConnection (java.net.HttpURLConnection)106 IOException (java.io.IOException)73 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)67 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)59 URLConnection (java.net.URLConnection)53 Response (okhttp3.Response)41 InputStream (java.io.InputStream)39 Connection (com.trilead.ssh2.Connection)36 Request (okhttp3.Request)36 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)25 OutputStream (java.io.OutputStream)19 InterruptedIOException (java.io.InterruptedIOException)15 Call (okhttp3.Call)14 BufferedSink (okio.BufferedSink)14