@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; } }
@Override public void resetBuffer() { if(isCommitted()){ throw new IllegalStateException("Already committed"); } buffer.clear(); writer = null; stream = null; }
/** * 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 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 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 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()); } }
req.asMultipartFormData(); Assert.assertFalse(EvoServletState.getResponse().isCommitted()); servlet.service(req, EvoServletState.getResponse()); Assert.assertTrue(EvoServletState.getResponse().isCommitted());