From 280707b0fc717fdd8898242f21589f0c7c7d87be Mon Sep 17 00:00:00 2001 From: Gabriel Couto Date: Thu, 19 Jul 2018 05:34:56 +0000 Subject: [PATCH] HttpClientConnection.getHeaderFields should support multiple values for same header Bug: 537166 Change-Id: I1ba645968efe01a88f2484f030b7572228ba169c Signed-off-by: Gabriel Couto --- lib/BUILD | 1 + .../http/apache/HttpClientConnection.java | 10 ++- org.eclipse.jgit.http.test/BUILD | 1 + .../META-INF/MANIFEST.MF | 3 + .../http/apache/HttpClientConnectionTest.java | 90 +++++++++++++++++++ 5 files changed, 102 insertions(+), 3 deletions(-) create mode 100644 org.eclipse.jgit.http.test/tst/org/eclipse/jgit/transport/http/apache/HttpClientConnectionTest.java diff --git a/lib/BUILD b/lib/BUILD index 10496a72e8..53eb19188a 100644 --- 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__", diff --git a/org.eclipse.jgit.http.apache/src/org/eclipse/jgit/transport/http/apache/HttpClientConnection.java b/org.eclipse.jgit.http.apache/src/org/eclipse/jgit/transport/http/apache/HttpClientConnection.java index 7f1fecb9bf..77c5dc0f3e 100644 --- a/org.eclipse.jgit.http.apache/src/org/eclipse/jgit/transport/http/apache/HttpClientConnection.java +++ b/org.eclipse.jgit.http.apache/src/org/eclipse/jgit/transport/http/apache/HttpClientConnection.java @@ -272,10 +272,14 @@ public class HttpClientConnection implements HttpConnection { public Map> getHeaderFields() { Map> ret = new HashMap<>(); for (Header hdr : resp.getAllHeaders()) { - List list = new LinkedList<>(); - for (HeaderElement hdrElem : hdr.getElements()) + List 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; } diff --git a/org.eclipse.jgit.http.test/BUILD b/org.eclipse.jgit.http.test/BUILD index 85a22422be..dcffa066d0 100644 --- a/org.eclipse.jgit.http.test/BUILD +++ b/org.eclipse.jgit.http.test/BUILD @@ -10,6 +10,7 @@ junit_tests( deps = [ ":helpers", "//lib:commons-logging", + "//lib:httpcore", "//lib:jetty-http", "//lib:jetty-io", "//lib:jetty-security", diff --git a/org.eclipse.jgit.http.test/META-INF/MANIFEST.MF b/org.eclipse.jgit.http.test/META-INF/MANIFEST.MF index 0bb8050515..678f77f244 100644 --- a/org.eclipse.jgit.http.test/META-INF/MANIFEST.MF +++ b/org.eclipse.jgit.http.test/META-INF/MANIFEST.MF @@ -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 index 0000000000..678ed64e8b --- /dev/null +++ b/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/transport/http/apache/HttpClientConnectionTest.java @@ -0,0 +1,90 @@ +package org.eclipse.jgit.transport.http.apache; + +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.ProtocolVersion; +import org.apache.http.StatusLine; +import org.apache.http.message.AbstractHttpMessage; +import org.junit.Test; + +import java.net.MalformedURLException; +import java.util.List; +import java.util.Locale; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class HttpClientConnectionTest { + @Test + public void testGetHeaderFieldsAllowMultipleValues() throws MalformedURLException { + HttpResponse responseMock = new HttpResponseMock(); + String headerField = "WWW-Authenticate"; + responseMock.addHeader(headerField, "Basic"); + responseMock.addHeader(headerField, "Digest"); + responseMock.addHeader(headerField, "NTLM"); + HttpClientConnection connection = new HttpClientConnection("http://0.0.0.0/"); + connection.resp = responseMock; + List headerValues = connection.getHeaderFields().get(headerField); + assertEquals(3, headerValues.size()); + assertTrue(headerValues.contains("Basic")); + assertTrue(headerValues.contains("Digest")); + assertTrue(headerValues.contains("NTLM")); + } + + private class HttpResponseMock extends AbstractHttpMessage implements HttpResponse { + @Override + public StatusLine getStatusLine() { + return null; + } + + @Override + public void setStatusLine(StatusLine statusLine) { + + } + + @Override + public void setStatusLine(ProtocolVersion protocolVersion, int i) { + + } + + @Override + public void setStatusLine(ProtocolVersion protocolVersion, int i, String s) { + + } + + @Override + public void setStatusCode(int i) throws IllegalStateException { + + } + + @Override + public void setReasonPhrase(String s) throws IllegalStateException { + + } + + @Override + public HttpEntity getEntity() { + return null; + } + + @Override + public void setEntity(HttpEntity httpEntity) { + + } + + @Override + public Locale getLocale() { + return null; + } + + @Override + public void setLocale(Locale locale) { + + } + + @Override + public ProtocolVersion getProtocolVersion() { + return null; + } + } +} -- 2.39.5