|
| 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