@Override public void sendError(int arg0) throws IOException, IllegalStateException { sendError(arg0,""); }
@Override public String getRequestURI() { return getContextPath()+getServletPath(); }
@Override public String getMethod() { return method.toString(); }
@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 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(); }
@Test public void testAsPost(){ String POST = "POST"; EvoHttpServletRequest req = new EvoHttpServletRequest(); String m = req.getMethod(); Assert.assertNotEquals(m,POST); //default should not be POST req.asPOST(); m = req.getMethod(); Assert.assertEquals(POST, m); }
@Override public String toString() { //TODO: might need more stuff return EvoHttpServletRequest.class.getSimpleName() + " [ " + getMethod() + " " + getRequestURI() + " ]"; }
@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 testGetUri(){ EvoHttpServletRequest req = new EvoHttpServletRequest(); Assert.assertNotNull(req.getRequestURI()); }
@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 resetBuffer() { if(isCommitted()){ throw new IllegalStateException("Already committed"); } buffer.clear(); writer = null; stream = null; }
@EvoSuiteInclude @Constraints(atMostOnce = true , excludeOthers = {"asGET","asPUT","as"}) public void asPOST(){ setHttpMethod(HttpMethod.POST); }
@EvoSuiteInclude @Constraints(atMostOnce = true , excludeOthers = {"asTextXml","asMultipartFormData"}, dependOnProperties = TestDataJavaEE.HTTP_REQUEST_CONTENT_TYPE) public void asTextHtml(){ setContentType(TEXT_HTML_CONTENT_FORMAT); }
@Override public void reset() { resetBuffer(); //TODO reset headers and state }
@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 String getParameter(String arg0) { if(arg0 != null){ TestDataJavaEE.getInstance().accessedHttpRequestParameter(arg0); } String[] params = getParameterValues(arg0); if(params != null){ return params[0]; } return null; }
@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; } }
@EvoSuiteInclude @Constraints(atMostOnce = true , excludeOthers = {"asPOST","asPUT","as"}) public void asGET(){ setHttpMethod(HttpMethod.GET); }
@EvoSuiteInclude @Constraints(atMostOnce = true , excludeOthers = {"asMultipartFormData","asTextHtml"}, dependOnProperties = TestDataJavaEE.HTTP_REQUEST_CONTENT_TYPE) public void asTextXml(){ setContentType(TEXT_XML_CONTENT_FORMAT); }
@EvoSuiteInclude @Constraints(atMostOnce = true , excludeOthers = {"asGET","asPOST","asPUT"}, noNullInputs = true) public void as(HttpMethod m){ setHttpMethod(m); }