Example #1
0
  @Test
  public void testRedirectsDoNotIncludeTooManyCookies() throws Exception {
    MockWebServer redirectTarget = new MockWebServer();
    redirectTarget.enqueue(new MockResponse().setBody("A"));
    redirectTarget.start();

    MockWebServer redirectSource = new MockWebServer();
    redirectSource.enqueue(
        new MockResponse()
            .setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP)
            .addHeader("Location: " + redirectTarget.url("/")));
    redirectSource.start();

    CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
    HttpCookie cookie = new HttpCookie("c", "cookie");
    cookie.setDomain(redirectSource.getHostName());
    cookie.setPath("/");
    String portList = Integer.toString(redirectSource.getPort());
    cookie.setPortlist(portList);
    cookieManager.getCookieStore().add(redirectSource.url("/").uri(), cookie);
    client.setCookieJar(new JavaNetCookieJar(cookieManager));

    get(redirectSource.url("/"));
    RecordedRequest request = redirectSource.takeRequest();

    assertEquals("c=cookie", request.getHeader("Cookie"));

    for (String header : redirectTarget.takeRequest().getHeaders().names()) {
      if (header.startsWith("Cookie")) {
        fail(header);
      }
    }
  }
Example #2
0
  @Test
  public void testRfc2109Response() throws Exception {
    CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
    client.setCookieJar(new JavaNetCookieJar(cookieManager));
    MockWebServer server = new MockWebServer();
    server.start();

    HttpUrl urlWithIpAddress = urlWithIpAddress(server, "/path/foo");
    server.enqueue(
        new MockResponse()
            .addHeader(
                "Set-Cookie: a=android; "
                    + "Comment=this cookie is delicious; "
                    + "Domain="
                    + urlWithIpAddress.host()
                    + "; "
                    + "Max-Age=60; "
                    + "Path=/path; "
                    + "Secure; "
                    + "Version=1"));
    get(urlWithIpAddress);

    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.getCommentURL());
    assertEquals(false, cookie.getDiscard());
    assertEquals(60.0, cookie.getMaxAge(), 1.0); // Converting to a fixed date can cause rounding!
    assertEquals("/path", cookie.getPath());
    assertEquals(true, cookie.getSecure());
  }
Example #3
0
  @Test
  public void testNetscapeResponse() throws Exception {
    CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
    client.setCookieJar(new JavaNetCookieJar(cookieManager));
    MockWebServer server = new MockWebServer();
    server.start();

    HttpUrl urlWithIpAddress = urlWithIpAddress(server, "/path/foo");
    server.enqueue(
        new MockResponse()
            .addHeader(
                "Set-Cookie: a=android; "
                    + "expires=Fri, 31-Dec-9999 23:59:59 GMT; "
                    + "path=/path; "
                    + "domain="
                    + urlWithIpAddress.host()
                    + "; "
                    + "secure"));
    get(urlWithIpAddress);

    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());
    assertTrue(cookie.getMaxAge() > 100000000000L);
    assertEquals("/path", cookie.getPath());
    assertEquals(true, cookie.getSecure());
    assertEquals(0, cookie.getVersion());
  }
Example #4
0
  @Test
  public void testCookiesSentIgnoresCase() throws Exception {
    client.setCookieJar(
        new JavaNetCookieJar(
            new CookieManager() {
              @Override
              public Map<String, List<String>> get(
                  URI uri, Map<String, List<String>> requestHeaders) throws IOException {
                Map<String, List<String>> result = new HashMap<>();
                result.put("COOKIE", Collections.singletonList("Bar=bar"));
                result.put("cooKIE2", Collections.singletonList("Baz=baz"));
                return result;
              }
            }));

    MockWebServer server = new MockWebServer();
    server.enqueue(new MockResponse());
    server.start();

    get(server.url("/"));

    RecordedRequest request = server.takeRequest();
    assertEquals("Bar=bar; Baz=baz", request.getHeader("Cookie"));
    assertNull(request.getHeader("Cookie2"));
    assertNull(request.getHeader("Quux"));
  }
 @Test
 public void testEventBusIsNotPostedOn500Error() throws Exception {
   server.start();
   server.enqueue(
       new MockResponse()
           .setResponseCode(500)
           .setBody(
               getStringFromFile(
                   RuntimeEnvironment.application, "incorrect_success_response.json")));
   MainActivity.URL = server.url("/").toString();
   serviceHelper.setEventBus(eventBus);
   serviceHelper.getIndividualEpisodeData("Friends", "7");
   verifyNoMoreInteractions(eventBus);
 }
 @Test
 public void testEventBusIsPostedWithNoDataOnServiceCallWithAnIncorrectResponse()
     throws Exception {
   server.start();
   server.enqueue(
       new MockResponse()
           .setResponseCode(200)
           .setBody(
               getStringFromFile(
                   RuntimeEnvironment.application, "incorrect_success_response.json")));
   MainActivity.URL = server.url("/").toString();
   serviceHelper.setEventBus(eventBus);
   serviceHelper.getIndividualEpisodeData("Friends", "7");
   verify(eventBus).post(anyObject());
   verify(eventBus).post(captor.capture());
   IndividualEpisodeResponseEvent episodeLists =
       (IndividualEpisodeResponseEvent) captor.getValue();
   assertNull(episodeLists.getEpisodesList());
 }
 @Test
 public void testEventBusIsPostedOnSuccessfulServiceCall() throws Exception {
   server.start();
   server.enqueue(
       new MockResponse()
           .setResponseCode(200)
           .setBody(
               getStringFromFile(RuntimeEnvironment.application, "episodes_list_success.json")));
   MainActivity.URL = server.url("/").toString();
   serviceHelper.setEventBus(eventBus);
   serviceHelper.getIndividualEpisodeData("Friends", "7");
   verify(eventBus).post(anyObject());
   verify(eventBus).post(captor.capture());
   IndividualEpisodeResponseEvent episodeLists =
       (IndividualEpisodeResponseEvent) captor.getValue();
   assertThat(
       "The One Where I am testing something",
       equalTo(episodeLists.getEpisodesList().get(0).getTitle()));
 }
Example #8
0
  @Test
  public void testSendingCookiesFromStore() throws Exception {
    MockWebServer server = new MockWebServer();
    server.enqueue(new MockResponse());
    server.start();

    CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
    HttpCookie cookieA = new HttpCookie("a", "android");
    cookieA.setDomain(server.getHostName());
    cookieA.setPath("/");
    cookieManager.getCookieStore().add(server.url("/").uri(), cookieA);
    HttpCookie cookieB = new HttpCookie("b", "banana");
    cookieB.setDomain(server.getHostName());
    cookieB.setPath("/");
    cookieManager.getCookieStore().add(server.url("/").uri(), cookieB);
    client.setCookieJar(new JavaNetCookieJar(cookieManager));

    get(server.url("/"));
    RecordedRequest request = server.takeRequest();

    assertEquals("a=android; b=banana", request.getHeader("Cookie"));
  }
Example #9
0
 @Before
 public void setUp() throws Exception {
   server.start();
   socksProxy.play();
 }