Class MockHttpServletResponse

  • All Implemented Interfaces:
    javax.servlet.http.HttpServletResponse, javax.servlet.ServletResponse, IMetaDataBufferingWebResponse

    public class MockHttpServletResponse
    extends Object
    implements javax.servlet.http.HttpServletResponse, IMetaDataBufferingWebResponse
    Mock servlet response. Implements all of the methods from the standard HttpServletResponse class plus helper methods to aid viewing the generated response.
    Author:
    Chris Turner
    • Method Detail

      • addCookie

        public void addCookie​(javax.servlet.http.Cookie cookie)
        Add a cookie to the response.
        Specified by:
        addCookie in interface javax.servlet.http.HttpServletResponse
        Parameters:
        cookie - The cookie to add
      • addDateHeader

        public void addDateHeader​(String name,
                                  long l)
        Add a date header.
        Specified by:
        addDateHeader in interface javax.servlet.http.HttpServletResponse
        Parameters:
        name - The header value
        l - The long value
      • addHeader

        public void addHeader​(String name,
                              String value)
        Add the given header value, including an additional entry if one already exists.
        Specified by:
        addHeader in interface javax.servlet.http.HttpServletResponse
        Parameters:
        name - The name for the header
        value - The value for the header
      • addIntHeader

        public void addIntHeader​(String name,
                                 int i)
        Add an int header value.
        Specified by:
        addIntHeader in interface javax.servlet.http.HttpServletResponse
        Parameters:
        name - The header name
        i - The value
      • containsHeader

        public boolean containsHeader​(String name)
        Check if the response contains the given header name.
        Specified by:
        containsHeader in interface javax.servlet.http.HttpServletResponse
        Parameters:
        name - The name to check
        Returns:
        Whether header in response or not
      • encodeRedirectUrl

        public String encodeRedirectUrl​(String url)
        Encode the redirectLocation URL. Does no changes as this test implementation uses cookie based url tracking.
        Specified by:
        encodeRedirectUrl in interface javax.servlet.http.HttpServletResponse
        Parameters:
        url - The url to encode
        Returns:
        The encoded url
      • encodeRedirectURL

        public String encodeRedirectURL​(String url)
        Encode the redirectLocation URL. Does no changes as this test implementation uses cookie based url tracking.
        Specified by:
        encodeRedirectURL in interface javax.servlet.http.HttpServletResponse
        Parameters:
        url - The url to encode
        Returns:
        The encoded url
      • encodeUrl

        public String encodeUrl​(String url)
        Encode the URL. Does no changes as this test implementation uses cookie based url tracking.
        Specified by:
        encodeUrl in interface javax.servlet.http.HttpServletResponse
        Parameters:
        url - The url to encode
        Returns:
        The encoded url
      • encodeURL

        public String encodeURL​(String url)
        Encode the URL. Does no changes as this test implementation uses cookie based url tracking.
        Specified by:
        encodeURL in interface javax.servlet.http.HttpServletResponse
        Parameters:
        url - The url to encode
        Returns:
        The encoded url
      • flushBuffer

        public void flushBuffer()
                         throws IOException
        Flush the buffer.
        Specified by:
        flushBuffer in interface javax.servlet.ServletResponse
        Throws:
        IOException
      • getBinaryContent

        public byte[] getBinaryContent()
        Get the binary content that was written to the servlet stream.
        Returns:
        The binary content
      • getBufferSize

        public int getBufferSize()
        Return the current buffer size
        Specified by:
        getBufferSize in interface javax.servlet.ServletResponse
        Returns:
        The buffer size
      • getCharacterEncoding

        public String getCharacterEncoding()
        Get the character encoding of the response.
        Specified by:
        getCharacterEncoding in interface javax.servlet.ServletResponse
        Returns:
        The character encoding
      • getCookies

        public List<javax.servlet.http.Cookie> getCookies()
        Get all of the cookies that have been added to the response.
        Returns:
        The collection of cookies
      • getDocument

        public String getDocument()
        Get the text document that was written as part of this response.
        Returns:
        The document
      • getErrorMessage

        public String getErrorMessage()
        Get the error message.
        Returns:
        The error message, or null if no message
      • getHeader

        public String getHeader​(String name)
        Return the value of the given named header.
        Specified by:
        getHeader in interface javax.servlet.http.HttpServletResponse
        Parameters:
        name - The header name
        Returns:
        The value, or null
      • getHeaderNames

        public Set<StringgetHeaderNames()
        Get the names of all of the headers.
        Specified by:
        getHeaderNames in interface javax.servlet.http.HttpServletResponse
        Returns:
        The header names
      • getLocale

        public Locale getLocale()
        Get the encoded locale
        Specified by:
        getLocale in interface javax.servlet.ServletResponse
        Returns:
        The locale
      • getOutputStream

        public javax.servlet.ServletOutputStream getOutputStream()
        Get the output stream for writing binary data from the servlet.
        Specified by:
        getOutputStream in interface javax.servlet.ServletResponse
        Returns:
        The binary output stream.
      • getRedirectLocation

        public String getRedirectLocation()
        Get the location that was redirected to.
        Returns:
        The redirect location, or null if not a redirect
      • getStatus

        public int getStatus()
        Get the status code.
        Specified by:
        getStatus in interface javax.servlet.http.HttpServletResponse
        Returns:
        The status code
      • getWriter

        public PrintWriter getWriter()
                              throws IOException
        Get the print writer for writing text output for this response.
        Specified by:
        getWriter in interface javax.servlet.ServletResponse
        Returns:
        The writer
        Throws:
        IOException - Not used
      • initialize

        public void initialize()
        Reset the response ready for reuse.
      • isCommitted

        public boolean isCommitted()
        Always returns false.
        Specified by:
        isCommitted in interface javax.servlet.ServletResponse
        Returns:
        Always false
      • isError

        public boolean isError()
        Return whether the servlet returned an error code or not.
        Returns:
        Whether an error occurred or not
      • isRedirect

        public boolean isRedirect()
        Check whether the response was redirected or not.
        Returns:
        Whether the state was redirected or not
      • reset

        public void reset()
        Delegate to initialize method.
        Specified by:
        reset in interface javax.servlet.ServletResponse
      • resetBuffer

        public void resetBuffer()
        Clears the buffer.
        Specified by:
        resetBuffer in interface javax.servlet.ServletResponse
      • sendError

        public void sendError​(int code)
                       throws IOException
        Send an error code. This implementation just sets the internal error state information.
        Specified by:
        sendError in interface javax.servlet.http.HttpServletResponse
        Parameters:
        code - The code
        Throws:
        IOException - Not used
      • sendError

        public void sendError​(int code,
                              String msg)
                       throws IOException
        Send an error code. This implementation just sets the internal error state information.
        Specified by:
        sendError in interface javax.servlet.http.HttpServletResponse
        Parameters:
        code - The error code
        msg - The error message
        Throws:
        IOException - Not used
      • sendRedirect

        public void sendRedirect​(String location)
                          throws IOException
        Indicate sending of a redirectLocation to a particular named resource. This implementation just keeps hold of the redirectLocation info and makes it available for query.
        Specified by:
        sendRedirect in interface javax.servlet.http.HttpServletResponse
        Parameters:
        location - The location to redirectLocation to
        Throws:
        IOException - Not used
      • setBufferSize

        public void setBufferSize​(int size)
        Method ignored.
        Specified by:
        setBufferSize in interface javax.servlet.ServletResponse
        Parameters:
        size - The size
      • setCharacterEncoding

        public void setCharacterEncoding​(String characterEncoding)
        Set the character encoding.
        Specified by:
        setCharacterEncoding in interface javax.servlet.ServletResponse
        Parameters:
        characterEncoding - The character encoding
      • setContentLength

        public void setContentLength​(int length)
        Set the content length.
        Specified by:
        setContentLength in interface javax.servlet.ServletResponse
        Parameters:
        length - The length
      • setContentLengthLong

        public void setContentLengthLong​(long len)
        Specified by:
        setContentLengthLong in interface javax.servlet.ServletResponse
      • setContentType

        public void setContentType​(String type)
        Set the content type.
        Specified by:
        setContentType in interface javax.servlet.ServletResponse
        Parameters:
        type - The content type
      • getContentType

        public String getContentType()
        Specified by:
        getContentType in interface javax.servlet.ServletResponse
        Returns:
        value of content-type header
      • setDateHeader

        public void setDateHeader​(String name,
                                  long l)
        Set a date header.
        Specified by:
        setDateHeader in interface javax.servlet.http.HttpServletResponse
        Parameters:
        name - The header name
        l - The long value
      • formatDate

        public static String formatDate​(long l)
        Parameters:
        l -
        Returns:
        formatted date
      • formatDate

        public static void formatDate​(StringBuilder buf,
                                      Calendar calendar,
                                      boolean cookie)
        Format HTTP date "EEE, dd MMM yyyy HH:mm:ss 'GMT'" or "EEE, dd-MMM-yy HH:mm:ss 'GMT'"for cookies
        Parameters:
        buf -
        calendar -
        cookie -
      • setHeader

        public void setHeader​(String name,
                              String value)
        Set the given header value.
        Specified by:
        setHeader in interface javax.servlet.http.HttpServletResponse
        Parameters:
        name - The name for the header
        value - The value for the header
      • setIntHeader

        public void setIntHeader​(String name,
                                 int i)
        Set an int header value.
        Specified by:
        setIntHeader in interface javax.servlet.http.HttpServletResponse
        Parameters:
        name - The header name
        i - The value
      • setLocale

        public void setLocale​(Locale locale)
        Set the locale in the response header.
        Specified by:
        setLocale in interface javax.servlet.ServletResponse
        Parameters:
        locale - The locale
      • setStatus

        public void setStatus​(int status)
        Set the status for this response.
        Specified by:
        setStatus in interface javax.servlet.http.HttpServletResponse
        Parameters:
        status - The status
      • setStatus

        @Deprecated
        public void setStatus​(int status,
                              String msg)
        Deprecated.
        Set the status for this response.
        Specified by:
        setStatus in interface javax.servlet.http.HttpServletResponse
        Parameters:
        status - The status
        msg - The message
      • getHeaders

        public Collection<StringgetHeaders​(String name)
        Specified by:
        getHeaders in interface javax.servlet.http.HttpServletResponse
        Parameters:
        name -
        Returns:
        headers with given name