From f3ced9e0ed9af7d4ef6f0b7395df37155d0cc806 Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Mon, 16 Mar 2015 15:19:07 -0600 Subject: [PATCH] allow zuul to have multiple values for each named header fixes gh-260 --- .../zuul/filters/ProxyRequestHelper.java | 13 ++-- .../zuul/filters/ProxyRequestHelperTests.java | 68 +++++++++++++++++++ 2 files changed, 76 insertions(+), 5 deletions(-) create mode 100644 spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/ProxyRequestHelperTests.java diff --git a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/ProxyRequestHelper.java b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/ProxyRequestHelper.java index 49b8ccef..dce47fe6 100644 --- a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/ProxyRequestHelper.java +++ b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/ProxyRequestHelper.java @@ -76,13 +76,16 @@ public class ProxyRequestHelper { HttpServletRequest request) { RequestContext context = RequestContext.getCurrentContext(); MultiValueMap headers = new LinkedMultiValueMap<>(); - Enumeration headerNames = request.getHeaderNames(); + Enumeration headerNames = request.getHeaderNames(); if (headerNames != null) { while (headerNames.hasMoreElements()) { - String name = (String) headerNames.nextElement(); - String value = request.getHeader(name); - if (isIncludedHeader(name)) { - headers.set(name, value); + String name = headerNames.nextElement(); + if (isIncludedHeader(name)) { + Enumeration values = request.getHeaders(name); + while (values.hasMoreElements()) { + String value = values.nextElement(); + headers.add(name, value); + } } } } diff --git a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/ProxyRequestHelperTests.java b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/ProxyRequestHelperTests.java new file mode 100644 index 00000000..87adc3f1 --- /dev/null +++ b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/ProxyRequestHelperTests.java @@ -0,0 +1,68 @@ +/* + * Copyright 2013-2015 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.netflix.zuul.filters; + +import java.util.List; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.springframework.boot.actuate.trace.TraceRepository; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.util.MultiValueMap; + +import static org.junit.Assert.*; +import static org.hamcrest.Matchers.*; +import static org.mockito.MockitoAnnotations.initMocks; + +/** + * @author Spencer Gibb + */ +public class ProxyRequestHelperTests { + + @Mock + private TraceRepository traceRepository; + + @Before + public void init() { + initMocks(this); + } + + @Test + public void buildZuulRequestHeadersWork() { + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/"); + request.addHeader("singleName", "singleValue"); + request.addHeader("multiName", "multiValue1"); + request.addHeader("multiName", "multiValue2"); + + ProxyRequestHelper helper = new ProxyRequestHelper(); + helper.setTraces(traceRepository); + + MultiValueMap headers = helper.buildZuulRequestHeaders(request); + List singleName = headers.get("singleName"); + assertThat(singleName, is(notNullValue())); + assertThat(singleName.size(), is(1)); + + List multiName = headers.get("multiName"); + assertThat(multiName, is(notNullValue())); + assertThat(multiName.size(), is(2)); + + List missingName = headers.get("missingName"); + assertThat(missingName, is(nullValue())); + + } +}