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.gwtwidgets.server.spring.test.TestRPCExporter.java

private HttpServletRequest getServiceRequest() throws Exception {
    MockHttpServletRequest serviceRequest = new MockHttpServletRequest("PUT", "/service");
    serviceRequest.setContentType("text/x-gwt-rpc");
    serviceRequest.setCharacterEncoding("UTF-8");
    serviceRequest.setContent(requestPayload.getBytes("UTF-8"));
    return serviceRequest;
}

From source file:com.nebhale.cyclinglibrary.web.GzipFilterTest.java

@Test
public void gzipRequest() throws ServletException, IOException {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader("Content-Encoding", "gzip");
    request.setContent(gzipContent("test-request-content"));

    MockHttpServletResponse response = new MockHttpServletResponse();
    MockFilterChain filterChain = new MockFilterChain();

    this.filter.doFilterInternal(request, response, filterChain);
    writeContent("test-response-content", filterChain.getResponse().getOutputStream());

    assertEquals("test-request-content", readContent(filterChain.getRequest().getInputStream()));
    assertEquals("test-response-content", response.getContentAsString());
}

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

/**
 * @see RoleController#create(SimpleObject, javax.servlet.http.HttpServletRequest, HttpServletResponse)
 * @verifies create a new Role/*from  w ww . j a  v  a2 s  .  c o m*/
 */
@Test
public void createRole_shouldCreateANewRole() throws Exception {

    long originalCount = getAllCount();

    SimpleObject location = new SimpleObject();
    location.add("name", "Role name");
    location.add("description", "Role description");

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

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

    SimpleObject newRole = deserialize(handle(req));

    Assert.assertNotNull(PropertyUtils.getProperty(newRole, "uuid"));
    Assert.assertEquals(originalCount + 1, getAllCount());

}

From source file:net.javacrumbs.smock.http.server.servlet.CommonServletBasedMockWebServiceClient.java

protected MockHttpServletRequest createRequest(String path, WebServiceMessage message) {
    String requestXml = serialize(getEnvelopeSource(message));
    MockHttpServletRequest request = new ExtendedMockHttpServletRequest();
    request.setMethod("POST");
    request.setPathInfo(path);/*from   w  ww.  ja  v a 2s  .  c  o  m*/
    request.setRequestURI(path);
    request.setContent(stringToBytes(requestXml));
    request.setContentType(CONTENT_TYPE);
    request.addHeader("Content-Type", CONTENT_TYPE);
    return request;
}

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

