Search in sources :

Example 76 with URLConnection

use of java.net.URLConnection in project okhttp by square.

the class URLConnectionTest method urlWithSpaceInHost.

@Test
public void urlWithSpaceInHost() throws Exception {
    URLConnection urlConnection = urlFactory.open(new URL("http://and roid.com/"));
    try {
        urlConnection.getInputStream();
        fail();
    } catch (UnknownHostException expected) {
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) HttpURLConnection(java.net.HttpURLConnection) OkHttpURLConnection(okhttp3.internal.huc.OkHttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) URL(java.net.URL) Test(org.junit.Test)

Example 77 with URLConnection

use of java.net.URLConnection 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 78 with URLConnection

use of java.net.URLConnection in project okhttp by square.

the class URLConnectionTest method responseCodeDisagreesWithHeaders.

@Test
public void responseCodeDisagreesWithHeaders() throws IOException, InterruptedException {
    server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NO_CONTENT).setBody("This body is not allowed!"));
    URLConnection connection = urlFactory.open(server.url("/").url());
    try {
        connection.getInputStream();
        fail();
    } catch (IOException expected) {
        assertEquals("HTTP 204 had non-zero Content-Length: 25", expected.getMessage());
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) IOException(java.io.IOException) HttpURLConnection(java.net.HttpURLConnection) OkHttpURLConnection(okhttp3.internal.huc.OkHttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Example 79 with URLConnection

use of java.net.URLConnection in project okhttp by square.

the class URLConnectionTest method streamDiscardingIsTimely.

@Test
public void streamDiscardingIsTimely() throws Exception {
    // This response takes at least a full second to serve: 10,000 bytes served 100 bytes at a time.
    server.enqueue(new MockResponse().setBody(new Buffer().write(new byte[10000])).throttleBody(100, 10, MILLISECONDS));
    server.enqueue(new MockResponse().setBody("A"));
    long startNanos = System.nanoTime();
    URLConnection connection1 = urlFactory.open(server.url("/").url());
    InputStream in = connection1.getInputStream();
    in.close();
    long elapsedNanos = System.nanoTime() - startNanos;
    long elapsedMillis = NANOSECONDS.toMillis(elapsedNanos);
    // If we're working correctly, this should be greater than 100ms, but less than double that.
    // Previously we had a bug where we would download the entire response body as long as no
    // individual read took longer than 100ms.
    assertTrue(Util.format("Time to close: %sms", elapsedMillis), elapsedMillis < 500);
    // Do another request to confirm that the discarded connection was not pooled.
    assertContent("A", urlFactory.open(server.url("/").url()));
    assertEquals(0, server.takeRequest().getSequenceNumber());
    // Connection is not pooled.
    assertEquals(0, server.takeRequest().getSequenceNumber());
}
Also used : Buffer(okio.Buffer) MockResponse(okhttp3.mockwebserver.MockResponse) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) HttpURLConnection(java.net.HttpURLConnection) OkHttpURLConnection(okhttp3.internal.huc.OkHttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Example 80 with URLConnection

use of java.net.URLConnection in project okhttp by square.

the class URLConnectionTest method missingChunkBody.

@Test
public void missingChunkBody() throws IOException {
    server.enqueue(new MockResponse().setBody("5").clearHeaders().addHeader("Transfer-encoding: chunked").setSocketPolicy(DISCONNECT_AT_END));
    URLConnection connection = urlFactory.open(server.url("/").url());
    try {
        readAscii(connection.getInputStream(), Integer.MAX_VALUE);
        fail();
    } catch (IOException e) {
    } finally {
        connection.getInputStream().close();
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) IOException(java.io.IOException) HttpURLConnection(java.net.HttpURLConnection) OkHttpURLConnection(okhttp3.internal.huc.OkHttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Aggregations

URLConnection (java.net.URLConnection)950 URL (java.net.URL)654 IOException (java.io.IOException)378 HttpURLConnection (java.net.HttpURLConnection)310 InputStream (java.io.InputStream)279 InputStreamReader (java.io.InputStreamReader)227 BufferedReader (java.io.BufferedReader)208 Test (org.junit.Test)177 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)145 File (java.io.File)95 MalformedURLException (java.net.MalformedURLException)91 MockResponse (okhttp3.mockwebserver.MockResponse)76 BufferedInputStream (java.io.BufferedInputStream)57 JarURLConnection (java.net.JarURLConnection)57 OutputStream (java.io.OutputStream)55 FileOutputStream (java.io.FileOutputStream)52 FileInputStream (java.io.FileInputStream)47 ArrayList (java.util.ArrayList)47 GZIPInputStream (java.util.zip.GZIPInputStream)37 URI (java.net.URI)35