类 MockHttpServletRequest

java.lang.Object
cn.taketoday.mock.web.MockHttpServletRequest
所有已实现的接口:
jakarta.servlet.http.HttpServletRequest, jakarta.servlet.ServletRequest
直接已知子类:
HtmlUnitRequestBuilder.HtmlUnitMockHttpServletRequest, MockMultipartHttpServletRequest

public class MockHttpServletRequest extends Object implements jakarta.servlet.http.HttpServletRequest
Mock implementation of the HttpServletRequest interface.

The default, preferred Locale for the server mocked by this request is Locale.ENGLISH. This value can be changed via addPreferredLocale(java.util.Locale) or setPreferredLocales(java.util.List<java.util.Locale>).

this set of mocks is designed on a Servlet 4.0 baseline.

从以下版本开始:
4.0
作者:
Juergen Hoeller, Rod Johnson, Rick Evans, Mark Fisher, Chris Beams, Sam Brannen, Brian Clozel
  • 字段详细资料

    • HTTP

      private static final String HTTP
      另请参阅:
    • HTTPS

      private static final String HTTPS
      另请参阅:
    • CHARSET_PREFIX

      private static final String CHARSET_PREFIX
      另请参阅:
    • GMT

      private static final TimeZone GMT
    • EMPTY_BUFFERED_READER

      private static final BufferedReader EMPTY_BUFFERED_READER
    • DATE_FORMATS

      private static final String[] DATE_FORMATS
      Date formats as specified in the HTTP RFC.
      另请参阅:
    • DEFAULT_PROTOCOL

      public static final String DEFAULT_PROTOCOL
      The default protocol: 'HTTP/1.1'.
      另请参阅:
    • DEFAULT_SCHEME

      public static final String DEFAULT_SCHEME
      The default scheme: 'http'.
      另请参阅:
    • DEFAULT_SERVER_ADDR

      public static final String DEFAULT_SERVER_ADDR
      The default server address: '127.0.0.1'.
      另请参阅:
    • DEFAULT_SERVER_NAME

      public static final String DEFAULT_SERVER_NAME
      The default server name: 'localhost'.
      另请参阅:
    • DEFAULT_SERVER_PORT

      public static final int DEFAULT_SERVER_PORT
      The default server port: '80'.
      另请参阅:
    • DEFAULT_REMOTE_ADDR

      public static final String DEFAULT_REMOTE_ADDR
      The default remote address: '127.0.0.1'.
      另请参阅:
    • DEFAULT_REMOTE_HOST

      public static final String DEFAULT_REMOTE_HOST
      The default remote host: 'localhost'.
      另请参阅:
    • servletContext

      private final jakarta.servlet.ServletContext servletContext
    • active

      private boolean active
    • attributes

      private final Map<String,Object> attributes
    • characterEncoding

      @Nullable private String characterEncoding
    • content

      @Nullable private byte[] content
    • contentType

      @Nullable private String contentType
    • inputStream

      @Nullable private jakarta.servlet.ServletInputStream inputStream
    • reader

      @Nullable private BufferedReader reader
    • parameters

      private final Map<String,String[]> parameters
    • protocol

      private String protocol
    • scheme

      private String scheme
    • serverName

      private String serverName
    • serverPort

      private int serverPort
    • remoteAddr

      private String remoteAddr
    • remoteHost

      private String remoteHost
    • locales

      private final LinkedList<Locale> locales
      List of locales in descending order.
    • secure

      private boolean secure
    • remotePort

      private int remotePort
    • localName

      private String localName
    • localAddr

      private String localAddr
    • localPort

      private int localPort
    • asyncStarted

      private boolean asyncStarted
    • asyncSupported

      private boolean asyncSupported
    • asyncContext

      @Nullable private MockAsyncContext asyncContext
    • dispatcherType

      private jakarta.servlet.DispatcherType dispatcherType
    • authType

      @Nullable private String authType
    • cookies

      @Nullable private jakarta.servlet.http.Cookie[] cookies
    • headers

      private final Map<String,HeaderValueHolder> headers
    • method

      @Nullable private String method
    • pathInfo

      @Nullable private String pathInfo
    • contextPath

      private String contextPath
    • queryString

      @Nullable private String queryString
    • remoteUser

      @Nullable private String remoteUser
    • userRoles

      private final Set<String> userRoles
    • userPrincipal

      @Nullable private Principal userPrincipal
    • requestedSessionId

      @Nullable private String requestedSessionId
    • requestURI

      @Nullable private String requestURI
    • servletPath

      private String servletPath
    • session

      @Nullable private jakarta.servlet.http.HttpSession session
    • requestedSessionIdValid

      private boolean requestedSessionIdValid
    • requestedSessionIdFromCookie

      private boolean requestedSessionIdFromCookie
    • requestedSessionIdFromURL

      private boolean requestedSessionIdFromURL
    • parts

      private final cn.taketoday.util.MultiValueMap<String,jakarta.servlet.http.Part> parts
    • httpServletMapping

      @Nullable private jakarta.servlet.http.HttpServletMapping httpServletMapping
  • 构造器详细资料

  • 方法详细资料

    • getServletContext

      public jakarta.servlet.ServletContext getServletContext()
      Return the ServletContext that this request is associated with. (Not available in the standard HttpServletRequest interface for some reason.)
      指定者:
      getServletContext 在接口中 jakarta.servlet.ServletRequest
    • isActive

      public boolean isActive()
      Return whether this request is still active (that is, not completed yet).
    • close

      public void close()
      Mark this request as completed, keeping its state.
    • invalidate

      public void invalidate()
      Invalidate this request, clearing its state.
    • checkActive

      protected void checkActive() throws IllegalStateException
      Check whether this request is still active (that is, not completed yet), throwing an IllegalStateException if not active anymore.
      抛出:
      IllegalStateException
    • getAttribute

      public Object getAttribute(String name)
      指定者:
      getAttribute 在接口中 jakarta.servlet.ServletRequest
    • getAttributeNames

      public Enumeration<String> getAttributeNames()
      指定者:
      getAttributeNames 在接口中 jakarta.servlet.ServletRequest
    • getCharacterEncoding

      @Nullable public String getCharacterEncoding()
      指定者:
      getCharacterEncoding 在接口中 jakarta.servlet.ServletRequest
    • setCharacterEncoding

      public void setCharacterEncoding(@Nullable String characterEncoding)
      指定者:
      setCharacterEncoding 在接口中 jakarta.servlet.ServletRequest
    • updateContentTypeHeader

      private void updateContentTypeHeader()
    • setContent

      public void setContent(@Nullable byte[] content)
      Set the content of the request body as a byte array.

      If the supplied byte array represents text such as XML or JSON, the character encoding should typically be set as well.

      另请参阅:
    • getContentAsByteArray

      @Nullable public byte[] getContentAsByteArray()
      Get the content of the request body as a byte array.
      返回:
      the content as a byte array (potentially null)
      另请参阅:
    • getContentAsString

      @Nullable public String getContentAsString() throws IllegalStateException, UnsupportedEncodingException
      Get the content of the request body as a String, using the configured character encoding.
      返回:
      the content as a String, potentially null
      抛出:
      IllegalStateException - if the character encoding has not been set
      UnsupportedEncodingException - if the character encoding is not supported
      另请参阅:
    • getContentLength

      public int getContentLength()
      指定者:
      getContentLength 在接口中 jakarta.servlet.ServletRequest
    • getContentLengthLong

      public long getContentLengthLong()
      指定者:
      getContentLengthLong 在接口中 jakarta.servlet.ServletRequest
    • setContentType

      public void setContentType(@Nullable String contentType)
    • getContentType

      @Nullable public String getContentType()
      指定者:
      getContentType 在接口中 jakarta.servlet.ServletRequest
    • getInputStream

      public jakarta.servlet.ServletInputStream getInputStream()
      指定者:
      getInputStream 在接口中 jakarta.servlet.ServletRequest
    • setParameter

      public void setParameter(String name, String value)
      Set a single value for the specified HTTP parameter.

      If there are already one or more values registered for the given parameter name, they will be replaced.

    • setParameter

      public void setParameter(String name, String... values)
      Set an array of values for the specified HTTP parameter.

      If there are already one or more values registered for the given parameter name, they will be replaced.

    • setParameters

      public void setParameters(Map<String,?> params)
      Set all provided parameters replacing any existing values for the provided parameter names. To add without replacing existing values, use addParameters(java.util.Map).
    • addParameter

      public void addParameter(String name, @Nullable String value)
      Add a single value for the specified HTTP parameter.

      If there are already one or more values registered for the given parameter name, the given value will be added to the end of the list.

    • addParameter

      public void addParameter(String name, String... values)
      Add an array of values for the specified HTTP parameter.

      If there are already one or more values registered for the given parameter name, the given values will be added to the end of the list.

    • addParameters

      public void addParameters(Map<String,?> params)
      Add all provided parameters without replacing any existing values. To replace existing values, use setParameters(java.util.Map).
    • removeParameter

      public void removeParameter(String name)
      Remove already registered values for the specified HTTP parameter, if any.
    • removeAllParameters

      public void removeAllParameters()
      Remove all existing parameters.
    • getParameter

      @Nullable public String getParameter(String name)
      指定者:
      getParameter 在接口中 jakarta.servlet.ServletRequest
    • getParameterNames

      public Enumeration<String> getParameterNames()
      指定者:
      getParameterNames 在接口中 jakarta.servlet.ServletRequest
    • getParameterValues

      public String[] getParameterValues(String name)
      指定者:
      getParameterValues 在接口中 jakarta.servlet.ServletRequest
    • getParameterMap

      public Map<String,String[]> getParameterMap()
      指定者:
      getParameterMap 在接口中 jakarta.servlet.ServletRequest
    • setProtocol

      public void setProtocol(String protocol)
    • getProtocol

      public String getProtocol()
      指定者:
      getProtocol 在接口中 jakarta.servlet.ServletRequest
    • setScheme

      public void setScheme(String scheme)
    • getScheme

      public String getScheme()
      指定者:
      getScheme 在接口中 jakarta.servlet.ServletRequest
    • setServerName

      public void setServerName(String serverName)
    • getServerName

      public String getServerName()
      指定者:
      getServerName 在接口中 jakarta.servlet.ServletRequest
    • setServerPort

      public void setServerPort(int serverPort)
    • getServerPort

      public int getServerPort()
      指定者:
      getServerPort 在接口中 jakarta.servlet.ServletRequest
    • getReader

      public BufferedReader getReader() throws UnsupportedEncodingException
      指定者:
      getReader 在接口中 jakarta.servlet.ServletRequest
      抛出:
      UnsupportedEncodingException
    • setRemoteAddr

      public void setRemoteAddr(String remoteAddr)
    • getRemoteAddr

      public String getRemoteAddr()
      指定者:
      getRemoteAddr 在接口中 jakarta.servlet.ServletRequest
    • setRemoteHost

      public void setRemoteHost(String remoteHost)
    • getRemoteHost

      public String getRemoteHost()
      指定者:
      getRemoteHost 在接口中 jakarta.servlet.ServletRequest
    • setAttribute

      public void setAttribute(String name, @Nullable Object value)
      指定者:
      setAttribute 在接口中 jakarta.servlet.ServletRequest
    • removeAttribute

      public void removeAttribute(String name)
      指定者:
      removeAttribute 在接口中 jakarta.servlet.ServletRequest
    • clearAttributes

      public void clearAttributes()
      Clear all of this request's attributes.
    • addPreferredLocale

      public void addPreferredLocale(Locale locale)
      Add a new preferred locale, before any existing locales.
      另请参阅:
    • setPreferredLocales

      public void setPreferredLocales(List<Locale> locales)
      Set the list of preferred locales, in descending order, effectively replacing any existing locales.
      从以下版本开始:
      4.0
      另请参阅:
    • updateAcceptLanguageHeader

      private void updateAcceptLanguageHeader()
    • getLocale

      public Locale getLocale()
      Return the first preferred locale configured in this mock request.

      If no locales have been explicitly configured, the default, preferred Locale for the server mocked by this request is Locale.ENGLISH.

      In contrast to the Servlet specification, this mock implementation does not take into consideration any locales specified via the Accept-Language header.

      指定者:
      getLocale 在接口中 jakarta.servlet.ServletRequest
      另请参阅:
    • getLocales

      public Enumeration<Locale> getLocales()
      Return an enumeration of the preferred locales configured in this mock request.

      If no locales have been explicitly configured, the default, preferred Locale for the server mocked by this request is Locale.ENGLISH.

      In contrast to the Servlet specification, this mock implementation does not take into consideration any locales specified via the Accept-Language header.

      指定者:
      getLocales 在接口中 jakarta.servlet.ServletRequest
      另请参阅:
    • setSecure

      public void setSecure(boolean secure)
      Set the boolean secure flag indicating whether the mock request was made using a secure channel, such as HTTPS.
      另请参阅:
    • isSecure

      public boolean isSecure()
      Return true if the secure flag has been set to true or if the scheme is https.
      指定者:
      isSecure 在接口中 jakarta.servlet.ServletRequest
      另请参阅:
      • ServletRequest.isSecure()
    • getRequestDispatcher

      public jakarta.servlet.RequestDispatcher getRequestDispatcher(String path)
      指定者:
      getRequestDispatcher 在接口中 jakarta.servlet.ServletRequest
    • setRemotePort

      public void setRemotePort(int remotePort)
    • getRemotePort

      public int getRemotePort()
      指定者:
      getRemotePort 在接口中 jakarta.servlet.ServletRequest
    • setLocalName

      public void setLocalName(String localName)
    • getLocalName

      public String getLocalName()
      指定者:
      getLocalName 在接口中 jakarta.servlet.ServletRequest
    • setLocalAddr

      public void setLocalAddr(String localAddr)
    • getLocalAddr

      public String getLocalAddr()
      指定者:
      getLocalAddr 在接口中 jakarta.servlet.ServletRequest
    • setLocalPort

      public void setLocalPort(int localPort)
    • getLocalPort

      public int getLocalPort()
      指定者:
      getLocalPort 在接口中 jakarta.servlet.ServletRequest
    • startAsync

      public jakarta.servlet.AsyncContext startAsync()
      指定者:
      startAsync 在接口中 jakarta.servlet.ServletRequest
    • startAsync

      public jakarta.servlet.AsyncContext startAsync(jakarta.servlet.ServletRequest request, @Nullable jakarta.servlet.ServletResponse response)
      指定者:
      startAsync 在接口中 jakarta.servlet.ServletRequest
    • setAsyncStarted

      public void setAsyncStarted(boolean asyncStarted)
    • isAsyncStarted

      public boolean isAsyncStarted()
      指定者:
      isAsyncStarted 在接口中 jakarta.servlet.ServletRequest
    • setAsyncSupported

      public void setAsyncSupported(boolean asyncSupported)
    • isAsyncSupported

      public boolean isAsyncSupported()
      指定者:
      isAsyncSupported 在接口中 jakarta.servlet.ServletRequest
    • setAsyncContext

      public void setAsyncContext(@Nullable MockAsyncContext asyncContext)
    • getAsyncContext

      @Nullable public jakarta.servlet.AsyncContext getAsyncContext()
      指定者:
      getAsyncContext 在接口中 jakarta.servlet.ServletRequest
    • setDispatcherType

      public void setDispatcherType(jakarta.servlet.DispatcherType dispatcherType)
    • getDispatcherType

      public jakarta.servlet.DispatcherType getDispatcherType()
      指定者:
      getDispatcherType 在接口中 jakarta.servlet.ServletRequest
    • getRequestId

      public String getRequestId()
      指定者:
      getRequestId 在接口中 jakarta.servlet.ServletRequest
    • getProtocolRequestId

      public String getProtocolRequestId()
      指定者:
      getProtocolRequestId 在接口中 jakarta.servlet.ServletRequest
    • getServletConnection

      public jakarta.servlet.ServletConnection getServletConnection()
      指定者:
      getServletConnection 在接口中 jakarta.servlet.ServletRequest
    • setAuthType

      public void setAuthType(@Nullable String authType)
    • getAuthType

      @Nullable public String getAuthType()
      指定者:
      getAuthType 在接口中 jakarta.servlet.http.HttpServletRequest
    • setCookies

      public void setCookies(@Nullable jakarta.servlet.http.Cookie... cookies)
    • encodeCookies

      private static String encodeCookies(@NonNull jakarta.servlet.http.Cookie... cookies)
    • getCookies

      @Nullable public jakarta.servlet.http.Cookie[] getCookies()
      指定者:
      getCookies 在接口中 jakarta.servlet.http.HttpServletRequest
    • addHeader

      public void addHeader(String name, Object value)
      Add an HTTP header entry for the given name.

      While this method can take any Object as a parameter, it is recommended to use the following types:

      另请参阅:
    • doAddHeaderValue

      private void doAddHeaderValue(String name, @Nullable Object value, boolean replace)
    • removeHeader

      public void removeHeader(String name)
      Remove already registered entries for the specified HTTP header, if any.
    • getDateHeader

      public long getDateHeader(String name)
      Return the long timestamp for the date header with the given name.

      If the internal value representation is a String, this method will try to parse it as a date using the supported date formats:

      • "EEE, dd MMM yyyy HH:mm:ss zzz"
      • "EEE, dd-MMM-yy HH:mm:ss zzz"
      • "EEE MMM dd HH:mm:ss yyyy"
      指定者:
      getDateHeader 在接口中 jakarta.servlet.http.HttpServletRequest
      参数:
      name - the header name
      另请参阅:
    • parseDateHeader

      private long parseDateHeader(String name, String value)
    • getHeader

      @Nullable public String getHeader(String name)
      指定者:
      getHeader 在接口中 jakarta.servlet.http.HttpServletRequest
    • getHeaders

      public Enumeration<String> getHeaders(String name)
      指定者:
      getHeaders 在接口中 jakarta.servlet.http.HttpServletRequest
    • getHeaderNames

      public Enumeration<String> getHeaderNames()
      指定者:
      getHeaderNames 在接口中 jakarta.servlet.http.HttpServletRequest
    • getIntHeader

      public int getIntHeader(String name)
      指定者:
      getIntHeader 在接口中 jakarta.servlet.http.HttpServletRequest
    • setMethod

      public void setMethod(@Nullable String method)
    • getMethod

      @Nullable public String getMethod()
      指定者:
      getMethod 在接口中 jakarta.servlet.http.HttpServletRequest
    • setPathInfo

      public void setPathInfo(@Nullable String pathInfo)
    • getPathInfo

      @Nullable public String getPathInfo()
      指定者:
      getPathInfo 在接口中 jakarta.servlet.http.HttpServletRequest
    • getPathTranslated

      @Nullable public String getPathTranslated()
      指定者:
      getPathTranslated 在接口中 jakarta.servlet.http.HttpServletRequest
    • setContextPath

      public void setContextPath(String contextPath)
    • getContextPath

      public String getContextPath()
      指定者:
      getContextPath 在接口中 jakarta.servlet.http.HttpServletRequest
    • setQueryString

      public void setQueryString(@Nullable String queryString)
    • getQueryString

      @Nullable public String getQueryString()
      指定者:
      getQueryString 在接口中 jakarta.servlet.http.HttpServletRequest
    • setRemoteUser

      public void setRemoteUser(@Nullable String remoteUser)
    • getRemoteUser

      @Nullable public String getRemoteUser()
      指定者:
      getRemoteUser 在接口中 jakarta.servlet.http.HttpServletRequest
    • addUserRole

      public void addUserRole(String role)
    • isUserInRole

      public boolean isUserInRole(String role)
      指定者:
      isUserInRole 在接口中 jakarta.servlet.http.HttpServletRequest
    • setUserPrincipal

      public void setUserPrincipal(@Nullable Principal userPrincipal)
    • getUserPrincipal

      @Nullable public Principal getUserPrincipal()
      指定者:
      getUserPrincipal 在接口中 jakarta.servlet.http.HttpServletRequest
    • setRequestedSessionId

      public void setRequestedSessionId(@Nullable String requestedSessionId)
    • getRequestedSessionId

      @Nullable public String getRequestedSessionId()
      指定者:
      getRequestedSessionId 在接口中 jakarta.servlet.http.HttpServletRequest
    • setRequestURI

      public void setRequestURI(@Nullable String requestURI)
    • getRequestURI

      @Nullable public String getRequestURI()
      指定者:
      getRequestURI 在接口中 jakarta.servlet.http.HttpServletRequest
    • getRequestURL

      public StringBuffer getRequestURL()
      指定者:
      getRequestURL 在接口中 jakarta.servlet.http.HttpServletRequest
    • setServletPath

      public void setServletPath(String servletPath)
    • getServletPath

      public String getServletPath()
      指定者:
      getServletPath 在接口中 jakarta.servlet.http.HttpServletRequest
    • setSession

      public void setSession(jakarta.servlet.http.HttpSession session)
    • getSession

      @Nullable public jakarta.servlet.http.HttpSession getSession(boolean create)
      指定者:
      getSession 在接口中 jakarta.servlet.http.HttpServletRequest
    • getSession

      @Nullable public jakarta.servlet.http.HttpSession getSession()
      指定者:
      getSession 在接口中 jakarta.servlet.http.HttpServletRequest
    • changeSessionId

      public String changeSessionId()
      The implementation of this (Servlet 3.1+) method calls MockHttpSession.changeSessionId() if the session is a mock session. Otherwise it simply returns the current session id.
      指定者:
      changeSessionId 在接口中 jakarta.servlet.http.HttpServletRequest
    • setRequestedSessionIdValid

      public void setRequestedSessionIdValid(boolean requestedSessionIdValid)
    • isRequestedSessionIdValid

      public boolean isRequestedSessionIdValid()
      指定者:
      isRequestedSessionIdValid 在接口中 jakarta.servlet.http.HttpServletRequest
    • setRequestedSessionIdFromCookie

      public void setRequestedSessionIdFromCookie(boolean requestedSessionIdFromCookie)
    • isRequestedSessionIdFromCookie

      public boolean isRequestedSessionIdFromCookie()
      指定者:
      isRequestedSessionIdFromCookie 在接口中 jakarta.servlet.http.HttpServletRequest
    • setRequestedSessionIdFromURL

      public void setRequestedSessionIdFromURL(boolean requestedSessionIdFromURL)
    • isRequestedSessionIdFromURL

      public boolean isRequestedSessionIdFromURL()
      指定者:
      isRequestedSessionIdFromURL 在接口中 jakarta.servlet.http.HttpServletRequest
    • authenticate

      public boolean authenticate(jakarta.servlet.http.HttpServletResponse response) throws IOException, jakarta.servlet.ServletException
      指定者:
      authenticate 在接口中 jakarta.servlet.http.HttpServletRequest
      抛出:
      IOException
      jakarta.servlet.ServletException
    • login

      public void login(String username, String password) throws jakarta.servlet.ServletException
      指定者:
      login 在接口中 jakarta.servlet.http.HttpServletRequest
      抛出:
      jakarta.servlet.ServletException
    • logout

      public void logout() throws jakarta.servlet.ServletException
      指定者:
      logout 在接口中 jakarta.servlet.http.HttpServletRequest
      抛出:
      jakarta.servlet.ServletException
    • addPart

      public void addPart(jakarta.servlet.http.Part part)
    • getPart

      @Nullable public jakarta.servlet.http.Part getPart(String name) throws IOException, jakarta.servlet.ServletException
      指定者:
      getPart 在接口中 jakarta.servlet.http.HttpServletRequest
      抛出:
      IOException
      jakarta.servlet.ServletException
    • getParts

      public Collection<jakarta.servlet.http.Part> getParts() throws IOException, jakarta.servlet.ServletException
      指定者:
      getParts 在接口中 jakarta.servlet.http.HttpServletRequest
      抛出:
      IOException
      jakarta.servlet.ServletException
    • setHttpServletMapping

      public void setHttpServletMapping(@Nullable jakarta.servlet.http.HttpServletMapping httpServletMapping)
    • getHttpServletMapping

      public jakarta.servlet.http.HttpServletMapping getHttpServletMapping()
      指定者:
      getHttpServletMapping 在接口中 jakarta.servlet.http.HttpServletRequest
    • determineMappingMatch

      @Nullable private jakarta.servlet.http.MappingMatch determineMappingMatch()
      Best effort to detect a Servlet path mapping, e.g. "/foo/*", by checking whether the length of requestURI > contextPath + servletPath. to take into account the Servlet path when parsing the requestURI.
    • upgrade

      public <T extends jakarta.servlet.http.HttpUpgradeHandler> T upgrade(Class<T> handlerClass) throws IOException, jakarta.servlet.ServletException
      指定者:
      upgrade 在接口中 jakarta.servlet.http.HttpServletRequest
      抛出:
      IOException
      jakarta.servlet.ServletException