http-client-branch: additional tests for header case-insensitivity http-client-branch
authorchegar
Tue, 15 May 2018 16:10:17 +0100
branchhttp-client-branch
changeset 56556 46bb98e9db71
parent 56539 a738880f0bd8
child 56572 c8fe5ffdfe98
http-client-branch: additional tests for header case-insensitivity
test/jdk/java/net/httpclient/AsFileDownloadTest.java
test/jdk/java/net/httpclient/RequestBuilderTest.java
--- a/test/jdk/java/net/httpclient/AsFileDownloadTest.java	Wed May 09 16:56:42 2018 -0700
+++ b/test/jdk/java/net/httpclient/AsFileDownloadTest.java	Tue May 15 16:10:17 2018 +0100
@@ -53,6 +53,7 @@
 import java.net.InetSocketAddress;
 import java.net.URI;
 import java.net.http.HttpClient;
+import java.net.http.HttpHeaders;
 import java.net.http.HttpRequest;
 import java.net.http.HttpRequest.BodyPublishers;
 import java.net.http.HttpResponse;
@@ -63,9 +64,8 @@
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
-import java.util.logging.ConsoleHandler;
-import java.util.logging.Level;
-import java.util.logging.Logger;
+import java.util.Locale;
+import java.util.Map;
 import javax.net.ssl.SSLContext;
 import jdk.testlibrary.SimpleSSLContext;
 import jdk.test.lib.util.FileUtils;
@@ -188,6 +188,10 @@
         assertEquals(response.headers().firstValue("Content-Disposition").get(),
                      contentDispositionValue);
         assertEquals(fileContents, "May the luck of the Irish be with you!");
+
+        // additional checks unrelated to file download
+        caseInsensitivityOfHeaders(request.headers());
+        caseInsensitivityOfHeaders(response.headers());
     }
 
     // --- Negative
@@ -372,4 +376,30 @@
             }
         }
     }
+
+    // ---
+
+    // Asserts case-insensitivity of headers (nothing to do with file
+    // download, just convenient as we have a couple of header instances. )
+    static void caseInsensitivityOfHeaders(HttpHeaders headers) {
+        try {
+            for (Map.Entry<String, List<String>> entry : headers.map().entrySet()) {
+                String headerName = entry.getKey();
+                List<String> headerValue = entry.getValue();
+
+                for (String name : List.of(headerName.toUpperCase(Locale.ROOT),
+                                           headerName.toLowerCase(Locale.ROOT))) {
+                    assertTrue(headers.firstValue(name).isPresent());
+                    assertEquals(headers.firstValue(name).get(), headerValue.get(0));
+                    assertEquals(headers.allValues(name).size(), headerValue.size());
+                    assertEquals(headers.allValues(name), headerValue);
+                    assertEquals(headers.map().get(name).size(), headerValue.size());
+                    assertEquals(headers.map().get(name), headerValue);
+                }
+            }
+        } catch (Throwable t) {
+            System.out.println("failure in caseInsensitivityOfHeaders with:" + headers);
+            throw t;
+        }
+    }
 }
--- a/test/jdk/java/net/httpclient/RequestBuilderTest.java	Wed May 09 16:56:42 2018 -0700
+++ b/test/jdk/java/net/httpclient/RequestBuilderTest.java	Tue May 15 16:10:17 2018 +0100
@@ -209,6 +209,7 @@
         for (HttpRequest r : requests) {
             assertEquals(r.headers().map().size(), 1);
             assertTrue(r.headers().firstValue("A").isPresent());
+            assertTrue(r.headers().firstValue("a").isPresent());
             assertEquals(r.headers().firstValue("A").get(), "B");
             assertEquals(r.headers().allValues("A"), List.of("B"));
             assertEquals(r.headers().allValues("C").size(), 0);
@@ -310,6 +311,30 @@
             assertThrows(UOE, () -> r.headers().allValues("A").addAll(List.of("Z")));
             assertThrows(UOE, () -> r.headers().allValues("A").add(1, "Z"));
         }
+
+        // case-insensitivity
+        requests = List.of(
+                newBuilder(uri)
+                        .header("Accept-Encoding", "gzip, deflate").build(),
+                newBuilder(uri)
+                        .header("accept-encoding", "gzip, deflate").build(),
+                newBuilder(uri)
+                        .header("AccePt-EncodINg", "gzip, deflate").build(),
+                newBuilder(uri)
+                        .header("AcCEpt-EncoDIng", "gzip, deflate").build()
+        );
+        for (HttpRequest r : requests) {
+            for (String name : List.of("Accept-Encoding", "accept-encoding",
+                                       "aCCept-EnCODing", "accepT-encodinG")) {
+                assertTrue(r.headers().firstValue(name).isPresent());
+                assertTrue(r.headers().allValues(name).contains("gzip, deflate"));
+                assertEquals(r.headers().firstValue(name).get(), "gzip, deflate");
+                assertEquals(r.headers().allValues(name).size(), 1);
+                assertEquals(r.headers().map().size(), 1);
+                assertEquals(r.headers().map().get(name).size(), 1);
+                assertEquals(r.headers().map().get(name).get(0), "gzip, deflate");
+            }
+        }
     }
 
     private static final Set<String> RESTRICTED = Set.of("connection", "content-length",