Search in sources :

Example 1 with MockHttpServletResponse

use of com.helger.servlet.mock.MockHttpServletResponse in project ph-web by phax.

the class AbstractScopeAwareJob method beforeExecute.

@Override
@OverrideOnDemand
@OverridingMethodsMustInvokeSuper
protected void beforeExecute(@Nonnull final JobDataMap aJobDataMap, @Nonnull final IJobExecutionContext aContext) {
    // Scopes (ensure to create a new scope each time!)
    final MockHttpServletRequest aHttpRequest = createMockHttpServletRequest();
    final MockHttpServletResponse aHttpResponse = createMockHttpServletResponse();
    WebScopeManager.onRequestBegin(aHttpRequest, aHttpResponse);
    // Invoke callback
    beforeExecuteInScope(aJobDataMap, aContext);
}
Also used : MockHttpServletRequest(com.helger.servlet.mock.MockHttpServletRequest) MockHttpServletResponse(com.helger.servlet.mock.MockHttpServletResponse) OverridingMethodsMustInvokeSuper(javax.annotation.OverridingMethodsMustInvokeSuper) OverrideOnDemand(com.helger.commons.annotation.OverrideOnDemand)

Example 2 with MockHttpServletResponse

use of com.helger.servlet.mock.MockHttpServletResponse in project ph-web by phax.

the class MockHttpServletResponseTest method testRequestResponse.

@Test
public void testRequestResponse() {
    // create a new Servlet context for testing
    final MockHttpServletResponse aResp = new MockHttpServletResponse();
    assertEquals(StandardCharsets.UTF_8.name(), aResp.getCharacterEncoding());
    aResp.getWriter().write(TEST_STRING);
    assertFalse(aResp.isCommitted());
    assertEquals(TEST_STRING, aResp.getContentAsString(StandardCharsets.UTF_8));
    assertTrue(aResp.isCommitted());
    // Start over
    aResp.setCommitted(false);
    aResp.reset();
    assertNull(aResp.getCharacterEncoding());
    assertFalse(aResp.isCommitted());
    // Set character encoding before writing
    aResp.setCharacterEncoding(StandardCharsets.ISO_8859_1);
    aResp.getWriter().write(TEST_STRING);
    assertEquals(StandardCharsets.ISO_8859_1.name(), aResp.getCharacterEncoding());
    assertEquals(TEST_STRING, aResp.getContentAsString(StandardCharsets.ISO_8859_1));
    // Start over again
    aResp.setCommitted(false);
    aResp.reset();
    assertNull(aResp.getCharacterEncoding());
    assertFalse(aResp.isCommitted());
    // Write in the system charset
    aResp.getWriter().write(TEST_STRING);
    // Set character encoding after writing
    aResp.setCharacterEncoding(StandardCharsets.UTF_16);
    assertEquals(StandardCharsets.UTF_16.name(), aResp.getCharacterEncoding());
    // It will fail in the selected charset
    assertNotEquals(TEST_STRING, aResp.getContentAsString(StandardCharsets.UTF_16));
    // Retrieving in the system charset will succeed
    assertEquals(TEST_STRING, aResp.getContentAsString(SystemHelper.getSystemCharset()));
}
Also used : MockHttpServletResponse(com.helger.servlet.mock.MockHttpServletResponse) Test(org.junit.Test)

Example 3 with MockHttpServletResponse

use of com.helger.servlet.mock.MockHttpServletResponse in project ph-web by phax.

the class RequestWebScopeMultipartTest method testRequestParamMap.

@Test
public void testRequestParamMap() {
    final MockHttpServletRequest aRequest = new MockHttpServletRequest();
    aRequest.addParameter("a", "...");
    aRequest.addParameter("page_name[de]", "deutscher name");
    aRequest.addParameter("page_name[en]", "english name");
    aRequest.addParameter("b", "...");
    aRequest.addParameter("c", "...");
    assertEquals(5, aRequest.getParameterMap().size());
    final RequestWebScopeMultipart aRequestScope = new RequestWebScopeMultipart(aRequest, new MockHttpServletResponse());
    aRequestScope.initScope();
    final IRequestParamMap aRPM = aRequestScope.getRequestParamMap();
    assertNotNull(aRPM);
    assertSame(aRPM, aRequestScope.getRequestParamMap());
    assertEquals(aRPM.toString(), 4, aRPM.size());
    assertTrue(aRPM.containsKey("a"));
    assertTrue(aRPM.containsKey("b"));
    assertTrue(aRPM.containsKey("c"));
    assertTrue(aRPM.containsKey("page_name"));
    assertFalse(aRPM.containsKey("page_name[de]"));
    // get page_name[de] and page_name[en]
    final IRequestParamMap aNames = aRPM.getMap("page_name");
    assertEquals(2, aNames.size());
    final ICommonsOrderedMap<String, String> aValueMap = aNames.getAsValueMap();
    assertEquals(2, aValueMap.size());
    assertEquals("deutscher name", aValueMap.get("de"));
    assertEquals("english name", aValueMap.get("en"));
}
Also used : MockHttpServletRequest(com.helger.servlet.mock.MockHttpServletRequest) IRequestParamMap(com.helger.servlet.request.IRequestParamMap) MockHttpServletResponse(com.helger.servlet.mock.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

MockHttpServletResponse (com.helger.servlet.mock.MockHttpServletResponse)3 MockHttpServletRequest (com.helger.servlet.mock.MockHttpServletRequest)2 Test (org.junit.Test)2 OverrideOnDemand (com.helger.commons.annotation.OverrideOnDemand)1 IRequestParamMap (com.helger.servlet.request.IRequestParamMap)1 OverridingMethodsMustInvokeSuper (javax.annotation.OverridingMethodsMustInvokeSuper)1