|
49 | 49 |
|
50 | 50 | import static java.nio.charset.StandardCharsets.UTF_8; |
51 | 51 | import static org.assertj.core.api.Assertions.assertThat; |
| 52 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
52 | 53 | import static org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED; |
53 | 54 | import static org.springframework.http.MediaType.APPLICATION_JSON; |
54 | 55 | import static org.springframework.http.MediaType.MULTIPART_FORM_DATA; |
@@ -132,6 +133,27 @@ void readForm() throws Exception { |
132 | 133 | assertThat(result.getFirst("name 3")).as("Invalid result").isNull(); |
133 | 134 | } |
134 | 135 |
|
| 136 | + @Test |
| 137 | + void readInvalidFormWithValueThatWontUrlDecode() { |
| 138 | + //java.net.URLDecoder doesn't like negative integer values after a % character |
| 139 | + String body = "name+1=value+1&name+2=value+2%" + ((char)-1); |
| 140 | + assertInvalidFormIsRejectedWithSpecificException(body); |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + void readInvalidFormWithNameThatWontUrlDecode() { |
| 145 | + //java.net.URLDecoder doesn't like negative integer values after a % character |
| 146 | + String body = "name+1=value+1&name+2%" + ((char)-1) + "=value+2"; |
| 147 | + assertInvalidFormIsRejectedWithSpecificException(body); |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + void readInvalidFormWithNameWithNoValueThatWontUrlDecode() { |
| 152 | + //java.net.URLDecoder doesn't like negative integer values after a % character |
| 153 | + String body = "name+1=value+1&name+2%" + ((char)-1); |
| 154 | + assertInvalidFormIsRejectedWithSpecificException(body); |
| 155 | + } |
| 156 | + |
135 | 157 | @Test |
136 | 158 | void writeForm() throws IOException { |
137 | 159 | MultiValueMap<String, String> body = new LinkedMultiValueMap<>(); |
@@ -410,6 +432,17 @@ private void assertCannotWrite(MediaType mediaType) { |
410 | 432 | assertThat(this.converter.canWrite(clazz, mediaType)).as(clazz.getSimpleName() + " : " + mediaType).isFalse(); |
411 | 433 | } |
412 | 434 |
|
| 435 | + private void assertInvalidFormIsRejectedWithSpecificException(String body) { |
| 436 | + MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.ISO_8859_1)); |
| 437 | + inputMessage.getHeaders().setContentType( |
| 438 | + new MediaType("application", "x-www-form-urlencoded", StandardCharsets.ISO_8859_1)); |
| 439 | + |
| 440 | + assertThatThrownBy(() -> this.converter.read(null, inputMessage)) |
| 441 | + .isInstanceOf(HttpMessageNotReadableException.class) |
| 442 | + .hasCauseInstanceOf(IllegalArgumentException.class) |
| 443 | + .hasMessage("Could not decode HTTP form payload"); |
| 444 | + } |
| 445 | + |
413 | 446 |
|
414 | 447 | private static class MockHttpOutputMessageRequestContext implements UploadContext { |
415 | 448 |
|
|
0 commit comments