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.openmrs1_8.PersonAttributeTypeController1_8Test.java

/**
 * @see PersonAttributeTypeController#createPersonAttributeType(SimpleObject,WebRequest)
 * @throws Exception /*from   w  w  w . ja  v a2s  .  c om*/
 * @verifies create a new PersonAttributeType
 */
@Test
public void createPersonAttributeType_shouldCreateANewPersonAttributeType() throws Exception {

    long originalCount = getAllCount();

    SimpleObject obj = new SimpleObject();
    obj.add("name", "Some attributeType");
    obj.add("description", "Attribute Type for test");
    obj.add("format", "java.lang.String");
    obj.add("searchable", "false");

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

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

    SimpleObject newPersonAttributeType = deserialize(handle(req));

    Util.log("Created person attribute type", newPersonAttributeType);
    Assert.assertEquals(originalCount + 1, getAllCount());
}

From source file:cc.redpen.server.api.RedPenResourceTest.java

public void testJSValidatorDoesntRunFromNonHomeDir() throws Exception {
    System.setProperty("REDPEN_HOME", ".");
    MockHttpServletRequest request = constructMockRequest("POST", "/document/validate/json", WILDCARD,
            APPLICATION_JSON);/*from  w w w  .ja  v a 2  s .c  o  m*/
    request.setContent(String.format(
            "{\"document\":\"Test, this is a test.\",\"format\":\"json2\",\"documentParser\":\"PLAIN\",\"config\":{\"lang\":\"en\",\"validators\":{\"JavaScript\":{\"properties\":{\"script-path\":\"%s\"}}}}}",
            "resources/js").getBytes());
    MockHttpServletResponse response = invoke(request);

    assertEquals("HTTP status", HttpStatus.OK.getCode(), response.getStatus());
    JSONArray errors = new JSONObject(response.getContentAsString()).getJSONArray("errors");
    assertEquals(0, errors.length());
}

From source file:cc.redpen.server.api.RedPenResourceTest.java

public void testJSValidatorRuns() throws Exception {
    if (new File("redpen-server").exists()) {
        System.setProperty("REDPEN_HOME", "redpen-server/src/test");
    } else {/*from w ww .j a va2 s.  c o  m*/
        System.setProperty("REDPEN_HOME", "src/test");
    }
    MockHttpServletRequest request = constructMockRequest("POST", "/document/validate/json", WILDCARD,
            APPLICATION_JSON);
    request.setContent(String.format(
            "{\"document\":\"Test, this is a test.\",\"format\":\"json2\",\"documentParser\":\"PLAIN\",\"config\":{\"lang\":\"en\",\"validators\":{\"JavaScript\":{\"properties\":{\"script-path\":\"%s\"}}}}}",
            "resources/js").getBytes());
    MockHttpServletResponse response = invoke(request);

    assertEquals("HTTP status", HttpStatus.OK.getCode(), response.getStatus());
    JSONArray errors = new JSONObject(response.getContentAsString()).getJSONArray("errors");
    assertTrue(errors.length() > 0);
    for (int i = 0; i < errors.length(); ++i) {
        JSONObject o = errors.getJSONObject(i).getJSONArray("errors").getJSONObject(0);
        assertEquals("[pass.js] called", o.getString("message"));
    }
}

From source file:org.floggy.synchronization.jme.server.SynchronizationServletTest.java

/**
* DOCUMENT ME!/*from w  w w .  j  a  v a 2  s  .  c  o  m*/
*
* @throws Exception DOCUMENT ME!
*/
@Test
public void testDoGetServletRequestServletResponseReceive() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();

    SynchronizationServlet servlet = new SynchronizationServlet();

    request.setContent(getContent(Person.class));

    servlet.doGet(request, response);

    JSONObject jsonObject = JSONObject.fromObject(response.getContentAsString());

    Person person = (Person) JSONObject.toBean(jsonObject, Person.class);

    Assert.assertEquals(23, person.getAge());
    Assert.assertEquals("Floggy", person.getName());
}

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

/**
 * @see PersonAttributeTypeController#updatePersonAttributeType(PersonAttributeType,SimpleObject,WebRequest)
 * @throws Exception //w  w  w .java 2s .  co m
 * @verifies change a property on a person
 */
@Test
public void updatePersonAttributeType_shouldChangeAPropertyOnAPersonAttributeType() throws Exception {

    final String newDescription = "Updated description";

    PersonAttributeType obj = service.getPersonAttributeTypeByUuid(getUuid());
    Assert.assertNotNull(obj);
    Assert.assertFalse(newDescription.equals(obj.getDescription()));
    Util.log("Old PersonAttributeType Description: ", obj.getDescription());

    String json = "{\"description\":\"Updated description\"}";
    MockHttpServletRequest req = request(RequestMethod.POST, getURI() + "/" + getUuid());
    req.setContent(json.getBytes());
    handle(req);

    PersonAttributeType editedAttr = service.getPersonAttributeTypeByUuid(getUuid());
    Assert.assertNotNull(editedAttr);
    Assert.assertEquals(newDescription, editedAttr.getDescription());
    Util.log("Edited PersonAttributeType Description: ", editedAttr.getDescription());
}

From source file:org.floggy.synchronization.jme.server.SynchronizationServletTest.java

/**
* DOCUMENT ME!/*from   w ww  .j  a  v  a  2s.  co m*/
*/
@Test
public void testDoGetServletRequestServletResponseException() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();

    SynchronizationServlet servlet = new SynchronizationServlet();

    try {
        request.setContent(getContent(Person.class));

        servlet.doGet(request, response);
    } catch (Exception e) {
        Assert.assertEquals(e.getCause().getClass(), SynchronizationException.class);
    }
}

From source file:edu.internet2.middleware.shibboleth.idp.system.conf1.SAML1ArtifactResolutionTest.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("/saml1/SOAP/ArtifactResolution");
    servletRequest.setContent(soapMessage.getBytes());

    MockHttpServletResponse servletResponse = new MockHttpServletResponse();

    ProfileHandlerManager handlerManager = (ProfileHandlerManager) getApplicationContext()
            .getBean("shibboleth.HandlerManager");
    ProfileHandler handler = handlerManager.getProfileHandler(servletRequest);
    assertNotNull(handler);//  www . j a  v  a2  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("saml1:Assertion"));
}

From source file:edu.internet2.middleware.shibboleth.idp.system.conf1.SAML1ArtifactResolutionTest.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("/saml1/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   ww w.  j a  va2  s. c  om

    // 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("saml1p:RequestDenied"));
}

From source file:com.cognitivabrasil.repositorio.web.FileControllerTest.java

@Test
public void testUploadFileErroNotMultipart() throws IOException, ServletException, FileUploadException {
    HttpServletResponse response = new MockHttpServletResponse();

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setCharacterEncoding("text/plain");
    request.setContent("algo".getBytes());
    request.setMethod("POST");

    FileController fileController = mockFiles();

    String result = fileController.upload(request, response);

    assertThat(result, equalTo(//from www. j  a  va  2 s  .  co  m
            "{\"jsonrpc\" : \"2.0\", \"error\" : {\"code\": 101, \"message\": \"Falha ao abrir o input stream.\"}, \"id\" : \"id\"}"));

}

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  www. j  a v a2  s . 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", "test user");
    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());
}