Skip to content

Commit 3b4a343

Browse files
committed
Ignore null header value in MockHttpServletResponse
Prior to this commit, calls to setHeader() and addHeader() in MockHttpServletResponse would result in an IllegalArgumentException or NullPointerException if the supplied header value was null. Although the Javadoc for setHeader(String, String) and addHeader(String, String) in javax.servlet.http.HttpServletResponse does not specify how a null header value should be handled, both Tomcat and Jetty simply ignore a null value. Furthermore, org.springframework.http.HttpHeaders.add(String, String) declares the headerValue parameter as @nullable. This commit therefore updates MockHttpServletResponse to silently ignore null header values passed to setHeader() and addHeader(). Closes gh-26488
1 parent c82a445 commit 3b4a343

File tree

3 files changed

+53
-13
lines changed

3 files changed

+53
-13
lines changed

spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -421,7 +421,7 @@ public Cookie getCookie(String name) {
421421

422422
@Override
423423
public boolean containsHeader(String name) {
424-
return (this.headers.get(name) != null);
424+
return this.headers.containsKey(name);
425425
}
426426

427427
/**
@@ -594,12 +594,12 @@ private DateFormat newDateFormat() {
594594
}
595595

596596
@Override
597-
public void setHeader(String name, String value) {
597+
public void setHeader(String name, @Nullable String value) {
598598
setHeaderValue(name, value);
599599
}
600600

601601
@Override
602-
public void addHeader(String name, String value) {
602+
public void addHeader(String name, @Nullable String value) {
603603
addHeaderValue(name, value);
604604
}
605605

@@ -613,15 +613,21 @@ public void addIntHeader(String name, int value) {
613613
addHeaderValue(name, value);
614614
}
615615

616-
private void setHeaderValue(String name, Object value) {
616+
private void setHeaderValue(String name, @Nullable Object value) {
617+
if (value == null) {
618+
return;
619+
}
617620
boolean replaceHeader = true;
618621
if (setSpecialHeader(name, value, replaceHeader)) {
619622
return;
620623
}
621624
doAddHeaderValue(name, value, replaceHeader);
622625
}
623626

624-
private void addHeaderValue(String name, Object value) {
627+
private void addHeaderValue(String name, @Nullable Object value) {
628+
if (value == null) {
629+
return;
630+
}
625631
boolean replaceHeader = false;
626632
if (setSpecialHeader(name, value, replaceHeader)) {
627633
return;

spring-test/src/test/java/org/springframework/mock/web/MockHttpServletResponseTests.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,6 +26,8 @@
2626
import javax.servlet.http.HttpServletResponse;
2727

2828
import org.junit.jupiter.api.Test;
29+
import org.junit.jupiter.params.ParameterizedTest;
30+
import org.junit.jupiter.params.provider.ValueSource;
2931

3032
import org.springframework.web.util.WebUtils;
3133

@@ -57,6 +59,32 @@ class MockHttpServletResponseTests {
5759
private MockHttpServletResponse response = new MockHttpServletResponse();
5860

5961

62+
@ParameterizedTest // gh-26488
63+
@ValueSource(strings = {
64+
CONTENT_TYPE,
65+
CONTENT_LENGTH,
66+
CONTENT_LANGUAGE,
67+
SET_COOKIE,
68+
"enigma"
69+
})
70+
void addHeaderWithNullValue(String headerName) {
71+
response.addHeader(headerName, null);
72+
assertThat(response.containsHeader(headerName)).isFalse();
73+
}
74+
75+
@ParameterizedTest // gh-26488
76+
@ValueSource(strings = {
77+
CONTENT_TYPE,
78+
CONTENT_LENGTH,
79+
CONTENT_LANGUAGE,
80+
SET_COOKIE,
81+
"enigma"
82+
})
83+
void setHeaderWithNullValue(String headerName) {
84+
response.setHeader(headerName, null);
85+
assertThat(response.containsHeader(headerName)).isFalse();
86+
}
87+
6088
@Test
6189
void setContentType() {
6290
String contentType = "test/plain";

spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockHttpServletResponse.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -421,7 +421,7 @@ public Cookie getCookie(String name) {
421421

422422
@Override
423423
public boolean containsHeader(String name) {
424-
return (this.headers.get(name) != null);
424+
return this.headers.containsKey(name);
425425
}
426426

427427
/**
@@ -594,12 +594,12 @@ private DateFormat newDateFormat() {
594594
}
595595

596596
@Override
597-
public void setHeader(String name, String value) {
597+
public void setHeader(String name, @Nullable String value) {
598598
setHeaderValue(name, value);
599599
}
600600

601601
@Override
602-
public void addHeader(String name, String value) {
602+
public void addHeader(String name, @Nullable String value) {
603603
addHeaderValue(name, value);
604604
}
605605

@@ -613,15 +613,21 @@ public void addIntHeader(String name, int value) {
613613
addHeaderValue(name, value);
614614
}
615615

616-
private void setHeaderValue(String name, Object value) {
616+
private void setHeaderValue(String name, @Nullable Object value) {
617+
if (value == null) {
618+
return;
619+
}
617620
boolean replaceHeader = true;
618621
if (setSpecialHeader(name, value, replaceHeader)) {
619622
return;
620623
}
621624
doAddHeaderValue(name, value, replaceHeader);
622625
}
623626

624-
private void addHeaderValue(String name, Object value) {
627+
private void addHeaderValue(String name, @Nullable Object value) {
628+
if (value == null) {
629+
return;
630+
}
625631
boolean replaceHeader = false;
626632
if (setSpecialHeader(name, value, replaceHeader)) {
627633
return;

0 commit comments

Comments
 (0)