DefaultServletRequestDispatcher implements RequestDispatcher { @Override public void forward(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { if (servletRequest.getDispatcherType().equals(ASYNC)) { asyncForward(servletRequest, servletResponse); return; } syncForward(servletRequest, servletResponse); } DefaultServletRequestDispatcher(DefaultServletInvocation servletInvocation, DefaultWebApplication webApplication); void request(DefaultWebApplicationRequest webappRequest, DefaultWebApplicationResponse httpResponse); @Override void forward(ServletRequest servletRequest, ServletResponse servletResponse); @Override void include(ServletRequest servletRequest, ServletResponse servletResponse); void error(String servletName, ServletRequest servletRequest, ServletResponse servletResponse, Throwable throwable);  }
@Test void testForward() throws Exception { TestWebApplicationRequest request = new TestWebApplicationRequest(); TestWebApplicationResponse response = new TestWebApplicationResponse(); DefaultWebApplication webApp = new DefaultWebApplication(); webApp.addServlet("Snoop", TestSnoopServlet.class); webApp.initialize(); webApp.start(); RequestDispatcher dispatcher = webApp.getNamedDispatcher("Snoop"); dispatcher.forward(request, response); String responseText = new String(response.getResponseBytes()); webApp.stop(); assertTrue(responseText.contains("<title>Snoop</title>")); }
@Test void testForward2() throws Exception { TestWebApplicationRequest request = new TestWebApplicationRequest(); TestWebApplicationResponse response = new TestWebApplicationResponse(); DefaultWebApplication webApp = new DefaultWebApplication(); webApp.setWebApplicationRequestMapper(new DefaultWebApplicationRequestMapper()); webApp.addServlet("Snoop", TestSnoopServlet.class); webApp.addServletMapping("Snoop", "/Snoop"); webApp.initialize(); webApp.start(); RequestDispatcher dispatcher = webApp.getRequestDispatcher("/Snoop"); dispatcher.forward(request, response); String responseText = new String(response.getResponseBytes()); webApp.stop(); assertTrue(responseText.contains("<title>Snoop</title>")); }
@Test void testForward3() throws Exception { TestWebApplicationRequest request = new TestWebApplicationRequest(); TestWebApplicationResponse response = new TestWebApplicationResponse(); DefaultWebApplication webApp = new DefaultWebApplication(); webApp.setWebApplicationRequestMapper(new DefaultWebApplicationRequestMapper()); webApp.addServlet("Error", TestIOExceptionServlet.class); webApp.addServletMapping("Error", "/Error"); webApp.initialize(); webApp.start(); RequestDispatcher dispatcher = webApp.getRequestDispatcher("/Error"); assertThrows(IOException.class, () -> dispatcher.forward(request, response)); }
@Test void testForward4() throws Exception { TestWebApplicationRequest request = new TestWebApplicationRequest(); TestWebApplicationResponse response = new TestWebApplicationResponse(); DefaultWebApplication webApp = new DefaultWebApplication(); webApp.setWebApplicationRequestMapper(new DefaultWebApplicationRequestMapper()); webApp.addServlet("Runtime", TestRuntimeExceptionServlet.class); webApp.addServletMapping("Runtime", "/Runtime"); webApp.initialize(); webApp.start(); RequestDispatcher dispatcher = webApp.getRequestDispatcher("/Runtime"); assertThrows(RuntimeException.class, () -> dispatcher.forward(request, response)); }