Search in sources :

Example 1 with MockHttpServletResponse

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

the class WServlet_Test method testSubSessionsDisabledNoSSID.

@Test
public void testSubSessionsDisabledNoSSID() 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();
    servlet.service(new MockHttpServletRequest(session), new MockHttpServletResponse());
    Assert.assertEquals("Incorrect invocation count", 1, servlet.lastInvocationCount);
    servlet.service(new MockHttpServletRequest(session), new MockHttpServletResponse());
    Assert.assertEquals("Incorrect invocation count", 2, 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 2 with MockHttpServletResponse

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

the class WServlet_Test method testSubSessionsEnabled.

@Test
public void testSubSessionsEnabled() 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();
    // Initial request will be missing a SSID, will create a new subsession (id = 0)
    MockHttpServletRequest request = new MockHttpServletRequest(session);
    servlet.service(request, new MockHttpServletResponse());
    Assert.assertEquals("Incorrect invocation count", 1, servlet.lastInvocationCount);
    // Passing in an invalid SSID should return same subsession (id = 0)
    request = new MockHttpServletRequest(session);
    request.setParameter("ssid", "asdf");
    servlet.service(request, new MockHttpServletResponse());
    Assert.assertEquals("Incorrect invocation count", 2, servlet.lastInvocationCount);
    // Missing SSID should create a new subession (id = 1)
    request = new MockHttpServletRequest(session);
    servlet.service(request, new MockHttpServletResponse());
    Assert.assertEquals("Incorrect invocation count", 1, servlet.lastInvocationCount);
    // Should update the primary session (id = 0)
    request = new MockHttpServletRequest(session);
    request.setParameter("ssid", "0");
    servlet.service(request, new MockHttpServletResponse());
    Assert.assertEquals("Incorrect invocation count", 3, servlet.lastInvocationCount);
    // Should update the secondary session (id = 1)
    request = new MockHttpServletRequest(session);
    request.setParameter("ssid", "1");
    servlet.service(request, new MockHttpServletResponse());
    Assert.assertEquals("Incorrect invocation count", 2, 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 3 with MockHttpServletResponse

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

the class WServlet_Test method testServiceWithException.

@Test
public void testServiceWithException() throws ServletException, IOException {
    // A null UI should result in an exception which should be handled internally
    MyWServlet servlet = new MyWServlet(null);
    servlet.init(new MockServletConfig());
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("GET");
    MockHttpServletResponse response = new MockHttpServletResponse();
    servlet.service(request, response);
    String output = new String(response.getOutput());
    Assert.assertNotNull("An error message should be written when an exception occurs", output);
}
Also used : 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 4 with MockHttpServletResponse

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

the class WServlet_Test method sendRequest.

/**
 * Simulates an HTTP GET request to a WServlet.
 *
 * @param session the current user's session
 * @param servlet the servlet to invoke request processing on.
 * @throws ServletException a servlet exception
 * @throws IOException an exception
 */
private void sendRequest(final MockHttpSession session, final WServlet servlet) throws ServletException, IOException {
    MockHttpServletRequest request = new MockHttpServletRequest(session);
    request.setRequestURI("http://localhost/foo");
    request.setMethod("GET");
    MockHttpServletResponse response = new MockHttpServletResponse();
    servlet.service(request, response);
    String output = new String(response.getOutput());
    Assert.assertTrue("Response is missing label text", output.indexOf(LABEL_TEXT) != -1);
}
Also used : MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) MockHttpServletResponse(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletResponse)

Example 5 with MockHttpServletResponse

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

the class ServletResponse_Test method testSetContentType.

@Test
public void testSetContentType() {
    String contentType = "ServletResponse_Test.testSetContentType.contentType";
    MockHttpServletResponse backing = new MockHttpServletResponse();
    ServletResponse response = new ServletResponse(backing);
    response.setContentType(contentType);
    Assert.assertEquals("Incorrect contentType", contentType, backing.getContentType());
}
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