@Override public void sendError(int arg0, String arg1) throws IOException, IllegalStateException { if(isCommitted()){ throw new IllegalStateException("Response already committed"); } contentType = "text/html"; resetBuffer(); PrintWriter out = getWriter(); out.println("ERROR page. Code: "+arg0+". Message: "+arg1); out.close(); }
@Override public PrintWriter getWriter() throws IOException,IllegalStateException { //TODO somehow we should use "encoding" here, but maybe not so important for unit testing if(writer != null){ return writer; } if(stream != null){ throw new IllegalStateException("Get writer failed because get stream has already been called"); } writer = new PrintWriter(getOutputStream()); return writer; }
@Override public void sendError(int arg0) throws IOException, IllegalStateException { sendError(arg0,""); }
@Test public void testSimpleWrite() throws IOException { String a = "<html>"; String b = "foo"; String c = "</html>"; EvoHttpServletResponse res = new EvoHttpServletResponse(); Assert.assertFalse(res.isCommitted()); PrintWriter out = res.getWriter(); out.print(a); out.print(b); out.print(c); Assert.assertEquals(EvoHttpServletResponse.WARN_NO_COMMITTED, res.getBody()); out.close(); Assert.assertTrue(res.isCommitted()); Assert.assertEquals(a+b+c, res.getBody()); } }
@Test public void testSimpleScenarioWithParams() throws IOException { EvoHttpServletRequest req = new EvoHttpServletRequest(); EvoHttpServletResponse res = new EvoHttpServletResponse(); req.addParam("foo", "bar"); PrintWriter out = res.getWriter(); out.print(req.getParameter("foo")); res.flushBuffer(); Assert.assertEquals("bar", res.getBody()); }
@Override public void setBufferSize(int arg0) { if(isCommitted() || buffer.size() > 0){ throw new IllegalStateException("Can only set buffer before writing any output"); } if(arg0 > 0){ bufferSize = arg0; } }
EvoHttpServletResponse resp = new EvoHttpServletResponse(); servlet.service(req, resp); String body = resp.getBody(); Assert.assertNotEquals(EvoHttpServletResponse.WARN_NO_COMMITTED, body); Assert.assertTrue(body.length() > 0);
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());
@EvoSuiteInclude @Constraints(atMostOnce = true, after = "initServlet") public static EvoHttpServletResponse getResponse() throws IllegalStateException{ checkInit(); if(resp == null){ resp = new EvoHttpServletResponse(); } return resp; }
@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); }
@Override public void reset() { resetBuffer(); //TODO reset headers and state }
@Override public void resetBuffer() { if(isCommitted()){ throw new IllegalStateException("Already committed"); } buffer.clear(); writer = null; stream = null; }
@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)); }
/** * This method should be used to see what the SUT has written in the * body of the response. * @return */ @EvoSuiteAssertionOnly public String getBody(){ if(!isCommitted()){ return WARN_NO_COMMITTED; } byte[] data = new byte[buffer.size()]; for(int i=0; i<data.length; i++){ data[i] = buffer.get(i); } return new String(data); }
@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; }