@Override public ServletResponse getResponse() { return EvoServletState.getResponse(); }
@Override public AsyncContext startAsync() throws IllegalStateException { if(!isAsyncSupported() || EvoServletState.getResponse().isCommitted()){ /* TODO Need also to handle: - this request is within the scope of a filter - this method is called again without any asynchronous dispatch (resulting from one of the AsyncContext.dispatch methods), is called outside the scope of any such dispatch, or is called again within the scope of the same dispatch */ throw new IllegalStateException("Cannot start async"); } if(asyncContext==null){ asyncContext = new EvoAsyncContext(); } else { //should re-init, as per JavaDoc } return asyncContext; }
@Test public void testAsyn() throws ServletException, IOException { HttpServlet servlet = new AnnotatedServlet_for_testAsyn(); EvoServletState.initServlet(servlet); boolean supported = EvoServletState.getRequest().isAsyncSupported(); Assert.assertTrue(supported); servlet.service(EvoServletState.getRequest(), EvoServletState.getResponse()); String body = EvoServletState.getResponse().getBody(); Assert.assertEquals("foo", body); }
req.asTextXml(); servlet.service(req, EvoServletState.getResponse()); Assert.assertTrue(EvoServletState.getResponse().getBody().contains("42")); req.asMultipartFormData(); Assert.assertFalse(EvoServletState.getResponse().isCommitted()); servlet.service(req, EvoServletState.getResponse()); Assert.assertFalse(EvoServletState.getResponse().getBody().contains("42")); Assert.assertTrue(EvoServletState.getResponse().isCommitted());
@Test public void testParts() throws ServletException, IOException{ HttpServlet servlet = new HttpServlet() { @Override public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Collection<Part> parts = req.getParts(); String s = ""; for(Part p : parts){ Scanner in = new Scanner(p.getInputStream()); s += in.nextLine(); in.close(); } PrintWriter out = resp.getWriter(); out.print(s); out.close(); } }; String msg0 = "foo"; String msg1 = "bar"; EvoServletState.initServlet(servlet); EvoHttpServletRequest req = EvoServletState.getRequest(); req.asPOST(); req.asMultipartFormData(); req.addPart(new EvoPart("first", msg0)); req.addPart(new EvoPart("second", msg1)); servlet.service(req, EvoServletState.getResponse()); Assert.assertTrue(TestDataJavaEE.getInstance().getViewOfParts().size() == 0); Assert.assertTrue(EvoServletState.getResponse().getBody().equals(msg0+msg1)); }
EvoHttpServletRequest req = EvoServletState.getRequest(); req.asPOST(); servlet.service(req, EvoServletState.getResponse()); Assert.assertTrue(val[0]); Assert.assertTrue(!val[1]); req = EvoServletState.getRequest(); req.asGET(); servlet.service(req, EvoServletState.getResponse()); Assert.assertTrue(!val[0]); Assert.assertTrue(val[1]); req = EvoServletState.getRequest(); req.asPUT(); servlet.service(req, EvoServletState.getResponse()); Assert.assertTrue(!val[0]); Assert.assertTrue(!val[1]);