Esempio n. 1
0
  /**
   * 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}.
   */
  @Test
  public void testHeadersSentToCookieHandler() throws IOException, InterruptedException {
    final Map<String, List<String>> cookieHandlerHeaders = new HashMap<>();
    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<>();
            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();
    server.enqueue(new MockResponse());
    server.play();

    HttpURLConnection connection = new OkUrlFactory(client).open(server.getUrl("/"));
    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/[email protected]/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"));
  }
Esempio n. 2
0
 protected static void beforeRequest(final HttpURLConnection connection) {
   if (Client.DEBUG) {
     for (String key : connection.getRequestProperties().keySet()) {
       System.out.println(
           "Connection Header: " + key + " = " + connection.getRequestProperty(key));
     }
   }
 }
 public Map<String, String> getAllHeaders() {
   Map<String, List<String>> origHeaders = connection.getRequestProperties();
   Map<String, String> headers = new HashMap<String, String>(origHeaders.size());
   for (String name : origHeaders.keySet()) {
     List<String> values = origHeaders.get(name);
     if (!values.isEmpty()) {
       headers.put(name, values.get(0));
     }
   }
   return headers;
 }
Esempio n. 4
0
 public void a(HttpURLConnection httpurlconnection, Object obj) {
   a("=== HTTP Request ===");
   a(
       (new StringBuilder())
           .append(httpurlconnection.getRequestMethod())
           .append(" ")
           .append(httpurlconnection.getURL().toString())
           .toString());
   if (obj instanceof String) {
     a((new StringBuilder()).append("Content: ").append((String) obj).toString());
   }
   a(httpurlconnection.getRequestProperties());
 }
Esempio n. 5
0
 public boolean UpdateCheck(String urlStr) {
   boolean update = false;
   try {
     URL url = new URL(urlStr);
     HttpURLConnection urlconn = (HttpURLConnection) url.openConnection();
     urlconn.setRequestMethod("GET");
     urlconn.setInstanceFollowRedirects(true);
     urlconn.getRequestProperties();
     urlconn.connect();
     String mETag = urlconn.getHeaderField("ETag");
     urlconn.disconnect();
     String ETag = GetPref("ETag");
     if (!ETag.equals(mETag)) {
       update = true;
       // if (mETag.equals("\"3401f-1fba4e-4c8c2d3f37100\"")) update = false; // for my tests (git)
       // if (mETag.equals("\"1a6f2-affb8c-4b366f9fd3640\"")) update = false; // for my tests
       // (stable)
     }
   } catch (Exception e) {
     e.printStackTrace();
   }
   return update;
 }
Esempio n. 6
0
 static void ensureRequestProperty(HttpURLConnection connection, String name, Object value) {
   if (!connection.getRequestProperties().containsKey(name)) {
     addRequestProperty(connection, name, value);
   }
 }
 public Map getRequestProperties() {
   return _flddelegate.getRequestProperties();
 }