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.dataconservancy.ui.api.FileControllerTest.java

/**
 * Test handling a good file request with specific "Accept" header
 * Expected: Status 200/*from   w  w w  . j  a  v  a  2s.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 handling a good non-text file request with specific "Accept" header
 * Expected: Status 200//from  w w w  . j a  v  a 2s .co  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 or not the response to a get file request has the correct
 * file name and size in the content disposition field of the header.
 * /*from   ww w . j  av a  2s. com*/
 * @throws IOException
 */
@Test
public void testGetFileRequestContentDisposition() throws IOException {

    MockHttpServletRequest req = new MockHttpServletRequest("GET", REQUEST_STRING);
    MockHttpServletResponse res = new MockHttpServletResponse();

    String expectedContentDispositionString = "attachment; filename=\"" + dataFileOne.getName() + "\";size="
            + dataFileOne.getSize();

    when(fileController.getAuthenticatedUser()).thenReturn(admin);

    fileController.handleFileGetRequest("foo", "*/*", null, req, res);

    String actualContentDispositionString = (String) res.getHeader(CONTENT_DISPOSITION);

    // Test requestheader content disposition
    assertEquals(expectedContentDispositionString, actualContentDispositionString);
}

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

/**
 * Tests whether a metadata file request returns the proper metadata file
 *//* w ww .j  av  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.geoserver.catalog.rest.StyleTest.java

@Test
public void testPostAsSLD() throws Exception {
    String xml = newSLDXML();/*from w  w  w.j av  a  2s .  c  o m*/

    MockHttpServletResponse response = postAsServletResponse("/rest/styles", xml, SLDHandler.MIMETYPE_10);
    assertEquals(201, response.getStatus());
    assertNotNull(response.getHeader("Location"));
    assertTrue(response.getHeader("Location").endsWith("/styles/foo"));

    assertNotNull(catalog.getStyleByName("foo"));
}

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

@Test
public void testPostAsSLDToWorkspace() throws Exception {
    assertNull(catalog.getStyleByName("gs", "foo"));

    String xml = newSLDXML();/*from  w  w  w.  ja v  a2  s  .c  o m*/

    MockHttpServletResponse response = postAsServletResponse("/rest/workspaces/gs/styles", xml,
            SLDHandler.MIMETYPE_10);
    assertEquals(201, response.getStatus());
    assertNotNull(response.getHeader("Location"));
    assertTrue(response.getHeader("Location").endsWith("/workspaces/gs/styles/foo"));

    assertNotNull(catalog.getStyleByName("gs", "foo"));

    GeoServerResourceLoader rl = getResourceLoader();
    assertNotNull(rl.find("workspaces", "gs", "styles", "foo.sld"));
}

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

@Test
public void testPostAsSLDWithName() throws Exception {
    String xml = newSLDXML();//from  w w  w  .j ava 2s .c o m

    MockHttpServletResponse response = postAsServletResponse("/rest/styles?name=bar", xml,
            SLDHandler.MIMETYPE_10);
    assertEquals(201, response.getStatus());
    assertNotNull(response.getHeader("Location"));
    assertTrue(response.getHeader("Location").endsWith("/styles/bar"));

    assertNotNull(catalog.getStyleByName("bar"));
}

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

@Test
public void testPostAsPSL() throws Exception {
    Properties props = new Properties();
    props.put("type", "point");
    props.put("color", "ff0000");

    StringWriter out = new StringWriter();
    props.store(out, "comment!");

    MockHttpServletResponse response = postAsServletResponse("/rest/styles?name=foo", out.toString(),
            PropertyStyleHandler.MIMETYPE);
    assertEquals(201, response.getStatus());
    assertNotNull(response.getHeader("Location"));
    assertTrue(response.getHeader("Location").endsWith("/styles/foo"));

    assertNotNull(catalog.getStyleByName("foo"));

    Resource style = getDataDirectory().style(getCatalog().getStyleByName("foo"));
    InputStream in = style.in();//w w  w  .  jav a 2s . c  o m

    props = new Properties();
    try {
        props.load(in);
        assertEquals("point", props.getProperty("type"));
    } finally {
        in.close();
    }

    in = style.in();
    try {
        out = new StringWriter();
        IOUtils.copy(in, out);
        assertFalse(out.toString().startsWith("#comment!"));
    } finally {
        in.close();
    }
}

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

@Test
public void testPostAsPSLRaw() throws Exception {
    Properties props = new Properties();
    props.put("type", "point");
    props.put("color", "ff0000");

    StringWriter out = new StringWriter();
    props.store(out, "comment!");

    MockHttpServletResponse response = postAsServletResponse("/rest/styles?name=foo&raw=true", out.toString(),
            PropertyStyleHandler.MIMETYPE);
    assertEquals(201, response.getStatus());
    assertNotNull(response.getHeader("Location"));
    assertTrue(response.getHeader("Location").endsWith("/styles/foo"));

    // check style on disk to ensure the exact contents was preserved
    Resource style = getDataDirectory().style(getCatalog().getStyleByName("foo"));
    InputStream in = style.in();/*from ww w. jav  a  2 s .  com*/
    try {
        out = new StringWriter();
        IOUtils.copy(in, out);
        assertTrue(out.toString().startsWith("#comment!"));
    } finally {
        in.close();
    }
}

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

@Test
public void testPostAsSE() throws Exception {
    String xml = "<StyledLayerDescriptor xmlns=\"http://www.opengis.net/sld\" "
            + "       xmlns:se=\"http://www.opengis.net/se\" version=\"1.1.0\"> " + " <NamedLayer> "
            + "  <UserStyle> " + "   <se:Name>UserSelection</se:Name> " + "   <se:FeatureTypeStyle> "
            + "    <se:Rule> " + "     <se:PolygonSymbolizer> " + "      <se:Fill> "
            + "       <se:SvgParameter name=\"fill\">#FF0000</se:SvgParameter> " + "      </se:Fill> "
            + "     </se:PolygonSymbolizer> " + "    </se:Rule> " + "   </se:FeatureTypeStyle> "
            + "  </UserStyle> " + " </NamedLayer> " + "</StyledLayerDescriptor>";

    MockHttpServletResponse response = postAsServletResponse("/rest/styles?name=foo", xml,
            SLDHandler.MIMETYPE_11);//from w w  w  .  ja  v a2  s. c o  m
    assertEquals(201, response.getStatus());
    assertNotNull(response.getHeader("Location"));
    assertTrue(response.getHeader("Location").endsWith("/styles/foo"));

    StyleInfo style = catalog.getStyleByName("foo");
    assertNotNull(style);

    assertEquals("sld", style.getFormat());
    assertEquals(SLDHandler.VERSION_11, style.getFormatVersion());
}