public MockHttpServletRequest newPostRequest(String requestURI, String content) {
    MockHttpServletRequest request = request(RequestMethod.POST, requestURI);
    try {//from   www .  jav a 2 s .c  om
        request.setContent(content.getBytes("UTF-8"));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return request;
}

From source file:org.geomajas.gwt.server.mvc.GeomajasControllerTest.java

@Test
public void testSerializationPolicy() throws UnsupportedEncodingException, ServletException {
    // create mock context that loads from the classpath
    MockServletConfig config = new MockServletConfig();
    MockHttpServletRequest request = new MockHttpServletRequest(config.getServletContext());
    request.setContentType("text/x-gwt-rpc");
    request.setCharacterEncoding("UTF-8");
    request.setContent(("6|0|10|http://apps.geomajas.org/explorer/be.geosparc.Explorer/"
            + "|54044FB0C988344F1715C8B91330B0A2|org.geomajas.gwt.client.GeomajasService|"
            + "execute|org.geomajas.gwt.client.command.GwtCommand/4093389776|command.configuration.GetMap|"
            + "org.geomajas.command.dto.GetMapConfigurationRequest/104733661|explorer|mainMap|"
            + "ss.TqRPfHFh24NVxB|1|2|3|4|1|5|5|6|7|8|9|0|10|").getBytes("UTF-8"));
    request.addHeader("X-GWT-Permutation", "54044FB0C988344F1715C8B91330B0A2");
    request.addHeader("X-GWT-Module-Base", "http://test/module/");
    MockHttpServletResponse response = new MockHttpServletResponse();
    customController.setServletConfig(config);
    customController.doPost(request, response);
    // expect the message that the type is missing from our policy file
    Assert.assertTrue(response.getContentAsString().contains(
            "Type 'org.geomajas.gwt.client.command.GwtCommand' was not included in the set of types"));
}

From source file:org.geomajas.gwt.server.mvc.GeomajasControllerTest.java

@Test
public void testMockWebContext() throws ServletException, IOException {
    // create mock context that loads from the classpath
    MockServletConfig config = new MockServletConfig();
    MockHttpServletRequest request = new MockHttpServletRequest(config.getServletContext());
    request.setContentType("text/x-gwt-rpc");
    request.setCharacterEncoding("UTF-8");
    request.setContent(("6|0|10|http://apps.geomajas.org/explorer/be.geosparc.Explorer/"
            + "|54044FB0C988344F1715C8B91330B0A2|org.geomajas.gwt.client.GeomajasService|"
            + "execute|org.geomajas.gwt.client.command.GwtCommand/4093389776|command.configuration.GetMap|"
            + "org.geomajas.command.dto.GetMapConfigurationRequest/104733661|explorer|mainMap|"
            + "ss.TqRPfHFh24NVxB|1|2|3|4|1|5|5|6|7|8|9|0|10|").getBytes("UTF-8"));
    request.addHeader("X-GWT-Permutation", "54044FB0C988344F1715C8B91330B0A2");
    request.addHeader("X-GWT-Module-Base", "http://test/module/");
    MockHttpServletResponse response = new MockHttpServletResponse();
    defaultController.setServletConfig(config);
    defaultController.doPost(request, response);
    // expect the message of the out-dated 1.3 policy of GWT
    Assert.assertTrue(response.getContentAsString()
            .contains("Type 'org.geomajas.gwt.client.command.GwtCommand' was not assignable"
                    + " to 'com.google.gwt.user.client.rpc.IsSerializable'"));
}

From source file:com.nebhale.cyclinglibrary.web.GzipFilterTest.java

@Test
public void gzipResponse() throws ServletException, IOException {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader("Accept-Encoding", "gzip");
    request.setContent("test-request-content".getBytes("UTF-8"));

    MockHttpServletResponse response = new MockHttpServletResponse();

    FilterChain filterChain = mock(FilterChain.class);
    doAnswer(new Answer<Void>() {

        @Override//from   w  ww.ja  va2s  . c  o  m
        public Void answer(InvocationOnMock invocation) throws Throwable {
            HttpServletRequest request = (HttpServletRequest) invocation.getArguments()[0];
            assertEquals("test-request-content", readContent(request.getInputStream()));

            HttpServletResponse response = (HttpServletResponse) invocation.getArguments()[1];
            writeContent("test-response-content", response.getOutputStream());
            return null;
        }
    }).when(filterChain).doFilter(any(ServletRequest.class), any(ServletResponse.class));

    this.filter.doFilterInternal(request, response, filterChain);

    assertEquals("test-response-content", gunzipContent(response.getContentAsByteArray()));
}

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

public MockHttpServletRequest newPutRequest(String requestURI, String content) {
    MockHttpServletRequest request = request(RequestMethod.PUT, requestURI);
    try {/*from   w ww.ja v a  2 s  . com*/
        request.setContent(content.getBytes("UTF-8"));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return request;
}

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

/**
 * @see RoleController#update(String, SimpleObject, javax.servlet.http.HttpServletRequest, HttpServletResponse)
 * @verifies change a property on a Role
 *///from   www  . j  a v a2 s  .c  om
@Test
public void updateRole_shouldChangeAPropertyOnARole() throws Exception {

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

    Role editedRole = service.getRoleByUuid(getUuid());
    Assert.assertNotNull(editedRole);
    Assert.assertEquals(editedDescription, editedRole.getDescription());

}