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();
}
use of com.google.mockwebserver.MockResponse in project robovm by robovm.
the class URLConnectionTest method testConnectionCloseInResponse.
public void testConnectionCloseInResponse() throws IOException, InterruptedException {
server.enqueue(new MockResponse().addHeader("Connection: close"));
server.enqueue(new MockResponse());
server.play();
HttpURLConnection a = (HttpURLConnection) server.getUrl("/").openConnection();
assertEquals(200, a.getResponseCode());
HttpURLConnection b = (HttpURLConnection) server.getUrl("/").openConnection();
assertEquals(200, b.getResponseCode());
assertEquals(0, server.takeRequest().getSequenceNumber());
assertEquals("When connection: close is used, each request should get its own connection", 0, server.takeRequest().getSequenceNumber());
}
Aggregations