use of com.google.mockwebserver.MockResponse in project robovm by robovm.
the class URLConnectionTest method testRedirectToAnotherOriginServer.
public void testRedirectToAnotherOriginServer() throws Exception {
MockWebServer server2 = new MockWebServer();
try {
// RoboVM note: Modified to call server2.shutdown() after test finishes regardless of outcome.
server2.enqueue(new MockResponse().setBody("This is the 2nd server!"));
server2.play();
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP).addHeader("Location: " + server2.getUrl("/").toString()).setBody("This page has moved!"));
server.enqueue(new MockResponse().setBody("This is the first server again!"));
server.play();
URLConnection connection = server.getUrl("/").openConnection();
assertEquals("This is the 2nd server!", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
assertEquals(server2.getUrl("/"), connection.getURL());
// make sure the first server was careful to recycle the connection
assertEquals("This is the first server again!", readAscii(server.getUrl("/").openStream(), Integer.MAX_VALUE));
RecordedRequest first = server.takeRequest();
assertContains(first.getHeaders(), "Host: " + hostName + ":" + server.getPort());
RecordedRequest second = server2.takeRequest();
assertContains(second.getHeaders(), "Host: " + hostName + ":" + server2.getPort());
RecordedRequest third = server.takeRequest();
assertEquals("Expected connection reuse", 1, third.getSequenceNumber());
} finally {
server2.shutdown();
}
}
use of com.google.mockwebserver.MockResponse in project robovm by robovm.
the class URLConnectionTest method testNonRetryableRequestBodyAfterBrokenConnection.
public void testNonRetryableRequestBodyAfterBrokenConnection() throws Exception {
TestSSLContext testSSLContext = TestSSLContext.create();
server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
server.enqueue(new MockResponse().setBody("abc").setSocketPolicy(DISCONNECT_AFTER_READING_REQUEST));
server.play();
HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/a").openConnection();
connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
connection.setDoOutput(true);
connection.setFixedLengthStreamingMode(3);
OutputStream out = connection.getOutputStream();
out.write(new byte[] { 1, 2, 3 });
out.close();
try {
connection.getInputStream();
fail();
} catch (IOException expected) {
}
}
use of com.google.mockwebserver.MockResponse in project robovm by robovm.
the class URLConnectionTest method testConnectViaHttpsReusingConnectionsDifferentFactories.
public void testConnectViaHttpsReusingConnectionsDifferentFactories() throws IOException, InterruptedException {
TestSSLContext testSSLContext = TestSSLContext.create();
server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
server.enqueue(new MockResponse().setBody("this response comes via HTTPS"));
server.enqueue(new MockResponse().setBody("another response via HTTPS"));
server.play();
// install a custom SSL socket factory so the server can be authorized
HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/").openConnection();
connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
assertContent("this response comes via HTTPS", connection);
connection = (HttpsURLConnection) server.getUrl("/").openConnection();
try {
readAscii(connection.getInputStream(), Integer.MAX_VALUE);
fail("without an SSL socket factory, the connection should fail");
} catch (SSLException expected) {
}
}
use of com.google.mockwebserver.MockResponse in project robovm by robovm.
the class URLConnectionTest method testReadTimeoutsOnRecycledConnections.
public void testReadTimeoutsOnRecycledConnections() throws Exception {
server.enqueue(new MockResponse().setBody("ABC"));
server.play();
// The request should work once and then fail
URLConnection connection = server.getUrl("").openConnection();
// Read timeout of a day, sure to cause the test to timeout and fail.
connection.setReadTimeout(24 * 3600 * 1000);
InputStream input = connection.getInputStream();
assertEquals("ABC", readAscii(input, Integer.MAX_VALUE));
input.close();
try {
connection = server.getUrl("").openConnection();
// Set the read timeout back to 100ms, this request will time out
// because we've only enqueued one response.
connection.setReadTimeout(100);
connection.getInputStream();
fail();
} catch (IOException expected) {
}
}
use of com.google.mockwebserver.MockResponse in project robovm by robovm.
the class URLConnectionTest method testSetDoOutputOrDoInputAfterConnectFails.
public void testSetDoOutputOrDoInputAfterConnectFails() throws Exception {
server.enqueue(new MockResponse());
server.play();
HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
connection.connect();
try {
connection.setDoOutput(true);
fail();
} catch (IllegalStateException expected) {
}
try {
connection.setDoInput(true);
fail();
} catch (IllegalStateException expected) {
}
connection.disconnect();
}
Aggregations