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(@Nullable String method, @Nullable String requestURI) 

Source Link

Document

Create a new MockHttpServletRequest with a default MockServletContext .

Usage

From source file:org.openmrs.web.controller.PortletControllerTest.java

/**
 * Convenience method to get the "model" from the controller's handleRequest method
 * //  w  w w. ja  v  a2  s  . c o  m
 * @param patientId the patient id to fetch
 * @return the Map from string to object of everything in the generated "model"
 * @throws Exception
 */
private Map<String, Object> getModelFromController(Integer patientId) throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "");
    HttpServletResponse response = new MockHttpServletResponse();

    request.setAttribute(WebConstants.INIT_REQ_UNIQUE_ID, "1");
    request.getSession().setAttribute(WebConstants.OPENMRS_PORTLET_LAST_REQ_ID, "0");
    request.setAttribute("javax.servlet.include.servlet_path", "testPortlet");
    request.setAttribute("org.openmrs.portlet.parameters", new HashMap());
    request.setAttribute("org.openmrs.portlet.patientId", patientId);

    ModelAndView modelAndView = new PortletController().handleRequest(request, response);

    return (Map<String, Object>) modelAndView.getModel().get("model");
}

From source file:org.apache.amber.oauth2.ext.dynamicreg.server.request.OAuthServerRegistrationRequestTest.java

@Test
public void testValidOAuthPushRequest() throws Exception {
    final String validJson = FileUtils.readTextFileAsString("json/push_valid.json");

    MockHttpServletRequest request = new MockHttpServletRequest("POST", "/oauth/register");
    request.setContentType(OAuth.ContentType.JSON);
    request.setContent(validJson.getBytes("UTF-8"));

    final JSONHttpServletRequestWrapper jsonWrapper = new JSONHttpServletRequestWrapper(request);
    OAuthServerRegistrationRequest registrationRequest = new OAuthServerRegistrationRequest(jsonWrapper);

    Assert.assertEquals("Uploading and also editing capabilities!", registrationRequest.getClientDescription());
    Assert.assertEquals("http://onlinephotogallery.com/icon.png", registrationRequest.getClientIcon());
    Assert.assertEquals("Online Photo Gallery", registrationRequest.getClientName());
    Assert.assertEquals("https://onlinephotogallery.com/client_reg", registrationRequest.getRedirectURI());
    Assert.assertEquals("push", registrationRequest.getType());
    Assert.assertEquals("http://onlinephotogallery.com", registrationRequest.getClientUrl());
}

From source file:ru.tsystems.project.BuyTicketTest.java

@Before
public void setUp() throws ParseException {
    httpRequest = new MockHttpServletRequest("POST", "/contexts");
    httpRequest.getSession().setAttribute("cityFrom", "Saint-Peterburg");
    httpRequest.getSession().setAttribute("cityTo", "Moscow");
    httpRequest.setParameter("firstName", "Ivan");
    httpRequest.setParameter("lastName", "Ivanov");
    httpRequest.setParameter("birthday", "1976-03-16");
    httpRequest.setParameter("index", "0");

    pass = new Passenger();
    pass.setFirstName("Ivan");
    pass.setLastName("Ivanov");
    DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    Date birthday = format.parse("1976-03-16");
    pass.setBirthday(birthday);//ww  w .j  a  v a2s.co  m

    list = new ArrayList<>();
    routeEntity = new RouteEntity();
    routeEntity.setRouteEntity_id(1);
    list.add(routeEntity);
    route = new Route();
    route.setRouteId(1);
    routeEntity.setRoute(route);
    httpRequest.getSession().setAttribute("tickets", list);
}

From source file:ar.com.zauber.commons.web.proxy.impl.RegexURLRequestMapperTest.java

/**  @throws MalformedURLException */
public final void testDir() throws MalformedURLException {
    final URLRequestMapper r = new RegexURLRequestMapper(
            new InmutableURLRequestMapper(
                    new InmutableURLResult(new URL("http://localhost:9095/nexus/content/repositories/"))),
            Pattern.compile("^/([^/]+)/([^/]+)/([^/]+)/(.*)$"), "$1-$2-$3/$4");

    final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/zauber/code/releases/foo/hola");
    assertEquals(/*from   w w w  .j  a v  a  2  s .co m*/
            new URL("http://localhost:9095/nexus/content/repositories/" + "zauber-code-releases/" + "foo/hola"),
            r.getProxiedURLFromRequest(request).getURL());
}

