File tree Expand file tree Collapse file tree 2 files changed +18
-0
lines changed
main/java/org/springframework/http
test/java/org/springframework/http Expand file tree Collapse file tree 2 files changed +18
-0
lines changed Original file line number Diff line number Diff line change @@ -969,8 +969,14 @@ public Locale getContentLanguage() {
969969 /**
970970 * Set the length of the body in bytes, as specified by the
971971 * {@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>
972975 */
973976 public void setContentLength (long contentLength ) {
977+ if (contentLength < 0 ){
978+ throw new IllegalArgumentException ("Content-Length must be a non-negative number" );
979+ }
974980 set (CONTENT_LENGTH , Long .toString (contentLength ));
975981 }
976982
Original file line number Diff line number Diff line change @@ -154,6 +154,18 @@ void contentLength() {
154154 assertThat (headers .getFirst ("Content-Length" )).as ("Invalid Content-Length header" ).isEqualTo ("42" );
155155 }
156156
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+
157169 @ Test
158170 void contentType () {
159171 MediaType contentType = new MediaType ("text" , "html" , StandardCharsets .UTF_8 );
You can’t perform that action at this time.
0 commit comments