HTTPCLIENT-1671: default headers should only be set if a header with the same name hasn't already been set

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1695155 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2015-08-10 19:43:53 +00:00
parent 51111452a7
commit e52e269dc8
2 changed files with 5 additions and 4 deletions

View File

@ -72,7 +72,9 @@ public class RequestDefaultHeaders implements HttpRequestInterceptor {
if (this.defaultHeaders != null) {
for (final Header defHeader : this.defaultHeaders) {
request.addHeader(defHeader);
if(!request.containsHeader(defHeader.getName())) {
request.addHeader(defHeader);
}
}
}
}

View File

@ -66,16 +66,15 @@ public class TestRequestDefaultHeaders {
final HttpRequest request = new BasicHttpRequest("GET", "/");
request.addHeader("custom", "stuff");
final List<Header> defheaders = new ArrayList<>();
defheaders.add(new BasicHeader("custom", "more stuff"));
defheaders.add(new BasicHeader("custom", "other stuff"));
final HttpContext context = new BasicHttpContext();
final HttpRequestInterceptor interceptor = new RequestDefaultHeaders(defheaders);
interceptor.process(request, context);
final Header[] headers = request.getHeaders("custom");
Assert.assertNotNull(headers);
Assert.assertEquals(2, headers.length);
Assert.assertEquals(1, headers.length);
Assert.assertEquals("stuff", headers[0].getValue());
Assert.assertEquals("more stuff", headers[1].getValue());
}
}