Example usage for org.springframework.mock.web MockHttpServletResponse getHeader

List of usage examples for org.springframework.mock.web MockHttpServletResponse getHeader

Introduction

In this page you can find the example usage for org.springframework.mock.web MockHttpServletResponse getHeader.

Prototype

@Override
@Nullable
public String getHeader(String name) 

Source Link

Document

Return the primary value for the given header as a String, if any.

Usage

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

private Integer putZip(String path) throws Exception {
    File file = new File(path);
    InputStream stream;/*from  w w  w  .  j  a v a2  s  .  co  m*/
    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 w  ww  . j  av  a2s . c  om
        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  w w w. jav  a2 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());
}

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

@Test
public void testPostMultiPartFormData() 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);
    assertNull(context.getData());//from   ww  w.ja  va  2s .co  m
    assertTrue(context.getTasks().isEmpty());

    File dir = unpack("shape/archsites_epsg_prj.zip");

    Part[] parts = new Part[] { new FilePart("archsites.shp", new File(dir, "archsites.shp")),
            new FilePart("archsites.dbf", new File(dir, "archsites.dbf")),
            new FilePart("archsites.shx", new File(dir, "archsites.shx")),
            new FilePart("archsites.prj", new File(dir, "archsites.prj")) };

    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());
    assertNull(context.getData());
    assertEquals(1, context.getTasks().size());

    ImportTask task = context.getTasks().get(0);
    assertTrue(task.getData() instanceof SpatialFile);
    assertEquals(ImportTask.State.READY, task.getState());
}

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

private ImportContext uploadGeotiffAndVerify(String taskName, InputStream geotiffResourceStream,
        String contentType, String createImportBody, String creationContentType) throws Exception {
    // upload  tif or zip file containing a tif and verify the results
    MockHttpServletResponse resp = postAsServletResponse(RestBaseController.ROOT_PATH + "/imports",
            createImportBody, creationContentType);
    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/" + taskName);
    req.setContentType(contentType);//from  w w  w  .ja  va 2s.  c  o  m
    req.addHeader("Content-Type", contentType);
    req.setMethod("PUT");
    req.setContent(org.apache.commons.io.IOUtils.toByteArray(geotiffResourceStream));
    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);
    assertEquals(ImportTask.State.READY, task.getState());

    ImportData importData = task.getData();
    assertTrue(importData instanceof SpatialFile);

    DataFormat format = importData.getFormat();
    assertTrue(format instanceof GridFormat);

    return context;
}

From source file:org.geoserver.opensearch.rest.CollectionLayerTest.java

@Before
public void setupTestCollectionAndProduct() throws IOException, Exception {
    // create the collection
    createTest123Collection();/*from   w  w w. ja  v a 2 s. c  om*/

    // create the product
    MockHttpServletResponse response = postAsServletResponse("rest/oseo/collections/TEST123/products",
            getTestData("/test123-product.json"), MediaType.APPLICATION_JSON_VALUE);
    assertEquals(201, response.getStatus());
    assertEquals("http://localhost:8080/geoserver/rest/oseo/collections/TEST123/products/TEST123_P1",
            response.getHeader("location"));

    // setup the base granule location
    File file = new File("./src/test/resources");
    resourceBase = file.getCanonicalFile().getAbsolutePath();
}

From source file:org.geoserver.opensearch.rest.CollectionsControllerTest.java

