use of okhttp3.Connection in project okhttp by square.
the class URLConnectionTest method redirectedOnHttps.
@Test
public void redirectedOnHttps() throws IOException, InterruptedException {
server.useHttps(sslClient.socketFactory, false);
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP).addHeader("Location: /foo").setBody("This page has moved!"));
server.enqueue(new MockResponse().setBody("This is the new location!"));
urlFactory.setClient(urlFactory.client().newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(new RecordingHostnameVerifier()).build());
connection = urlFactory.open(server.url("/").url());
assertEquals("This is the new location!", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
RecordedRequest first = server.takeRequest();
assertEquals("GET / HTTP/1.1", first.getRequestLine());
RecordedRequest retry = server.takeRequest();
assertEquals("GET /foo HTTP/1.1", retry.getRequestLine());
assertEquals("Expected connection reuse", 1, retry.getSequenceNumber());
}
use of okhttp3.Connection 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();
}
}
use of okhttp3.Connection in project okhttp by square.
the class URLConnectionTest method fullyBufferedPostIsTooLong.
@Test
public void fullyBufferedPostIsTooLong() throws Exception {
server.enqueue(new MockResponse().setBody("A"));
connection = urlFactory.open(server.url("/b").url());
connection.setRequestProperty("Content-Length", "3");
connection.setRequestMethod("POST");
OutputStream out = connection.getOutputStream();
out.write('a');
out.write('b');
out.write('c');
try {
out.write('d');
out.flush();
fail();
} catch (IOException expected) {
}
}
use of okhttp3.Connection in project okhttp by square.
the class URLConnectionTest method fullyBufferedPostIsTooShort.
@Test
public void fullyBufferedPostIsTooShort() throws Exception {
server.enqueue(new MockResponse().setBody("A"));
connection = urlFactory.open(server.url("/b").url());
connection.setRequestProperty("Content-Length", "4");
connection.setRequestMethod("POST");
OutputStream out = connection.getOutputStream();
out.write('a');
out.write('b');
out.write('c');
try {
out.close();
fail();
} catch (IOException expected) {
}
}
use of okhttp3.Connection in project okhttp by square.
the class URLConnectionTest method bufferedBodyWithClientRequestTimeout.
@Test
public void bufferedBodyWithClientRequestTimeout() throws Exception {
enqueueClientRequestTimeoutResponses();
HttpURLConnection connection = urlFactory.open(server.url("/").url());
connection.setRequestMethod("POST");
connection.getOutputStream().write("Hello".getBytes("UTF-8"));
assertEquals(200, connection.getResponseCode());
assertEquals("Body", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
RecordedRequest request1 = server.takeRequest();
assertEquals("Hello", request1.getBody().readUtf8());
RecordedRequest request2 = server.takeRequest();
assertEquals("Hello", request2.getBody().readUtf8());
}
Aggregations