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

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

Introduction

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

Prototype

public String getContentAsString() throws UnsupportedEncodingException 

Source Link

Document

Get the content of the response body as a String , using the charset specified for the response by the application, either through HttpServletResponse methods or through a charset parameter on the Content-Type .

Usage

From source file:org.geoserver.rest.catalog.StyleControllerTest.java

@Test
public void testPostExternalEntityAsSLD() throws Exception {
    String xml = IOUtils.toString(TestData.class.getResource("externalEntities.sld"), "UTF-8");

    MockHttpServletResponse response = postAsServletResponse(RestBaseController.ROOT_PATH + "/styles", xml,
            SLDHandler.MIMETYPE_10);//from w  ww. ja  v  a2 s .c  o  m
    assertEquals(500, response.getStatus());
    String message = response.getContentAsString();
    assertThat(message, containsString("Entity resolution disallowed"));
    assertThat(message, containsString("/this/file/does/not/exist"));
}

From source file:org.geoserver.rest.catalog.StyleControllerTest.java

@Test
public void testPostWithExternalEntities() throws Exception {
    URL zip = getClass().getResource("test-data/externalEntities.zip");
    byte[] bytes = FileUtils.readFileToByteArray(DataUtilities.urlToFile(zip));

    MockHttpServletResponse response = postAsServletResponse(
            RestBaseController.ROOT_PATH + "/workspaces/gs/styles", bytes, "application/zip");
    // expecting a failure with explanation
    assertEquals(400, response.getStatus());
    final String content = response.getContentAsString();
    assertThat(content, containsString("Entity resolution disallowed"));
    assertThat(content, containsString("/this/file/does/not/exist"));
}

From source file:org.geoserver.restupload.ResumableUploadTest.java

@Test
public void testUploadFull() throws Exception {
    String uploadId = sendPostRequest();
    File uploadedFile = getTempPath(uploadId);
    assertTrue(uploadedFile.exists());/*from  w  w w  . j  a  v  a  2  s  .co  m*/
    MockHttpServletRequest request = createRequest("/rest/resumableupload/" + uploadId);
    request.setMethod("PUT");
    request.setContentType("application/octet-stream");
    byte[] bigFile = generateFileAsBytes();
    request.setContent(bigFile);
    request.addHeader("Content-type", "application/octet-stream");
    request.addHeader("Content-Length", String.valueOf(bigFile.length));
    MockHttpServletResponse response = dispatch(request);
    assertEquals(Status.SUCCESS_OK.getCode(), response.getStatus());
    assertFalse(uploadedFile.exists());
    File destinationFile = new File(FilenameUtils.concat(root, fileName.replaceAll("^/", "")));
    assertTrue(destinationFile.exists());
    assertEquals(bigFile.length, destinationFile.length());
    // Check uploaded file byte by byte
    boolean checkBytes = Arrays.equals(bigFile, toBytes(new FileInputStream(destinationFile)));
    assertTrue(checkBytes);
    // Check response content
    String restUrl = response.getContentAsString();
    assertEquals(fileName.replaceAll("^/", ""), restUrl);
}

From source file:org.geoserver.restupload.ResumableUploadTest.java

@Test
public void testUploadFullResume() throws Exception {
    String uploadId = sendPostRequest();
    byte[] bigFile = generateFileAsBytes();
    byte[] partialFile1 = ArrayUtils.subarray(bigFile, 0, (int) partialSize);
    // First upload

    MockHttpServletRequest request = createRequest("/rest/resumableupload/" + uploadId);
    request.setMethod("PUT");
    request.setContentType("application/octet-stream");
    request.setContent(partialFile1);/*from   w w  w  .jav a  2s  .  c  o  m*/
    request.addHeader("Content-type", "application/octet-stream");
    request.addHeader("Content-Length", String.valueOf(bigFile.length));
    dispatch(request);

    // Resume upload
    request = createRequest("/rest/resumableupload/" + uploadId);
    request.setMethod("PUT");
    request.setContentType("application/octet-stream");
    byte[] partialFile2 = ArrayUtils.subarray(bigFile, (int) partialSize, bigFile.length);
    request.setContent(partialFile2);
    request.addHeader("Content-type", "application/octet-stream");
    request.addHeader("Content-Length", String.valueOf(partialFile2.length));
    request.addHeader("Content-Range", "bytes " + partialSize + "-" + bigFile.length + "/" + bigFile.length);
    MockHttpServletResponse response = dispatch(request);
    assertEquals(Status.SUCCESS_OK.getCode(), response.getStatus());

    File uploadedFile = getTempPath(uploadId);

    assertFalse(uploadedFile.exists());
    File destinationFile = new File(FilenameUtils.concat(root, fileName.replaceAll("^/", "")));
    assertTrue(destinationFile.exists());
    assertEquals(bigFile.length, destinationFile.length());
    // Check uploaded file byte by byte
    boolean checkBytes = Arrays.equals(bigFile, toBytes(new FileInputStream(destinationFile)));
    assertTrue(checkBytes);
    // Check response content
    String restUrl = response.getContentAsString();
    assertEquals(fileName.replaceAll("^/", ""), restUrl);
}

From source file:org.geoserver.restupload.ResumableUploadTest.java

