File tree 2 files changed +18
-0
lines changed
main/java/org/springframework/http
test/java/org/springframework/http
2 files changed +18
-0
lines changed Original file line number Diff line number Diff line change @@ -969,8 +969,14 @@ public Locale getContentLanguage() {
969
969
/**
970
970
* Set the length of the body in bytes, as specified by the
971
971
* {@code Content-Length} header.
972
+ * @param contentLength content length (greater than or equal to zero)
973
+ * @throws IllegalArgumentException if the content length is negative
974
+ * @see <a href="https://www.rfc-editor.org/rfc/rfc2616#section-14.13">RFC 2616, section 14.13</a>
972
975
*/
973
976
public void setContentLength (long contentLength ) {
977
+ if (contentLength < 0 ){
978
+ throw new IllegalArgumentException ("Content-Length must be a non-negative number" );
979
+ }
974
980
set (CONTENT_LENGTH , Long .toString (contentLength ));
975
981
}
976
982
Original file line number Diff line number Diff line change @@ -154,6 +154,18 @@ void contentLength() {
154
154
assertThat (headers .getFirst ("Content-Length" )).as ("Invalid Content-Length header" ).isEqualTo ("42" );
155
155
}
156
156
157
+ @ Test
158
+ void setContentLengthWithNegativeValue () {
159
+ assertThatIllegalArgumentException ().isThrownBy (() ->
160
+ headers .setContentLength (-1 ));
161
+ }
162
+
163
+ @ Test
164
+ void getContentLengthReturnsMinusOneForAbsentHeader () {
165
+ headers .remove (HttpHeaders .CONTENT_LENGTH );
166
+ assertThat (headers .getContentLength ()).isEqualTo (-1 );
167
+ }
168
+
157
169
@ Test
158
170
void contentType () {
159
171
MediaType contentType = new MediaType ("text" , "html" , StandardCharsets .UTF_8 );
You can’t perform that action at this time.
0 commit comments