use of libcore.javax.net.ssl.TestSSLContext in project robovm by robovm.
the class URLConnectionTest method testSecureStreamingPost.
/**
* Users have reported problems using HTTPS with streaming request bodies.
* http://code.google.com/p/android/issues/detail?id=12860
*/
private void testSecureStreamingPost(StreamingMode streamingMode) throws Exception {
TestSSLContext testSSLContext = TestSSLContext.create();
server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
server.enqueue(new MockResponse().setBody("Success!"));
server.play();
HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/").openConnection();
connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
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();
assertEquals("Success!", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
RecordedRequest request = server.takeRequest();
assertEquals("POST / HTTP/1.1", request.getRequestLine());
if (streamingMode == StreamingMode.FIXED_LENGTH) {
assertEquals(Collections.<Integer>emptyList(), request.getChunkSizes());
} else if (streamingMode == StreamingMode.CHUNKED) {
assertEquals(Arrays.asList(4), request.getChunkSizes());
}
assertEquals(Arrays.toString(requestBody), Arrays.toString(request.getBody()));
}
use of libcore.javax.net.ssl.TestSSLContext in project platform_frameworks_base by android.
the class AbstractProxyTest method testConnectToHttps.
public void testConnectToHttps() throws Exception {
TestSSLContext testSSLContext = TestSSLContext.create();
server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
server.enqueue(new MockResponse().setResponseCode(200).setBody("this response comes via HTTPS"));
server.play();
HttpClient httpClient = newHttpClient();
SSLSocketFactory sslSocketFactory = newSslSocketFactory(testSSLContext);
sslSocketFactory.setHostnameVerifier(new AllowAllHostnameVerifier());
httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", sslSocketFactory, server.getPort()));
HttpResponse response = httpClient.execute(new HttpGet("https://localhost:" + server.getPort() + "/foo"));
assertEquals("this response comes via HTTPS", contentToString(response));
RecordedRequest request = server.takeRequest();
assertEquals("GET /foo HTTP/1.1", request.getRequestLine());
}
use of libcore.javax.net.ssl.TestSSLContext 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 libcore.javax.net.ssl.TestSSLContext 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 libcore.javax.net.ssl.TestSSLContext in project robovm by robovm.
the class URLConnectionTest method testInspectSslBeforeConnect.
public void testInspectSslBeforeConnect() throws Exception {
TestSSLContext testSSLContext = TestSSLContext.create();
server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
server.enqueue(new MockResponse());
server.play();
HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/").openConnection();
connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
assertNotNull(connection.getHostnameVerifier());
try {
connection.getLocalCertificates();
fail();
} catch (IllegalStateException expected) {
}
try {
connection.getServerCertificates();
fail();
} catch (IllegalStateException expected) {
}
try {
connection.getCipherSuite();
fail();
} catch (IllegalStateException expected) {
}
try {
connection.getPeerPrincipal();
fail();
} catch (IllegalStateException expected) {
}
}
Aggregations