Example usage for org.springframework.mock.web MockHttpServletRequest MockHttpServletRequest

List of usage examples for org.springframework.mock.web MockHttpServletRequest MockHttpServletRequest

Introduction

In this page you can find the example usage for org.springframework.mock.web MockHttpServletRequest MockHttpServletRequest.

Prototype

public MockHttpServletRequest() 

Source Link

Document

Create a new MockHttpServletRequest with a default MockServletContext .

Usage

From source file:com.mtgi.analytics.servlet.SpringSessionContextTest.java

@Test
public void testAuthenticated() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRemoteUser("testUser");
    ServletRequestAttributes atts = new ServletRequestAttributes(request);
    RequestContextHolder.setRequestAttributes(atts);

    assertEquals("context inherits user from request attributes", "testUser", inst.getContextUserId());
    assertEquals("context inherits session from request attributes", request.getSession().getId(),
            inst.getContextSessionId());
}

From source file:org.jmesa.test.AbstractTestCase.java

protected WebContext createWebContext() {

    MockHttpServletRequest request = new MockHttpServletRequest();
    return new HttpServletRequestWebContext(request);
}

From source file:cherry.foundation.onetimetoken.OneTimeTokenIssuerImplTest.java

@Test
public void testNewToken() {
    HttpServletRequest request = new MockHttpServletRequest();
    OneTimeToken token = oneTimeTokenIssuer.newToken(request);
    assertEquals("__OneTimeToken__", token.getName());
    assertNotNull(token.getValue());/*from  ww w .  ja  va2s.  com*/

    assertNotNull(request.getSession(false));
    assertEquals(token.getValue(), request.getSession().getAttribute(token.getName()));
}

From source file:com.gisgraphy.helper.HTMLHelperTest.java

@Test
public void isParametesrEmptyShouldReturnValidResult() {
    String parameterName1 = "param1";
    String parameterName2 = "param2";
    MockHttpServletRequest req = new MockHttpServletRequest();
    assertTrue(HTMLHelper.isParametersEmpty(req, parameterName1));
    assertTrue(HTMLHelper.isParametersEmpty(req, parameterName1, parameterName2));

    //one param empty string
    req.setParameter(parameterName1, "");
    assertTrue(HTMLHelper.isParametersEmpty(req, parameterName1));
    assertTrue(HTMLHelper.isParametersEmpty(req, parameterName1, parameterName2));
    req.removeAllParameters();/*from ww  w . j a va  2  s.c o  m*/

    //one empty, one not
    req.setParameter(parameterName1, "value1");
    assertFalse(HTMLHelper.isParametersEmpty(req, parameterName1));
    assertTrue(HTMLHelper.isParametersEmpty(req, parameterName1, parameterName2));
    req.removeAllParameters();

    //all not empty
    req.setParameter(parameterName1, "value1");
    req.setParameter(parameterName2, "value2");
    assertFalse(HTMLHelper.isParametersEmpty(req, parameterName1));
    assertFalse(HTMLHelper.isParametersEmpty(req, parameterName1, parameterName2));

}

From source file:localdomain.localhost.MyServletTest.java

@Ignore
@Test/*from ww w .ja  va 2 s  . co m*/
public void testService() throws Exception {
    InputStream credentials = Thread.currentThread().getContextClassLoader()
            .getResourceAsStream("credentials.properties");
    if (credentials == null) {
        System.out.println("No credentials found, use default ones");
    } else {
        Properties props = new Properties();
        props.load(credentials);
        System.setProperties(props);
    }

    MockHttpServletRequest req = new MockHttpServletRequest();
    MockHttpServletResponse resp = new MockHttpServletResponse();
    MyServlet myServlet = new MyServlet();
    myServlet.service(req, resp);

    System.out.println(resp.getContentAsString());

}

From source file:io.jmnarloch.spring.request.correlation.support.RequestCorrelationUtilsTest.java

@Before
public void setUp() throws Exception {

    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(new MockHttpServletRequest()));
}

From source file:ar.com.zauber.commons.spring.web.controllers.ExceptionControllerTest.java

/** test */
@Before/*  ww w  .  j  a v  a 2  s. c om*/
public final void before() {
    res = new MockHttpServletResponse();
    req = new MockHttpServletRequest();

    final Map<Integer, String> map = new HashMap<Integer, String>();
    map.put(404, "exceptions/notfound");
    map.put(500, "exceptions/internalerror");

    exceptionController = new ExceptionController(map, "exceptions/default");
}

From source file:com.google.api.server.spi.config.model.StandardParametersTest.java

@Test
public void shouldPrettyPrint_true() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setParameter("prettyPrint", "true");
    assertThat(StandardParameters.shouldPrettyPrint(request)).isTrue();
}

From source file:org.mifos.ui.core.controller.RedirectionControllerTest.java

public void testHandleRequest() throws ServletException, IOException {

    String expectedPageToRedirectTo = "foopage";
    RedirectionController controller = new RedirectionController();
    controller.setViewToRedirectTo(expectedPageToRedirectTo);
    MockHttpServletRequest mockRequest = new MockHttpServletRequest();
    MockHttpServletResponse mockResponse = new MockHttpServletResponse();
    ModelAndView modelAndView = controller.handleRequest(mockRequest, mockResponse);

    Assert.assertEquals(expectedPageToRedirectTo, modelAndView.getViewName());
    Assert.assertNotNull(modelAndView.getModel());
    Map<String, Object> modelMap = (Map<String, Object>) modelAndView.getModel().get("model");
    Object response = modelMap.get("response");
    Assert.assertNotNull(response);/*from  ww w  . j  a  va  2 s. c o  m*/
    Assert.assertEquals(MockHttpServletResponse.class, response.getClass());
}

From source file:org.openmrs.contrib.metadatarepository.webapp.filter.LocaleFilterTest.java

public void testSetLocaleInSessionWhenSessionIsNull() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter("locale", "es");

    MockHttpServletResponse response = new MockHttpServletResponse();
    filter.doFilter(request, response, new MockFilterChain());

    // no session, should result in null
    assertNull(request.getSession().getAttribute(Constants.PREFERRED_LOCALE_KEY));
    // thread locale should always have it, regardless of session
    assertNotNull(LocaleContextHolder.getLocale());
}