Exemplo n.º 1
0
  @Test
  public void noneResponseSourceHeaders() throws Exception {
    server.enqueue(new MockResponse().setBody("Isla Nublar"));

    HttpURLConnection connection1 = factory.open(server.getUrl("/"));
    assertResponseHeader(connection1, "NETWORK 200");
    assertResponseBody(connection1, "Isla Nublar");

    HttpURLConnection connection2 = factory.open(server.getUrl("/"));
    connection2.setRequestProperty("Cache-Control", "only-if-cached");
    assertResponseHeader(connection2, "NONE");
  }
Exemplo n.º 2
0
  @Test
  public void cacheResponseSourceHeaders() throws Exception {
    server.enqueue(
        new MockResponse()
            .addHeader("Expires: " + formatDate(2, TimeUnit.HOURS))
            .setBody("Isla Nublar"));

    HttpURLConnection connection1 = factory.open(server.getUrl("/"));
    assertResponseHeader(connection1, "NETWORK 200");
    assertResponseBody(connection1, "Isla Nublar");

    HttpURLConnection connection2 = factory.open(server.getUrl("/"));
    assertResponseHeader(connection2, "CACHE 200");
    assertResponseBody(connection2, "Isla Nublar");
  }
Exemplo n.º 3
0
  @Test
  public void conditionalCacheMissResponseSourceHeaders() throws Exception {
    server.enqueue(
        new MockResponse()
            .addHeader("Last-Modified: " + formatDate(0, TimeUnit.SECONDS))
            .addHeader("Cache-Control: max-age=0")
            .setBody("Isla Nublar"));
    server.enqueue(new MockResponse().setBody("Isla Sorna"));

    HttpURLConnection connection1 = factory.open(server.getUrl("/"));
    assertResponseHeader(connection1, "NETWORK 200");
    assertResponseBody(connection1, "Isla Nublar");

    HttpURLConnection connection2 = factory.open(server.getUrl("/"));
    assertResponseHeader(connection2, "CONDITIONAL_CACHE 200");
    assertResponseBody(connection2, "Isla Sorna");
  }
Exemplo n.º 4
0
  @Test
  public void networkFailureResponseSourceHeader() throws Exception {
    server.enqueue(new MockResponse().setResponseCode(404));

    HttpURLConnection connection = factory.open(server.getUrl("/"));
    assertResponseHeader(connection, "NETWORK 404");
    connection.getErrorStream().close();
  }
Exemplo n.º 5
0
  @Test
  public void networkResponseSourceHeader() throws Exception {
    server.enqueue(new MockResponse().setBody("Isla Sorna"));

    HttpURLConnection connection = factory.open(server.getUrl("/"));
    assertResponseHeader(connection, "NETWORK 200");
    assertResponseBody(connection, "Isla Sorna");
  }
Exemplo n.º 6
0
  @Test
  public void setInstanceFollowRedirectsFalse() throws Exception {
    server.enqueue(new MockResponse().setResponseCode(302).addHeader("Location: /b").setBody("A"));
    server.enqueue(new MockResponse().setBody("B"));

    HttpURLConnection connection = factory.open(server.getUrl("/a"));
    connection.setInstanceFollowRedirects(false);
    assertResponseBody(connection, "A");
    assertResponseCode(connection, 302);
  }
Exemplo n.º 7
0
  /**
   * Response code 407 should only come from proxy servers. Android's client throws if it is sent by
   * an origin server.
   */
  @Test
  public void originServerSends407() throws Exception {
    server.enqueue(new MockResponse().setResponseCode(407));

    HttpURLConnection conn = factory.open(server.getUrl("/"));
    try {
      conn.getResponseCode();
      fail();
    } catch (IOException ignored) {
    }
  }
Exemplo n.º 8
0
 @Override
 protected HttpURLConnection createConnection(URL url) throws IOException {
   return mFactory.open(url);
 }