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.cloudifysource.rest.UploadControllerTest.java

@Test
public void testUploadExceededSizeLimitFile() throws Exception {
    uploadRepo.setUploadSizeLimitBytes(TEST_UPLOAD_SIZE_LIMIT_BYTES);
    File uploadFile = new File(TEST_FILE_PATH);
    MockHttpServletResponse response = null;
    long fileSize = uploadFile.length();
    try {//  w w w.j ava2  s  . co  m
        response = testPostFile(versionedUploadUri, uploadFile);
        Assert.fail("Tring to upload a file of zise " + fileSize + "expected to failed. response "
                + response.getContentAsString());
    } catch (RestErrorException e) {
        Map<String, Object> errorDescription = e.getErrorDescription();
        String status = (String) errorDescription.get("status");
        Assert.assertEquals("error", status);
        String errorMsg = (String) errorDescription.get("error");
        Assert.assertEquals(CloudifyMessageKeys.UPLOAD_FILE_SIZE_LIMIT_EXCEEDED.getName(), errorMsg);
        Object[] args = (Object[]) errorDescription.get("error_args");
        Object[] expectedArgs = { UPLOADED_FILE_NAME, fileSize, uploadRepo.getUploadSizeLimitBytes() };
        Assert.assertArrayEquals(expectedArgs, args);
    } finally {
        uploadRepo.setUploadSizeLimitBytes(CloudifyConstants.DEFAULT_UPLOAD_SIZE_LIMIT_BYTES);
    }
}

From source file:org.dataconservancy.deposit.status.SimpleStatusServletTest.java

private void doBasicRetrieval(String depositid, String mgrid) throws Exception {
    final String BASEURL = "http://example.org/webapp/depositServlet";
    final String MGR_ID_1 = "id_1";

    final String STATUS_CONTENT = "status content";
    final String STATUS_MIME = "text/foo";

    DepositDocumentServlet serv = new DepositStatusServlet();
    serv.setBaseURL(BASEURL);/*from www  .  j  a  v  a2 s  .  c  om*/

    List<StatusTestDepositManager> mgrs = getTestManagers(MGR_ID_1, mgrid);
    serv.setDepositManagers(mgrs);

    MockDepositInfo status = new MockDepositInfo(depositid, mgrid);
    MockDepositDocument statusDoc = new MockDepositDocument();
    statusDoc.setInputStream(IOUtils.toInputStream(STATUS_CONTENT));
    statusDoc.setMimeType(STATUS_MIME);
    status.setDepositStatus(statusDoc);

    mgrs.get(1).setDepositStatus(depositid, status);

    String url = serv.getURL(status);

    MockHttpServletRequest request = new MockHttpServletRequest("GET", url);
    request.setPathInfo(url.replace(BASEURL, ""));

    MockHttpServletResponse response = new MockHttpServletResponse();

    serv.doGet(request, response);

    assertEquals("Wrong response http code", HttpServletResponse.SC_OK, response.getStatus());
    assertEquals("Wrong response mime type", response.getContentType(), STATUS_MIME);
    assertEquals("Did not return correct content", STATUS_CONTENT, response.getContentAsString());
}

From source file:org.dataconservancy.ui.api.FileControllerTest.java

/**
 * Tests whether a metadata file request returns the proper metadata file
 *///from  w w w  .j  a va 2  s.  com
@Test
public void testGetMetadataFile() throws Exception {
    MockHttpServletRequest req = new MockHttpServletRequest("GET", REQUEST_STRING);
    MockHttpServletResponse res = new MockHttpServletResponse();
    final int lowContentLength = 5;
    final int highContentLength = 20;

    metadataFileController.handleFileGetRequest(null, null, null, req, res);

    //Test status code
    assertEquals(200, res.getStatus());
    //Test headers
    assertNotNull(res.getHeader(ETAG));
    assertNotNull(res.getHeader(CONTENT_DISPOSITION));
    assertNotNull(res.getContentType());
    assertNotNull(res.getHeader(LAST_MODIFIED));
    assertEquals(rfcDateFormatter(lastModifiedDate), res.getHeader(LAST_MODIFIED));
    assertTrue("Content Length out of bounds: " + res.getContentAsString().length(),
            res.getContentAsString().length() > lowContentLength
                    && res.getContentAsString().length() < highContentLength);

    byte[] originalContent = METADATA_FILE_ONE_CONTENT.getBytes();
    assertEquals(new String(originalContent), new String(res.getContentAsByteArray()).trim());
}

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

