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

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

Introduction

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

Prototype

public void setContent(@Nullable byte[] content) 

Source Link

Document

Set the content of the request body as a byte array.

Usage

From source file:org.openmrs.module.webservices.rest.web.v1_0.controller.openmrs2_0.UserController2_0Test.java

/**
 * @see UserController#createUser(SimpleObject,WebRequest)
 * @throws Exception/*from  ww w .  j  a  v  a  2s.  c  o  m*/
 * @verifies create a new user
 */
@Test
public void createUser_shouldCreateANewUser() throws Exception {

    long originalCount = getAllCount();

    SimpleObject user = new SimpleObject();
    user.add("username", "testuser");
    user.add("password", "Secret123");
    user.add("person", "da7f524f-27ce-4bb2-86d6-6d1d05312bd5");

    String json = new ObjectMapper().writeValueAsString(user);

    MockHttpServletRequest req = request(RequestMethod.POST, getURI());
    req.setContent(json.getBytes());

    SimpleObject newUser = deserialize(handle(req));

    Util.log("Created User", newUser);
    Assert.assertNotNull(PropertyUtils.getProperty(newUser, "uuid"));
    Assert.assertEquals(originalCount + 1, getAllCount());
}

From source file:edu.internet2.middleware.shibboleth.idp.system.conf1.SAML1AttributeQueryTestCase.java

/** Tests that the attribute query handler correctly fails out if the profile is not configured. */
public void testAuthenticationWithoutConfiguredQuery() throws Exception {
    AttributeQuery query = buildAttributeQuery("urn:example.org:BogusSP");
    String soapMessage = getSOAPMessage(query);

    MockHttpServletRequest servletRequest = new MockHttpServletRequest();
    servletRequest.setMethod("POST");
    servletRequest.setPathInfo("/saml1/SOAP/AttributeQuery");
    servletRequest.setContent(soapMessage.getBytes());

    MockHttpServletResponse servletResponse = new MockHttpServletResponse();

    ProfileHandlerManager handlerManager = (ProfileHandlerManager) getApplicationContext()
            .getBean("shibboleth.HandlerManager");
    ProfileHandler handler = handlerManager.getProfileHandler(servletRequest);
    assertNotNull(handler);//from  w  w  w .j  av  a  2s.c  o m

    // Process request
    HTTPInTransport profileRequest = new HttpServletRequestAdapter(servletRequest);
    HTTPOutTransport profileResponse = new HttpServletResponseAdapter(servletResponse, false);
    handler.processRequest(profileRequest, profileResponse);

    String response = servletResponse.getContentAsString();
    assertTrue(response.contains("saml1p:Responder"));
    assertTrue(response.contains("saml1p:RequestDenied"));
}

From source file:edu.internet2.middleware.shibboleth.idp.system.conf1.SAML2AttributeQueryTestCase.java

/** Tests that the attribute query handler correctly fails out if the profile is not configured. */
public void testAuthenticationWithoutConfiguredQuery() throws Exception {
    AttributeQuery query = buildAttributeQuery("urn:example.org:BogusSP");
    String soapMessage = getSOAPMessage(query);

    MockHttpServletRequest servletRequest = new MockHttpServletRequest();
    servletRequest.setMethod("POST");
    servletRequest.setPathInfo("/saml2/SOAP/AttributeQuery");
    servletRequest.setContent(soapMessage.getBytes());

    MockHttpServletResponse servletResponse = new MockHttpServletResponse();

    ProfileHandlerManager handlerManager = (ProfileHandlerManager) getApplicationContext()
            .getBean("shibboleth.HandlerManager");
    ProfileHandler handler = handlerManager.getProfileHandler(servletRequest);
    assertNotNull(handler);/*from w ww.ja  v  a 2 s .  co  m*/

    // Process request
    HTTPInTransport profileRequest = new HttpServletRequestAdapter(servletRequest);
    HTTPOutTransport profileResponse = new HttpServletResponseAdapter(servletResponse, false);
    handler.processRequest(profileRequest, profileResponse);

    String response = servletResponse.getContentAsString();
    assertTrue(response.contains("urn:oasis:names:tc:SAML:2.0:status:Responder"));
    assertTrue(response.contains("urn:oasis:names:tc:SAML:2.0:status:RequestDenied"));
}

From source file:edu.internet2.middleware.shibboleth.idp.system.conf1.SAML1AttributeQueryTestCase.java

