use of com.google.mockwebserver.RecordedRequest in project robovm by robovm.
the class URLConnectionTest method testSetChunkedEncodingAsRequestProperty.
public void testSetChunkedEncodingAsRequestProperty() throws IOException, InterruptedException {
server.enqueue(new MockResponse());
server.play();
HttpURLConnection urlConnection = (HttpURLConnection) server.getUrl("/").openConnection();
urlConnection.setRequestProperty("Transfer-encoding", "chunked");
urlConnection.setDoOutput(true);
urlConnection.getOutputStream().write("ABC".getBytes("UTF-8"));
assertEquals(200, urlConnection.getResponseCode());
RecordedRequest request = server.takeRequest();
assertEquals("ABC", new String(request.getBody(), "UTF-8"));
}
use of com.google.mockwebserver.RecordedRequest in project robovm by robovm.
the class URLConnectionTest method testConnectViaHttpProxyToHttps.
/**
* We were verifying the wrong hostname when connecting to an HTTPS site
* through a proxy. http://b/3097277
*/
private void testConnectViaHttpProxyToHttps(ProxyConfig proxyConfig) throws Exception {
TestSSLContext testSSLContext = TestSSLContext.create();
RecordingHostnameVerifier hostnameVerifier = new RecordingHostnameVerifier();
server.useHttps(testSSLContext.serverContext.getSocketFactory(), true);
server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END).clearHeaders());
server.enqueue(new MockResponse().setBody("this response comes via a secure proxy"));
server.play();
URL url = new URL("https://android.com/foo");
HttpsURLConnection connection = (HttpsURLConnection) proxyConfig.connect(server, url);
connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
connection.setHostnameVerifier(hostnameVerifier);
assertContent("this response comes via a secure proxy", connection);
RecordedRequest connect = server.takeRequest();
assertEquals("Connect line failure on proxy", "CONNECT android.com:443 HTTP/1.1", connect.getRequestLine());
assertContains(connect.getHeaders(), "Host: android.com");
RecordedRequest get = server.takeRequest();
assertEquals("GET /foo HTTP/1.1", get.getRequestLine());
assertContains(get.getHeaders(), "Host: android.com");
assertEquals(Arrays.asList("verify android.com"), hostnameVerifier.calls);
}
use of com.google.mockwebserver.RecordedRequest 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.RecordedRequest 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.RecordedRequest 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