@Test
public void testGetWrongStyle() throws Exception {
    // Parameters for the request
    String ws = "gs";
    String style = "foooooo";
    // Request path
    String requestPath = "/rest/styles/" + style + ".html";
    String requestPath2 = "/rest/workspaces/" + ws + "/styles/" + style + ".html";
    // Exception path
    String exception = "No such style: " + style;
    String exception2 = "No such style " + style + " in workspace " + ws;

    // CASE 1: No workspace set

    // First request should thrown an exception
    MockHttpServletResponse response = getAsServletResponse(requestPath);
    assertEquals(404, response.getStatus());
    assertTrue(response.getContentAsString().contains(exception));

    // Same request with ?quietOnNotFound should not throw an exception
    response = getAsServletResponse(requestPath + "?quietOnNotFound=true");
    assertEquals(404, response.getStatus());
    assertFalse(response.getContentAsString().contains(exception));
    // No exception thrown
    assertTrue(response.getContentAsString().isEmpty());

    // CASE 2: workspace set

    // First request should thrown an exception
    response = getAsServletResponse(requestPath2);
    assertEquals(404, response.getStatus());
    assertTrue(response.getContentAsString().contains(exception2));

    // Same request with ?quietOnNotFound should not throw an exception
    response = getAsServletResponse(requestPath2 + "?quietOnNotFound=true");
    assertEquals(404, response.getStatus());
    assertFalse(response.getContentAsString().contains(exception2));
    // No exception thrown
    assertTrue(response.getContentAsString().isEmpty());
}

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

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

    MockHttpServletResponse response = postAsServletResponse("/rest/styles", xml, SLDHandler.MIMETYPE_10);
    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.catalog.rest.StyleTest.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("/rest/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.opensearch.rest.CollectionsControllerTest.java

@Test
public void testNonExistingCollection() throws Exception {
    MockHttpServletResponse response = getAsServletResponse("/rest/oseo/collections/foobar");
    assertEquals(404, response.getStatus());
    assertThat(response.getContentAsString(), containsString("foobar"));
}

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

@Test
public void testCreateInvalidAttributeSyntax() throws Exception {
    String testData = new String(getTestData("/collection.json"), "UTF-8");
    // inject an invalid attribute name
    String invalidTestData = testData.replace("primary", "1:2:primary");
    MockHttpServletResponse response = postAsServletResponse("rest/oseo/collections", invalidTestData,
            MediaType.APPLICATION_JSON_VALUE);
    assertEquals(400, response.getStatus());
    assertThat(response.getContentAsString(), containsString("1:2:primary"));
}

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

@Test
public void testCreateInvalidAttributePrefix() throws Exception {
    String testData = new String(getTestData("/collection.json"), "UTF-8");
    // inject an invalid attribute name
    String invalidTestData = testData.replace("eo:productType", "abc:productType");
    MockHttpServletResponse response = postAsServletResponse("rest/oseo/collections", invalidTestData,
            MediaType.APPLICATION_JSON_VALUE);
    assertEquals(400, response.getStatus());
    assertThat(response.getContentAsString(), containsString("abc:productType"));
}

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

@Test
public void testCreateInvalidAttributeName() throws Exception {
    String testData = new String(getTestData("/collection.json"), "UTF-8");
    // inject an invalid attribute name
    String invalidTestData = testData.replace("eo:productType", "eo:newProductType");
    MockHttpServletResponse response = postAsServletResponse("rest/oseo/collections", invalidTestData,
            MediaType.APPLICATION_JSON_VALUE);
    assertEquals(400, response.getStatus());
    assertThat(response.getContentAsString(), containsString("eo:newProductType"));
}