private void testCreateCollectionAsZip(Set<CollectionPart> parts) throws Exception {
    LOGGER.info("Testing: " + parts);
    byte[] zip = null;
    try (final ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ZipOutputStream zos = new ZipOutputStream(bos)) {
        for (CollectionPart part : parts) {
            String resource, name;
            switch (part) {
            case Collection:
                resource = "/collection.json";
                name = "collection.json";
                break;
            case Description:
                resource = "/test123-description.html";
                name = "description.html";
                break;
            case Metadata:
                resource = "/test123-metadata.xml";
                name = "metadata.xml";
                break;
            case OwsLinks:
                resource = "/test123-links.json";
                name = "owsLinks.json";
                break;
            default:
                throw new RuntimeException("Unexpected part " + part);
            }/*from  w  ww . ja va 2  s.c  o m*/

            ZipEntry entry = new ZipEntry(name);
            zos.putNextEntry(entry);
            IOUtils.copy(getClass().getResourceAsStream(resource), zos);
            zos.closeEntry();
        }
        zip = bos.toByteArray();
    }

    MockHttpServletResponse response = postAsServletResponse("rest/oseo/collections", zip,
            MediaTypeExtensions.APPLICATION_ZIP_VALUE);
    if (parts.contains(CollectionPart.Collection)) {
        assertEquals(201, response.getStatus());
        assertEquals("http://localhost:8080/geoserver/rest/oseo/collections/TEST123",
                response.getHeader("location"));

        assertTest123CollectionCreated();
    } else {
        assertEquals(400, response.getStatus());
        assertThat(response.getContentAsString(), containsString("collection.json"));
        // failed, nothing else to check
        return;
    }

    if (parts.contains(CollectionPart.Description)) {
        assertTest123Description();
    }
    if (parts.contains(CollectionPart.Metadata)) {
        assertTest123Metadata();
    }
    if (parts.contains(CollectionPart.OwsLinks)) {
        assertTest123Links();
    }

}

From source file:org.geoserver.opensearch.rest.OSEORestTestSupport.java

protected void createTest123Collection() throws Exception, IOException {
    // create the collection
    MockHttpServletResponse response = postAsServletResponse("rest/oseo/collections",
            getTestData("/collection.json"), MediaType.APPLICATION_JSON_VALUE);
    assertEquals(201, response.getStatus());
    assertEquals("http://localhost:8080/geoserver/rest/oseo/collections/TEST123",
            response.getHeader("location"));
}

From source file:org.geoserver.opensearch.rest.ProductsControllerTest.java

@Test
public void testCreateProduct() throws Exception {
    MockHttpServletResponse response = postAsServletResponse("rest/oseo/collections/SENTINEL2/products",
            getTestData("/product.json"), MediaType.APPLICATION_JSON_VALUE);
    assertEquals(201, response.getStatus());
    assertEquals(/*from   w w  w .  j  av a  2 s  .com*/
            "http://localhost:8080/geoserver/rest/oseo/collections/SENTINEL2/products/S2A_OPER_MSI_L1C_TL_SGS__20180101T000000_A006640_T32TPP_N02.04",
            response.getHeader("location"));

    // check it's really there
    assertProductCreated();
}

From source file:org.geoserver.opensearch.rest.ProductsControllerTest.java

@Test
public void testUpdateProduct() throws Exception {
    // create the product
    MockHttpServletResponse response = postAsServletResponse("rest/oseo/collections/SENTINEL2/products",
            getTestData("/product.json"), MediaType.APPLICATION_JSON_VALUE);
    assertEquals(201, response.getStatus());
    assertEquals(//from   w  w w. ja v  a 2s . c  om
            "http://localhost:8080/geoserver/rest/oseo/collections/SENTINEL2/products/S2A_OPER_MSI_L1C_TL_SGS__20180101T000000_A006640_T32TPP_N02.04",
            response.getHeader("location"));

    // grab the JSON to modify some bits
    JSONObject feature = (JSONObject) getAsJSON(
            "rest/oseo/collections/SENTINEL2/products/S2A_OPER_MSI_L1C_TL_SGS__20180101T000000_A006640_T32TPP_N02.04");
    JSONObject properties = feature.getJSONObject("properties");
    properties.element("eop:orbitNumber", 66);
    properties.element("timeStart", "2017-01-01T00:00:00Z");

    // send it back
    response = putAsServletResponse(
            "rest/oseo/collections/SENTINEL2/products/S2A_OPER_MSI_L1C_TL_SGS__20180101T000000_A006640_T32TPP_N02.04",
            feature.toString(), "application/json");
    assertEquals(200, response.getStatus());

    // check the changes
    DocumentContext json = getAsJSONPath(
            "rest/oseo/collections/SENTINEL2/products/S2A_OPER_MSI_L1C_TL_SGS__20180101T000000_A006640_T32TPP_N02.04",
            200);
    assertEquals(Integer.valueOf(66), json.read("$.properties['eop:orbitNumber']"));
    assertEquals("2017-01-01T00:00:00.000+0000", json.read("$.properties['timeStart']"));
}