@Override public RequestDispatcher getRequestDispatcher(String path) { return new MockRequestDispatcher(path); }
@Override public void include(ServletRequest request, ServletResponse response) { Assert.notNull(request, "Request must not be null"); Assert.notNull(response, "Response must not be null"); getMockHttpServletResponse(response).addIncludedUrl(this.resource); if (logger.isDebugEnabled()) { logger.debug("MockRequestDispatcher: including [" + this.resource + "]"); } }
@Override public void forward(ServletRequest request, ServletResponse response) { super.forward(request, response); MockHttpServletResponse mockResponse = getMockHttpServletResponse(response); if(mockResponse.getForwardedUrl()!=null){ jspParser.parse(mockResponse.getForwardedUrl(),(HttpServletRequest)request,(HttpServletResponse)response); } }
@Override public RequestDispatcher getRequestDispatcher(String path) { Assert.isTrue(path.startsWith("/"), () -> "RequestDispatcher path [" + path + "] at ServletContext level must start with '/'"); return new MockRequestDispatcher(path); }
/** * Obtain the underlying {@link MockHttpServletResponse}, unwrapping * {@link HttpServletResponseWrapper} decorators if necessary. */ protected MockHttpServletResponse getMockHttpServletResponse(ServletResponse response) { if (response instanceof MockHttpServletResponse) { return (MockHttpServletResponse) response; } if (response instanceof HttpServletResponseWrapper) { return getMockHttpServletResponse(((HttpServletResponseWrapper) response).getResponse()); } throw new IllegalArgumentException("MockRequestDispatcher requires MockHttpServletResponse"); }
/** * Create a new {@code MockServletContext} using the supplied resource base * path and resource loader. * <p>Registers a {@link MockRequestDispatcher} for the Servlet named * {@literal 'default'}. * @param resourceBasePath the root directory of the WAR (should not end with a slash) * @param resourceLoader the ResourceLoader to use (or null for the default) * @see #registerNamedDispatcher */ public MockServletContext(String resourceBasePath, @Nullable ResourceLoader resourceLoader) { this.resourceLoader = (resourceLoader != null ? resourceLoader : new DefaultResourceLoader()); this.resourceBasePath = resourceBasePath; // Use JVM temp dir as ServletContext temp dir. String tempDir = System.getProperty(TEMP_DIR_SYSTEM_PROPERTY); if (tempDir != null) { this.attributes.put(WebUtils.TEMP_DIR_CONTEXT_ATTRIBUTE, new File(tempDir)); } registerNamedDispatcher(this.defaultServletName, new MockRequestDispatcher(this.defaultServletName)); }
@Override public void forward(ServletRequest request, ServletResponse response) { Assert.notNull(request, "Request must not be null"); Assert.notNull(response, "Response must not be null"); Assert.state(!response.isCommitted(), "Cannot perform forward - response is already committed"); getMockHttpServletResponse(response).setForwardedUrl(this.resource); if (logger.isDebugEnabled()) { logger.debug("MockRequestDispatcher: forwarding to [" + this.resource + "]"); } }
/** * Set the name of the <em>default</em> {@code Servlet}. * <p>Also {@link #unregisterNamedDispatcher unregisters} the current default * {@link RequestDispatcher} and {@link #registerNamedDispatcher replaces} * it with a {@link MockRequestDispatcher} for the provided * {@code defaultServletName}. * @param defaultServletName the name of the <em>default</em> {@code Servlet}; * never {@code null} or empty * @see #getDefaultServletName */ public void setDefaultServletName(String defaultServletName) { Assert.hasText(defaultServletName, "defaultServletName must not be null or empty"); unregisterNamedDispatcher(this.defaultServletName); this.defaultServletName = defaultServletName; registerNamedDispatcher(this.defaultServletName, new MockRequestDispatcher(this.defaultServletName)); }
@Override protected MockHttpServletResponse getMockHttpServletResponse( ServletResponse response) { if (response instanceof TargetInstanceProxy) { VRaptorResponse testResponse = (VRaptorResponse) ((TargetInstanceProxy<?>) response) .getTargetInstance(); return super.getMockHttpServletResponse(testResponse); } return super.getMockHttpServletResponse(response); }
@Override public RequestDispatcher getNamedDispatcher(String path) { return new MockRequestDispatcher("/"); }
@Override public void include(ServletRequest request, ServletResponse response) { Assert.notNull(request, "Request must not be null"); Assert.notNull(response, "Response must not be null"); getMockHttpServletResponse(response).addIncludedUrl(this.resource); if (logger.isDebugEnabled()) { logger.debug("MockRequestDispatcher: including [" + this.resource + "]"); } }
@Test public void registerAndUnregisterNamedDispatcher() throws Exception { final String name = "test-servlet"; final String url = "/test"; assertNull(sc.getNamedDispatcher(name)); sc.registerNamedDispatcher(name, new MockRequestDispatcher(url)); RequestDispatcher namedDispatcher = sc.getNamedDispatcher(name); assertNotNull(namedDispatcher); MockHttpServletResponse response = new MockHttpServletResponse(); namedDispatcher.forward(new MockHttpServletRequest(sc), response); assertEquals(url, response.getForwardedUrl()); sc.unregisterNamedDispatcher(name); assertNull(sc.getNamedDispatcher(name)); }
/** * Obtain the underlying {@link MockHttpServletResponse}, unwrapping * {@link HttpServletResponseWrapper} decorators if necessary. */ protected MockHttpServletResponse getMockHttpServletResponse(ServletResponse response) { if (response instanceof MockHttpServletResponse) { return (MockHttpServletResponse) response; } if (response instanceof HttpServletResponseWrapper) { return getMockHttpServletResponse(((HttpServletResponseWrapper) response).getResponse()); } throw new IllegalArgumentException("MockRequestDispatcher requires MockHttpServletResponse"); }
@Override public RequestDispatcher getRequestDispatcher(String path) { return new MockRequestDispatcher(path); }
@Override public void include(ServletRequest request, ServletResponse response) { Assert.notNull(request, "Request must not be null"); Assert.notNull(response, "Response must not be null"); getMockHttpServletResponse(response).addIncludedUrl(this.resource); if (logger.isDebugEnabled()) { logger.debug("MockRequestDispatcher: including [" + this.resource + "]"); } }
@Override public RequestDispatcher getRequestDispatcher(String path) { return new MockRequestDispatcher(path); }
/** * Obtain the underlying {@link MockHttpServletResponse}, unwrapping * {@link HttpServletResponseWrapper} decorators if necessary. */ protected MockHttpServletResponse getMockHttpServletResponse(ServletResponse response) { if (response instanceof MockHttpServletResponse) { return (MockHttpServletResponse) response; } if (response instanceof HttpServletResponseWrapper) { return getMockHttpServletResponse(((HttpServletResponseWrapper) response).getResponse()); } throw new IllegalArgumentException("MockRequestDispatcher requires MockHttpServletResponse"); }
@Override public RequestDispatcher getRequestDispatcher(String path) { Assert.isTrue(path.startsWith("/"), () -> "RequestDispatcher path [" + path + "] at ServletContext level must start with '/'"); return new MockRequestDispatcher(path); }
@Override public void forward(ServletRequest request, ServletResponse response) { Assert.notNull(request, "Request must not be null"); Assert.notNull(response, "Response must not be null"); Assert.state(!response.isCommitted(), "Cannot perform forward - response is already committed"); getMockHttpServletResponse(response).setForwardedUrl(this.resource); if (logger.isDebugEnabled()) { logger.debug("MockRequestDispatcher: forwarding to [" + this.resource + "]"); } }
@Override public RequestDispatcher getRequestDispatcher(String path) { if (!path.startsWith("/")) { throw new IllegalArgumentException("RequestDispatcher path at ServletContext level must start with '/'"); } return new MockRequestDispatcher(path); }