Class MockHttpServletRequest

java.lang.Object
org.apache.wicket.protocol.http.mock.MockHttpServletRequest
All Implemented Interfaces:
jakarta.servlet.http.HttpServletRequest, jakarta.servlet.ServletRequest

public class MockHttpServletRequest extends Object implements jakarta.servlet.http.HttpServletRequest
Mock servlet request. Implements all of the methods from the standard HttpServletRequest class plus helper methods to aid setting up a request.
Author:
Chris Turner
  • Constructor Details

    • MockHttpServletRequest

      public MockHttpServletRequest(Application application, jakarta.servlet.http.HttpSession session, jakarta.servlet.ServletContext context, Locale locale)
      Create the request using the supplied session object. Note that in order for temporary sessions to work, the supplied session must be an instance of MockHttpSession
      Parameters:
      application - The application that this request is for
      session - The session object
      context - The current servlet context
      locale - The current locale
    • MockHttpServletRequest

      public MockHttpServletRequest(Application application, jakarta.servlet.http.HttpSession session, jakarta.servlet.ServletContext context)
  • Method Details

    • addCookie

      public void addCookie(jakarta.servlet.http.Cookie cookie)
      Add a new cookie.
      Parameters:
      cookie - The cookie
    • addCookies

      public void addCookies(Iterable<jakarta.servlet.http.Cookie> cookies)
      Parameters:
      cookies -
    • addFile

      public void addFile(String fieldName, File file, String contentType)
      Add an uploaded file to the request. Use this to simulate a file that has been uploaded to a field.
      Parameters:
      fieldName - The fieldname of the upload field.
      file - The file to upload.
      contentType - The content type of the file. Must be a correct mimetype.
    • addHeader

      public void addHeader(String name, String value)
      Add a header to the request.
      Parameters:
      name - The name of the header to add
      value - The value
    • setHeader

      public void setHeader(String name, String value)
      Sets a header to the request. Overrides any previous value of this header.
      Parameters:
      name - The name of the header to add
      value - The value
      See Also:
    • addDateHeader

      public void addDateHeader(String name, long date)
      Parameters:
      name -
      date -
    • getAttribute

      public Object getAttribute(String name)
      Get an attribute.
      Specified by:
      getAttribute in interface jakarta.servlet.ServletRequest
      Parameters:
      name - The attribute name
      Returns:
      The value, or null
    • getAttributeNames

      Get the names of all of the values.
      Specified by:
      getAttributeNames in interface jakarta.servlet.ServletRequest
      Returns:
      The names
    • getAuthType

      public String getAuthType()
      Get the auth type.
      Specified by:
      getAuthType in interface jakarta.servlet.http.HttpServletRequest
      Returns:
      The auth type
    • getCharacterEncoding

      Get the current character encoding.
      Specified by:
      getCharacterEncoding in interface jakarta.servlet.ServletRequest
      Returns:
      The character encoding
    • getCharset

      public Charset getCharset()
      Get the current character set.
      Returns:
      The character set
    • setUseMultiPartContentType

      public void setUseMultiPartContentType(boolean useMultiPartContentType)
      true will force Request generate multiPart ContentType and ContentLength
      Parameters:
      useMultiPartContentType -
    • getContentLength

      public int getContentLength()
      Return the length of the content. This is always -1 except if useMultiPartContentType set as true. Then the length will be the length of the generated request.
      Specified by:
      getContentLength in interface jakarta.servlet.ServletRequest
      Returns:
      -1 if useMultiPartContentType is false. Else the length of the generated request.
    • getContentLengthLong

      public long getContentLengthLong()
      Specified by:
      getContentLengthLong in interface jakarta.servlet.ServletRequest
    • getContentType

      If useMultiPartContentType set as true return the correct content-type.
      Specified by:
      getContentType in interface jakarta.servlet.ServletRequest
      Returns:
      The correct multipart content-type if useMultiPartContentType is true. Else null.
    • getContextPath

      Get the context path. For this mock implementation the name of the application is always returned.
      Specified by:
      getContextPath in interface jakarta.servlet.http.HttpServletRequest
      Returns:
      The context path
    • getCookie

      public jakarta.servlet.http.Cookie getCookie(String name)
      Parameters:
      name -
      Returns:
      Cookie
    • getCookies

      public jakarta.servlet.http.Cookie[] getCookies()
      Get all of the cookies for this request.
      Specified by:
      getCookies in interface jakarta.servlet.http.HttpServletRequest
      Returns:
      The cookies
    • getDateHeader

      public long getDateHeader(String name) throws IllegalArgumentException
      Get the given header as a date.
      Specified by:
      getDateHeader in interface jakarta.servlet.http.HttpServletRequest
      Parameters:
      name - The header name
      Returns:
      The date, or -1 if header not found
      Throws:
      IllegalArgumentException - If the header cannot be converted
    • getHeader

      public String getHeader(String name)
      Get the given header value.
      Specified by:
      getHeader in interface jakarta.servlet.http.HttpServletRequest
      Parameters:
      name - The header name
      Returns:
      The header value or null
    • getHeaderNames

      Get the names of all of the headers.
      Specified by:
      getHeaderNames in interface jakarta.servlet.http.HttpServletRequest
      Returns:
      The header names
    • getHeaders

      Get enumeration of all header values with the given name.
      Specified by:
      getHeaders in interface jakarta.servlet.http.HttpServletRequest
      Parameters:
      name - The name
      Returns:
      The header values
    • getInputStream

      public jakarta.servlet.ServletInputStream getInputStream() throws IOException
      Returns an input stream if there has been added some uploaded files. Use addFile(String, File, String) to add some uploaded files.
      Specified by:
      getInputStream in interface jakarta.servlet.ServletRequest
      Returns:
      The input stream
      Throws:
      IOException - If an I/O related problem occurs
    • getIntHeader

      public int getIntHeader(String name)
      Get the given header as an int.
      Specified by:
      getIntHeader in interface jakarta.servlet.http.HttpServletRequest
      Parameters:
      name - The header name
      Returns:
      The header value or -1 if header not found
      Throws:
      NumberFormatException - If the header is not formatted correctly
    • getLocale

      public Locale getLocale()
      Get the locale of the request. Attempts to decode the Accept-Language header and if not found returns the default locale of the JVM.
      Specified by:
      getLocale in interface jakarta.servlet.ServletRequest
      Returns:
      The locale
    • setLocale

      public void setLocale(Locale locale)
    • getLocales

      Return all the accepted locales. This implementation always returns just one.
      Specified by:
      getLocales in interface jakarta.servlet.ServletRequest
      Returns:
      The locales
    • getMethod

      public String getMethod()
      Get the method.
      Specified by:
      getMethod in interface jakarta.servlet.http.HttpServletRequest
      Returns:
      The method
    • getParameter

      public String getParameter(String name)
      Get the request parameter with the given name.
      Specified by:
      getParameter in interface jakarta.servlet.ServletRequest
      Parameters:
      name - The parameter name
      Returns:
      The parameter value, or null
    • getParameterMap

      Get the map of all of the parameters.
      Specified by:
      getParameterMap in interface jakarta.servlet.ServletRequest
      Returns:
      The parameters
    • getParameterNames

      Get the names of all of the parameters.
      Specified by:
      getParameterNames in interface jakarta.servlet.ServletRequest
      Returns:
      The parameter names
    • getParameterValues

      public String[] getParameterValues(String name)
      Get the values for the given parameter.
      Specified by:
      getParameterValues in interface jakarta.servlet.ServletRequest
      Parameters:
      name - The name of the parameter
      Returns:
      The return values
    • getPathInfo

      public String getPathInfo()
      Get the path info.
      Specified by:
      getPathInfo in interface jakarta.servlet.http.HttpServletRequest
      Returns:
      The path info
    • getPathTranslated

      Always returns null.
      Specified by:
      getPathTranslated in interface jakarta.servlet.http.HttpServletRequest
      Returns:
      null
    • getProtocol

      public String getProtocol()
      Get the protocol.
      Specified by:
      getProtocol in interface jakarta.servlet.ServletRequest
      Returns:
      Always HTTP/1.1
    • getQueryString

      Get the query string part of the request.
      Specified by:
      getQueryString in interface jakarta.servlet.http.HttpServletRequest
      Returns:
      The query string
    • getReader

      This feature is not implemented at this time as we are not supporting binary servlet input. This functionality may be added in the future.
      Specified by:
      getReader in interface jakarta.servlet.ServletRequest
      Returns:
      The reader
      Throws:
      IOException - If an I/O related problem occurs
    • getRemoteAddr

      Specified by:
      getRemoteAddr in interface jakarta.servlet.ServletRequest
      Returns:
      the remote address of the client
    • setRemoteAddr

      public void setRemoteAddr(String addr)
      Parameters:
      addr - Format: "aaa.bbb.ccc.ddd"
    • getRemoteHost

      Get the remote host.
      Specified by:
      getRemoteHost in interface jakarta.servlet.ServletRequest
      Returns:
      Return 'localhost' by default
    • getRemoteUser

      Get the name of the remote user from the REMOTE_USER header.
      Specified by:
      getRemoteUser in interface jakarta.servlet.http.HttpServletRequest
      Returns:
      The name of the remote user
    • getRequestDispatcher

      public jakarta.servlet.RequestDispatcher getRequestDispatcher(String name)
      Return a dummy dispatcher that just records that dispatch has occurred without actually doing anything.
      Specified by:
      getRequestDispatcher in interface jakarta.servlet.ServletRequest
      Parameters:
      name - The name to dispatch to
      Returns:
      The dispatcher
    • getRequestedSessionId

      Get the requested session id. Always returns the id of the current session.
      Specified by:
      getRequestedSessionId in interface jakarta.servlet.http.HttpServletRequest
      Returns:
      The session id
    • getRequestURI

      Returns context path and servlet path concatenated, typically /applicationClassName/applicationClassName
      Specified by:
      getRequestURI in interface jakarta.servlet.http.HttpServletRequest
      Returns:
      The path value
      See Also:
      • HttpServletRequest.getRequestURI()
    • getRequestURL

      Try to build a rough URL.
      Specified by:
      getRequestURL in interface jakarta.servlet.http.HttpServletRequest
      Returns:
      The url
      See Also:
      • HttpServletRequest.getRequestURL()
    • getScheme

      public String getScheme()
      Get the scheme.
      Specified by:
      getScheme in interface jakarta.servlet.ServletRequest
      Returns:
      the scheme of this request
    • setScheme

      public void setScheme(String scheme)
      Sets the scheme of this request

      set the secure flag accordingly (true for 'https', false otherwise)

      Parameters:
      scheme - protocol scheme (e.g. https, http, ftp)
      See Also:
    • getServerName

      Get the server name.
      Specified by:
      getServerName in interface jakarta.servlet.ServletRequest
      Returns:
      by default returns 'localhost'
    • setServerName

      public void setServerName(String serverName)
      Set the server name.
      Parameters:
      serverName - content of 'Host' header
    • getServerPort

      public int getServerPort()
      Specified by:
      getServerPort in interface jakarta.servlet.ServletRequest
      Returns:
      the server port of this request
    • setServerPort

      public void setServerPort(int port)
      Sets the server port for this request
      Parameters:
      port -
    • getServletPath

      The servlet path may either be the application name or /. For test purposes we always return the servlet name.
      Specified by:
      getServletPath in interface jakarta.servlet.http.HttpServletRequest
      Returns:
      The servlet path
    • getSession

      public jakarta.servlet.http.HttpSession getSession()
      Get the sessions.
      Specified by:
      getSession in interface jakarta.servlet.http.HttpServletRequest
      Returns:
      The session
    • changeSessionId

      Specified by:
      changeSessionId in interface jakarta.servlet.http.HttpServletRequest
    • getSession

      public jakarta.servlet.http.HttpSession getSession(boolean createNew)
      Get the session.
      Specified by:
      getSession in interface jakarta.servlet.http.HttpServletRequest
      Parameters:
      createNew - Ignored, there is always a session
      Returns:
      The session
    • getUserPrincipal

      Get the user principal.
      Specified by:
      getUserPrincipal in interface jakarta.servlet.http.HttpServletRequest
      Returns:
      A user principal
    • hasUploadedFiles

      public boolean hasUploadedFiles()
      Returns:
      True if there has been added files to this request using addFile(String, File, String)
    • initialize

      public void initialize(Locale locale)
      Reset the request back to a default state.
      Parameters:
      locale -
    • isRequestedSessionIdFromCookie

      Check whether session id is from a cookie. Always returns true.
      Specified by:
      isRequestedSessionIdFromCookie in interface jakarta.servlet.http.HttpServletRequest
      Returns:
      Always true
    • authenticate

      public boolean authenticate(jakarta.servlet.http.HttpServletResponse response) throws IOException, jakarta.servlet.ServletException
      Specified by:
      authenticate in interface jakarta.servlet.http.HttpServletRequest
      Throws:
      IOException
      jakarta.servlet.ServletException
    • login

      public void login(String username, String password) throws jakarta.servlet.ServletException
      Specified by:
      login in interface jakarta.servlet.http.HttpServletRequest
      Throws:
      jakarta.servlet.ServletException
    • logout

      public void logout() throws jakarta.servlet.ServletException
      Specified by:
      logout in interface jakarta.servlet.http.HttpServletRequest
      Throws:
      jakarta.servlet.ServletException
    • getParts

      public Collection<jakarta.servlet.http.Part> getParts() throws IOException, jakarta.servlet.ServletException
      Specified by:
      getParts in interface jakarta.servlet.http.HttpServletRequest
      Throws:
      IOException
      jakarta.servlet.ServletException
    • getPart

      public jakarta.servlet.http.Part getPart(String name) throws IOException, jakarta.servlet.ServletException
      Specified by:
      getPart in interface jakarta.servlet.http.HttpServletRequest
      Throws:
      IOException
      jakarta.servlet.ServletException
    • upgrade

      public <T extends jakarta.servlet.http.HttpUpgradeHandler> T upgrade(Class<T> aClass) throws IOException, jakarta.servlet.ServletException
      Specified by:
      upgrade in interface jakarta.servlet.http.HttpServletRequest
      Throws:
      IOException
      jakarta.servlet.ServletException
    • setPart

      public MockHttpServletRequest setPart(String name, jakarta.servlet.http.Part part)
    • isRequestedSessionIdFromURL

      public boolean isRequestedSessionIdFromURL()
      Check whether session id is from a url rewrite. Always returns false.
      Specified by:
      isRequestedSessionIdFromURL in interface jakarta.servlet.http.HttpServletRequest
      Returns:
      Always false
    • isRequestedSessionIdValid

      public boolean isRequestedSessionIdValid()
      Check whether the session id is valid.
      Specified by:
      isRequestedSessionIdValid in interface jakarta.servlet.http.HttpServletRequest
      Returns:
      Always true
    • isSecure

      public boolean isSecure()
      Specified by:
      isSecure in interface jakarta.servlet.ServletRequest
      Returns:
      true if this request's scheme is 'https', false - otherwise
    • setSecure

      public void setSecure(boolean secure)
      Parameters:
      secure -
    • isUserInRole

      public boolean isUserInRole(String name)
      NOT IMPLEMENTED.
      Specified by:
      isUserInRole in interface jakarta.servlet.http.HttpServletRequest
      Parameters:
      name - The role name
      Returns:
      Always false
    • removeAttribute

      public void removeAttribute(String name)
      Remove the given attribute.
      Specified by:
      removeAttribute in interface jakarta.servlet.ServletRequest
      Parameters:
      name - The name of the attribute
    • setAttribute

      public void setAttribute(String name, Object o)
      Set the given attribute.
      Specified by:
      setAttribute in interface jakarta.servlet.ServletRequest
      Parameters:
      name - The attribute name
      o - The value to set
    • setAuthType

      public void setAuthType(String authType)
      Set the auth type.
      Parameters:
      authType - The auth type
    • setCharacterEncoding

      Set the character encoding.
      Specified by:
      setCharacterEncoding in interface jakarta.servlet.ServletRequest
      Parameters:
      encoding - The character encoding
      Throws:
      UnsupportedEncodingException - If encoding not supported
    • setCookies

      public void setCookies(jakarta.servlet.http.Cookie[] theCookies)
      Set the cookies.
      Parameters:
      theCookies - The cookies
    • setMethod

      public void setMethod(String method)
      Set the method.
      Parameters:
      method - The method
    • setParameter

      public void setParameter(String name, String value)
      Set a parameter.
      Parameters:
      name - The name
      value - The value
    • addParameter

      public void addParameter(String name, String value)
      Parameters:
      name -
      value -
    • setParameters

      public void setParameters(Map<String,String[]> parameters)
      Sets a map of parameters.
      Parameters:
      parameters - the parameters to set
    • setPath

      public void setPath(String path)
      Set the path that this request is supposed to be serving. The path is relative to the web application root and should start with a / character
      Parameters:
      path -
    • setURL

      public void setURL(String url)
      Set the complete url for this request. The url will be analyzed.
      Parameters:
      url -
    • getLocalAddr

      public String getLocalAddr()
      Specified by:
      getLocalAddr in interface jakarta.servlet.ServletRequest
      Returns:
      local address
    • getLocalName

      public String getLocalName()
      Specified by:
      getLocalName in interface jakarta.servlet.ServletRequest
      Returns:
      local host name
    • getLocalPort

      public int getLocalPort()
      Specified by:
      getLocalPort in interface jakarta.servlet.ServletRequest
      Returns:
      local port
    • getRemotePort

      public int getRemotePort()
      Specified by:
      getRemotePort in interface jakarta.servlet.ServletRequest
      Returns:
      remote port
    • setUrl

      public void setUrl(Url url)
      Parameters:
      url -
    • getUrl

      public Url getUrl()
      Returns:
      request url
    • getPostParameters

      Returns:
      post parameters
    • getFilterPrefix

      Returns:
      filter prefix
    • getServletContext

      public jakarta.servlet.ServletContext getServletContext()
      Specified by:
      getServletContext in interface jakarta.servlet.ServletRequest
      Returns:
      ServletContext
    • startAsync

      public jakarta.servlet.AsyncContext startAsync() throws IllegalStateException
      Specified by:
      startAsync in interface jakarta.servlet.ServletRequest
      Throws:
      IllegalStateException
    • startAsync

      public jakarta.servlet.AsyncContext startAsync(jakarta.servlet.ServletRequest servletRequest, jakarta.servlet.ServletResponse servletResponse) throws IllegalStateException
      Specified by:
      startAsync in interface jakarta.servlet.ServletRequest
      Throws:
      IllegalStateException
    • isAsyncStarted

      public boolean isAsyncStarted()
      Specified by:
      isAsyncStarted in interface jakarta.servlet.ServletRequest
    • isAsyncSupported

      public boolean isAsyncSupported()
      Specified by:
      isAsyncSupported in interface jakarta.servlet.ServletRequest
    • getAsyncContext

      public jakarta.servlet.AsyncContext getAsyncContext()
      Specified by:
      getAsyncContext in interface jakarta.servlet.ServletRequest
    • getDispatcherType

      public jakarta.servlet.DispatcherType getDispatcherType()
      Specified by:
      getDispatcherType in interface jakarta.servlet.ServletRequest
    • getRequestId

      public String getRequestId()
      Specified by:
      getRequestId in interface jakarta.servlet.ServletRequest
    • getProtocolRequestId

      Specified by:
      getProtocolRequestId in interface jakarta.servlet.ServletRequest
    • getServletConnection

      public jakarta.servlet.ServletConnection getServletConnection()
      Specified by:
      getServletConnection in interface jakarta.servlet.ServletRequest