Browse Source

Updates webflux ProxyExchange to overwrite incomming headers rather than add to them.

Added the test to mvc ProxyExchange and it currently exhibits this behavior already and the test passes.

see gh-643
pull/656/head
Spencer Gibb 6 years ago
parent
commit
0eb7206ab9
No known key found for this signature in database
GPG Key ID: 7788A47380690861
  1. 32
      spring-cloud-gateway-mvc/src/test/java/org/springframework/cloud/gateway/mvc/ProductionConfigurationTests.java
  2. 6
      spring-cloud-gateway-webflux/src/main/java/org/springframework/cloud/gateway/webflux/ProxyExchange.java
  3. 18
      spring-cloud-gateway-webflux/src/test/java/org/springframework/cloud/gateway/webflux/ProductionConfigurationTests.java

32
spring-cloud-gateway-mvc/src/test/java/org/springframework/cloud/gateway/mvc/ProductionConfigurationTests.java

@ -44,6 +44,7 @@ import org.springframework.http.RequestEntity; @@ -44,6 +44,7 @@ import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
@ -230,7 +231,21 @@ public class ProductionConfigurationTests { @@ -230,7 +231,21 @@ public class ProductionConfigurationTests {
.isEqualTo("host=localhost;foobar");
}
@SpringBootApplication
@Test
@SuppressWarnings({"Duplicates", "unchecked"})
public void headers() throws Exception {
Map<String, List<String>> headers = rest.exchange(RequestEntity.get(rest.getRestTemplate().getUriTemplateHandler()
.expand("/proxy/headers")).header("foo", "bar").header("abc", "xyz").build(), Map.class).getBody();
System.out.println(headers);
assertThat(headers).doesNotContainKey("foo")
.doesNotContainKey("hello")
.containsKeys("bar", "abc");
assertThat(headers.get("bar")).containsOnly("hello");
assertThat(headers.get("abc")).containsOnly("123");
}
@SpringBootApplication
static class TestApplication {
@RestController
@ -359,6 +374,17 @@ public class ProductionConfigurationTests { @@ -359,6 +374,17 @@ public class ProductionConfigurationTests {
proxy.forward(path);
}
@GetMapping("/proxy/headers")
@SuppressWarnings("Duplicates")
public ResponseEntity<Map<String, List<String>>> headers(ProxyExchange<Map<String, List<String>>> proxy) {
proxy.sensitive("foo");
proxy.sensitive("hello");
proxy.header("bar", "hello");
proxy.header("abc", "123");
proxy.header("hello", "world");
return proxy.uri(home.toString() + "/headers").get();
}
private <T> ResponseEntity<T> first(ResponseEntity<List<T>> response) {
return ResponseEntity.status(response.getStatusCode())
.headers(response.getHeaders())
@ -397,6 +423,10 @@ public class ProductionConfigurationTests { @@ -397,6 +423,10 @@ public class ProductionConfigurationTests {
return Arrays.asList(new Bar(custom + foos.iterator().next().getName()));
}
@GetMapping("/headers")
public Map<String, List<String>> headers(@RequestHeader HttpHeaders headers) {
return new LinkedMultiValueMap<>(headers);
}
}
@JsonIgnoreProperties(ignoreUnknown = true)

6
spring-cloud-gateway-webflux/src/main/java/org/springframework/cloud/gateway/webflux/ProxyExchange.java

@ -350,8 +350,10 @@ public class ProxyExchange<T> { @@ -350,8 +350,10 @@ public class ProxyExchange<T> {
}
private void addHeaders(HttpHeaders headers, HttpHeaders toAdd) {
Set<String> filteredHeaders = filterHeaderKeys(toAdd);
filteredHeaders.stream().forEach(header -> headers.addAll(header, toAdd.get(header)));
Set<String> filteredKeys = filterHeaderKeys(toAdd);
filteredKeys.stream()
.filter(key -> !headers.containsKey(key))
.forEach(header -> headers.addAll(header, toAdd.get(header)));
}
private Set<String> filterHeaderKeys(HttpHeaders headers) {

18
spring-cloud-gateway-webflux/src/test/java/org/springframework/cloud/gateway/webflux/ProductionConfigurationTests.java

@ -178,13 +178,16 @@ public class ProductionConfigurationTests { @@ -178,13 +178,16 @@ public class ProductionConfigurationTests {
}
@Test
@SuppressWarnings({"Duplicates", "unchecked"})
public void headers() throws Exception {
Map<String, List<String>> headers = rest.exchange(RequestEntity.get(rest.getRestTemplate().getUriTemplateHandler()
.expand("/proxy/headers")).header("foo", "bar").header("abc", "xyz").build(), Map.class).getBody();
assertTrue(!headers.containsKey("foo"));
assertTrue(!headers.containsKey("hello"));
assertEquals("hello", headers.get("bar"));
assertEquals("123", headers.get("abc"));
assertThat(headers).doesNotContainKey("foo")
.doesNotContainKey("hello")
.containsKeys("bar", "abc");
assertThat(headers.get("bar")).containsOnly("hello");
assertThat(headers.get("abc")).containsOnly("123");
}
@SpringBootApplication
@ -282,14 +285,13 @@ public class ProductionConfigurationTests { @@ -282,14 +285,13 @@ public class ProductionConfigurationTests {
}
@GetMapping("/proxy/headers")
public Mono<ResponseEntity<Map<String, String>>> headers(ProxyExchange<Map<String, String>> proxy) {
public Mono<ResponseEntity<Map<String, List<String>>>> headers(ProxyExchange<Map<String, List<String>>> proxy) {
proxy.sensitive("foo");
proxy.sensitive("hello");
proxy.header("bar", "hello");
proxy.header("abc", "123");
proxy.header("hello", "world");
return proxy.uri(home.toString() + "/headers").get();
}
private <T> ResponseEntity<T> first(ResponseEntity<List<T>> response) {
@ -332,8 +334,8 @@ public class ProductionConfigurationTests { @@ -332,8 +334,8 @@ public class ProductionConfigurationTests {
}
@GetMapping("/headers")
public Map<String, String> headers(@RequestHeader HttpHeaders headers) {
return headers.toSingleValueMap();
public Map<String, List<String>> headers(@RequestHeader HttpHeaders headers) {
return headers;
}
}

Loading…
Cancel
Save