Class TestHttpRequest

java.lang.Object
com.oorian.test.TestHttpRequest
All Implemented Interfaces:
OorianHttpRequest

public class TestHttpRequest extends Object implements OorianHttpRequest
In-memory implementation of OorianHttpRequest for headless testing.

Provides a configurable HTTP request object that does not require a servlet container. All request properties (headers, parameters, path, cookies, etc.) can be set before the request is used in a test.

Usage:


 TestHttpRequest request = new TestHttpRequest("/my-page");
 request.setParameter("id", "42");
 request.setHeader("Accept-Language", "en-US");
 
Since:
2.1
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Details

    • TestHttpRequest

      public TestHttpRequest(String pathInfo)
      Creates a new TestHttpRequest for the specified path.
      Parameters:
      pathInfo - the request path (e.g., "/my-page")
  • Method Details

    • setScheme

      public TestHttpRequest setScheme(String scheme)
      Sets the HTTP scheme (http or https).
      Parameters:
      scheme - the scheme
      Returns:
      this instance for method chaining
    • setServerName

      public TestHttpRequest setServerName(String serverName)
      Sets the server name.
      Parameters:
      serverName - the server name
      Returns:
      this instance for method chaining
    • setServerPort

      public TestHttpRequest setServerPort(int serverPort)
      Sets the server port.
      Parameters:
      serverPort - the server port
      Returns:
      this instance for method chaining
    • setContextPath

      public TestHttpRequest setContextPath(String contextPath)
      Sets the application context path.
      Parameters:
      contextPath - the context path (e.g., "/myapp")
      Returns:
      this instance for method chaining
    • setServletPath

      public TestHttpRequest setServletPath(String servletPath)
      Sets the servlet path.
      Parameters:
      servletPath - the servlet path
      Returns:
      this instance for method chaining
    • setPathInfo

      public TestHttpRequest setPathInfo(String pathInfo)
      Sets the path info (page path).
      Parameters:
      pathInfo - the path info
      Returns:
      this instance for method chaining
    • setQueryString

      public TestHttpRequest setQueryString(String queryString)
      Sets the query string.
      Parameters:
      queryString - the query string (without the leading '?')
      Returns:
      this instance for method chaining
    • setMethod

      public TestHttpRequest setMethod(String method)
      Sets the HTTP method.
      Parameters:
      method - the HTTP method (e.g., "GET", "POST")
      Returns:
      this instance for method chaining
    • setContentType

      public TestHttpRequest setContentType(String contentType)
      Sets the content type.
      Parameters:
      contentType - the content type
      Returns:
      this instance for method chaining
    • setBody

      public TestHttpRequest setBody(String body)
      Sets the request body content.
      Parameters:
      body - the body content
      Returns:
      this instance for method chaining
    • setSession

      public TestHttpRequest setSession(TestHttpSession session)
      Sets the session for this request.
      Parameters:
      session - the test session
      Returns:
      this instance for method chaining
    • setHeader

      public TestHttpRequest setHeader(String name, String value)
      Sets a request header. Replaces any existing values for the header name.
      Parameters:
      name - the header name
      value - the header value
      Returns:
      this instance for method chaining
    • addHeader

      public TestHttpRequest addHeader(String name, String value)
      Adds a value to a request header (supports multi-value headers).
      Parameters:
      name - the header name
      value - the header value to add
      Returns:
      this instance for method chaining
    • setParameter

      public TestHttpRequest setParameter(String name, String value)
      Sets a single-valued request parameter.
      Parameters:
      name - the parameter name
      value - the parameter value
      Returns:
      this instance for method chaining
    • setParameter

      public TestHttpRequest setParameter(String name, String... values)
      Sets a multi-valued request parameter.
      Parameters:
      name - the parameter name
      values - the parameter values
      Returns:
      this instance for method chaining
    • addCookie

      public TestHttpRequest addCookie(OorianCookie cookie)
      Adds a cookie to the request.
      Parameters:
      cookie - the cookie to add
      Returns:
      this instance for method chaining
    • getHeader

      public String getHeader(String name)
      Description copied from interface: OorianHttpRequest
      Returns the value of the specified request header.
      Specified by:
      getHeader in interface OorianHttpRequest
      Parameters:
      name - the header name
      Returns:
      the header value, or null if not present
    • getHeaderNames

      public Enumeration<String> getHeaderNames()
      Description copied from interface: OorianHttpRequest
      Returns an enumeration of all header names in this request.
      Specified by:
      getHeaderNames in interface OorianHttpRequest
      Returns:
      an enumeration of header names
    • getHeaders

      public Enumeration<String> getHeaders(String name)
      Description copied from interface: OorianHttpRequest
      Returns all values of the specified request header as an enumeration.
      Specified by:
      getHeaders in interface OorianHttpRequest
      Parameters:
      name - the header name
      Returns:
      an enumeration of header values
    • getPathInfo

      public String getPathInfo()
      Description copied from interface: OorianHttpRequest
      Returns the path info portion of the request URL.
      Specified by:
      getPathInfo in interface OorianHttpRequest
      Returns:
      the path info, or null if none
    • getQueryString

      public String getQueryString()
      Description copied from interface: OorianHttpRequest
      Returns the query string portion of the request URL.
      Specified by:
      getQueryString in interface OorianHttpRequest
      Returns:
      the query string, or null if none
    • getRequestURL

      public StringBuffer getRequestURL()
      Description copied from interface: OorianHttpRequest
      Returns the request URL as a StringBuffer.
      Specified by:
      getRequestURL in interface OorianHttpRequest
      Returns:
      the request URL
    • getRequestURI

      public String getRequestURI()
      Description copied from interface: OorianHttpRequest
      Returns the request URI.
      Specified by:
      getRequestURI in interface OorianHttpRequest
      Returns:
      the request URI
    • getServletPath

      public String getServletPath()
      Description copied from interface: OorianHttpRequest
      Returns the servlet path portion of the request URL.
      Specified by:
      getServletPath in interface OorianHttpRequest
      Returns:
      the servlet path
    • getParameter

      public String getParameter(String name)
      Description copied from interface: OorianHttpRequest
      Returns the value of a request parameter.
      Specified by:
      getParameter in interface OorianHttpRequest
      Parameters:
      name - the parameter name
      Returns:
      the parameter value, or null if not present
    • getParameterMap

      public Map<String,String[]> getParameterMap()
      Description copied from interface: OorianHttpRequest
      Returns a map of all request parameters.
      Specified by:
      getParameterMap in interface OorianHttpRequest
      Returns:
      the parameter map
    • getContentType

      public String getContentType()
      Description copied from interface: OorianHttpRequest
      Returns the content type of the request body.
      Specified by:
      getContentType in interface OorianHttpRequest
      Returns:
      the content type, or null if not specified
    • getCharacterEncoding

      public String getCharacterEncoding()
      Description copied from interface: OorianHttpRequest
      Returns the character encoding of the request body.
      Specified by:
      getCharacterEncoding in interface OorianHttpRequest
      Returns:
      the character encoding, or null if not specified
    • getRemoteAddr

      public String getRemoteAddr()
      Description copied from interface: OorianHttpRequest
      Returns the IP address of the client that sent the request.
      Specified by:
      getRemoteAddr in interface OorianHttpRequest
      Returns:
      the remote address
    • getContextPath

      public String getContextPath()
      Description copied from interface: OorianHttpRequest
      Returns the context path of the web application.
      Specified by:
      getContextPath in interface OorianHttpRequest
      Returns:
      the context path
    • getCookies

      public OorianCookie[] getCookies()
      Description copied from interface: OorianHttpRequest
      Returns the cookies sent with this request.
      Specified by:
      getCookies in interface OorianHttpRequest
      Returns:
      an array of cookies, or null if no cookies were sent
    • getParts

      public Collection<OorianUploadPart> getParts()
      Description copied from interface: OorianHttpRequest
      Returns the multipart parts of this request.
      Specified by:
      getParts in interface OorianHttpRequest
      Returns:
      a collection of upload parts
    • getSession

      public OorianHttpSession getSession(boolean create)
      Description copied from interface: OorianHttpRequest
      Returns the session associated with this request.
      Specified by:
      getSession in interface OorianHttpRequest
      Parameters:
      create - whether to create a new session if one doesn't exist
      Returns:
      the web session, or null if create is false and no session exists
    • getServerName

      public String getServerName()
      Description copied from interface: OorianHttpRequest
      Returns the name of the server that received the request.
      Specified by:
      getServerName in interface OorianHttpRequest
      Returns:
      the server name
    • getMethod

      public String getMethod()
      Description copied from interface: OorianHttpRequest
      Returns the HTTP method of this request (GET, POST, PUT, DELETE, etc.).
      Specified by:
      getMethod in interface OorianHttpRequest
      Returns:
      the HTTP method name
    • getReader

      public BufferedReader getReader()
      Description copied from interface: OorianHttpRequest
      Returns a BufferedReader for reading the request body as character data.
      Specified by:
      getReader in interface OorianHttpRequest
      Returns:
      a BufferedReader for reading the request body
    • getInputStream

      public InputStream getInputStream()
      Description copied from interface: OorianHttpRequest
      Returns an input stream for reading the request body as binary data.
      Specified by:
      getInputStream in interface OorianHttpRequest
      Returns:
      the input stream
    • getScheme

      public String getScheme()
      Description copied from interface: OorianHttpRequest
      Returns the scheme (protocol) used for the request (e.g., "http" or "https").
      Specified by:
      getScheme in interface OorianHttpRequest
      Returns:
      the scheme
    • getServerPort

      public int getServerPort()
      Description copied from interface: OorianHttpRequest
      Returns the port number on which the request was received.
      Specified by:
      getServerPort in interface OorianHttpRequest
      Returns:
      the server port
    • isSecure

      public boolean isSecure()
      Description copied from interface: OorianHttpRequest
      Returns whether this request was made using a secure channel (HTTPS).
      Specified by:
      isSecure in interface OorianHttpRequest
      Returns:
      true if the request is secure
    • getContentLength

      public int getContentLength()
      Description copied from interface: OorianHttpRequest
      Returns the length of the request body in bytes, or -1 if not known.
      Specified by:
      getContentLength in interface OorianHttpRequest
      Returns:
      the content length, or -1
    • getAttribute

      public Object getAttribute(String name)
      Description copied from interface: OorianHttpRequest
      Returns the value of the specified request attribute.
      Specified by:
      getAttribute in interface OorianHttpRequest
      Parameters:
      name - the attribute name
      Returns:
      the attribute value, or null if not set
    • setAttribute

      public void setAttribute(String name, Object value)
      Description copied from interface: OorianHttpRequest
      Sets a request attribute.
      Specified by:
      setAttribute in interface OorianHttpRequest
      Parameters:
      name - the attribute name
      value - the attribute value
    • getLocale

      public Locale getLocale()
      Description copied from interface: OorianHttpRequest
      Returns the preferred locale of the client, based on the Accept-Language header.
      Specified by:
      getLocale in interface OorianHttpRequest
      Returns:
      the preferred locale
    • setLocale

      public TestHttpRequest setLocale(Locale locale)
      Sets the preferred locale for this request.
      Parameters:
      locale - the locale
      Returns:
      this instance for method chaining
    • changeSessionId

      public String changeSessionId()
      Description copied from interface: OorianHttpRequest
      Changes the session ID associated with this request.

      This method generates a new session ID for the current session while preserving all session attributes. It is used for session fixation protection after authentication.

      Specified by:
      changeSessionId in interface OorianHttpRequest
      Returns:
      the new session ID