Search in sources :

Example 11 with MockHttpServletResponse

use of com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletResponse 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 12 with MockHttpServletResponse

use of com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletResponse 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 13 with MockHttpServletResponse

use of com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletResponse 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 14 with MockHttpServletResponse

use of com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletResponse in project wcomponents by BorderTech.

the class ServletResponse_Test method testSendRedirect.

@Test
public void testSendRedirect() throws IOException {
    String redirect = "http://test.invalid/ServletResponse_Test.testSendRedirect.redirect";
    MockHttpServletResponse backing = new MockHttpServletResponse();
    ServletResponse response = new ServletResponse(backing);
    response.sendRedirect(redirect);
    Assert.assertEquals("Incorrect redirect", redirect, backing.getSendRedirection());
}
Also used : MockHttpServletResponse(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletResponse) MockHttpServletResponse(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletResponse) Test(org.junit.Test)

Example 15 with MockHttpServletResponse

use of com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletResponse in project wcomponents by BorderTech.

the class ServletResponse_Test method testSetHeader.

@Test
public void testSetHeader() {
    String headerName = "ServletResponse_Test.testSetHeader.headerName";
    String headerValue = "ServletResponse_Test.testSetHeaderType.headerValue";
    MockHttpServletResponse backing = new MockHttpServletResponse();
    ServletResponse response = new ServletResponse(backing);
    response.setHeader(headerName, headerValue);
    Assert.assertEquals("Incorrect header", headerValue, backing.getHeader(headerName));
}
Also used : MockHttpServletResponse(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletResponse) MockHttpServletResponse(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

MockHttpServletResponse (com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletResponse)21 Test (org.junit.Test)15 MockHttpServletRequest (com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest)13 MockServletConfig (com.github.bordertech.wcomponents.util.mock.servlet.MockServletConfig)8 MockHttpSession (com.github.bordertech.wcomponents.util.mock.servlet.MockHttpSession)6 WText (com.github.bordertech.wcomponents.WText)5 WServletHelper (com.github.bordertech.wcomponents.servlet.WServlet.WServletHelper)2 UIContext (com.github.bordertech.wcomponents.UIContext)1 ServletException (javax.servlet.ServletException)1