use of com.google.mockwebserver.RecordedRequest in project robovm by robovm.
the class URLConnectionTest method testAuthenticateWithPost.
public void testAuthenticateWithPost() throws Exception {
MockResponse pleaseAuthenticate = new MockResponse().setResponseCode(401).addHeader("WWW-Authenticate: Basic realm=\"protected area\"").setBody("Please authenticate.");
// fail auth three times...
server.enqueue(pleaseAuthenticate);
server.enqueue(pleaseAuthenticate);
server.enqueue(pleaseAuthenticate);
// ...then succeed the fourth time
server.enqueue(new MockResponse().setBody("Successful auth!"));
server.play();
Authenticator.setDefault(new SimpleAuthenticator());
HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
connection.setDoOutput(true);
byte[] requestBody = { 'A', 'B', 'C', 'D' };
OutputStream outputStream = connection.getOutputStream();
outputStream.write(requestBody);
outputStream.close();
assertEquals("Successful auth!", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
// no authorization header for the first request...
RecordedRequest request = server.takeRequest();
assertContainsNoneMatching(request.getHeaders(), "Authorization: .*");
// ...but the three requests that follow include an authorization header
for (int i = 0; i < 3; i++) {
request = server.takeRequest();
assertEquals("POST / HTTP/1.1", request.getRequestLine());
assertContains(request.getHeaders(), "Authorization: Basic " + SimpleAuthenticator.BASE_64_CREDENTIALS);
assertEquals(Arrays.toString(requestBody), Arrays.toString(request.getBody()));
}
}
use of com.google.mockwebserver.RecordedRequest in project robovm by robovm.
the class URLConnectionTest method testClientConfiguredCustomContentEncoding.
public void testClientConfiguredCustomContentEncoding() throws Exception {
server.enqueue(new MockResponse().setBody("ABCDE").addHeader("Content-Encoding: custom"));
server.play();
URLConnection connection = server.getUrl("/").openConnection();
connection.addRequestProperty("Accept-Encoding", "custom");
assertEquals("ABCDE", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
RecordedRequest request = server.takeRequest();
assertContains(request.getHeaders(), "Accept-Encoding: custom");
}
use of com.google.mockwebserver.RecordedRequest in project robovm by robovm.
the class URLConnectionTest method testResponseRedirectedWithPost.
private void testResponseRedirectedWithPost(int redirectCode) throws Exception {
server.enqueue(new MockResponse().setResponseCode(redirectCode).addHeader("Location: /page2").setBody("This page has moved!"));
server.enqueue(new MockResponse().setBody("Page 2"));
server.play();
HttpURLConnection connection = (HttpURLConnection) server.getUrl("/page1").openConnection();
connection.setDoOutput(true);
byte[] requestBody = { 'A', 'B', 'C', 'D' };
OutputStream outputStream = connection.getOutputStream();
outputStream.write(requestBody);
outputStream.close();
assertEquals("Page 2", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
assertTrue(connection.getDoOutput());
RecordedRequest page1 = server.takeRequest();
assertEquals("POST /page1 HTTP/1.1", page1.getRequestLine());
assertEquals(Arrays.toString(requestBody), Arrays.toString(page1.getBody()));
RecordedRequest page2 = server.takeRequest();
assertEquals("GET /page2 HTTP/1.1", page2.getRequestLine());
}
use of com.google.mockwebserver.RecordedRequest in project robovm by robovm.
the class URLConnectionTest method testRequestHeaders.
public void testRequestHeaders() throws IOException, InterruptedException {
server.enqueue(new MockResponse());
server.play();
HttpURLConnection urlConnection = (HttpURLConnection) server.getUrl("/").openConnection();
urlConnection.addRequestProperty("D", "e");
urlConnection.addRequestProperty("D", "f");
assertEquals("f", urlConnection.getRequestProperty("D"));
assertEquals("f", urlConnection.getRequestProperty("d"));
Map<String, List<String>> requestHeaders = urlConnection.getRequestProperties();
assertEquals(newSet("e", "f"), new HashSet<String>(requestHeaders.get("D")));
assertEquals(newSet("e", "f"), new HashSet<String>(requestHeaders.get("d")));
try {
requestHeaders.put("G", Arrays.asList("h"));
fail("Modified an unmodifiable view.");
} catch (UnsupportedOperationException expected) {
}
try {
requestHeaders.get("D").add("i");
fail("Modified an unmodifiable view.");
} catch (UnsupportedOperationException expected) {
}
try {
urlConnection.setRequestProperty(null, "j");
fail();
} catch (NullPointerException expected) {
}
try {
urlConnection.addRequestProperty(null, "k");
fail();
} catch (NullPointerException expected) {
}
// should fail silently!
urlConnection.setRequestProperty("NullValue", null);
assertNull(urlConnection.getRequestProperty("NullValue"));
// should fail silently!
urlConnection.addRequestProperty("AnotherNullValue", null);
assertNull(urlConnection.getRequestProperty("AnotherNullValue"));
urlConnection.getResponseCode();
RecordedRequest request = server.takeRequest();
assertContains(request.getHeaders(), "D: e");
assertContains(request.getHeaders(), "D: f");
assertContainsNoneMatching(request.getHeaders(), "NullValue.*");
assertContainsNoneMatching(request.getHeaders(), "AnotherNullValue.*");
assertContainsNoneMatching(request.getHeaders(), "G:.*");
assertContainsNoneMatching(request.getHeaders(), "null:.*");
try {
urlConnection.addRequestProperty("N", "o");
fail("Set header after connect");
} catch (IllegalStateException expected) {
}
try {
urlConnection.setRequestProperty("P", "q");
fail("Set header after connect");
} catch (IllegalStateException expected) {
}
try {
urlConnection.getRequestProperties();
fail();
} catch (IllegalStateException expected) {
}
}
use of com.google.mockwebserver.RecordedRequest in project robovm by robovm.
the class URLConnectionTest method testProxyConnectIncludesProxyHeadersOnly.
/**
* Test which headers are sent unencrypted to the HTTP proxy.
*/
public void testProxyConnectIncludesProxyHeadersOnly() throws IOException, InterruptedException {
RecordingHostnameVerifier hostnameVerifier = new RecordingHostnameVerifier();
TestSSLContext testSSLContext = TestSSLContext.create();
server.useHttps(testSSLContext.serverContext.getSocketFactory(), true);
server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END).clearHeaders());
server.enqueue(new MockResponse().setBody("encrypted response from the origin server"));
server.play();
URL url = new URL("https://android.com/foo");
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(server.toProxyAddress());
connection.addRequestProperty("Private", "Secret");
connection.addRequestProperty("Proxy-Authorization", "bar");
connection.addRequestProperty("User-Agent", "baz");
connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
connection.setHostnameVerifier(hostnameVerifier);
assertContent("encrypted response from the origin server", connection);
RecordedRequest connect = server.takeRequest();
assertContainsNoneMatching(connect.getHeaders(), "Private.*");
assertContains(connect.getHeaders(), "Proxy-Authorization: bar");
assertContains(connect.getHeaders(), "User-Agent: baz");
assertContains(connect.getHeaders(), "Host: android.com");
assertContains(connect.getHeaders(), "Proxy-Connection: Keep-Alive");
RecordedRequest get = server.takeRequest();
assertContains(get.getHeaders(), "Private: Secret");
assertEquals(Arrays.asList("verify android.com"), hostnameVerifier.calls);
}
Aggregations