From source file:org.openmrs.web.controller.observation.ObsFormControllerTest.java

/**
 * Tests that an "encounterId" parameter sets the obs.encounter attribute on an empty obs
 * //from  w  w w  .  ja v a 2 s . co m
 * @throws Exception
 */
@Test
public void shouldGetObsFormWithEncounterFilledIn() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "");
    request.setParameter("encounterId", "3");

    HttpServletResponse response = new MockHttpServletResponse();

    ObsFormController controller = new ObsFormController();

    ModelAndView modelAndView = controller.handleRequest(request, response);

    // make sure there is an "encounterId" element on the obs
    Obs commandObs = (Obs) modelAndView.getModel().get("command");
    Assert.assertNotNull(commandObs.getEncounter());

}

From source file:org.openmrs.web.controller.GlobalPropertyPortletControllerTest.java

/**
 * @see GlobalPropertyPortletController#populateModel(HttpServletRequest,Map)
 *///from   w  w w .  j  a  va2 s .c  om
@SuppressWarnings("unchecked")
@Test
@Verifies(value = "should exclude multiple prefixes", method = "populateModel(HttpServletRequest,Map<String,Object>)")
public void populateModel_shouldExcludeMultiplePrefixes() throws Exception {
    //given
    GlobalPropertyPortletController portletController = new GlobalPropertyPortletController();
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "");
    Map<String, Object> model = new HashMap<String, Object>();

    //when
    String excludePrefix = "file.started;file.mandatory";
    model.put("excludePrefix", excludePrefix);
    GlobalProperty[] globalProperties = { new GlobalProperty("file.started", ""),
            new GlobalProperty("file.mandatory", ""), new GlobalProperty("file.other", "") };
    Context.getAdministrationService().saveGlobalProperties(Arrays.asList(globalProperties));

    //then
    portletController.populateModel(request, model);
    List<GlobalProperty> properties = (List<GlobalProperty>) model.get("properties");
    Assert.assertFalse(properties.contains(globalProperties[0]));
    Assert.assertFalse(properties.contains(globalProperties[1]));
    Assert.assertTrue(properties.contains(globalProperties[2]));
}

From source file:net.eusashead.hateoas.response.impl.OptionsResponseBuilderImplTest.java

@Before
public void before() {
    request = new MockHttpServletRequest("OPTIONS", "http://localhost/resource/1");
    builder = new OptionsResponseBuilderImpl<Entity>(request);
}

From source file:net.eusashead.hateoas.response.impl.GetResponseBuilderImplTest.java

@Before
public void before() {
    request = new MockHttpServletRequest("GET", "http://localhost/resource/1");
    builder = new EntityResponseBuilderImpl<Entity>(request);
}

From source file:de.otto.jsonhome.controller.HtmlControllerTest.java

@Test
public void testGetRel() throws Exception {
    // given/*from   ww w  . j a v  a2  s  .  c om*/
    final HtmlController controller = relController(
            ControllerWithRequestMappingAndLinkRelationTypeAtClassLevel.class);
    // when
    final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/rel/foo");
    request.setServerName("rel.example.org");
    request.setScheme("http");
    final ModelAndView resourcesMap = controller.getRelationshipType(request);
    // then
    assertEquals(resourcesMap.getViewName(), "directresource");
    assertNotNull(resourcesMap.getModel().get("resource"));
    @SuppressWarnings("unchecked")
    final DirectLink model = (DirectLink) resourcesMap.getModel().get("resource");
    assertEquals(model.getHref(), URI.create("http://app.example.org/bar"));
}

From source file:com.google.api.server.spi.handlers.CorsHandlerTest.java

@Before
public void setUp() {
    request = new MockHttpServletRequest("OPTIONS", "http://test.com/some/path");
    response = new MockHttpServletResponse();
}