]> source.dussan.org Git - jgit.git/commitdiff
HttpClientConnection.getHeaderFields should support multiple values for same header 97/126297/7
authorGabriel Couto <gmcouto@gmail.com>
Thu, 19 Jul 2018 05:34:56 +0000 (05:34 +0000)
committerDavid Pursehouse <david.pursehouse@gmail.com>
Mon, 23 Jul 2018 05:56:14 +0000 (06:56 +0100)
Bug: 537166
Change-Id: I1ba645968efe01a88f2484f030b7572228ba169c
Signed-off-by: Gabriel Couto <gmcouto@gmail.com>
lib/BUILD
org.eclipse.jgit.http.apache/src/org/eclipse/jgit/transport/http/apache/HttpClientConnection.java
org.eclipse.jgit.http.test/BUILD
org.eclipse.jgit.http.test/META-INF/MANIFEST.MF
org.eclipse.jgit.http.test/tst/org/eclipse/jgit/transport/http/apache/HttpClientConnectionTest.java [new file with mode: 0644]

index 10496a72e80abb84d48e616e3611b04142c20383..53eb19188aefb539577b4973255d7fc106be3b1b 100644 (file)
--- a/lib/BUILD
+++ b/lib/BUILD
@@ -51,6 +51,7 @@ java_library(
     name = "httpcore",
     visibility = [
         "//org.eclipse.jgit.http.apache:__pkg__",
+        "//org.eclipse.jgit.http.test:__pkg__",
         "//org.eclipse.jgit.lfs.server:__pkg__",
         "//org.eclipse.jgit.lfs.server.test:__pkg__",
         "//org.eclipse.jgit.pgm:__pkg__",
index 7f1fecb9bf0ba5049b146bcc94a6d0dbc8165854..77c5dc0f3eaafefa5972eb20c1a2647910bad039 100644 (file)
@@ -272,10 +272,14 @@ public class HttpClientConnection implements HttpConnection {
        public Map<String, List<String>> getHeaderFields() {
                Map<String, List<String>> ret = new HashMap<>();
                for (Header hdr : resp.getAllHeaders()) {
-                       List<String> list = new LinkedList<>();
-                       for (HeaderElement hdrElem : hdr.getElements())
+                       List<String> list = ret.get(hdr.getName());
+                       if (list == null) {
+                               list = new LinkedList<>();
+                               ret.put(hdr.getName(), list);
+                       }
+                       for (HeaderElement hdrElem : hdr.getElements()) {
                                list.add(hdrElem.toString());
-                       ret.put(hdr.getName(), list);
+                       }
                }
                return ret;
        }
index 85a22422be67f4eada74aaabb0b589f9c9d13297..dcffa066d03bd3292509c0bc7bf7d08ae53dd849 100644 (file)
@@ -10,6 +10,7 @@ junit_tests(
     deps = [
         ":helpers",
         "//lib:commons-logging",
+        "//lib:httpcore",
         "//lib:jetty-http",
         "//lib:jetty-io",
         "//lib:jetty-security",
index 0bb805051507aeb30c61b0a0d72aa5aef17800cf..678f77f2449169c51b2eabe55d61d06ae466b9f2 100644 (file)
@@ -11,6 +11,9 @@ Import-Package: javax.servlet;version="[2.5.0,3.2.0)",
  javax.servlet.http;version="[2.5.0,3.2.0)",
  org.apache.commons.codec;version="[1.6.0,2.0.0)",
  org.apache.commons.codec.binary;version="[1.6.0,2.0.0)",
+ org.apache.http;version="[4.3.0,5.0.0)",
+ org.apache.http.client;version="[4.3.0,5.0.0)",
+ org.apache.http.message;version="[4.3.0,5.0.0)",
  org.eclipse.jetty.continuation;version="[9.4.5,10.0.0)",
  org.eclipse.jetty.http;version="[9.4.5,10.0.0)",
  org.eclipse.jetty.io;version="[9.4.5,10.0.0)",
diff --git a/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/transport/http/apache/HttpClientConnectionTest.java b/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/transport/http/apache/HttpClientConnectionTest.java
new file mode 100644 (file)
index 0000000..678ed64
--- /dev/null
@@ -0,0 +1,90 @@
+package org.eclipse.jgit.transport.http.apache;\r
+\r
+import org.apache.http.HttpEntity;\r
+import org.apache.http.HttpResponse;\r
+import org.apache.http.ProtocolVersion;\r
+import org.apache.http.StatusLine;\r
+import org.apache.http.message.AbstractHttpMessage;\r
+import org.junit.Test;\r
+\r
+import java.net.MalformedURLException;\r
+import java.util.List;\r
+import java.util.Locale;\r
+\r
+import static org.junit.Assert.assertEquals;\r
+import static org.junit.Assert.assertTrue;\r
+\r
+public class HttpClientConnectionTest {\r
+    @Test\r
+    public void testGetHeaderFieldsAllowMultipleValues() throws MalformedURLException {\r
+        HttpResponse responseMock = new HttpResponseMock();\r
+        String headerField = "WWW-Authenticate";\r
+        responseMock.addHeader(headerField, "Basic");\r
+        responseMock.addHeader(headerField, "Digest");\r
+        responseMock.addHeader(headerField, "NTLM");\r
+        HttpClientConnection connection = new HttpClientConnection("http://0.0.0.0/");\r
+        connection.resp = responseMock;\r
+        List<String> headerValues = connection.getHeaderFields().get(headerField);\r
+        assertEquals(3, headerValues.size());\r
+        assertTrue(headerValues.contains("Basic"));\r
+        assertTrue(headerValues.contains("Digest"));\r
+        assertTrue(headerValues.contains("NTLM"));\r
+    }\r
+\r
+    private class HttpResponseMock extends AbstractHttpMessage implements HttpResponse {\r
+        @Override\r
+        public StatusLine getStatusLine() {\r
+            return null;\r
+        }\r
+\r
+        @Override\r
+        public void setStatusLine(StatusLine statusLine) {\r
+\r
+        }\r
+\r
+        @Override\r
+        public void setStatusLine(ProtocolVersion protocolVersion, int i) {\r
+\r
+        }\r
+\r
+        @Override\r
+        public void setStatusLine(ProtocolVersion protocolVersion, int i, String s) {\r
+\r
+        }\r
+\r
+        @Override\r
+        public void setStatusCode(int i) throws IllegalStateException {\r
+\r
+        }\r
+\r
+        @Override\r
+        public void setReasonPhrase(String s) throws IllegalStateException {\r
+\r
+        }\r
+\r
+        @Override\r
+        public HttpEntity getEntity() {\r
+            return null;\r
+        }\r
+\r
+        @Override\r
+        public void setEntity(HttpEntity httpEntity) {\r
+\r
+        }\r
+\r
+        @Override\r
+        public Locale getLocale() {\r
+            return null;\r
+        }\r
+\r
+        @Override\r
+        public void setLocale(Locale locale) {\r
+\r
+        }\r
+\r
+        @Override\r
+        public ProtocolVersion getProtocolVersion() {\r
+            return null;\r
+        }\r
+    }\r
+}\r