use of com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest in project wcomponents by BorderTech.
the class WServlet_Test method testSubSessionsEnabledNoSSID.
@Test
public void testSubSessionsEnabledNoSSID() throws ServletException, IOException {
Config.getInstance().setProperty(ConfigurationProperties.SERVLET_ENABLE_SUBSESSIONS, "true");
MyWServlet servlet = new MyWServlet(new WText("test"));
servlet.init(new MockServletConfig());
MockHttpSession session = new MockHttpSession();
servlet.service(new MockHttpServletRequest(session), new MockHttpServletResponse());
Assert.assertEquals("Incorrect invocation count", 1, servlet.lastInvocationCount);
// No SSID should trigger a new subsession
servlet.service(new MockHttpServletRequest(session), new MockHttpServletResponse());
Assert.assertEquals("Incorrect invocation count", 1, servlet.lastInvocationCount);
}
use of com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest in project wcomponents by BorderTech.
the class WServlet_Test method testHttpMethodTypes.
@Test
public void testHttpMethodTypes() throws ServletException, IOException {
MyWServlet servlet = new MyWServlet(new WText("test"));
servlet.init(new MockServletConfig());
MockHttpServletRequest request = new MockHttpServletRequest();
request.setRequestURI("http://localhost/foo");
MockHttpServletResponse response = new MockHttpServletResponse();
request.setMethod("GET");
servlet.service(request, response);
Assert.assertEquals("Incorrect status code", 200, response.getStatus());
Assert.assertTrue("Should have written content", response.getOutput().length > 0);
response = new MockHttpServletResponse();
request.setMethod("POST");
servlet.service(request, response);
Assert.assertEquals("Incorrect status code", 200, response.getStatus());
Assert.assertTrue("Should have written content", response.getOutput().length > 0);
response = new MockHttpServletResponse();
request.setMethod("HEAD");
servlet.service(request, response);
Assert.assertEquals("Incorrect status code", 200, response.getStatus());
Assert.assertNull("Should not have written any content for HEAD request", response.getOutput());
response = new MockHttpServletResponse();
request.setMethod("DELETE");
servlet.service(request, response);
Assert.assertEquals("Incorrect status code", 501, response.getStatus());
Assert.assertNull("Should not have written any content for HEAD request", response.getOutput());
}
use of com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest in project wcomponents by BorderTech.
the class WServlet_Test method testServiceAjaxRequest.
@Test
public void testServiceAjaxRequest() throws ServletException, IOException {
AjaxTestUI ui = new AjaxTestUI();
ui.setLocked(true);
MockHttpSession session1 = new MockHttpSession();
UIContextImpl uic1 = new UIContextImpl();
uic1.setEnvironment(new WServlet.WServletEnvironment("app", "http://localhost", ""));
uic1.setUI(ui);
uic1.getEnvironment().setSessionToken("1");
uic1.getEnvironment().setStep(1);
MockHttpSession session2 = new MockHttpSession();
UIContextImpl uic2 = new UIContextImpl();
uic2.setEnvironment(new WServlet.WServletEnvironment("app", "http://localhost", ""));
uic2.setUI(ui);
uic2.getEnvironment().setSessionToken("2");
// Request cycle for session 1
setActiveContext(uic1);
ui.serviceRequest(new ServletRequest(new MockHttpServletRequest(session1)));
ui.preparePaint(new ServletRequest(new MockHttpServletRequest(session1)));
ui.paint(new WebXmlRenderContext(new PrintWriter(new NullWriter())));
// Request cycle for session 2
setActiveContext(uic2);
ui.serviceRequest(new ServletRequest(new MockHttpServletRequest(session2)));
ui.preparePaint(new ServletRequest(new MockHttpServletRequest(session2)));
ui.paint(new WebXmlRenderContext(new PrintWriter(new NullWriter())));
// check handle request / paint counts for each session - label should not have been painted for either.
setActiveContext(uic1);
Assert.assertEquals("HandleRequest should have been called for main panel in session1", 1, ui.mainPanel.getHandleRequestCount());
Assert.assertEquals("HandleRequest should have been called for ajax panel in session1", 1, ui.ajaxPanel.getHandleRequestCount());
Assert.assertEquals("HandleRequest should have been called for label panel in session1", 1, ui.label.getHandleRequestCount());
Assert.assertEquals("Main panel should have painted in session1", 1, ui.mainPanel.getPaintCount());
Assert.assertEquals("Ajax panel should have painted in session1", 1, ui.ajaxPanel.getPaintCount());
Assert.assertEquals("Label should not have painted in session1", 0, ui.label.getPaintCount());
setActiveContext(uic2);
Assert.assertEquals("HandleRequest should have been called for main panel in session2", 1, ui.mainPanel.getHandleRequestCount());
Assert.assertEquals("HandleRequest should have been called for ajax panel in session2", 1, ui.ajaxPanel.getHandleRequestCount());
Assert.assertEquals("HandleRequest should have been called for label panel in session2", 1, ui.label.getHandleRequestCount());
Assert.assertEquals("Main panel should have painted in session2", 1, ui.mainPanel.getPaintCount());
Assert.assertEquals("Ajax panel should have painted in session2", 1, ui.ajaxPanel.getPaintCount());
Assert.assertEquals("Label should not have painted in session2", 0, ui.label.getPaintCount());
}
use of com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest in project wcomponents by BorderTech.
the class WServlet_Test method testSubSessionsDisabled.
@Test
public void testSubSessionsDisabled() throws ServletException, IOException {
Config.getInstance().setProperty(ConfigurationProperties.SERVLET_ENABLE_SUBSESSIONS, "false");
MyWServlet servlet = new MyWServlet(new WText("test"));
servlet.init(new MockServletConfig());
MockHttpSession session = new MockHttpSession();
MockHttpServletRequest request = new MockHttpServletRequest(session);
request.setParameter("ssid", "1");
servlet.service(request, new MockHttpServletResponse());
Assert.assertEquals("Incorrect invocation count", 1, servlet.lastInvocationCount);
request = new MockHttpServletRequest(session);
request.setParameter("ssid", "2");
servlet.service(request, new MockHttpServletResponse());
Assert.assertEquals("Incorrect invocation count", 2, servlet.lastInvocationCount);
request = new MockHttpServletRequest(session);
request.setParameter("ssid", "1");
servlet.service(request, new MockHttpServletResponse());
Assert.assertEquals("Incorrect invocation count", 3, servlet.lastInvocationCount);
}
use of com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest in project wcomponents by BorderTech.
the class ServletRequest_Test method testAppSessionAttributeAccessors.
@Test
public void testAppSessionAttributeAccessors() {
String attributeName = "ServletRequest_Test.testAppSessionAttributeAccessors.attributeName";
String attributeValue1 = "ServletRequest_Test.testAppSessionAttributeAccessors.attributeValue1";
String attributeValue2 = "ServletRequest_Test.testAppSessionAttributeAccessors.attributeValue2";
// Test with no session
MockHttpServletRequest backing = new MockHttpServletRequest();
ServletRequest request = new ServletRequest(backing);
Assert.assertNull("Attribute should be null if no session", request.getAppSessionAttribute(attributeName));
request.setAppSessionAttribute(attributeName, attributeValue1);
Assert.assertEquals("Incorrect attribute value after setAppSessionAttribute", attributeValue1, request.getAppSessionAttribute(attributeName));
// Test with a session
MockHttpSession session = new MockHttpSession();
session.setAttribute(attributeName, attributeValue1);
backing = new MockHttpServletRequest(session);
request = new ServletRequest(backing);
Assert.assertEquals("Incorrect attribute value", attributeValue1, request.getAppSessionAttribute(attributeName));
request.setAppSessionAttribute(attributeName, attributeValue2);
Assert.assertEquals("Incorrect attribute value after setAppSessionAttribute", attributeValue2, request.getAppSessionAttribute(attributeName));
}
Aggregations