Search in sources :

Example 36 with MockWebServer

use of com.google.mockwebserver.MockWebServer 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();
    }
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) MockWebServer(com.google.mockwebserver.MockWebServer) HttpCookie(java.net.HttpCookie) CookieManager(java.net.CookieManager)

Example 37 with MockWebServer

use of com.google.mockwebserver.MockWebServer in project robovm by robovm.

the class CookiesTest method testNetscapeResponse.

public void testNetscapeResponse() throws Exception {
    CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
    CookieHandler.setDefault(cookieManager);
    MockWebServer server = new MockWebServer();
    try {
        // RoboVM note: Modified to call server.shutdown() after test finishes.
        server.play();
        server.enqueue(new MockResponse().addHeader("Set-Cookie: a=android; " + "expires=Fri, 31-Dec-9999 23:59:59 GMT; " + "path=/path; " + "domain=" + server.getCookieDomain() + "; " + "secure"));
        get(server, "/path/foo");
        List<HttpCookie> cookies = cookieManager.getCookieStore().getCookies();
        assertEquals(1, cookies.size());
        HttpCookie cookie = cookies.get(0);
        assertEquals("a", cookie.getName());
        assertEquals("android", cookie.getValue());
        assertEquals(null, cookie.getComment());
        assertEquals(null, cookie.getCommentURL());
        assertEquals(false, cookie.getDiscard());
        assertEquals(server.getCookieDomain(), cookie.getDomain());
        assertTrue(cookie.getMaxAge() > 100000000000L);
        assertEquals("/path", cookie.getPath());
        assertEquals(true, cookie.getSecure());
        assertEquals(0, cookie.getVersion());
    } finally {
        server.shutdown();
    }
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) MockWebServer(com.google.mockwebserver.MockWebServer) HttpCookie(java.net.HttpCookie) CookieManager(java.net.CookieManager)

Example 38 with MockWebServer

use of com.google.mockwebserver.MockWebServer 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();
    }
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) MockWebServer(com.google.mockwebserver.MockWebServer) HttpCookie(java.net.HttpCookie) CookieManager(java.net.CookieManager)

Example 39 with MockWebServer

use of com.google.mockwebserver.MockWebServer 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();
    }
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) HashMap(java.util.HashMap) IOException(java.io.IOException) URI(java.net.URI) HttpURLConnection(java.net.HttpURLConnection) MockWebServer(com.google.mockwebserver.MockWebServer) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) CookieManager(java.net.CookieManager)

Example 40 with MockWebServer

use of com.google.mockwebserver.MockWebServer in project robovm by robovm.

the class ExpatSaxParserTest method testExternalEntityDownload.

public void testExternalEntityDownload() throws IOException, SAXException {
    final MockWebServer server = new MockWebServer();
    try {
        // RoboVM note: Modified to call server.shutdown() after test finishes.
        server.enqueue(new MockResponse().setBody("<bar></bar>"));
        server.play();
        class Handler extends DefaultHandler {

            final List<String> elementNames = new ArrayList<String>();

            @Override
            public InputSource resolveEntity(String publicId, String systemId) throws IOException {
                // The parser should have resolved the systemId.
                assertEquals(server.getUrl("/systemBar").toString(), systemId);
                return new InputSource(systemId);
            }

            @Override
            public void startElement(String uri, String localName, String qName, Attributes attributes) {
                elementNames.add(localName);
            }

            @Override
            public void endElement(String uri, String localName, String qName) {
                elementNames.add("/" + localName);
            }
        }
        // 'systemBar', the external entity, is relative to 'systemFoo':
        Reader in = new StringReader("<?xml version=\"1.0\"?>\n" + "<!DOCTYPE foo [\n" + "  <!ENTITY bar SYSTEM 'systemBar'>\n" + "]>\n" + "<foo>&bar;</foo>");
        ExpatReader reader = new ExpatReader();
        Handler handler = new Handler();
        reader.setContentHandler(handler);
        reader.setEntityResolver(handler);
        InputSource source = new InputSource(in);
        source.setSystemId(server.getUrl("/systemFoo").toString());
        reader.parse(source);
        assertEquals(Arrays.asList("foo", "bar", "/bar", "/foo"), handler.elementNames);
    } finally {
        server.shutdown();
    }
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) InputSource(org.xml.sax.InputSource) ExpatReader(org.apache.harmony.xml.ExpatReader) MockWebServer(com.google.mockwebserver.MockWebServer) Attributes(org.xml.sax.Attributes) StringReader(java.io.StringReader) ExpatReader(org.apache.harmony.xml.ExpatReader) Reader(java.io.Reader) XMLReader(org.xml.sax.XMLReader) StringReader(java.io.StringReader) DefaultHandler(org.xml.sax.helpers.DefaultHandler) ContentHandler(org.xml.sax.ContentHandler) ArrayList(java.util.ArrayList) List(java.util.List) DefaultHandler(org.xml.sax.helpers.DefaultHandler)

Aggregations

MockWebServer (com.google.mockwebserver.MockWebServer)56 MockResponse (com.google.mockwebserver.MockResponse)39 CookieManager (java.net.CookieManager)15 RecordedRequest (com.google.mockwebserver.RecordedRequest)14 Test (org.junit.Test)14 ByteBuf (io.netty.buffer.ByteBuf)13 HttpCookie (java.net.HttpCookie)11 BaseLoadBalancer (com.netflix.loadbalancer.BaseLoadBalancer)7 Server (com.netflix.loadbalancer.Server)7 IClientConfig (com.netflix.client.config.IClientConfig)6 AvailabilityFilteringRule (com.netflix.loadbalancer.AvailabilityFilteringRule)6 DummyPing (com.netflix.loadbalancer.DummyPing)6 HttpResourceGroup (com.netflix.ribbon.http.HttpResourceGroup)6 IOException (java.io.IOException)6 HttpHost (org.apache.http.HttpHost)6 HttpResponse (org.apache.http.HttpResponse)6 HttpClient (org.apache.http.client.HttpClient)6 HttpGet (org.apache.http.client.methods.HttpGet)6 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)6 Map (java.util.Map)5