com.ajaxjs.web.mock.MockRequest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ajaxjs-web Show documentation
Show all versions of ajaxjs-web Show documentation
AJAXJS Web aims to full-stack, not only the server-side framework, but also integrates the front-end library. It's written in HTML5 + Java, a successor to the JVM platform, efficient, secure, stable, cross-platform and many other advantages, but it abandoned the traditional enterprise architecture brought about by the large and bloated, emphasizing the lightweight, and fast, very suitable for the Internet fast application.
package com.ajaxjs.web.mock;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.FilterChain;
import javax.servlet.ReadListener;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import com.ajaxjs.js.JsonHelper;
import com.ajaxjs.keyvalue.MapHelper;
import com.ajaxjs.mvc.controller.MvcDispatcher;
/**
* 为方便单元测试,模拟请求对象
*
* @author Frank Cheung
*
*/
public class MockRequest extends HttpServletRequestWrapper {
public MockRequest(HttpServletRequest request) {
super(request);
}
public BaseControllerTest testInstance;
public HttpServletRequest old;
public MockRequest(BaseControllerTest testInstance, String contextPath, String path) {
this(mock(HttpServletRequest.class), testInstance, contextPath, path);
}
public MockRequest(HttpServletRequest request, BaseControllerTest testInstance, String contextPath, String path) {
super(request);
old = request;
when(getPathInfo()).thenReturn(contextPath + path);
when(getRequestURI()).thenReturn(contextPath + path);
when(getContextPath()).thenReturn(contextPath);
testInstance.response = mock(HttpServletResponse.class);
testInstance.writer = MockResponse.writerFactory(testInstance.response);
this.testInstance = testInstance;
}
public MockRequest setSession(Map sessionMap) {
MockRequest.mockSession(this, sessionMap);
return this;
}
public MockRequest setMethod(String string) {
when(getMethod()).thenReturn(string);
return this;
}
public MockRequest setHeader(String header, String value) {
when(getHeader(header)).thenReturn(value);
return this;
}
public MockRequest setParameterMap(Map parameterMap) {
when(getParameterMap()).thenReturn(parameterMap);
return this;
}
public MockRequest setParameter(Map parameter) {
for (String key : parameter.keySet()) {
when(getParameter(key)).thenReturn(parameter.get(key));
}
return this;
}
public MockRequest setParameter(String key, String value) {
when(getParameter(key)).thenReturn(value);
return this;
}
public void sendTest(MvcDispatcher dispatcher, FilterChain chain) {
try {
dispatcher.doFilter(this, testInstance.response, chain);
} catch (IOException | ServletException e) {
e.printStackTrace();
}
}
/**
* 返回 MVC 的 JSP 跳转
* @return
*/
public String getRequestDispatcheResult() {
return MockResponse.getRequestDispatcheResult(old);
}
/**
* 返回控制器输出的响应文本
* @return
*/
public String getStringResult() {
return testInstance.writer.toString();
}
/**
* 返回控制器输出的响应 JSON Map
* @return
*/
public Map getStringResultJson() {
return JsonHelper.parseMap(getStringResult());
}
public MockRequest setRequestAttribute(String... keys) {
MockRequest.mockRequestAttribute(old, keys);
return this;
}
/**
* 模拟请求对象的 attribute 属性
*
* @param request 请求假对象
* @param keys
*/
public static void mockRequestAttribute(HttpServletRequest request, String... keys) {
final Map hash = new HashMap<>();
doAnswer(new Answer() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
String key = invocation.getArgumentAt(0, String.class);
Object value = invocation.getArgumentAt(1, Object.class);
hash.put(key, value);
return null;
}
}).when(request).setAttribute(anyString(), anyObject());
Answer