4040
4141import org .springframework .http .HttpHeaders ;
4242import org .springframework .http .MediaType ;
43+ import org .springframework .lang .Nullable ;
4344import org .springframework .util .Assert ;
4445import org .springframework .util .LinkedCaseInsensitiveMap ;
4546import org .springframework .util .StringUtils ;
@@ -72,6 +73,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
7273
7374 private boolean writerAccessAllowed = true ;
7475
76+ @ Nullable
7577 private String characterEncoding = WebUtils .DEFAULT_CHARACTER_ENCODING ;
7678
7779 private boolean charset = false ;
@@ -80,10 +82,12 @@ public class MockHttpServletResponse implements HttpServletResponse {
8082
8183 private final ServletOutputStream outputStream = new ResponseServletOutputStream (this .content );
8284
85+ @ Nullable
8386 private PrintWriter writer ;
8487
8588 private long contentLength = 0 ;
8689
90+ @ Nullable
8791 private String contentType ;
8892
8993 private int bufferSize = 4096 ;
@@ -103,8 +107,10 @@ public class MockHttpServletResponse implements HttpServletResponse {
103107
104108 private int status = HttpServletResponse .SC_OK ;
105109
110+ @ Nullable
106111 private String errorMessage ;
107112
113+ @ Nullable
108114 private String forwardedUrl ;
109115
110116 private final List <String > includedUrls = new ArrayList <>();
@@ -170,6 +176,7 @@ private void updateContentTypeHeader() {
170176 }
171177
172178 @ Override
179+ @ Nullable
173180 public String getCharacterEncoding () {
174181 return this .characterEncoding ;
175182 }
@@ -221,7 +228,7 @@ public long getContentLengthLong() {
221228 }
222229
223230 @ Override
224- public void setContentType (String contentType ) {
231+ public void setContentType (@ Nullable String contentType ) {
225232 this .contentType = contentType ;
226233 if (contentType != null ) {
227234 try {
@@ -244,6 +251,7 @@ public void setContentType(String contentType) {
244251 }
245252
246253 @ Override
254+ @ Nullable
247255 public String getContentType () {
248256 return this .contentType ;
249257 }
@@ -352,6 +360,7 @@ public Cookie[] getCookies() {
352360 return this .cookies .toArray (new Cookie [0 ]);
353361 }
354362
363+ @ Nullable
355364 public Cookie getCookie (String name ) {
356365 Assert .notNull (name , "Cookie name must not be null" );
357366 for (Cookie cookie : this .cookies ) {
@@ -387,6 +396,7 @@ public Collection<String> getHeaderNames() {
387396 * @return the associated header value, or {@code null} if none
388397 */
389398 @ Override
399+ @ Nullable
390400 public String getHeader (String name ) {
391401 HeaderValueHolder header = HeaderValueHolder .getByName (this .headers , name );
392402 return (header != null ? header .getStringValue () : null );
@@ -417,6 +427,7 @@ public List<String> getHeaders(String name) {
417427 * @param name the name of the header
418428 * @return the associated header value, or {@code null} if none
419429 */
430+ @ Nullable
420431 public Object getHeaderValue (String name ) {
421432 HeaderValueHolder header = HeaderValueHolder .getByName (this .headers , name );
422433 return (header != null ? header .getValue () : null );
@@ -495,6 +506,7 @@ public void sendRedirect(String url) throws IOException {
495506 setCommitted (true );
496507 }
497508
509+ @ Nullable
498510 public String getRedirectedUrl () {
499511 return getHeader (HttpHeaders .LOCATION );
500512 }
@@ -625,6 +637,7 @@ public int getStatus() {
625637 return this .status ;
626638 }
627639
640+ @ Nullable
628641 public String getErrorMessage () {
629642 return this .errorMessage ;
630643 }
@@ -634,21 +647,23 @@ public String getErrorMessage() {
634647 // Methods for MockRequestDispatcher
635648 //---------------------------------------------------------------------
636649
637- public void setForwardedUrl (String forwardedUrl ) {
650+ public void setForwardedUrl (@ Nullable String forwardedUrl ) {
638651 this .forwardedUrl = forwardedUrl ;
639652 }
640653
654+ @ Nullable
641655 public String getForwardedUrl () {
642656 return this .forwardedUrl ;
643657 }
644658
645- public void setIncludedUrl (String includedUrl ) {
659+ public void setIncludedUrl (@ Nullable String includedUrl ) {
646660 this .includedUrls .clear ();
647661 if (includedUrl != null ) {
648662 this .includedUrls .add (includedUrl );
649663 }
650664 }
651665
666+ @ Nullable
652667 public String getIncludedUrl () {
653668 int count = this .includedUrls .size ();
654669 Assert .state (count <= 1 ,
@@ -702,7 +717,7 @@ public ResponsePrintWriter(Writer out) {
702717 }
703718
704719 @ Override
705- public void write (char buf [] , int off , int len ) {
720+ public void write (char [] buf , int off , int len ) {
706721 super .write (buf , off , len );
707722 super .flush ();
708723 setCommittedIfBufferSizeExceeded ();
@@ -727,6 +742,13 @@ public void flush() {
727742 super .flush ();
728743 setCommitted (true );
729744 }
745+
746+ @ Override
747+ public void close () {
748+ super .flush ();
749+ super .close ();
750+ setCommitted (true );
751+ }
730752 }
731753
732754}
0 commit comments