Class TestHttpResponse

java.lang.Object
com.oorian.test.TestHttpResponse
All Implemented Interfaces:
OorianHttpResponse

public class TestHttpResponse extends Object implements OorianHttpResponse
In-memory implementation of OorianHttpResponse for headless testing.

Captures all response output (headers, status, content) in memory for inspection during testing. The rendered content can be retrieved via getContent().

Usage:


 TestHttpResponse response = new TestHttpResponse();
 page.write(request, response);
 String html = response.getContent();
 int status = response.getStatus();
 
Since:
2.1
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Details

    • TestHttpResponse

      public TestHttpResponse()
      Creates a new TestHttpResponse.
  • Method Details

    • getContent

      public String getContent()
      Returns the content written to this response via getWriter().
      Returns:
      the response content as a string
    • getOutputContent

      public byte[] getOutputContent()
      Returns the content written to this response via getOutputStream().
      Returns:
      the response content as a byte array
    • getRedirectUrl

      public String getRedirectUrl()
      Returns the redirect URL if sendRedirect(String) was called.
      Returns:
      the redirect URL, or null if no redirect was issued
    • getCookies

      public List<OorianCookie> getCookies()
      Returns all cookies added to this response.
      Returns:
      the list of response cookies
    • getHeaderValues

      public List<String> getHeaderValues(String name)
      Returns all values for a response header.
      Parameters:
      name - the header name
      Returns:
      the list of header values, or an empty list if not set
    • setContentType

      public void setContentType(String type)
      Description copied from interface: OorianHttpResponse
      Sets the content type of the response.
      Specified by:
      setContentType in interface OorianHttpResponse
      Parameters:
      type - the MIME content type
    • setCharacterEncoding

      public void setCharacterEncoding(String charset)
      Description copied from interface: OorianHttpResponse
      Sets the character encoding of the response.
      Specified by:
      setCharacterEncoding in interface OorianHttpResponse
      Parameters:
      charset - the character encoding name
    • setContentLength

      public void setContentLength(int len)
      Description copied from interface: OorianHttpResponse
      Sets the content length of the response body.
      Specified by:
      setContentLength in interface OorianHttpResponse
      Parameters:
      len - the content length in bytes
    • getOutputStream

      public OutputStream getOutputStream() throws IOException
      Description copied from interface: OorianHttpResponse
      Returns the output stream for writing binary response data.
      Specified by:
      getOutputStream in interface OorianHttpResponse
      Returns:
      the output stream
      Throws:
      IOException - if an I/O error occurs
    • getWriter

      public PrintWriter getWriter() throws IOException
      Description copied from interface: OorianHttpResponse
      Returns a PrintWriter for writing character response data.
      Specified by:
      getWriter in interface OorianHttpResponse
      Returns:
      the print writer
      Throws:
      IOException - if an I/O error occurs
    • setHeader

      public void setHeader(String name, String value)
      Description copied from interface: OorianHttpResponse
      Sets a response header, replacing any existing values.
      Specified by:
      setHeader in interface OorianHttpResponse
      Parameters:
      name - the header name
      value - the header value
    • addHeader

      public void addHeader(String name, String value)
      Description copied from interface: OorianHttpResponse
      Adds a response header without replacing existing values.
      Specified by:
      addHeader in interface OorianHttpResponse
      Parameters:
      name - the header name
      value - the header value
    • isCommitted

      public boolean isCommitted()
      Description copied from interface: OorianHttpResponse
      Returns whether the response has been committed.
      Specified by:
      isCommitted in interface OorianHttpResponse
      Returns:
      true if the response has been committed
    • sendError

      public void sendError(int sc, String msg) throws IOException
      Description copied from interface: OorianHttpResponse
      Sends an error response with the specified status code and message.
      Specified by:
      sendError in interface OorianHttpResponse
      Parameters:
      sc - the HTTP status code
      msg - the error message
      Throws:
      IOException - if an I/O error occurs
    • setStatus

      public void setStatus(int sc)
      Description copied from interface: OorianHttpResponse
      Sets the HTTP status code of the response.
      Specified by:
      setStatus in interface OorianHttpResponse
      Parameters:
      sc - the status code
    • sendRedirect

      public void sendRedirect(String url) throws IOException
      Description copied from interface: OorianHttpResponse
      Sends a temporary redirect response to the specified URL.
      Specified by:
      sendRedirect in interface OorianHttpResponse
      Parameters:
      url - the redirect URL
      Throws:
      IOException - if an I/O error occurs
    • addCookie

      public void addCookie(OorianCookie cookie)
      Description copied from interface: OorianHttpResponse
      Adds a cookie to the response.
      Specified by:
      addCookie in interface OorianHttpResponse
      Parameters:
      cookie - the cookie to add
    • getStatus

      public int getStatus()
      Description copied from interface: OorianHttpResponse
      Returns the current HTTP status code of this response.
      Specified by:
      getStatus in interface OorianHttpResponse
      Returns:
      the status code
    • containsHeader

      public boolean containsHeader(String name)
      Description copied from interface: OorianHttpResponse
      Returns whether the response contains the specified header.
      Specified by:
      containsHeader in interface OorianHttpResponse
      Parameters:
      name - the header name
      Returns:
      true if the header has been set
    • flushBuffer

      public void flushBuffer() throws IOException
      Description copied from interface: OorianHttpResponse
      Forces any content in the buffer to be written to the client.
      Specified by:
      flushBuffer in interface OorianHttpResponse
      Throws:
      IOException - if an I/O error occurs