/** * Test that calling session.isNew() works for the initial as well as subsequent requests. */ @Test public void testIsNew() throws Exception { Callback c = new Callback() { @Override public void call(HttpServletRequest request, HttpServletResponse response) throws IOException { HttpSession session = request.getSession(); response.getWriter().write(Boolean.toString(session.isNew())); } }; servlet.getServletContext().setAttribute("callback", c); WebConversation wc = new WebConversation(); WebRequest req = new GetMethodWebRequest(String.format("http://localhost:%d/test", port)); req.setParameter("cmd", QueryCommand.CALLBACK.name()); req.setParameter("param", "callback"); WebResponse response = wc.getResponse(req); assertEquals("true", response.getText()); response = wc.getResponse(req); assertEquals("false", response.getText()); }
@Test public void testCommitSessionValveInvalidSession() throws Exception { Callback c = new Callback() { @Override public void call(HttpServletRequest request, HttpServletResponse response) throws IOException { HttpSession session = request.getSession(); session.invalidate(); response.getWriter().write("done"); } }; servlet.getServletContext().setAttribute("callback", c); WebConversation wc = new WebConversation(); WebRequest req = new GetMethodWebRequest(String.format("http://localhost:%d/test", port)); // Execute the callback req.setParameter("cmd", QueryCommand.CALLBACK.name()); req.setParameter("param", "callback"); WebResponse response = wc.getResponse(req); assertEquals("done", response.getText()); }
/** * Test callback functionality. This is here really just as an example. Callbacks are useful to * implement per test actions which can be defined within the actual test method instead of in a * separate servlet class. */ @Test public void testCallback() throws Exception { final String helloWorld = "Hello World"; Callback c = new Callback() { @Override public void call(HttpServletRequest request, HttpServletResponse response) throws IOException { PrintWriter out = response.getWriter(); out.write(helloWorld); } }; servlet.getServletContext().setAttribute("callback", c); WebConversation wc = new WebConversation(); WebRequest req = new GetMethodWebRequest(String.format("http://localhost:%d/test", port)); req.setParameter("cmd", QueryCommand.CALLBACK.name()); req.setParameter("param", "callback"); WebResponse response = wc.getResponse(req); assertEquals(helloWorld, response.getText()); }
/** * Test for issue #45 Sessions are being created for every request */ @Test public void testExtraSessionsNotCreated() throws Exception { Callback c = new Callback() { @Override public void call(HttpServletRequest request, HttpServletResponse response) throws IOException { // Do nothing with sessions response.getWriter().write("done"); } }; servlet.getServletContext().setAttribute("callback", c); WebConversation wc = new WebConversation(); WebRequest req = new GetMethodWebRequest(String.format("http://localhost:%d/test", port)); // Execute the callback req.setParameter("cmd", QueryCommand.CALLBACK.name()); req.setParameter("param", "callback"); WebResponse response = wc.getResponse(req); assertEquals("done", response.getText()); assertEquals("The region should be empty", 0, region.size()); }
/** * Test setting the session expiration */ @Test public void testSessionExpiration1() throws Exception { // TestSessions only live for a second sessionManager.setMaxInactiveInterval(1); String key = "value_testSessionExpiration1"; String value = "Foo"; WebConversation wc = new WebConversation(); WebRequest req = new GetMethodWebRequest(String.format("http://localhost:%d/test", port)); // Set an attribute req.setParameter("cmd", QueryCommand.SET.name()); req.setParameter("param", key); req.setParameter("value", value); WebResponse response = wc.getResponse(req); // Sleep a while Thread.sleep(2000); // The attribute should not be accessible now... req.setParameter("cmd", QueryCommand.GET.name()); req.setParameter("param", key); response = wc.getResponse(req); assertEquals("", response.getText()); }
/** * Test expiration of a session by the tomcat container, rather than gemfire expiration */ @Test public void testSessionExpirationByContainer() throws Exception { String key = "value_testSessionExpiration1"; String value = "Foo"; WebConversation wc = new WebConversation(); WebRequest req = new GetMethodWebRequest(String.format("http://localhost:%d/test", port)); // Set an attribute req.setParameter("cmd", QueryCommand.SET.name()); req.setParameter("param", key); req.setParameter("value", value); WebResponse response = wc.getResponse(req); // Set the session timeout of this one session. req.setParameter("cmd", QueryCommand.SET_MAX_INACTIVE.name()); req.setParameter("value", "1"); response = wc.getResponse(req); // Wait until the session should expire Thread.sleep(2000); // Do a request, which should cause the session to be expired req.setParameter("cmd", QueryCommand.GET.name()); req.setParameter("param", key); response = wc.getResponse(req); assertEquals("", response.getText()); }
/** * Test that removing a session attribute also removes it from the region */ @Test public void testRemoveAttribute() throws Exception { String key = "value_testRemoveAttribute"; String value = "Foo"; WebConversation wc = new WebConversation(); WebRequest req = new GetMethodWebRequest(String.format("http://localhost:%d/test", port)); // Set an attribute req.setParameter("cmd", QueryCommand.SET.name()); req.setParameter("param", key); req.setParameter("value", value); WebResponse response = wc.getResponse(req); String sessionId = response.getNewCookieValue("JSESSIONID"); // Implicitly remove the attribute req.removeParameter("value"); wc.getResponse(req); // The attribute should not be accessible now... req.setParameter("cmd", QueryCommand.GET.name()); req.setParameter("param", key); response = wc.getResponse(req); assertEquals("", response.getText()); assertNull(region.get(sessionId).getAttribute(key)); }
/** * Check that our session persists. The values we pass in as query params are used to set * attributes on the session. */ @Test public void testSessionPersists1() throws Exception { String key = "value_testSessionPersists1"; String value = "Foo"; WebConversation wc = new WebConversation(); WebRequest req = new GetMethodWebRequest(String.format("http://localhost:%d/test", port)); req.setParameter("cmd", QueryCommand.SET.name()); req.setParameter("param", key); req.setParameter("value", value); WebResponse response = wc.getResponse(req); String sessionId = response.getNewCookieValue("JSESSIONID"); assertNotNull("No apparent session cookie", sessionId); // The request retains the cookie from the prior response... req.setParameter("cmd", QueryCommand.GET.name()); req.setParameter("param", key); req.removeParameter("value"); response = wc.getResponse(req); assertEquals(value, response.getText()); }
/** * Test that invalidating a session makes it's attributes inaccessible. */ @Test public void testInvalidate() throws Exception { String key = "value_testInvalidate"; String value = "Foo"; WebConversation wc = new WebConversation(); WebRequest req = new GetMethodWebRequest(String.format("http://localhost:%d/test", port)); // Set an attribute req.setParameter("cmd", QueryCommand.SET.name()); req.setParameter("param", key); req.setParameter("value", value); WebResponse response = wc.getResponse(req); // Invalidate the session req.removeParameter("param"); req.removeParameter("value"); req.setParameter("cmd", QueryCommand.INVALIDATE.name()); wc.getResponse(req); // The attribute should not be accessible now... req.setParameter("cmd", QueryCommand.GET.name()); req.setParameter("param", key); response = wc.getResponse(req); assertEquals("", response.getText()); }
public String toString() { try { return "DefaultWebResponse [" + getText() + "]"; } catch (IOException e) { // should never happen return "DefaultWebResponse [???]"; } } }
/** * Returns a buffered input stream for reading the contents of this reply. **/ public InputStream getInputStream() throws IOException { if (_inputStream == null) _inputStream = new ByteArrayInputStream( getText().getBytes() ); return _inputStream; }
/** * Returns a buffered input stream for reading the contents of this reply. **/ public InputStream getInputStream() throws IOException { if (_inputStream == null) _inputStream = new ByteArrayInputStream( getText().getBytes() ); return _inputStream; }
/** * Returns a buffered input stream for reading the contents of this reply. **/ public InputStream getInputStream() throws IOException { if (_inputStream == null) _inputStream = new ByteArrayInputStream( getText().getBytes() ); return _inputStream; }
/** * Obtains the HTML of the response as a dom4j document. */ static Document getDom4j( WebResponse wr ) throws IOException, SAXException { try { return new SAXReader(new SAXParser()).read(new StringReader(wr.getText())); } catch (DocumentException e) { SAXException se = new SAXException(e); se.initCause(e); throw se; } }
private void verifyPhase(WebConversation conversation, String jobId, String expectedPhase) throws Throwable { // GET request to the phase resource. String resourceUrl = serviceUrl + "/" + jobId + "/phase"; WebResponse response = get(conversation, resourceUrl, "text/plain"); log.debug(Util.getResponseHeaders(response)); log.debug("Response.getText():\r\n" + response.getText()); assertEquals("GET response Content-Type header to " + resourceUrl + " is incorrect", "text/plain", response.getContentType()); assertEquals("response should return only a string of the phase.", expectedPhase, response.getText()); }
/** * Returns the contents of an included script, given its src attribute. * @param srcAttribute the location of the script. * @return the contents of the script. * @throws java.io.IOException if there is a problem retrieving the script */ String getIncludedScript( String srcAttribute ) throws IOException { WebRequest req = new GetMethodWebRequest( getBaseURL(), srcAttribute ); WebWindow window = getResponse().getWindow(); if (window == null) throw new IllegalStateException( "Unable to retrieve script included by this response, since it was loaded by getResource(). Use getResponse() instead."); return window.getResource( req ).getText(); }
/** * Returns the contents of an included script, given its src attribute. * @param srcAttribute the location of the script. * @return the contents of the script. * @throws java.io.IOException if there is a problem retrieving the script */ String getIncludedScript( String srcAttribute ) throws IOException { WebRequest req = new GetMethodWebRequest( getBaseURL(), srcAttribute ); WebWindow window = getResponse().getWindow(); if (window == null) throw new IllegalStateException( "Unable to retrieve script included by this response, since it was loaded by getResource(). Use getResponse() instead."); return window.getResource( req ).getText(); }
/** * Returns the contents of an included script, given its src attribute. * @param srcAttribute the location of the script. * @return the contents of the script. * @throws java.io.IOException if there is a problem retrieving the script */ String getIncludedScript( String srcAttribute ) throws IOException { WebRequest req = new GetMethodWebRequest( getBaseURL(), srcAttribute ); WebWindow window = getResponse().getWindow(); if (window == null) throw new IllegalStateException( "Unable to retrieve script included by this response, since it was loaded by getResource(). Use getResponse() instead."); return window.getResource( req ).getText(); }
@Test public void testGetUnformatServiceList() throws Exception { ServletUnitClient client = newClient(); client.setExceptionsThrownOnErrorStatus(false); WebResponse res = client.getResponse(CONTEXT_URL + "/services?formatted=false"); assertTrue(res.getText().contains("http://localhost/mycontext/services/greeter3")); assertTrue(res.getText().contains("http://localhost/mycontext/services/greeter2")); assertTrue(res.getText().contains("http://localhost/mycontext/services/greeter")); }
public void testGoodLogin() throws Exception { WebConversation conversation = new WebConversation(); WebRequest request = new GetMethodWebRequest( "http://www.meterware.com/servlet/TopSecret" ); WebResponse response = conversation.getResponse( request ); WebForm loginForm = response.getForms()[0]; request = loginForm.getRequest(); request.setParameter( "name", "master" ); response = conversation.getResponse( request ); assertTrue( "Login not accepted", response.getText().indexOf( "You made it!" ) != -1 ); assertEquals( "Page title", "Top Secret", response.getTitle() ); }