private String sendPostRequest() throws Exception {
    MockHttpServletRequest request = createRequest("/rest/resumableupload/");
    request.setMethod("POST");
    request.setContentType("text/plain");
    request.setContent(fileName.getBytes("UTF-8"));
    request.addHeader("Content-type", "text/plain");
    MockHttpServletResponse response = dispatch(request);
    assertEquals(Status.SUCCESS_CREATED.getCode(), response.getStatus());
    String responseBody = response.getContentAsString();
    String url = responseBody.split("\\r?\\n")[1];
    String uploadId = FilenameUtils.getBaseName(url);
    return uploadId;
}

From source file:org.geoserver.test.NestedElementsFilteringTest.java

@Test
public void testWfsGetFeatureWithAdvancedNestedFilter() throws Exception {
    // execute the WFS 2.0 request
    MockHttpServletResponse response = postAsServletResponse("wfs",
            readResource("/test-data/stations/nestedElements/requests/wfs_get_feature_1.xml"));
    // check that station 1 was returned
    String content = response.getContentAsString();
    assertThat(content, containsString("gml:id=\"ins.1\""));
    assertThat(StringUtils.countMatches(content, "<wfs:member>"), is(1));
}

From source file:org.geoserver.wms.wms_1_1_1.GetLegendGraphicTest.java

@Test
public void testEntityExpansionSldBody() throws Exception {
    String base = "wms?LEGEND_OPTIONS=forceLabels:on&REQUEST=GetLegendGraphic&VERSION=1.0.0&FORMAT=image/png&WIDTH=200&HEIGHT=20&LAYER="
            + getLayerId(MockData.POLYGONS) + "&SLD_BODY=";
    String sld = IOUtils.toString(TestData.class.getResource("externalEntities.sld"));
    MockHttpServletResponse response = getAsServletResponse(base + URLEncoder.encode(sld, "UTF-8"));
    // should fail with an error message poiting at entity resolution
    assertEquals("application/vnd.ogc.se_xml", response.getContentType());
    final String content = response.getContentAsString();
    assertThat(content, containsString("Entity resolution disallowed"));
    assertThat(content, containsString("/this/file/does/not/exist"));
}

From source file:org.geoserver.wms.wms_1_1_1.SLDWithInlineFeatureTest.java

@Test
public void testGetMapPostEntityExpansion() throws Exception {
    String body = IOUtils.toString(getClass().getResourceAsStream("GetMapExternalEntity.xml"), "UTF-8");
    MockHttpServletResponse response = postAsServletResponse("wms", body);
    // should fail with an error message pointing at entity resolution
    assertEquals("application/vnd.ogc.se_xml", response.getContentType());
    final String content = response.getContentAsString();
    assertThat(content, containsString("Entity resolution disallowed"));
    assertThat(content, containsString("/this/file/does/not/exist"));
}

From source file:org.geoserver.wps.ExecuteTest.java

@Test
public void testWKTInlineRawOutput() throws Exception { // Standard Test A.4.4.3
    String xml = "<wps:Execute service='WPS' version='1.0.0' xmlns:wps='http://www.opengis.net/wps/1.0.0' "
            + "xmlns:ows='http://www.opengis.net/ows/1.1'>" + "<ows:Identifier>JTS:buffer</ows:Identifier>"
            + "<wps:DataInputs>" + "<wps:Input>" + "<ows:Identifier>distance</ows:Identifier>" + "<wps:Data>"
            + "<wps:LiteralData>1</wps:LiteralData>" + "</wps:Data>" + "</wps:Input>" + "<wps:Input>"
            + "<ows:Identifier>geom</ows:Identifier>" + "<wps:Data>"
            + "<wps:ComplexData mimeType=\"application/wkt\">"
            + "<![CDATA[POLYGON((1 1, 2 1, 2 2, 1 2, 1 1))]]>" + "</wps:ComplexData>" + "</wps:Data>"
            + "</wps:Input>" + "</wps:DataInputs>" + "<wps:ResponseForm>"
            + "    <wps:RawDataOutput mimeType=\"application/wkt\">"
            + "        <ows:Identifier>result</ows:Identifier>" + "    </wps:RawDataOutput>"
            + "  </wps:ResponseForm>" + "</wps:Execute>";

    // print(dom(new StringInputStream("<?xml version=\"1.0\" encoding=\"UTF-16\"?>\n" + xml)));

    MockHttpServletResponse response = postAsServletResponse("wps", xml);
    // System.out.println(response.getOutputStreamContent());
    assertEquals("application/wkt", response.getContentType());
    String cd = response.getHeader("Content-Disposition");
    assertTrue(cd.endsWith("filename=result.wkt"));
    Geometry g = new WKTReader().read(response.getContentAsString());
    Assert.assertTrue(g instanceof Polygon);
}

From source file:org.geoserver.wps.ExecuteTest.java

@Test
public void testWKTInlineKVPRawOutput() throws Exception {
    String request = "wps?service=WPS&version=1.0.0&request=Execute&Identifier=JTS:buffer" + "&DataInputs="
            + urlEncode("geom=POLYGON((1 1, 2 1, 2 2, 1 2, 1 1))@mimetype=application/wkt;distance=1")
            + "&RawDataOutput=" + urlEncode("result=@mimetype=application/wkt");
    MockHttpServletResponse response = getAsServletResponse(request);
    // System.out.println(response.getOutputStreamContent());
    assertEquals("application/wkt", response.getContentType());
    Geometry g = new WKTReader().read(response.getContentAsString());
    Assert.assertTrue(g instanceof Polygon);
}