Search in sources :

Example 11 with MockHttpServletRequest

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

the class WComponentsPerformance_Test method sendSimpleRequest.

/**
 * Invokes simple request processing.
 *
 * @param session the http session
 * @param step the step count
 */
private void sendSimpleRequest(final HttpSession session, final int step) {
    MockHttpServletRequest request = new MockHttpServletRequest(session);
    request.setParameter("formBean.property1", "p1_" + step);
    request.setParameter("formBean.property2", "p2_" + step);
    request.setParameter("submit", "Submit");
    doSimpleRequest(request, new MockHttpServletResponse());
}
Also used : MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) MockHttpServletResponse(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletResponse)

Example 12 with MockHttpServletRequest

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

the class ServletRequest_Test method testAttributeAccessors.

@Test
public void testAttributeAccessors() {
    String attributeName = "ServletRequest_Test.testAttributeAccessors.attributeName";
    String attributeValue1 = "ServletRequest_Test.testAttributeAccessors.attributeValue1";
    String attributeValue2 = "ServletRequest_Test.testAttributeAccessors.attributeValue2";
    MockHttpServletRequest backing = new MockHttpServletRequest();
    backing.setAttribute(attributeName, attributeValue1);
    ServletRequest request = new ServletRequest(backing);
    Assert.assertEquals("Incorrect attribute value", attributeValue1, request.getAttribute(attributeName));
    request.setAttribute(attributeName, attributeValue2);
    Assert.assertEquals("Incorrect attribute value after setAttribute", attributeValue2, request.getAttribute(attributeName));
}
Also used : MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) Test(org.junit.Test)

Example 13 with MockHttpServletRequest

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

the class ServletRequest_Test method testFileUpload.

@Test
public void testFileUpload() throws UnsupportedEncodingException {
    String queryParamName = "ServletRequest_Test.testFileUpload.queryParamName";
    String queryParamValue = "ServletRequest_Test.testFileUpload.queryParamValue";
    String formParamName = "ServletRequest_Test.testFileUpload.formParamName";
    String formParamValue = "ServletRequest_Test.testFileUpload.formParamValue";
    String fileParamName = "ServletRequest_Test.testFileUpload.fileParamName";
    String fileName = "ServletRequest_Test.testFileUpload.fileName";
    String fileContents = "ServletRequest_Test.testFileUpload.fileContents";
    MockHttpServletRequest backing = new MockHttpServletRequest();
    backing.setContentType("multipart/form-data; boundary=zzzzzz");
    backing.setParameter(queryParamName, queryParamValue);
    String formContent = "--zzzzzz" + "\r\ncontent-disposition: form-data; name=\"" + formParamName + "\"" + "\r\n" + "\r\n" + formParamValue + "\r\n--zzzzzz" + "\r\ncontent-disposition: form-data; name=\"" + fileParamName + "\"" + "\r\nContent-type: multipart/mixed; boundary=yyyyyy" + "\r\n" + "\r\n--yyyyyy" + "\r\nContent-disposition: attachment; filename=\"" + fileName + "\"" + "\r\nContent-Type: text/plain" + "\r\n" + "\r\n" + fileContents + "\r\n--yyyyyy--" + "\r\n--zzzzzz--";
    backing.setContent(formContent.getBytes("UTF-8"));
    ServletRequest request = new ServletRequest(backing);
    Assert.assertEquals("Incorrect query parameter value", queryParamValue, request.getParameter(queryParamName));
    Assert.assertEquals("Incorrect form parameter value", formParamValue, request.getParameter(formParamName));
    Assert.assertEquals("Incorrect file name", fileName, request.getParameter(fileParamName));
    Assert.assertEquals("Incorrect file contents", fileContents, new String(request.getFileContents(fileParamName), "UTF-8"));
}
Also used : MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) Test(org.junit.Test)

Example 14 with MockHttpServletRequest

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

the class ServletRequest_Test method testConstructor.

@Test
public void testConstructor() {
    String paramName = "ServletRequest_Test.testConstructor.paramName";
    String paramValue = "ServletRequest_Test.testConstructor.paramValue";
    MockHttpServletRequest backing = new MockHttpServletRequest();
    backing.setParameter(paramName, paramValue);
    ServletRequest request = new ServletRequest(backing);
    backing.removeParameter(paramName);
    Assert.assertEquals("Request should have a local copy of the parameters", paramValue, request.getParameter(paramName));
}
Also used : MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) Test(org.junit.Test)

Example 15 with MockHttpServletRequest

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

the class ServletRequest_Test method testSessionAttributeAccessors.

@Test
public void testSessionAttributeAccessors() {
    String attributeName = "ServletRequest_Test.testSessionAttributeAccessors.attributeName";
    String attributeValue1 = "ServletRequest_Test.testSessionAttributeAccessors.attributeValue1";
    String attributeValue2 = "ServletRequest_Test.testSessionAttributeAccessors.attributeValue2";
    // Test with no session
    MockHttpServletRequest backing = new MockHttpServletRequest();
    ServletRequest request = new ServletRequest(backing);
    Assert.assertNull("Attribute should be null if no session", request.getSessionAttribute(attributeName));
    request.setSessionAttribute(attributeName, attributeValue1);
    Assert.assertEquals("Incorrect attribute value after setSessionAttribute", attributeValue1, request.getSessionAttribute(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.getSessionAttribute(attributeName));
    request.setSessionAttribute(attributeName, attributeValue2);
    Assert.assertEquals("Incorrect attribute value after setSessionAttribute", attributeValue2, request.getSessionAttribute(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