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

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

Introduction

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

Prototype

public void setMethod(@Nullable String method) 

Source Link

Usage

From source file:org.cloudifysource.rest.ControllerTest.java

private MockHttpServletRequest createMockGetRequest(final String requestUri) {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRequestURI(requestUri);//from  w w  w .  j  a va2s.  com
    request.setMethod("GET");
    request.setContentType(MediaType.APPLICATION_JSON);

    return request;
}

From source file:org.cloudifysource.rest.ControllerTest.java

private MockHttpServletRequest createMockPostRequest(final String requestUri, final String contentAsJson) {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRequestURI(requestUri);//from  w  ww.j  a va2  s  .com
    request.setMethod("POST");
    request.setContentType(MediaType.APPLICATION_JSON);

    if (StringUtils.isNotBlank(contentAsJson)) {
        request.setContent(contentAsJson.getBytes());
    }

    return request;
}

From source file:org.cloudifysource.rest.ControllerTest.java

private MockHttpServletRequest createMockDeleteRequest(final String requestUri) {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRequestURI(requestUri);//from  ww  w  . j  a va2  s .co m
    request.setMethod("DELETE");
    request.setContentType(MediaType.APPLICATION_JSON);

    return request;
}

From source file:org.finra.dm.ui.RequestLoggingFilterTest.java

private MockHttpServletRequest createServletRequest() {
    MockHttpServletRequest request = new MockHttpServletRequest(null, "/test");
    request.setQueryString("param=value");
    request.setMethod("POST");
    MockHttpSession session = new MockHttpSession();
    request.setContent(PAYLOAD_CONTENT.getBytes());
    request.setSession(session);/*from  w  ww. j a v  a2  s.com*/
    request.setRemoteUser("Test Remote User");
    return request;
}

From source file:org.geogig.geoserver.functional.GeoServerTestSupport.java

/**
 * Issue a POST request to the provided URL with the given content.
 *
 * @param contentType the content type of the data
 * @param resourceUri the url to issue the request to
 * @param postContent the content to be posted
 *
 * @return the response to the request//from  ww w  .  j ava2 s  .  com
 */
public MockHttpServletResponse postContent(String contentType, String resourceUri, String postContent)
        throws Exception {

    MockHttpServletRequest req = createRequest(resourceUri);

    req.setContentType(contentType);
    req.addHeader("Content-Type", contentType);
    req.setMethod("POST");
    req.setContent(postContent == null ? null : postContent.getBytes());

    return dispatch(req);
}

From source file:org.geogig.geoserver.functional.GeoServerTestSupport.java

/**
 * Issue a request with the given {@link HttpMethod} to the provided resource URI.
 *
 * @param method the http method to use//from w w w  . ja v  a 2s . c om
 * @param resourceUri the uri to issue the request to
 *
 * @return the response to the request
 */
public MockHttpServletResponse callInternal(HttpMethod method, String resourceUri) throws Exception {
    MockHttpServletRequest request = super.createRequest(resourceUri);
    request.setMethod(method.name());

    return dispatch(request, null);

}

From source file:org.geogig.geoserver.functional.GeoServerTestSupport.java

public MockHttpServletResponse callWithContentTypeInternal(HttpMethod method, String resourceUri,
        String payload, String contentType) throws Exception {
    MockHttpServletRequest request = super.createRequest(resourceUri);
    request.setMethod(method.name());
    // set the JSON payload
    request.setContent(payload.getBytes());
    request.setContentType(contentType);

    return dispatch(request, null);
}

From source file:org.geoserver.importer.rest.ImportTaskControllerTest.java

private Integer putZip(String path) throws Exception {
    File file = new File(path);
    InputStream stream;/*  w ww . j  av  a2 s  .  com*/
    if (file.exists()) {
        stream = new FileInputStream(file);
    } else {
        stream = ImporterTestSupport.class.getResourceAsStream("../test-data/" + path);
    }
    MockHttpServletResponse resp = postAsServletResponse(RestBaseController.ROOT_PATH + "/imports", "");
    assertEquals(201, resp.getStatus());
    assertNotNull(resp.getHeader("Location"));

    String[] split = resp.getHeader("Location").split("/");
    Integer id = Integer.parseInt(split[split.length - 1]);
    ImportContext context = importer.getContext(id);

    MockHttpServletRequest req = createRequest(
            RestBaseController.ROOT_PATH + "/imports/" + id + "/tasks/" + file.getName());
    req.setContentType("application/zip");
    req.addHeader("Content-Type", "application/zip");
    req.setMethod("PUT");
    req.setContent(org.apache.commons.io.IOUtils.toByteArray(stream));
    resp = dispatch(req);

    assertEquals(201, resp.getStatus());

    context = importer.getContext(context.getId());
    assertNull(context.getData());
    assertEquals(1, context.getTasks().size());

    ImportTask task = context.getTasks().get(0);
    assertTrue(task.getData() instanceof SpatialFile);

    return id;
}

