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);
}
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) {
}
}
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());
}
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"));
}
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();
}
}
Aggregations