Skip to content

Commit

Permalink
Always copy ServerResponse headers
Browse files Browse the repository at this point in the history
Prior to this commit, ServerResponse headers would only be written if
there were no existing headers with the same name, thus making it
impossible to overwrite existing headers.

With the changes in this commit, headers are always written.

Closes gh-27741
  • Loading branch information
poutsma committed Nov 30, 2021
1 parent 40d2058 commit 2a5713f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,7 @@ private void writeStatusAndHeaders(ServerHttpResponse response) {

private static <K,V> void copy(MultiValueMap<K,V> src, MultiValueMap<K,V> dst) {
if (!src.isEmpty()) {
src.entrySet().stream()
.filter(entry -> !dst.containsKey(entry.getKey()))
.forEach(entry -> dst.put(entry.getKey(), entry.getValue()));
dst.putAll(src);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,23 @@ public void copyCookies() {
assertThat(serverResponse.block().cookies().isEmpty()).isFalse();
}

@Test
public void overwriteHeaders() {
ServerResponse serverResponse =
ServerResponse.ok().headers(headers -> headers.set("Foo", "Bar")).build().block();
assertThat(serverResponse).isNotNull();

MockServerWebExchange mockExchange = MockServerWebExchange
.builder(MockServerHttpRequest.get("https://example.org"))
.build();
MockServerHttpResponse response = mockExchange.getResponse();
response.getHeaders().set("Foo", "Baz");

serverResponse.writeTo(mockExchange, EMPTY_CONTEXT).block();

assertThat(response.getHeaders().getFirst("Foo")).isEqualTo("Bar");
}


@Test
public void build() {
Expand Down

0 comments on commit 2a5713f

Please sign in to comment.