From source file:org.geoserver.importer.rest.ImportTaskControllerTest.java

private Integer putZipAsURL(String zip) throws Exception {
    MockHttpServletResponse resp = postAsServletResponse(RestBaseController.ROOT_PATH + "/imports", "");
    assertEquals(201, resp.getStatus());
    assertNotNull(resp.getHeader("Location"));

    String[] split = resp.getHeader("Location").split("/");
    Integer id = Integer.parseInt(split[split.length - 1]);
    ImportContext context = importer.getContext(id);

    MockHttpServletRequest req = createRequest(RestBaseController.ROOT_PATH + "/imports/" + id + "/tasks/");
    MultiValueMap<String, Object> form = new LinkedMultiValueMap<String, Object>(1);
    form.add("url", new File(zip).getAbsoluteFile().toURI().toString());
    final ByteArrayOutputStream stream = new ByteArrayOutputStream();
    final HttpHeaders headers = new HttpHeaders();
    new FormHttpMessageConverter().write(form, MediaType.APPLICATION_FORM_URLENCODED, new HttpOutputMessage() {
        @Override//from  ww  w. ja v a2  s  .c  o m
        public OutputStream getBody() throws IOException {
            return stream;
        }

        @Override
        public HttpHeaders getHeaders() {
            return headers;
        }
    });
    req.setContent(stream.toByteArray());
    req.setMethod("POST");
    req.setContentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE);
    req.addHeader("Content-Type", MediaType.APPLICATION_FORM_URLENCODED_VALUE);
    resp = dispatch(req);

    assertEquals(201, resp.getStatus());

    context = importer.getContext(context.getId());
    assertNull(context.getData());
    assertEquals(1, context.getTasks().size());

    ImportTask task = context.getTasks().get(0);
    assertTrue(task.getData() instanceof SpatialFile);

    return id;
}

From source file:org.geoserver.importer.rest.ImportTaskControllerTest.java

@Test
public void testDeleteTask() throws Exception {
    MockHttpServletResponse resp = postAsServletResponse(RestBaseController.ROOT_PATH + "/imports", "");
    assertEquals(201, resp.getStatus());
    assertNotNull(resp.getHeader("Location"));

    String[] split = resp.getHeader("Location").split("/");
    Integer id = Integer.parseInt(split[split.length - 1]);

    ImportContext context = importer.getContext(id);

    File dir = unpack("shape/archsites_epsg_prj.zip");
    unpack("shape/bugsites_esri_prj.tar.gz", dir);

    new File(dir, "extra.file").createNewFile();
    File[] files = dir.listFiles();
    Part[] parts = new Part[files.length];
    for (int i = 0; i < files.length; i++) {
        parts[i] = new FilePart(files[i].getName(), files[i]);
    }/*from www  . ja  va 2 s. c  o  m*/

    MultipartRequestEntity multipart = new MultipartRequestEntity(parts, new PostMethod().getParams());

    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    multipart.writeRequest(bout);

    MockHttpServletRequest req = createRequest(RestBaseController.ROOT_PATH + "/imports/" + id + "/tasks");
    req.setContentType(multipart.getContentType());
    req.addHeader("Content-Type", multipart.getContentType());
    req.setMethod("POST");
    req.setContent(bout.toByteArray());
    resp = dispatch(req);

    context = importer.getContext(context.getId());
    assertEquals(2, context.getTasks().size());

    req = createRequest(RestBaseController.ROOT_PATH + "/imports/" + id + "/tasks/1");
    req.setMethod("DELETE");
    resp = dispatch(req);
    assertEquals(204, resp.getStatus());

    context = importer.getContext(context.getId());
    assertEquals(1, context.getTasks().size());
}