|
3 | 3 | import org.junit.jupiter.api.DisplayName; |
4 | 4 | import org.junit.jupiter.api.Test; |
5 | 5 | import org.junit.jupiter.params.ParameterizedTest; |
| 6 | +import org.junit.jupiter.params.provider.Arguments; |
6 | 7 | import org.junit.jupiter.params.provider.CsvSource; |
| 8 | +import org.junit.jupiter.params.provider.MethodSource; |
| 9 | + |
| 10 | +import java.util.stream.Stream; |
7 | 11 |
|
8 | 12 | import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
9 | 13 |
|
@@ -123,17 +127,34 @@ void setContentTypeFromFilename_allCases(String filename, String expectedContent |
123 | 127 | assertThat(result).contains("Content-Type: " + expectedContentType); |
124 | 128 | } |
125 | 129 |
|
126 | | - @Test |
127 | | - @DisplayName("Should override previous Content-Type when set again") |
128 | | - void setContentTypeFromFilename_overridesPrevious() { |
| 130 | + @ParameterizedTest |
| 131 | + @MethodSource("provideHeaderDuplicationScenarios") |
| 132 | + @DisplayName("Should not duplicate headers when manually set") |
| 133 | + void build_doesNotDuplicateHeaders(String headerName, String manualValue, String bodyContent) { |
129 | 134 | HttpResponseBuilder builder = new HttpResponseBuilder(); |
130 | | - builder.setContentTypeFromFilename("file.txt"); |
131 | | - builder.setContentTypeFromFilename("file.html"); |
132 | | - builder.setBody("Test"); |
| 135 | + builder.setHeader(headerName, manualValue); |
| 136 | + builder.setBody(bodyContent); |
133 | 137 |
|
134 | 138 | String result = builder.build(); |
135 | 139 |
|
136 | | - assertThat(result).contains("Content-Type: text/html; charset=UTF-8"); |
137 | | - assertThat(result).doesNotContain("text/plain"); |
| 140 | + // Count occurrences of the header |
| 141 | + long count = result.lines() |
| 142 | + .filter(line -> line.startsWith(headerName + ":")) |
| 143 | + .count(); |
| 144 | + |
| 145 | + assertThat(count).isEqualTo(1); |
| 146 | + assertThat(result).contains(headerName + ": " + manualValue); |
| 147 | + } |
| 148 | + |
| 149 | + private static Stream<Arguments> provideHeaderDuplicationScenarios() { |
| 150 | + return Stream.of( |
| 151 | + Arguments.of("Content-Length", "999", "Hello"), |
| 152 | + Arguments.of("Content-Length", "0", ""), |
| 153 | + Arguments.of("Content-Length", "12345", "Test content"), |
| 154 | + Arguments.of("Connection", "keep-alive", "Hello"), |
| 155 | + Arguments.of("Connection", "upgrade", "WebSocket data"), |
| 156 | + Arguments.of("Connection", "close", "Goodbye") |
| 157 | + ); |
138 | 158 | } |
| 159 | + |
139 | 160 | } |
0 commit comments