use of com.google.mockwebserver.RecordedRequest in project robovm by robovm.
the class URLConnectionTest method testRedirectedOnHttps.
public void testRedirectedOnHttps() throws IOException, InterruptedException {
TestSSLContext testSSLContext = TestSSLContext.create();
server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP).addHeader("Location: /foo").setBody("This page has moved!"));
server.enqueue(new MockResponse().setBody("This is the new location!"));
server.play();
HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/").openConnection();
connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
assertEquals("This is the new location!", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
RecordedRequest first = server.takeRequest();
assertEquals("GET / HTTP/1.1", first.getRequestLine());
RecordedRequest retry = server.takeRequest();
assertEquals("GET /foo HTTP/1.1", retry.getRequestLine());
assertEquals("Expected connection reuse", 1, retry.getSequenceNumber());
}
use of com.google.mockwebserver.RecordedRequest in project robovm by robovm.
the class URLConnectionTest method testGzipEncodingEnabledByDefault.
/**
* This test checks whether connections are gzipped by default. This
* behavior in not required by the API, so a failure of this test does not
* imply a bug in the implementation.
*/
public void testGzipEncodingEnabledByDefault() throws IOException, InterruptedException {
server.enqueue(new MockResponse().setBody(gzip("ABCABCABC".getBytes("UTF-8"))).addHeader("Content-Encoding: gzip"));
server.play();
URLConnection connection = server.getUrl("/").openConnection();
assertEquals("ABCABCABC", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
assertNull(connection.getContentEncoding());
assertEquals(-1, connection.getContentLength());
RecordedRequest request = server.takeRequest();
assertContains(request.getHeaders(), "Accept-Encoding: gzip");
}
use of com.google.mockwebserver.RecordedRequest in project robovm by robovm.
the class URLConnectionTest method testRedirected.
private void testRedirected(TransferKind transferKind, boolean reuse) throws Exception {
MockResponse response = new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP).addHeader("Location: /foo");
transferKind.setBody(response, "This page has moved!", 10);
server.enqueue(response);
server.enqueue(new MockResponse().setBody("This is the new location!"));
server.play();
URLConnection connection = server.getUrl("/").openConnection();
assertEquals("This is the new location!", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
RecordedRequest first = server.takeRequest();
assertEquals("GET / HTTP/1.1", first.getRequestLine());
RecordedRequest retry = server.takeRequest();
assertEquals("GET /foo HTTP/1.1", retry.getRequestLine());
if (reuse) {
assertEquals("Expected connection reuse", 1, retry.getSequenceNumber());
}
}
use of com.google.mockwebserver.RecordedRequest in project robovm by robovm.
the class URLConnectionTest method testAuthenticateWithGet.
public void testAuthenticateWithGet() 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();
SimpleAuthenticator authenticator = new SimpleAuthenticator();
Authenticator.setDefault(authenticator);
HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
assertEquals("Successful auth!", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
assertEquals(Authenticator.RequestorType.SERVER, authenticator.requestorType);
assertEquals(server.getPort(), authenticator.requestingPort);
assertEquals(InetAddress.getByName(server.getHostName()), authenticator.requestingSite);
assertEquals("protected area", authenticator.requestingPrompt);
assertEquals("http", authenticator.requestingProtocol);
assertEquals("Basic", authenticator.requestingScheme);
// no authorization header for the first request...
RecordedRequest request = server.takeRequest();
assertContainsNoneMatching(request.getHeaders(), "Authorization: .*");
// ...but the three requests that follow requests include an authorization header
for (int i = 0; i < 3; i++) {
request = server.takeRequest();
assertEquals("GET / HTTP/1.1", request.getRequestLine());
assertContains(request.getHeaders(), "Authorization: Basic " + SimpleAuthenticator.BASE_64_CREDENTIALS);
}
}
use of com.google.mockwebserver.RecordedRequest in project robovm by robovm.
the class URLConnectionTest method testClientSendsContentLength.
public void testClientSendsContentLength() throws Exception {
server.enqueue(new MockResponse().setBody("A"));
server.play();
HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
connection.setDoOutput(true);
OutputStream out = connection.getOutputStream();
out.write(new byte[] { 'A', 'B', 'C' });
out.close();
assertEquals("A", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
RecordedRequest request = server.takeRequest();
assertContains(request.getHeaders(), "Content-Length: 3");
}
Aggregations