Search in sources :

Example 81 with RecordedRequest

use of com.google.mockwebserver.RecordedRequest in project j2objc by google.

the class URLConnectionTest method testRedirectToAnotherOriginServer.

public void testRedirectToAnotherOriginServer() throws Exception {
    MockWebServer server2 = new MockWebServer();
    server2.enqueue(new MockResponse().setBody("This is the 2nd server!"));
    server2.play();
    server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP).addHeader("Location: " + server2.getUrl("/").toString()).setBody("This page has moved!"));
    server.enqueue(new MockResponse().setBody("This is the first server again!"));
    server.play();
    URLConnection connection = server.getUrl("/").openConnection();
    assertEquals("This is the 2nd server!", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
    assertEquals(server2.getUrl("/"), connection.getURL());
    // make sure the first server was careful to recycle the connection
    assertEquals("This is the first server again!", readAscii(server.getUrl("/").openStream(), Integer.MAX_VALUE));
    RecordedRequest first = server.takeRequest();
    assertContains(first.getHeaders(), "Host: " + hostName + ":" + server.getPort());
    RecordedRequest second = server2.takeRequest();
    assertContains(second.getHeaders(), "Host: " + hostName + ":" + server2.getPort());
    RecordedRequest third = server.takeRequest();
    // assertEquals("Expected connection reuse", 1, third.getSequenceNumber()); // JVM failure
    server2.shutdown();
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) MockWebServer(com.google.mockwebserver.MockWebServer) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 82 with RecordedRequest

use of com.google.mockwebserver.RecordedRequest in project j2objc by google.

the class AbstractCookiesTest method testSendingCookiesFromStore.

public void testSendingCookiesFromStore() throws Exception {
    server.enqueue(new MockResponse());
    server.play();
    CookieManager cookieManager = new CookieManager(createCookieStore(), ACCEPT_ORIGINAL_SERVER);
    HttpCookie cookieA = createCookie("a", "android", server.getCookieDomain(), "/");
    cookieManager.getCookieStore().add(server.getUrl("/").toURI(), cookieA);
    HttpCookie cookieB = createCookie("b", "banana", server.getCookieDomain(), "/");
    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() + "\"");
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) HttpCookie(java.net.HttpCookie) CookieManager(java.net.CookieManager)

Example 83 with RecordedRequest

use of com.google.mockwebserver.RecordedRequest in project j2objc by google.

the class AbstractCookiesTest method testCookiesSentIgnoresCase.

// TODO(zgao): b/65289980.
// public void testRedirectsDoNotIncludeTooManyCookies() throws Exception {
// MockWebServer redirectTarget = new MockWebServer();
// redirectTarget.enqueue(new MockResponse().setBody("A"));
// redirectTarget.play();
// 
// MockWebServer redirectSource = new MockWebServer();
// redirectSource.enqueue(new MockResponse()
// .setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP)
// .addHeader("Location: " + redirectTarget.getUrl("/")));
// redirectSource.play();
// 
// CookieManager cookieManager = new CookieManager(createCookieStore(),
// ACCEPT_ORIGINAL_SERVER);
// HttpCookie cookie = createCookie("c", "cookie", redirectSource.getCookieDomain(), "/");
// 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);
// }
// }
// redirectSource.shutdown();
// redirectTarget.shutdown();
// }
/**
 * 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}.
 */
// TODO(zgao): b/65289980.
// public void testHeadersSentToCookieHandler() throws IOException, InterruptedException {
// final Map<String, List<String>> cookieHandlerHeaders = new HashMap<String, List<String>>();
// CookieHandler.setDefault(new CookieManager(createCookieStore(), null) {
// @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;
// }
// });
// 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"));
// }
public void testCookiesSentIgnoresCase() throws Exception {
    CookieHandler.setDefault(new CookieManager(createCookieStore(), null) {

        @Override
        public Map<String, List<String>> get(URI uri, Map<String, List<String>> requestHeaders) throws IOException {
            Map<String, List<String>> result = new HashMap<String, List<String>>();
            result.put("COOKIE", Collections.singletonList("Bar=bar"));
            result.put("cooKIE2", Collections.singletonList("Baz=baz"));
            return result;
        }
    });
    server.enqueue(new MockResponse());
    server.play();
    get(server, "/");
    RecordedRequest request = server.takeRequest();
    assertContainsAll(request.getHeaders(), "COOKIE: Bar=bar", "cooKIE2: Baz=baz");
    assertFalse(request.getHeaders().contains("Quux: quux"));
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) URI(java.net.URI) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) CookieManager(java.net.CookieManager)

Aggregations

MockResponse (com.google.mockwebserver.MockResponse)83 RecordedRequest (com.google.mockwebserver.RecordedRequest)83 HttpResponse (org.apache.http.HttpResponse)36 HttpClient (org.apache.http.client.HttpClient)36 HttpGet (org.apache.http.client.methods.HttpGet)36 HttpURLConnection (java.net.HttpURLConnection)27 TestSSLContext (libcore.javax.net.ssl.TestSSLContext)22 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)18 MockWebServer (com.google.mockwebserver.MockWebServer)14 Scheme (org.apache.http.conn.scheme.Scheme)12 AllowAllHostnameVerifier (org.apache.http.conn.ssl.AllowAllHostnameVerifier)12 SSLSocketFactory (org.apache.http.conn.ssl.SSLSocketFactory)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 OutputStream (java.io.OutputStream)11 GZIPOutputStream (java.util.zip.GZIPOutputStream)11 URL (java.net.URL)10 CookieManager (java.net.CookieManager)8 URLConnection (java.net.URLConnection)8 HttpHost (org.apache.http.HttpHost)6 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)6