use of com.google.mockwebserver.MockResponse in project robovm by robovm.
the class URLConnectionTest method testConnectViaHttpsWithSSLFallback.
public void testConnectViaHttpsWithSSLFallback() throws IOException, InterruptedException {
TestSSLContext testSSLContext = TestSSLContext.create();
server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
server.enqueue(new MockResponse().setSocketPolicy(DISCONNECT_AT_START));
server.enqueue(new MockResponse().setBody("this response comes via SSL"));
server.play();
HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/foo").openConnection();
connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
assertContent("this response comes via SSL", connection);
// The first request will be an incomplete (bookkeeping) request
// that the server disconnected from at start.
server.takeRequest();
// The request will be retried.
RecordedRequest request = server.takeRequest();
assertEquals("GET /foo HTTP/1.1", request.getRequestLine());
}
use of com.google.mockwebserver.MockResponse in project robovm by robovm.
the class URLConnectionTest method testSslFallback.
public void testSslFallback() throws Exception {
TestSSLContext testSSLContext = TestSSLContext.create();
server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.FAIL_HANDSHAKE));
server.enqueue(new MockResponse().setBody("This required a 2nd handshake"));
server.play();
HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/").openConnection();
connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
assertEquals("This required a 2nd handshake", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
RecordedRequest first = server.takeRequest();
assertEquals(0, first.getSequenceNumber());
RecordedRequest retry = server.takeRequest();
assertEquals(0, retry.getSequenceNumber());
assertEquals("SSLv3", retry.getSslProtocol());
}
use of com.google.mockwebserver.MockResponse in project robovm by robovm.
the class URLConnectionTest method testGetContentOfType.
public void testGetContentOfType() throws Exception {
server.enqueue(new MockResponse().setBody("A"));
server.play();
HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
try {
connection.getContent(null);
fail();
} catch (NullPointerException expected) {
}
try {
connection.getContent(new Class[] { null });
fail();
} catch (NullPointerException expected) {
}
assertNull(connection.getContent(new Class[] { getClass() }));
connection.disconnect();
}
use of com.google.mockwebserver.MockResponse in project robovm by robovm.
the class URLConnectionTest method testServerShutdownOutput.
public void testServerShutdownOutput() throws Exception {
// This test causes MockWebServer to log a "connection failed" stack trace
server.enqueue(new MockResponse().setBody("Output shutdown after this response").setSocketPolicy(SHUTDOWN_OUTPUT_AT_END));
server.enqueue(new MockResponse().setBody("This response will fail to write"));
server.enqueue(new MockResponse().setBody("This comes after a busted connection"));
server.play();
assertContent("Output shutdown after this response", server.getUrl("/a").openConnection());
assertEquals(0, server.takeRequest().getSequenceNumber());
assertContent("This comes after a busted connection", server.getUrl("/b").openConnection());
assertEquals(1, server.takeRequest().getSequenceNumber());
assertEquals(0, server.takeRequest().getSequenceNumber());
}
use of com.google.mockwebserver.MockResponse in project robovm by robovm.
the class URLConnectionTest method testAuthenticateWithStreamingPost.
private void testAuthenticateWithStreamingPost(StreamingMode streamingMode) throws Exception {
MockResponse pleaseAuthenticate = new MockResponse().setResponseCode(401).addHeader("WWW-Authenticate: Basic realm=\"protected area\"").setBody("Please authenticate.");
server.enqueue(pleaseAuthenticate);
server.play();
Authenticator.setDefault(new SimpleAuthenticator());
HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
connection.setDoOutput(true);
byte[] requestBody = { 'A', 'B', 'C', 'D' };
if (streamingMode == StreamingMode.FIXED_LENGTH) {
connection.setFixedLengthStreamingMode(requestBody.length);
} else if (streamingMode == StreamingMode.CHUNKED) {
connection.setChunkedStreamingMode(0);
}
OutputStream outputStream = connection.getOutputStream();
outputStream.write(requestBody);
outputStream.close();
try {
connection.getInputStream();
fail();
} catch (HttpRetryException expected) {
}
// no authorization header for the request...
RecordedRequest request = server.takeRequest();
assertContainsNoneMatching(request.getHeaders(), "Authorization: Basic .*");
assertEquals(Arrays.toString(requestBody), Arrays.toString(request.getBody()));
}
Aggregations