NanoPiranha implements Piranha { public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException { Iterator<Filter> iterator = filters.descendingIterator(); NanoFilterChain chain = new NanoFilterChain(servlet); while (iterator.hasNext()) { Filter filter = iterator.next(); NanoFilterChain previousChain = chain; chain = new NanoFilterChain(filter, previousChain); } if (servletRequest.getServletContext() == null && servletRequest instanceof NanoRequest) { NanoRequest nanoRequest = (NanoRequest) servletRequest; nanoRequest.setWebApplication(webApplication); } if (servletResponse instanceof NanoResponse) { NanoResponse nanoResponse = (NanoResponse) servletResponse; nanoResponse.setWebApplication(webApplication); } chain.doFilter(servletRequest, servletResponse); servletResponse.flushBuffer(); } NanoPiranha(); void addFilter(Filter filter); List<Filter> getFilters(); Servlet getServlet(); @Override String getVersion(); WebApplication getWebApplication(); void service(ServletRequest servletRequest, ServletResponse servletResponse); void setServlet(Servlet servlet); void setWebApplication(WebApplication webApplication);  }
@Test void testService() throws Exception { NanoPiranha piranha = new NanoPiranhaBuilder() .servlet("TestHelloWorldServlet", new TestHelloWorldServlet()) .build(); NanoRequest request = new NanoRequestBuilder() .method("GET") .servletPath("/index.html") .build(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); NanoResponse response = new NanoResponseBuilder() .outputStream(outputStream) .build(); piranha.service(request, response); assertEquals("Hello World", outputStream.toString()); }
@Test void testService2() throws Exception { NanoPiranha piranha = new NanoPiranhaBuilder() .directoryResource("src/test/jsp") .servlet("JSP Servlet", new JspServlet()) .servletInitParam("JSP Servlet", "classpath", System.getProperty("jdk.module.path") + System.getProperty("path.separator") + System.getProperty("java.class.path")) .servletInitParam("JSP Servlet", "compilerSourceVM", "1.8") .servletInitParam("JSP Servlet", "compilerTargetVM", "1.8") .build(); NanoRequest request = new NanoRequestBuilder() .method("GET") .servletPath("/index.jsp") .build(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); NanoResponse response = new NanoResponseBuilder() .outputStream(outputStream) .build(); piranha.service(request, response); assertTrue(outputStream.toString().contains("Hello JSP")); }
@Test void testService3() throws Exception { NanoPiranha piranha = new NanoPiranhaBuilder() .directoryResource("src/test/jsp") .servlet("JSP Servlet", new JspServlet()) .servletInitParam("JSP Servlet", "classpath", System.getProperty("jdk.module.path") + System.getProperty("path.separator") + System.getProperty("java.class.path")) .servletInitParam("JSP Servlet", "compilerSourceVM", "1.8") .servletInitParam("JSP Servlet", "compilerTargetVM", "1.8") .build(); NanoRequest request = new NanoRequestBuilder() .method("GET") .servletPath("/date.jsp") .build(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); NanoResponse response = new NanoResponseBuilder() .outputStream(outputStream) .build(); piranha.service(request, response); assertTrue(outputStream.toString().contains("Date = ")); }
@Test void testService4() throws Exception { NanoPiranha piranha = new NanoPiranhaBuilder() .servlet("TestQueryStringServlet", new TestQueryStringServlet()) .build(); NanoRequest request = new NanoRequestBuilder() .method("GET") .servletPath("/index.html") .queryString("q=value") .build(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); NanoResponse response = new NanoResponseBuilder() .outputStream(outputStream) .build(); piranha.service(request, response); assertEquals("q=value", outputStream.toString()); }
@Test void testService5() throws Exception { NanoPiranha piranha = new NanoPiranhaBuilder() .servlet("TestHeaderServlet", new TestHeaderServlet()) .build(); NanoRequest request = new NanoRequestBuilder() .method("GET") .servletPath("/index.xhtml") .header("header", "value") .build(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); NanoResponse response = new NanoResponseBuilder() .bodyOnly(true) .outputStream(outputStream) .build(); piranha.service(request, response); assertEquals("value", outputStream.toString()); }
@Test void testService6() throws Exception { NanoPiranha piranha = new NanoPiranhaBuilder() .directoryResource("src/test/wicket") .filter("WicketFilter", new WicketFilter()) .filterInitParam("WicketFilter", "applicationClassName", "cloud.piranha.nano.WicketApplication") .filterInitParam("WicketFilter", "filterMappingUrlPattern", "/*") .filterInitParam("WicketFilter", "wicket.configuration", "deployment") .build(); NanoRequest request = new NanoRequestBuilder() .servletPath("/") .build(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); NanoResponse response = new NanoResponseBuilder() .outputStream(outputStream) .build(); piranha.service(request, response); assertTrue(outputStream.toString().contains("Hello Wicket")); }
@Test void testService7() throws Exception { NanoPiranha piranha = new NanoPiranhaBuilder() .webApplication(new DefaultWebApplication()) .directoryResource("src/test/wicket") .filter("WicketFilter2", new WicketFilter()) .filterInitParam("WicketFilter2", "applicationClassName", "cloud.piranha.nano.WicketApplication") .filterInitParam("WicketFilter2", "filterMappingUrlPattern", "/*") .filterInitParam("WicketFilter2", "wicket.configuration", "deployment") .build(); NanoRequest request = new NanoRequestBuilder() .servletPath("/") .build(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); NanoResponse response = new NanoResponseBuilder() .outputStream(outputStream) .build(); piranha.service(request, response); assertTrue(outputStream.toString().contains("Hello Wicket")); }