|
28 | 28 | package org.apache.hc.core5.http2.impl; |
29 | 29 |
|
30 | 30 | import java.util.Arrays; |
| 31 | +import java.util.Collections; |
31 | 32 | import java.util.List; |
32 | 33 |
|
33 | 34 | import org.apache.hc.core5.http.Header; |
@@ -180,5 +181,25 @@ void testConvertFromFieldsMultipleCookies() throws Exception { |
180 | 181 | Assertions.assertEquals("a=b; c=d; e=f", allHeaders[1].getValue()); |
181 | 182 | } |
182 | 183 |
|
| 184 | + @Test |
| 185 | + void testConvertFromFieldsStatusCodeMustBeStrictThreeDigit() { |
| 186 | + final DefaultH2ResponseConverter converter = new DefaultH2ResponseConverter(); |
| 187 | + |
| 188 | + // Demonstrate why Integer.parseInt(...) is insufficient (it accepts non-3-digit formats). |
| 189 | + final int parsedPlus = Assertions.assertDoesNotThrow(() -> Integer.parseInt("+200")); |
| 190 | + Assertions.assertEquals(200, parsedPlus); |
| 191 | + |
| 192 | + final int parsedLeadingZero = Assertions.assertDoesNotThrow(() -> Integer.parseInt("0200")); |
| 193 | + Assertions.assertEquals(200, parsedLeadingZero); |
| 194 | + |
| 195 | + // Converter must be strict: :status is exactly 3 digits, in range 100..599. |
| 196 | + Assertions.assertThrows(HttpException.class, |
| 197 | + () -> converter.convert(Collections.singletonList(new BasicHeader(":status", "+200")))); |
| 198 | + Assertions.assertThrows(HttpException.class, () -> converter.convert(Collections.singletonList(new BasicHeader(":status", "0200")))); |
| 199 | + |
| 200 | + Assertions.assertThrows(HttpException.class, () -> converter.convert(Collections.singletonList(new BasicHeader(":status", "099")))); |
| 201 | + Assertions.assertThrows(HttpException.class, () -> converter.convert(Collections.singletonList(new BasicHeader(":status", "600")))); |
| 202 | + } |
| 203 | + |
183 | 204 | } |
184 | 205 |
|
0 commit comments