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