Search in sources :

Example 71 with URLConnection

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

the class MockWebServerTest method throttleRequest.

/**
   * Throttle the request body by sleeping 500ms after every 3 bytes. With a 6-byte request, this
   * should yield one sleep for a total delay of 500ms.
   */
@Test
public void throttleRequest() throws Exception {
    server.enqueue(new MockResponse().throttleBody(3, 500, TimeUnit.MILLISECONDS));
    long startNanos = System.nanoTime();
    URLConnection connection = server.url("/").url().openConnection();
    connection.setDoOutput(true);
    connection.getOutputStream().write("ABCDEF".getBytes("UTF-8"));
    InputStream in = connection.getInputStream();
    assertEquals(-1, in.read());
    long elapsedNanos = System.nanoTime() - startNanos;
    long elapsedMillis = NANOSECONDS.toMillis(elapsedNanos);
    assertTrue(Util.format("Request + Response: %sms", elapsedMillis), elapsedMillis >= 500);
    assertTrue(Util.format("Request + Response: %sms", elapsedMillis), elapsedMillis < 1000);
}
Also used : InputStream(java.io.InputStream) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) Test(org.junit.Test)

Example 72 with URLConnection

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

the class MockWebServerTest method nonHexadecimalChunkSize.

@Test
public void nonHexadecimalChunkSize() throws Exception {
    server.enqueue(new MockResponse().setBody("G\r\nxxxxxxxxxxxxxxxx\r\n0\r\n\r\n").clearHeaders().addHeader("Transfer-encoding: chunked"));
    URLConnection connection = server.url("/").url().openConnection();
    InputStream in = connection.getInputStream();
    try {
        in.read();
        fail();
    } catch (IOException expected) {
    }
}
Also used : InputStream(java.io.InputStream) IOException(java.io.IOException) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) Test(org.junit.Test)

Example 73 with URLConnection

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

the class MockWebServerTest method responseTimeout.

@Test
public void responseTimeout() throws Exception {
    server.enqueue(new MockResponse().setBody("ABC").clearHeaders().addHeader("Content-Length: 4"));
    server.enqueue(new MockResponse().setBody("DEF"));
    URLConnection urlConnection = server.url("/").url().openConnection();
    urlConnection.setReadTimeout(1000);
    InputStream in = urlConnection.getInputStream();
    assertEquals('A', in.read());
    assertEquals('B', in.read());
    assertEquals('C', in.read());
    try {
        // if Content-Length was accurate, this would return -1 immediately
        in.read();
        fail();
    } catch (SocketTimeoutException expected) {
    }
    URLConnection urlConnection2 = server.url("/").url().openConnection();
    InputStream in2 = urlConnection2.getInputStream();
    assertEquals('D', in2.read());
    assertEquals('E', in2.read());
    assertEquals('F', in2.read());
    assertEquals(-1, in2.read());
    assertEquals(0, server.takeRequest().getSequenceNumber());
    assertEquals(0, server.takeRequest().getSequenceNumber());
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) InputStream(java.io.InputStream) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) Test(org.junit.Test)

Example 74 with URLConnection

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

the class URLConnectionTest method clientConfiguredGzipContentEncoding.

@Test
public void clientConfiguredGzipContentEncoding() throws Exception {
    Buffer bodyBytes = gzip("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    server.enqueue(new MockResponse().setBody(bodyBytes).addHeader("Content-Encoding: gzip"));
    URLConnection connection = urlFactory.open(server.url("/").url());
    connection.addRequestProperty("Accept-Encoding", "gzip");
    InputStream gunzippedIn = new GZIPInputStream(connection.getInputStream());
    assertEquals("ABCDEFGHIJKLMNOPQRSTUVWXYZ", readAscii(gunzippedIn, Integer.MAX_VALUE));
    assertEquals(bodyBytes.size(), connection.getContentLength());
    RecordedRequest request = server.takeRequest();
    assertEquals("gzip", request.getHeader("Accept-Encoding"));
}
Also used : Buffer(okio.Buffer) GZIPInputStream(java.util.zip.GZIPInputStream) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) 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 75 with URLConnection

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

the class URLConnectionTest method malformedChunkSize.

@Test
public void malformedChunkSize() throws IOException {
    server.enqueue(new MockResponse().setBody("5:x\r\nABCDE\r\n0\r\n\r\n").clearHeaders().addHeader("Transfer-encoding: chunked"));
    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