/** Tests that the attribute query handler correctly handles an incomming query. */
public void testAttributeQuery() throws Exception {
    AttributeQuery query = buildAttributeQuery("urn:example.org:sp1");
    String soapMessage = getSOAPMessage(query);

    MockHttpServletRequest servletRequest = new MockHttpServletRequest();
    servletRequest.setMethod("POST");
    servletRequest.setPathInfo("/saml1/SOAP/AttributeQuery");
    servletRequest.setContent(soapMessage.getBytes());

    MockHttpServletResponse servletResponse = new MockHttpServletResponse();

    ProfileHandlerManager handlerManager = (ProfileHandlerManager) getApplicationContext()
            .getBean("shibboleth.HandlerManager");
    ProfileHandler handler = handlerManager.getProfileHandler(servletRequest);
    assertNotNull(handler);//  w w  w.ja v  a  2 s .c  o  m

    // Process request
    HTTPInTransport profileRequest = new HttpServletRequestAdapter(servletRequest);
    HTTPOutTransport profileResponse = new HttpServletResponseAdapter(servletResponse, false);
    handler.processRequest(profileRequest, profileResponse);

    String response = servletResponse.getContentAsString();
    assertTrue(response.contains("saml1p:Success"));
    assertTrue(response.contains("AttributeName=\"urn:mace:dir:attribute-def:eduPersonEntitlement\""));
    assertTrue(response.contains("urn:example.org:entitlement:entitlement1"));
}

From source file:edu.internet2.middleware.shibboleth.idp.system.conf1.SAML2AttributeQueryTestCase.java

/** Tests that the attribute query handler correctly handles an incomming query. */
public void testAttributeQuery() throws Exception {
    AttributeQuery query = buildAttributeQuery("urn:example.org:sp1");
    String soapMessage = getSOAPMessage(query);

    MockHttpServletRequest servletRequest = new MockHttpServletRequest();
    servletRequest.setMethod("POST");
    servletRequest.setPathInfo("/saml2/SOAP/AttributeQuery");
    servletRequest.setContent(soapMessage.getBytes());

    MockHttpServletResponse servletResponse = new MockHttpServletResponse();

    ProfileHandlerManager handlerManager = (ProfileHandlerManager) getApplicationContext()
            .getBean("shibboleth.HandlerManager");
    ProfileHandler handler = handlerManager.getProfileHandler(servletRequest);
    assertNotNull(handler);/*from w  ww .  j av  a2s. com*/

    // Process request
    HTTPInTransport profileRequest = new HttpServletRequestAdapter(servletRequest);
    HTTPOutTransport profileResponse = new HttpServletResponseAdapter(servletResponse, false);
    handler.processRequest(profileRequest, profileResponse);

    String response = servletResponse.getContentAsString();
    assertTrue(response.contains("urn:oasis:names:tc:SAML:2.0:status:Success"));
    assertTrue(response.contains(" Name=\"urn:oid:1.3.6.1.4.1.5923.1.1.1.7\""));
    assertTrue(response.contains("urn:example.org:entitlement:entitlement1"));
}

From source file:org.openmrs.module.webservices.rest.web.v1_0.controller.openmrs1_8.UserController1_8Test.java

/**
 * @see UserController#createUser(SimpleObject,WebRequest)
 * @throws Exception /*from   w  ww.j  a v a 2s  . co m*/
 * @verifies create a new user
 */
@Test
public void createUser_shouldCreateANewUserWithRoles() throws Exception {

    long originalCount = getAllCount();

    SimpleObject user = new SimpleObject();
    user.add("username", "test user");
    user.add("password", "Secret123");
    user.add("person", "da7f524f-27ce-4bb2-86d6-6d1d05312bd5");
    user.add("roles", new String[] { "3480cb6d-c291-46c8-8d3a-96dc33d199fb" });

    String json = new ObjectMapper().writeValueAsString(user);

    MockHttpServletRequest req = request(RequestMethod.POST, getURI());
    req.setContent(json.getBytes());

    SimpleObject newUser = deserialize(handle(req));

    Util.log("Created another user with a role this time.", newUser);
    Assert.assertNotNull(PropertyUtils.getProperty(newUser, "uuid"));
    Assert.assertEquals(originalCount + 1, getAllCount());

    User createdUser = service.getUserByUuid(getUuid());
    Assert.assertNotNull(createdUser);
    Assert.assertTrue(createdUser.hasRole("Provider"));
}

From source file:org.openmrs.module.webservices.rest.web.v1_0.controller.openmrs2_0.UserController2_0Test.java

/**
 * @see UserController#createUser(SimpleObject,WebRequest)
 * @throws Exception/*from www. j  av a2 s . c  o  m*/
 * @verifies create a new user
 */
