@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); }
public EvoHttpServletRequest(){ /* Note: quite a few of these fields could be good to be set by the test cases directly, but they might just increase the search space without any real benefit. Should carefully check what is actually used in practice */ contextPath = "/EvoSuiteContext"; servletPath = "/EvoSuiteServlet"; requestedSessionId = null; asGET(); contentType = null; //TODO check unsure if null only in GET protocol = "HTTP/1.1"; localName = "MockedJavaEEServer"; localAddr = VirtualNetwork.getInstance().dnsResolve(localName); localPort = 80; remoteHost = "MockedRemoteEvoSuiteRequestClient"; remoteAddr = VirtualNetwork.getInstance().dnsResolve(remoteHost); remotePort = 61386; //any would do parameters = new LinkedHashMap<>(); asyncContext = null; parts = new LinkedHashMap<>(); }
@Override public String toString() { //TODO: might need more stuff return EvoHttpServletRequest.class.getSimpleName() + " [ " + getMethod() + " " + getRequestURI() + " ]"; }
@Test public void testGetUri(){ EvoHttpServletRequest req = new EvoHttpServletRequest(); Assert.assertNotNull(req.getRequestURI()); }
@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()); }
req.asPOST(); servlet.service(req, EvoServletState.getResponse()); Assert.assertTrue(val[0]); EvoServletState.initServlet(servlet); req = EvoServletState.getRequest(); req.asGET(); servlet.service(req, EvoServletState.getResponse()); Assert.assertTrue(!val[0]); EvoServletState.initServlet(servlet); req = EvoServletState.getRequest(); req.asPUT(); servlet.service(req, EvoServletState.getResponse()); Assert.assertTrue(!val[0]);
@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 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)); }
@Test public void testParametersMap(){ String p1 = "p1"; String p2 = "p2"; Assert.assertFalse(TestDataJavaEE.getInstance().getViewOfHttpRequestParameters().contains(p1)); Assert.assertFalse(TestDataJavaEE.getInstance().getViewOfHttpRequestParameters().contains(p2)); EvoHttpServletRequest req = new EvoHttpServletRequest(); req.getParameterMap().containsKey(p1); Assert.assertTrue(TestDataJavaEE.getInstance().getViewOfHttpRequestParameters().contains(p1)); Assert.assertFalse(TestDataJavaEE.getInstance().getViewOfHttpRequestParameters().contains(p2)); req.getParameterMap().keySet().contains(p2); Assert.assertTrue(TestDataJavaEE.getInstance().getViewOfHttpRequestParameters().contains(p1)); Assert.assertTrue(TestDataJavaEE.getInstance().getViewOfHttpRequestParameters().contains(p2)); } }
req.asPOST(); req.asTextXml(); EvoServletState.initServlet(servlet); req = EvoServletState.getRequest(); req.asPOST(); req.asMultipartFormData();
@EvoSuiteInclude @Constraints(atMostOnce = true, after = "initServlet") public static EvoHttpServletRequest getRequest() throws IllegalStateException{ checkInit(); if(req == null){ req = new EvoHttpServletRequest(); } return req; }
@Test public void testAccessToParam(){ String param = "foo"; Assert.assertFalse(TestDataJavaEE.getInstance().getViewOfHttpRequestParameters().contains(param)); EvoHttpServletRequest req = new EvoHttpServletRequest(); String val = req.getParameter(param); Assert.assertNull(val); Assert.assertTrue(TestDataJavaEE.getInstance().getViewOfHttpRequestParameters().contains(param)); req.addParam(param, "some value"); val = req.getParameter(param); Assert.assertNotNull(val); Assert.assertTrue(TestDataJavaEE.getInstance().getViewOfHttpRequestParameters().contains(param)); }
@Test public void testNoAsyn() throws ServletException { HttpServlet servlet = new HttpServlet() { @Override public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { } }; EvoServletState.initServlet(servlet); boolean supported = EvoServletState.getRequest().isAsyncSupported(); Assert.assertFalse(supported); }
EvoHttpServletRequest req = new EvoHttpServletRequest(); EvoHttpServletResponse resp = new EvoHttpServletResponse();
@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); }