use of com.google.mockwebserver.RecordedRequest in project robovm by robovm.
the class CookiesTest method testRedirectsDoNotIncludeTooManyCookies.
public void testRedirectsDoNotIncludeTooManyCookies() throws Exception {
MockWebServer redirectTarget = new MockWebServer();
try {
// RoboVM note: Modified to call redirectTarget.shutdown() after test finishes.
redirectTarget.enqueue(new MockResponse().setBody("A"));
redirectTarget.play();
MockWebServer redirectSource = new MockWebServer();
try {
// RoboVM note: Modified to call redirectSource.shutdown() after test finishes.
redirectSource.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP).addHeader("Location: " + redirectTarget.getUrl("/")));
redirectSource.play();
CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
HttpCookie cookie = new HttpCookie("c", "cookie");
cookie.setDomain(redirectSource.getCookieDomain());
cookie.setPath("/");
String portList = Integer.toString(redirectSource.getPort());
cookie.setPortlist(portList);
cookieManager.getCookieStore().add(redirectSource.getUrl("/").toURI(), cookie);
CookieHandler.setDefault(cookieManager);
get(redirectSource, "/");
RecordedRequest request = redirectSource.takeRequest();
assertContains(request.getHeaders(), "Cookie: $Version=\"1\"; " + "c=\"cookie\";$Path=\"/\";$Domain=\"" + redirectSource.getCookieDomain() + "\";$Port=\"" + portList + "\"");
for (String header : redirectTarget.takeRequest().getHeaders()) {
if (header.startsWith("Cookie")) {
fail(header);
}
}
} finally {
redirectSource.shutdown();
}
} finally {
redirectTarget.shutdown();
}
}
use of com.google.mockwebserver.RecordedRequest in project robovm by robovm.
the class CookiesTest method testSendingCookiesFromStore.
public void testSendingCookiesFromStore() throws Exception {
MockWebServer server = new MockWebServer();
try {
// RoboVM note: Modified to call server.shutdown() after test finishes.
server.enqueue(new MockResponse());
server.play();
CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
HttpCookie cookieA = new HttpCookie("a", "android");
cookieA.setDomain(server.getCookieDomain());
cookieA.setPath("/");
cookieManager.getCookieStore().add(server.getUrl("/").toURI(), cookieA);
HttpCookie cookieB = new HttpCookie("b", "banana");
cookieB.setDomain(server.getCookieDomain());
cookieB.setPath("/");
cookieManager.getCookieStore().add(server.getUrl("/").toURI(), cookieB);
CookieHandler.setDefault(cookieManager);
get(server, "/");
RecordedRequest request = server.takeRequest();
List<String> receivedHeaders = request.getHeaders();
assertContains(receivedHeaders, "Cookie: $Version=\"1\"; " + "a=\"android\";$Path=\"/\";$Domain=\"" + server.getCookieDomain() + "\"; " + "b=\"banana\";$Path=\"/\";$Domain=\"" + server.getCookieDomain() + "\"");
} finally {
server.shutdown();
}
}
use of com.google.mockwebserver.RecordedRequest in project robovm by robovm.
the class CookiesTest method testHeadersSentToCookieHandler.
/**
* Test which headers show up where. The cookie manager should be notified
* of both user-specified and derived headers like {@code Host}. Headers
* named {@code Cookie} or {@code Cookie2} that are returned by the cookie
* manager should show up in the request and in {@code
* getRequestProperties}.
*/
public void testHeadersSentToCookieHandler() throws IOException, InterruptedException {
final Map<String, List<String>> cookieHandlerHeaders = new HashMap<String, List<String>>();
CookieHandler.setDefault(new CookieManager() {
@Override
public Map<String, List<String>> get(URI uri, Map<String, List<String>> requestHeaders) throws IOException {
cookieHandlerHeaders.putAll(requestHeaders);
Map<String, List<String>> result = new HashMap<String, List<String>>();
result.put("Cookie", Collections.singletonList("Bar=bar"));
result.put("Cookie2", Collections.singletonList("Baz=baz"));
result.put("Quux", Collections.singletonList("quux"));
return result;
}
});
MockWebServer server = new MockWebServer();
try {
// RoboVM note: Modified to call server.shutdown() after test finishes.
server.enqueue(new MockResponse());
server.play();
HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
assertEquals(Collections.<String, List<String>>emptyMap(), connection.getRequestProperties());
connection.setRequestProperty("Foo", "foo");
connection.setDoOutput(true);
connection.getOutputStream().write(5);
connection.getOutputStream().close();
connection.getInputStream().close();
RecordedRequest request = server.takeRequest();
assertContainsAll(cookieHandlerHeaders.keySet(), "Foo");
assertContainsAll(cookieHandlerHeaders.keySet(), "Content-Type", "User-Agent", "Connection", "Host");
assertFalse(cookieHandlerHeaders.containsKey("Cookie"));
/*
* The API specifies that calling getRequestProperties() on a connected instance should fail
* with an IllegalStateException, but the RI violates the spec and returns a valid map.
* http://www.mail-archive.com/net-dev@openjdk.java.net/msg01768.html
*/
try {
assertContainsAll(connection.getRequestProperties().keySet(), "Foo");
assertContainsAll(connection.getRequestProperties().keySet(), "Content-Type", "Content-Length", "User-Agent", "Connection", "Host");
assertContainsAll(connection.getRequestProperties().keySet(), "Cookie", "Cookie2");
assertFalse(connection.getRequestProperties().containsKey("Quux"));
} catch (IllegalStateException expected) {
}
assertContainsAll(request.getHeaders(), "Foo: foo", "Cookie: Bar=bar", "Cookie2: Baz=baz");
assertFalse(request.getHeaders().contains("Quux: quux"));
} finally {
server.shutdown();
}
}
use of com.google.mockwebserver.RecordedRequest in project robovm by robovm.
the class URLConnectionTest method testClientConfiguredGzipContentEncoding.
public void testClientConfiguredGzipContentEncoding() throws Exception {
byte[] bodyBytes = gzip("ABCDEFGHIJKLMNOPQRSTUVWXYZ".getBytes("UTF-8"));
server.enqueue(new MockResponse().setBody(bodyBytes).addHeader("Content-Encoding: gzip").addHeader("Content-Length: " + bodyBytes.length));
server.play();
URLConnection connection = server.getUrl("/").openConnection();
connection.addRequestProperty("Accept-Encoding", "gzip");
InputStream gunzippedIn = new GZIPInputStream(connection.getInputStream());
assertEquals("ABCDEFGHIJKLMNOPQRSTUVWXYZ", readAscii(gunzippedIn, Integer.MAX_VALUE));
assertEquals(bodyBytes.length, 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 testSetChunkedStreamingMode.
/**
* Obnoxiously test that the chunk sizes transmitted exactly equal the
* requested data+chunk header size. Although setChunkedStreamingMode()
* isn't specific about whether the size applies to the data or the
* complete chunk, the RI interprets it as a complete chunk.
*/
public void testSetChunkedStreamingMode() throws IOException, InterruptedException {
server.enqueue(new MockResponse());
server.play();
HttpURLConnection urlConnection = (HttpURLConnection) server.getUrl("/").openConnection();
urlConnection.setChunkedStreamingMode(8);
urlConnection.setDoOutput(true);
OutputStream outputStream = urlConnection.getOutputStream();
outputStream.write("ABCDEFGHIJKLMNOPQ".getBytes("US-ASCII"));
assertEquals(200, urlConnection.getResponseCode());
RecordedRequest request = server.takeRequest();
assertEquals("ABCDEFGHIJKLMNOPQ", new String(request.getBody(), "US-ASCII"));
assertEquals(Arrays.asList(3, 3, 3, 3, 3, 2), request.getChunkSizes());
}
Aggregations