@Test
public void createUser_shouldCreateANewUserWithRoles() throws Exception {

    long originalCount = getAllCount();

    SimpleObject user = new SimpleObject();
    user.add("username", "testuser");
    user.add("password", "Secret123");
    user.add("person", "da7f524f-27ce-4bb2-86d6-6d1d05312bd5");
    user.add("roles", new String[] { "3480cb6d-c291-46c8-8d3a-96dc33d199fb" });

    String json = new ObjectMapper().writeValueAsString(user);

    MockHttpServletRequest req = request(RequestMethod.POST, getURI());
    req.setContent(json.getBytes());

    SimpleObject newUser = deserialize(handle(req));

    Util.log("Created another user with a role this time.", newUser);
    Assert.assertNotNull(PropertyUtils.getProperty(newUser, "uuid"));
    Assert.assertEquals(originalCount + 1, getAllCount());

    User createdUser = service.getUserByUuid(getUuid());
    Assert.assertNotNull(createdUser);
    Assert.assertTrue(createdUser.hasRole("Provider"));
}

From source file:edu.internet2.middleware.shibboleth.idp.system.conf1.SAML2ArtifactResolutionTest.java

public void testWithoutConfiguration() throws Exception {
    String relyingPartyId = "urn:example.org:BogusSP";
    SAMLArtifactMapEntry artifactEntry = stageArtifact(relyingPartyId);
    String soapMessage = buildRequestMessage(relyingPartyId, artifactEntry.getArtifact());

    MockHttpServletRequest servletRequest = new MockHttpServletRequest();
    servletRequest.setMethod("POST");
    servletRequest.setPathInfo("/saml2/SOAP/ArtifactResolution");
    servletRequest.setContent(soapMessage.getBytes());

    MockHttpServletResponse servletResponse = new MockHttpServletResponse();

    ProfileHandlerManager handlerManager = (ProfileHandlerManager) getApplicationContext()
            .getBean("shibboleth.HandlerManager");
    ProfileHandler handler = handlerManager.getProfileHandler(servletRequest);
    assertNotNull(handler);/*from www. jav a 2s. co m*/

    // Process request
    HTTPInTransport profileRequest = new HttpServletRequestAdapter(servletRequest);
    HTTPOutTransport profileResponse = new HttpServletResponseAdapter(servletResponse, false);
    handler.processRequest(profileRequest, profileResponse);
    String response = servletResponse.getContentAsString();
    assertTrue(response.contains("urn:oasis:names:tc:SAML:2.0:status:Success"));
    assertTrue(response.contains("urn:oasis:names:tc:SAML:2.0:status:RequestDenied"));
}

From source file:edu.internet2.middleware.shibboleth.idp.system.conf1.SAML2ArtifactResolutionTest.java

public void testArtifactResolution() throws Exception {
    String relyingPartyId = "urn:example.org:sp1";
    SAMLArtifactMapEntry artifactEntry = stageArtifact(relyingPartyId);
    String soapMessage = buildRequestMessage(relyingPartyId, artifactEntry.getArtifact());

    MockHttpServletRequest servletRequest = new MockHttpServletRequest();
    servletRequest.setMethod("POST");
    servletRequest.setPathInfo("/saml2/SOAP/ArtifactResolution");
    servletRequest.setContent(soapMessage.getBytes());

    MockHttpServletResponse servletResponse = new MockHttpServletResponse();

    ProfileHandlerManager handlerManager = (ProfileHandlerManager) getApplicationContext()
            .getBean("shibboleth.HandlerManager");
    ProfileHandler handler = handlerManager.getProfileHandler(servletRequest);
    assertNotNull(handler);/*from  w w  w  . j av a2  s.co m*/

    // Process request
    HTTPInTransport profileRequest = new HttpServletRequestAdapter(servletRequest);
    HTTPOutTransport profileResponse = new HttpServletResponseAdapter(servletResponse, false);
    handler.processRequest(profileRequest, profileResponse);

    String response = servletResponse.getContentAsString();
    assertTrue(response.contains("saml2p:ArtifactResponse"));
    assertTrue(response.contains("urn:oasis:names:tc:SAML:2.0:status:Success"));
    assertTrue(response.contains("saml2:Assertion"));
}

From source file:org.bahmni.module.bahmnicore.web.v1_0.search.BahmniMainResourceControllerTest.java

public MockHttpServletRequest newPostRequest(String requestURI, Object content) {
    MockHttpServletRequest request = request(RequestMethod.POST, requestURI);
    try {//  w  w w  .  j  ava2s .  c  o  m
        String json = new ObjectMapper().writeValueAsString(content);
        request.setContent(json.getBytes("UTF-8"));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return request;
}