Skip to content

Commit 4424636

Browse files
committed
GH-1556 Document and test header forwarding of the exporter source
Document which source response headers are forwarded to the sink, and add tests that credentials, cookies and configured ignored headers are not forwarded and that includeHeaders=false forwards none. Resolves #1556 Signed-off-by: Oleg Zhurakousky <oleg.zhurakousky@broadcom.com>
1 parent b09aff1 commit 4424636

2 files changed

Lines changed: 78 additions & 1 deletion

File tree

‎spring-cloud-function-web/src/main/java/org/springframework/cloud/function/web/source/ExporterProperties.java‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,10 @@ public static class Source {
103103

104104
/**
105105
* Include the incoming headers in the outgoing Supplier. If true the supplier
106-
* will be of generic type Message of T equal to the source type.
106+
* will be of generic type Message of T equal to the source type. The headers of
107+
* the source response are forwarded to the sink, except for credentials, cookies
108+
* and the other headers that are never propagated, and those listed in
109+
* 'spring.cloud.function.http.ignored-headers'. Set to false to forward none.
107110
*/
108111
private boolean includeHeaders = true;
109112

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2026-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.function.web.source;
18+
19+
import java.util.List;
20+
21+
import org.junit.jupiter.api.Test;
22+
import reactor.core.publisher.Mono;
23+
24+
import org.springframework.cloud.function.web.FunctionHttpProperties;
25+
import org.springframework.http.HttpStatus;
26+
import org.springframework.messaging.Message;
27+
import org.springframework.web.reactive.function.client.ClientResponse;
28+
import org.springframework.web.reactive.function.client.WebClient;
29+
30+
import static org.assertj.core.api.Assertions.assertThat;
31+
32+
/**
33+
* Tests for the header handling of {@link HttpSupplier}.
34+
*
35+
* @author Oleg Zhurakousky
36+
*/
37+
class HttpSupplierTests {
38+
39+
@Test
40+
void sourceResponseHeadersAreSanitizedBeforeBeingForwarded() {
41+
FunctionHttpProperties httpProperties = new FunctionHttpProperties();
42+
httpProperties.setIgnoredHeaders(List.of("X-Internal-Token"));
43+
44+
Message<?> message = (Message<?>) supplier(true, httpProperties).get().blockFirst();
45+
46+
assertThat(message.getPayload()).isEqualTo("hello");
47+
assertThat(message.getHeaders()).containsEntry("x-trace-id", "trace-123")
48+
.doesNotContainKeys("set-cookie", "authorization", "x-internal-token", "location");
49+
}
50+
51+
@Test
52+
void noHeadersAreForwardedWhenIncludeHeadersIsDisabled() {
53+
Object value = supplier(false, new FunctionHttpProperties()).get().blockFirst();
54+
55+
assertThat(value).isEqualTo("hello");
56+
}
57+
58+
private static HttpSupplier supplier(boolean includeHeaders, FunctionHttpProperties httpProperties) {
59+
WebClient client = WebClient.builder().exchangeFunction(request -> Mono.just(ClientResponse.create(HttpStatus.OK)
60+
.header("X-Trace-Id", "trace-123")
61+
.header("X-Internal-Token", "secret")
62+
.header("Set-Cookie", "session=abc")
63+
.header("Authorization", "Bearer abc")
64+
.header("Location", "/elsewhere")
65+
.header("Content-Type", "text/plain")
66+
.body("hello").build())).build();
67+
ExporterProperties properties = new ExporterProperties();
68+
properties.getSource().setUrl("http://localhost/source");
69+
properties.getSource().setType(String.class);
70+
properties.getSource().setIncludeHeaders(includeHeaders);
71+
return new HttpSupplier(client, properties, httpProperties);
72+
}
73+
74+
}

0 commit comments

Comments
 (0)