⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mockhttpservletrequest.java

📁 spring的源代码
💻 JAVA
字号:
/*
 * Copyright 2002-2004 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ 

package org.springframework.web.mock;

import java.io.BufferedReader;
import java.io.Serializable;
import java.security.Principal;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletInputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Dummy request for use outside a server to unit test servlets and
 * RequestHandlers. Can never fully implement methods, but should at least
 * finish off attribute and parameter methods (the main purpose of this class).
 * @author Rod Johnson
 * @since November 28, 2000
 */
public class MockHttpServletRequest implements HttpServletRequest, Serializable {

	//---------------------------------------------------------------------
	// Instance data
	//---------------------------------------------------------------------
	private HashMap	params = new HashMap(), atts = new HashMap(), headers = new HashMap();
	private String	url;
	private HttpSession session;
	//private SerializableCookie[] cookies;
	private Cookie[] cookies;
	private String	servletPath;
	private int		port = 80;
	private String	contentType;
	private Principal userPrincipal;
	private String pathInfo;
	private String	method;
	private String contextPath;
	
	/** List of locales in descending order */
	private List locales = new LinkedList();
	
	/** role name -> object or null */
	private Map	roles = new HashMap();

	private transient ServletContext servletContext;

	//---------------------------------------------------------------------
	// Constructors
	//---------------------------------------------------------------------
	/** Creates a new TestRequest */
	public MockHttpServletRequest(ServletContext sc, String method, String url) {
		this.url = url;
		this.contextPath = "";
		this.servletPath = "";
		this.method = method;
		
		// We must save the request dispatcher
		this.servletContext = sc;
		
		locales.add(Locale.ENGLISH);
	}
	
	public int getIntHeader(String p1) {
		return -1;
	}

	public Principal getUserPrincipal() {
		return userPrincipal;
	}

	public Enumeration getHeaders(String s) {
		List l = new LinkedList();
		l.add(headers.get(s));
		return Collections.enumeration(l);
	}

	public RequestDispatcher getRequestDispatcher(String url) {
		return servletContext.getRequestDispatcher(url);
	}

	public Enumeration getLocales() {
		return Collections.enumeration(this.locales);
	}

	public boolean isSecure() {
		return false;
	}

	public Locale getLocale() {
		return (Locale) this.locales.get(0);
	}

	public boolean isUserInRole(String role) {
		return roles.get(role) != null;
	}

	public String getContextPath() {
		return contextPath;
	}

	public String getPathTranslated() {
		return null;
	}

	public Enumeration getHeaderNames() {
		return  Collections.enumeration(headers.keySet());
	}

	public HttpSession getSession() {
		return getSession(true);
	}

	public String getPathInfo() {
		return pathInfo;
	}

	public long getDateHeader(java.lang.String p1) {
		return 1L;
	}

	public boolean isRequestedSessionIdValid() {
		return true;
	}

	public String getHeader(String p1) {
		return (String) headers.get(p1);
	}

	public String getRemoteUser() {
		return null;
	}
	public String getRequestedSessionId() {
		return null;
	}

	public boolean isRequestedSessionIdFromURL() {
		return false;
	}

	public boolean isRequestedSessionIdFromUrl() {
		return false;
	}

	public String getServletPath() {
		return servletPath;
	}

	public HttpSession getSession(boolean create) {
		if (session == null && create)
			session = new MockHttpSession();
		return session;
	}

	/** Returns null if no cookies */
	public Cookie[] getCookies() {
		return cookies;
	}

	public String getRequestURI() {
		return url;
	}

	public String getMethod() {
		return method;
	}

	public String getQueryString() {
		return null;
	}

	public boolean isRequestedSessionIdFromCookie() {
		return true;
	}

	public java.lang.String getAuthType() {
		return null;
	}

	public ServletInputStream getInputStream() throws java.io.IOException {
		return null;
	}

	public Enumeration getAttributeNames() {
		return Collections.enumeration(atts.keySet());
	}

	public String getScheme() {
		return null;
	}

	public int getServerPort() {
		return port;
	}

	public Object getAttribute(java.lang.String p1) {
		return atts.get(p1);
	}

	public void removeAttribute(String name) {
		atts.remove(name);
	}

	public void setAttribute(String p1, Object p2) {
		atts.put(p1, p2);
	}

	public java.lang.String getProtocol() {
		return null;
	}

	public BufferedReader getReader() throws java.io.IOException {
		return null;
	}

	public String getRemoteHost() {
		return null;
	}

	public String getServerName() {
		return null;
	}

	public Enumeration getParameterNames() {
		return Collections.enumeration(params.keySet());
	}

	public String getParameter(String name) {
		return (String) params.get(name);
	}

	public String[] getParameterValues(String name) {
		Object obj = params.get(name);
		if (obj instanceof String[])
			return (String[]) obj;
		else
			return new String[] {obj.toString()};
	}

	public String getRealPath(java.lang.String p1) {
		return null;
	}

	public String getRemoteAddr() {
		return null;
	}

	public String getCharacterEncoding() {
		return null;
	}

	public int getContentLength() {
		return -1;
	}

	public String getContentType() {
		return contentType;
	}

	//---------------------------------------------------------------------
	// Setters to allow manipulation
	//---------------------------------------------------------------------

	/** Add a new preferred locale, before any existing locales */
	public void addPreferredLocale(Locale l) {
		locales.add(0, l);
	}

	public void addParameter(String name, String value) {
		params.put(name, value);
	}

	public void addParameter(String name, String[] values) {
		params.put(name, values);
	}

	public void addRole(String role) {
		roles.put(role, Boolean.TRUE);
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	public void setServletPath(String sp) {
		servletPath = sp;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public void setSession(HttpSession sess) {
		this.session = sess;
	}

	public void setPathInfo(String pi) {
		this.pathInfo = pi;
	}

	/** Use after Serialization */
	//public void setServletConfig(ServletConfig servletConfig) {
	//	this.servletConfig = servletConfig;
	//}

	//public ServletConfig getServletConfig() {
	//	return servletConfig;
	//}

	public void  setUserPrincipal(Principal p) {
		this.userPrincipal = p;
	}

	public void setMethod(String method) {
		this.method = method;
	}

	public void setCookies(Cookie[] cks) {
		//cookies = new SerializableCookie[cks.length];
		//for (int i = 0; i < cookies.length; i++)
		//	cookies[i] = new SerializableCookie(cks[i]);
		this.cookies = cks;
	}

	public void setContextPath(String s) {
		contextPath = s;
	}

	public void addHeader(String name, String val) {
		headers.put(name, val);
	}
	
	public StringBuffer getRequestURL() {
		return new StringBuffer(contextPath);
	}
	
	public Map getParameterMap() {
		throw new UnsupportedOperationException("getParameterMap");
	}
	
	public void setCharacterEncoding(String enc) {
		throw new UnsupportedOperationException("setCharacterEncoding");
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -