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

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

Introduction

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

Prototype

@Override
    public int getStatus() 

Source Link

Usage

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

/**
 * Test trying to retrieve a file as a user with registration status of pending.
 * Expected return code: 401/* www  .  j a v a2 s . c om*/
 * @throws BizPolicyException
 * @throws IOException
 * @throws ArchiveServiceException
 * @throws RelationshipConstraintException
 */
@Test
public void testGetFileRequestByPendingUser()
        throws BizPolicyException, IOException, ArchiveServiceException, RelationshipConstraintException {
    MockHttpServletRequest req = new MockHttpServletRequest("GET", REQUEST_STRING);
    MockHttpServletResponse res = new MockHttpServletResponse();

    FileBizService bizService = fileController.getFileBizService();
    when(bizService.getFile(dataFileOne.getId(), admin))
            .thenThrow(new BizPolicyException("mock exception", BizPolicyException.Type.AUTHENTICATION_ERROR));

    //run the handle request
    fileController.handleFileGetRequest("foo", "application/*", null, req, res);
    //Test status code
    assertEquals(401, res.getStatus());
}

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

/**
 * Test handling a good file request with null "Accept" header and null "If-Modified-Since" header
 * Expected: Status 200/*w w w .  j a  v a  2s  .com*/
 *           Etag header
 *           Content-Disposition header
 *           Content-Type header
 *           Content-Lenth header
 *           Last-modified header
 *           File bytestream
 * @throws IOException
 */
@Test
public void testGetFileRequestNullAcceptModifiedSinceHeader() throws IOException {
    MockHttpServletRequest req = new MockHttpServletRequest("GET", REQUEST_STRING);
    MockHttpServletResponse res = new MockHttpServletResponse();
    final int lowContentLength = 4;
    final int highContentLength = 8;

    //run the handle request
    fileController.handleFileGetRequest("foo", 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.getContentLength(),
            res.getContentLength() > lowContentLength && res.getContentLength() < highContentLength);

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

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

/**
 * Test handling a good file request with specific "Accept" header
 * Expected: Status 200//from ww  w. j  a  v a2s  .  co m
 *           Etag header
 *           Content-Disposition header
 *           Content-Type header
 *           Content-Lenth header
 *           Last-modified header
 *           File bytestream
 * @throws IOException
 */
@Test
public void testGetFileRequestSpecificAcceptHeader() throws IOException {
    MockHttpServletRequest req = new MockHttpServletRequest("GET", REQUEST_STRING);
    MockHttpServletResponse res = new MockHttpServletResponse();
    final int lowContentLength = 4;
    final int highContentLength = 8;
    String expectedMimeType = URLConnection.getFileNameMap().getContentTypeFor(dataFileOne.getName());

    //run the handle request
    fileController.handleFileGetRequest("foo", expectedMimeType, 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.getContentLength(),
            res.getContentLength() > lowContentLength && res.getContentLength() < highContentLength);
    byte[] originalContent = DATA_FILE_ONE_CONTENT.getBytes();
    assertEquals(new String(originalContent), new String(res.getContentAsByteArray()).trim());
}

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

/**
 * Test requesting for a file with non acceptable mime type
 * Expected return code 406//from   w w w.j a  v a 2 s  .  c  o m
 * @throws IOException
 */
@Test
public void testGetFileRequestUnacceptedMimeType() throws IOException {
    MockHttpServletRequest req = new MockHttpServletRequest("GET", REQUEST_STRING);
    MockHttpServletResponse res = new MockHttpServletResponse();

    //run the handle request
    fileController.handleFileGetRequest("foo", "image/*", null, req, res);
    //Test status code
    assertEquals(406, res.getStatus());
}

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

/**
 * Test handling exception on unresolveable file id on a good file retrieval request.
 * Expected return code: 500/*from  w  w  w .ja v a 2s  . c om*/
 * @throws BizPolicyException
 * @throws IOException
 * @throws ArchiveServiceException
 * @throws RelationshipConstraintException
 */
@Test
public void testGetFileRequestUnresolvableId()
        throws BizPolicyException, IOException, ArchiveServiceException, RelationshipConstraintException {
    MockHttpServletRequest req = new MockHttpServletRequest("GET", REQUEST_STRING);
    MockHttpServletResponse res = new MockHttpServletResponse();

    RequestUtil util = fileController.getRequestUtil();
    when(util.buildRequestUrl(any(HttpServletRequest.class))).thenReturn("  ");

    fileController.handleFileGetRequest("foo", "application/*", null, req, res);
    //Test status code
    assertEquals(500, res.getStatus());
}

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

/**
 * Test handling of ArchiveServiceException file id on a good file retrieval request.
 * Expected return code: 500//  www . ja va 2s  .  co m
 * @throws BizPolicyException
 * @throws IOException
 * @throws ArchiveServiceException
 * @throws RelationshipConstraintException
 */
@Test
public void testGetFileRequestHandlingArchiveServiceException()
        throws BizPolicyException, IOException, ArchiveServiceException, RelationshipConstraintException {
    MockHttpServletRequest req = new MockHttpServletRequest("GET", REQUEST_STRING);
    MockHttpServletResponse res = new MockHttpServletResponse();

    FileBizService fileBizService = fileController.getFileBizService();
    when(fileBizService.getFile(dataFileOne.getId(), admin))
            .thenThrow(new ArchiveServiceException("Mocked exception"));
    fileController.handleFileGetRequest("foo", "application/*", null, req, res);
    //Test status code
    assertEquals(500, res.getStatus());
}

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

/**
 * Test handling of RelationshipConstraintException on a good file retrieval request.
 * Expected return code: 500//from  w ww  .  j  av a2s. com
 * @throws BizPolicyException
 * @throws IOException
 * @throws ArchiveServiceException
 * @throws RelationshipConstraintException
 */
@Test
public void testGetFileRequestHandlingRelationshipConstraintException()
        throws BizPolicyException, IOException, ArchiveServiceException, RelationshipConstraintException {
    MockHttpServletRequest req = new MockHttpServletRequest("GET", REQUEST_STRING);
    MockHttpServletResponse res = new MockHttpServletResponse();

    FileBizService fileBizService = fileController.getFileBizService();
    when(fileBizService.getFile(dataFileOne.getId(), admin))
            .thenThrow(new RelationshipConstraintException("Mocked exception"));
    fileController.handleFileGetRequest("foo", "application/*", null, req, res);
    //Test status code
    assertEquals(500, res.getStatus());
}

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

/**
 * Test handling a good non-text file request with specific "Accept" header
 * Expected: Status 200/* w  ww . ja v  a 2s.c  o  m*/
 *           Etag header
 *           Content-Disposition header
 *           Content-Type header
 *           Content-Lenth header
 *           Last-modified header
 *           File bytestream
 * @throws IOException
 */
@Test
public void testGetFileRequestBinaryFile() throws Exception {
    MockHttpServletRequest req = new MockHttpServletRequest("GET", "file/foo");
    MockHttpServletResponse res = new MockHttpServletResponse();

    java.io.File binaryFile = new java.io.File(FileControllerTest.class.getResource(FILES_ONLY_ZIP).toURI());
    byte fileContents[] = IOUtils.toByteArray(new FileInputStream(binaryFile));
    long expectedContentLength = binaryFile.length();
    DataFile binaryDataFile = new DataFile("foo", "ZipFileName.zip",
            binaryFile.toURI().toURL().toExternalForm(), "application/zip", binaryFile.getPath(),
            binaryFile.length(), new ArrayList<String>());

    FileBizService fileBizService = fileController.getFileBizService();
    when(fileBizService.getFile("foo", admin)).thenReturn(binaryDataFile);
    when(fileBizService.getLastModifiedDate("foo")).thenReturn(lastModifiedDate);
    RequestUtil requestUtil = fileController.getRequestUtil();
    when(requestUtil.buildRequestUrl(any(HttpServletRequest.class))).thenReturn("foo");

    String expectedMimeType = URLConnection.getFileNameMap().getContentTypeFor(binaryDataFile.getName());

    //run the handle request
    fileController.handleFileGetRequest("foo", 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());
    assertEquals(expectedMimeType, res.getContentType());
    assertNotNull(res.getHeader(LAST_MODIFIED));
    assertEquals(rfcDateFormatter(lastModifiedDate), res.getHeader(LAST_MODIFIED));
    assertEquals(expectedContentLength, res.getContentLength());

    byte[] responseContent = res.getContentAsByteArray();
    assertArrayEquals(fileContents, responseContent);

}

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

/**
 * Tests whether a metadata file request returns the proper metadata file
 *//*from w w w  .jav  a2  s .c om*/
@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.geogig.geoserver.functional.GeoServerFunctionalTestContext.java

/**
 * @return the status code of the last response
 *//*from www  . jav a2 s .co  m*/
@Override
public int getLastResponseStatus() {
    MockHttpServletResponse response = getLastResponse();
    //        int code = response.getStatusCode();
    //        if (response.getStatusCode() == 200) {
    //            code = response.getErrorCode();
    //        }
    //        return code;
    return response.getStatus();
}