Search in sources :

Example 26 with MockHttpServletRequest

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);
}
Also used : WText(com.github.bordertech.wcomponents.WText) MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) MockServletConfig(com.github.bordertech.wcomponents.util.mock.servlet.MockServletConfig) MockHttpSession(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpSession) MockHttpServletResponse(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletResponse) Test(org.junit.Test)

Example 27 with MockHttpServletRequest

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());
}
Also used : WText(com.github.bordertech.wcomponents.WText) MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) MockServletConfig(com.github.bordertech.wcomponents.util.mock.servlet.MockServletConfig) MockHttpServletResponse(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletResponse) Test(org.junit.Test)

Example 28 with MockHttpServletRequest

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());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) UIContextImpl(com.github.bordertech.wcomponents.UIContextImpl) MockHttpSession(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpSession) NullWriter(com.github.bordertech.wcomponents.util.NullWriter) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 29 with MockHttpServletRequest

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);
}
Also used : WText(com.github.bordertech.wcomponents.WText) MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) MockServletConfig(com.github.bordertech.wcomponents.util.mock.servlet.MockServletConfig) MockHttpSession(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpSession) MockHttpServletResponse(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletResponse) Test(org.junit.Test)

Example 30 with MockHttpServletRequest

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));
}
Also used : MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) MockHttpSession(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpSession) Test(org.junit.Test)

Aggregations

MockHttpServletRequest (com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest)36 Test (org.junit.Test)25 MockHttpServletResponse (com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletResponse)13 MockHttpSession (com.github.bordertech.wcomponents.util.mock.servlet.MockHttpSession)13 MockServletConfig (com.github.bordertech.wcomponents.util.mock.servlet.MockServletConfig)9 WText (com.github.bordertech.wcomponents.WText)5 PrintWriter (java.io.PrintWriter)5 UIContext (com.github.bordertech.wcomponents.UIContext)4 WebXmlRenderContext (com.github.bordertech.wcomponents.servlet.WebXmlRenderContext)4 MockResponse (com.github.bordertech.wcomponents.util.mock.MockResponse)4 ServletRequest (com.github.bordertech.wcomponents.servlet.ServletRequest)3 NullWriter (com.github.bordertech.wcomponents.util.NullWriter)3 AbstractWComponent (com.github.bordertech.wcomponents.AbstractWComponent)2 WComponent (com.github.bordertech.wcomponents.WComponent)2 InterceptorComponent (com.github.bordertech.wcomponents.container.InterceptorComponent)2 WServlet (com.github.bordertech.wcomponents.servlet.WServlet)2 WServletHelper (com.github.bordertech.wcomponents.servlet.WServlet.WServletHelper)2 MockRequest (com.github.bordertech.wcomponents.util.mock.MockRequest)2 StringWriter (java.io.StringWriter)2 Locale (java.util.Locale)2