Class MockHttpServletResponse

java.lang.Object
org.springframework.mock.web.MockHttpServletResponse
All Implemented Interfaces:
jakarta.servlet.http.HttpServletResponse, jakarta.servlet.ServletResponse

public class MockHttpServletResponse extends Object implements jakarta.servlet.http.HttpServletResponse
Mock implementation of the HttpServletResponse interface.

As of Spring 7.0, this set of mocks is designed on a Servlet 6.1 baseline.

Since:
1.0.2
Author:
Juergen Hoeller, Rod Johnson, Brian Clozel, Vedran Pavic, Sebastien Deleuze, Sam Brannen
  • Constructor Details

    • MockHttpServletResponse

      public MockHttpServletResponse()
  • Method Details

    • setOutputStreamAccessAllowed

      public void setOutputStreamAccessAllowed(boolean outputStreamAccessAllowed)
      Set whether getOutputStream() access is allowed.

      Default is true.

    • isOutputStreamAccessAllowed

      public boolean isOutputStreamAccessAllowed()
      Return whether getOutputStream() access is allowed.
    • setWriterAccessAllowed

      public void setWriterAccessAllowed(boolean writerAccessAllowed)
      Set whether getWriter() access is allowed.

      Default is true.

    • isWriterAccessAllowed

      public boolean isWriterAccessAllowed()
      Return whether getOutputStream() access is allowed.
    • setDefaultCharacterEncoding

      public void setDefaultCharacterEncoding(String characterEncoding)
      Set the default character encoding for the response.

      If this method is not invoked, ISO-8859-1 will be used as the default character encoding.

      If the character encoding for the response has not already been explicitly set via setCharacterEncoding(String) or setContentType(String), the character encoding for the response will be set to the supplied default character encoding.

      Parameters:
      characterEncoding - the default character encoding
      Since:
      5.3.10
      See Also:
    • isCharset

      public boolean isCharset()
      Determine whether the character encoding has been explicitly set through HttpServletResponse methods or through a charset parameter on the Content-Type.

      If false, getCharacterEncoding() will return the default character encoding.

    • setCharacterEncoding

      public void setCharacterEncoding(@Nullable String characterEncoding)
      Specified by:
      setCharacterEncoding in interface jakarta.servlet.ServletResponse
    • getCharacterEncoding

      public String getCharacterEncoding()
      Specified by:
      getCharacterEncoding in interface jakarta.servlet.ServletResponse
    • getOutputStream

      public jakarta.servlet.ServletOutputStream getOutputStream()
      Specified by:
      getOutputStream in interface jakarta.servlet.ServletResponse
    • getWriter

      public PrintWriter getWriter() throws UnsupportedEncodingException
      Specified by:
      getWriter in interface jakarta.servlet.ServletResponse
      Throws:
      UnsupportedEncodingException
    • getContentAsByteArray

      public byte[] getContentAsByteArray()
    • getContentAsString

      public String getContentAsString() throws UnsupportedEncodingException
      Get the content of the response body as a String, using the charset specified for the response by the application, either through HttpServletResponse methods or through a charset parameter on the Content-Type. If no charset has been explicitly defined, the default character encoding will be used.
      Returns:
      the content as a String
      Throws:
      UnsupportedEncodingException - if the character encoding is not supported
      See Also:
    • getContentAsString

      public String getContentAsString(Charset fallbackCharset) throws UnsupportedEncodingException
      Get the content of the response body as a String, using the provided fallbackCharset if no charset has been explicitly defined and otherwise using the charset specified for the response by the application, either through HttpServletResponse methods or through a charset parameter on the Content-Type.
      Returns:
      the content as a String
      Throws:
      UnsupportedEncodingException - if the character encoding is not supported
      Since:
      5.2
      See Also:
    • setContentLength

      public void setContentLength(int contentLength)
      Specified by:
      setContentLength in interface jakarta.servlet.ServletResponse
    • getContentLength

      public int getContentLength()
      Get the length of the content body from the HTTP Content-Length header.
      Returns:
      the value of the Content-Length header
      See Also:
    • setContentLengthLong

      public void setContentLengthLong(long contentLength)
      Specified by:
      setContentLengthLong in interface jakarta.servlet.ServletResponse
    • getContentLengthLong

      public long getContentLengthLong()
    • setContentType

      public void setContentType(@Nullable String contentType)
      Specified by:
      setContentType in interface jakarta.servlet.ServletResponse
    • getContentType

      public @Nullable String getContentType()
      Specified by:
      getContentType in interface jakarta.servlet.ServletResponse
    • setBufferSize

      public void setBufferSize(int bufferSize)
      Specified by:
      setBufferSize in interface jakarta.servlet.ServletResponse
    • getBufferSize

      public int getBufferSize()
      Specified by:
      getBufferSize in interface jakarta.servlet.ServletResponse
    • flushBuffer

      public void flushBuffer()
      Specified by:
      flushBuffer in interface jakarta.servlet.ServletResponse
    • resetBuffer

      public void resetBuffer()
      Specified by:
      resetBuffer in interface jakarta.servlet.ServletResponse
    • setCommitted

      public void setCommitted(boolean committed)
    • isCommitted

      public boolean isCommitted()
      Specified by:
      isCommitted in interface jakarta.servlet.ServletResponse
    • reset

      public void reset()
      Specified by:
      reset in interface jakarta.servlet.ServletResponse
    • setLocale

      public void setLocale(@Nullable Locale locale)
      Specified by:
      setLocale in interface jakarta.servlet.ServletResponse
    • getLocale

      public Locale getLocale()
      Specified by:
      getLocale in interface jakarta.servlet.ServletResponse
    • addCookie

      public void addCookie(jakarta.servlet.http.Cookie cookie)
      Specified by:
      addCookie in interface jakarta.servlet.http.HttpServletResponse
    • getCookies

      public jakarta.servlet.http.Cookie[] getCookies()
    • getCookie

      public @Nullable jakarta.servlet.http.Cookie getCookie(String name)
    • containsHeader

      public boolean containsHeader(String name)
      Specified by:
      containsHeader in interface jakarta.servlet.http.HttpServletResponse
    • getHeaderNames

      public Collection<String> getHeaderNames()
      Return the names of all specified headers as a Set of Strings.

      As of Servlet 3.0, this method is also defined in HttpServletResponse.

      Specified by:
      getHeaderNames in interface jakarta.servlet.http.HttpServletResponse
      Returns:
      the Set of header name Strings, or an empty Set if none
    • getHeader

      public @Nullable String getHeader(String name)
      Return the primary value for the given header as a String, if any.

      Will return the first value in case of multiple values.

      Returns a stringified value for Servlet 3.0 compatibility. Consider using getHeaderValue(String) for raw Object access.

      Specified by:
      getHeader in interface jakarta.servlet.http.HttpServletResponse
      Parameters:
      name - the name of the header
      Returns:
      the associated header value, or null if none
      See Also:
      • HttpServletResponse.getHeader(String)
    • getHeaders

      public List<String> getHeaders(String name)
      Return all values for the given header as a List of Strings.

      Returns a List of stringified values for Servlet 3.0 compatibility. Consider using getHeaderValues(String) for raw Object access.

      Specified by:
      getHeaders in interface jakarta.servlet.http.HttpServletResponse
      Parameters:
      name - the name of the header
      Returns:
      the associated header values, or an empty List if none
      See Also:
      • HttpServletResponse.getHeaders(String)
    • getHeaderValue

      public @Nullable Object getHeaderValue(String name)
      Return the primary value for the given header, if any.

      Will return the first value in case of multiple values.

      Parameters:
      name - the name of the header
      Returns:
      the associated header value, or null if none
    • getHeaderValues

      public List<Object> getHeaderValues(String name)
      Return all values for the given header as a List of value objects.
      Parameters:
      name - the name of the header
      Returns:
      the associated header values, or an empty List if none
    • encodeURL

      public String encodeURL(String url)
      The default implementation returns the given URL String as-is.

      Can be overridden in subclasses, appending a session id or the like.

      Specified by:
      encodeURL in interface jakarta.servlet.http.HttpServletResponse
    • encodeRedirectURL

      public String encodeRedirectURL(String url)
      The default implementation delegates to encodeURL(String), returning the given URL String as-is.

      Can be overridden in subclasses, appending a session id or the like in a redirect-specific fashion. For general URL encoding rules, override the common encodeURL(String) method instead, applying to redirect URLs as well as to general URLs.

      Specified by:
      encodeRedirectURL in interface jakarta.servlet.http.HttpServletResponse
    • sendError

      public void sendError(int status, String errorMessage) throws IOException
      Specified by:
      sendError in interface jakarta.servlet.http.HttpServletResponse
      Throws:
      IOException
    • sendError

      public void sendError(int status) throws IOException
      Specified by:
      sendError in interface jakarta.servlet.http.HttpServletResponse
      Throws:
      IOException
    • sendRedirect

      public void sendRedirect(String url) throws IOException
      Specified by:
      sendRedirect in interface jakarta.servlet.http.HttpServletResponse
      Throws:
      IOException
    • sendRedirect

      public void sendRedirect(String url, int sc, boolean clearBuffer) throws IOException
      Specified by:
      sendRedirect in interface jakarta.servlet.http.HttpServletResponse
      Throws:
      IOException
    • getRedirectedUrl

      public @Nullable String getRedirectedUrl()
    • setDateHeader

      public void setDateHeader(String name, long value)
      Specified by:
      setDateHeader in interface jakarta.servlet.http.HttpServletResponse
    • addDateHeader

      public void addDateHeader(String name, long value)
      Specified by:
      addDateHeader in interface jakarta.servlet.http.HttpServletResponse
    • getDateHeader

      public long getDateHeader(String name)
    • setHeader

      public void setHeader(@Nullable String name, @Nullable String value)
      Specified by:
      setHeader in interface jakarta.servlet.http.HttpServletResponse
    • addHeader

      public void addHeader(@Nullable String name, @Nullable String value)
      Specified by:
      addHeader in interface jakarta.servlet.http.HttpServletResponse
    • setIntHeader

      public void setIntHeader(String name, int value)
      Specified by:
      setIntHeader in interface jakarta.servlet.http.HttpServletResponse
    • addIntHeader

      public void addIntHeader(String name, int value)
      Specified by:
      addIntHeader in interface jakarta.servlet.http.HttpServletResponse
    • setStatus

      public void setStatus(int status)
      Specified by:
      setStatus in interface jakarta.servlet.http.HttpServletResponse
    • getStatus

      public int getStatus()
      Specified by:
      getStatus in interface jakarta.servlet.http.HttpServletResponse
    • getErrorMessage

      public @Nullable String getErrorMessage()
      Return the error message used when calling HttpServletResponse.sendError(int, String).
    • sendEarlyHints

      public void sendEarlyHints()
    • setForwardedUrl

      public void setForwardedUrl(@Nullable String forwardedUrl)
    • getForwardedUrl

      public @Nullable String getForwardedUrl()
    • setIncludedUrl

      public void setIncludedUrl(@Nullable String includedUrl)
    • getIncludedUrl

      public @Nullable String getIncludedUrl()
    • addIncludedUrl

      public void addIncludedUrl(String includedUrl)
    • getIncludedUrls

      public List<String> getIncludedUrls()