List of usage examples for org.springframework.mock.web MockHttpServletResponse getContentAsByteArray
public byte[] getContentAsByteArray()
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 . jav a2 s . 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 a metadata file request returns the proper metadata file *///from w w w. j a v a 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.geogig.geoserver.functional.GeoServerFunctionalTestContext.java
protected byte[] getBinary(MockHttpServletResponse response) { // try { return response.getContentAsByteArray(); // } catch (Exception e) { // throw new RuntimeException("Whoops, did you change the MockRunner version? " + // "If so, you might want to change this method too", e); // }// ww w .j a v a 2 s.c o m }
From source file:org.geoserver.opensearch.eo.OSEOTestSupport.java
/** * Returns the DOM after checking the status code is 200 and the returned mime type is the expected one * /* w ww .j av a2 s .c om*/ * @param path * @param expectedMimeType * @return * @throws Exception */ protected Document getAsDOM(String path, int expectedStatusCode, String expectedMimeType) throws Exception { MockHttpServletResponse response = getAsServletResponse(path); assertEquals(expectedMimeType, response.getContentType()); assertEquals(expectedStatusCode, response.getStatus()); Document dom = dom(new ByteArrayInputStream(response.getContentAsByteArray())); return dom; }
From source file:org.geoserver.ows.StylePublisherTest.java
@Test public void testEncoding() throws Exception { for (String[] path : paths) { MockHttpServletResponse response = request(path, null); assertEquals(Arrays.toString(path), 200, response.getStatus()); assertArrayEquals(/*from ww w. j a v a 2 s . com*/ IOUtils.toByteArray(this.getClass().getClassLoader() .getResourceAsStream("org/geoserver/ows/" + path[path.length - 1])), response.getContentAsByteArray()); } }
From source file:org.geoserver.ows.StylePublisherTest.java
@Test public void testOverride() throws Exception { String[] path = new String[] { "styles", "override.png" }; MockHttpServletResponse response = request(path, null); assertEquals(Arrays.toString(path), 200, response.getStatus()); assertArrayEquals(//w w w . j av a 2s.c om IOUtils.toByteArray( this.getClass().getClassLoader().getResourceAsStream("org/geoserver/ows/smileyface.png")), response.getContentAsByteArray()); path = new String[] { "styles", "cite", "override.png" }; response = request(path, null); assertEquals(Arrays.toString(path), 200, response.getStatus()); assertArrayEquals( IOUtils.toByteArray( this.getClass().getClassLoader().getResourceAsStream("org/geoserver/ows/grass_fill.png")), response.getContentAsByteArray()); path = new String[] { "styles", "icons", "override.png" }; response = request(path, null); assertEquals(Arrays.toString(path), 200, response.getStatus()); assertArrayEquals( IOUtils.toByteArray( this.getClass().getClassLoader().getResourceAsStream("org/geoserver/ows/smileyface.png")), response.getContentAsByteArray()); path = new String[] { "styles", "cite", "icons", "override.png" }; response = request(path, null); assertEquals(Arrays.toString(path), 200, response.getStatus()); assertArrayEquals( IOUtils.toByteArray( this.getClass().getClassLoader().getResourceAsStream("org/geoserver/ows/grass_fill.png")), response.getContentAsByteArray()); }
From source file:org.geoserver.ows.StylePublisherTest.java
@Test public void testLastModified() throws Exception { for (String[] path : paths) { MockHttpServletResponse response = request(path, null); String lastModified = response.getHeader("Last-Modified"); assertNotNull(lastModified);/*from w w w.j a va 2 s .c o m*/ response = request(path, lastModified); assertEquals(304, response.getStatus()); long timeStamp = AbstractURLPublisher.lastModified(lastModified) + 10000; response = request(path, AbstractURLPublisher.lastModified(timeStamp)); assertEquals(304, response.getStatus()); timeStamp -= 20000; response = request(path, AbstractURLPublisher.lastModified(timeStamp)); assertEquals(200, response.getStatus()); assertArrayEquals( IOUtils.toByteArray(this.getClass().getClassLoader() .getResourceAsStream("org/geoserver/ows/" + path[path.length - 1])), response.getContentAsByteArray()); } }
From source file:org.geoserver.wms.map.PDFGetMapTest.java
@Test public void testBasicPolygonMap() throws Exception { MockHttpServletResponse response = getAsServletResponse(requestBase + "&styles="); assertEquals("application/pdf", response.getContentType()); PDTilingPattern tilingPattern = getTilingPattern(response.getContentAsByteArray()); assertNull(tilingPattern);/* w ww . j a va2s .c o m*/ }
From source file:org.geoserver.wms.map.PDFGetMapTest.java
@Test public void testSvgFillOptimization() throws Exception { // get a single polygon to ease testing MockHttpServletResponse response = getAsServletResponse( requestBase + "&styles=burg-fill&featureId=BasicPolygons.1107531493630"); assertEquals("application/pdf", response.getContentType()); PDTilingPattern tilingPattern = getTilingPattern(response.getContentAsByteArray()); assertNotNull(tilingPattern);/*from ww w . j ava 2 s . c om*/ assertEquals(20, tilingPattern.getXStep(), 0d); assertEquals(20, tilingPattern.getYStep(), 0d); }
From source file:org.geoserver.wms.map.PDFGetMapTest.java
@Test public void testSvgFillOptimizationDisabled() throws Exception { PDFMapResponse.ENCODE_TILING_PATTERNS = false; // get a single polygon to ease testing MockHttpServletResponse response = getAsServletResponse( requestBase + "&styles=burg-fill&featureId=BasicPolygons.1107531493630"); assertEquals("application/pdf", response.getContentType()); // the tiling pattern encoding has been disabled PDTilingPattern tilingPattern = getTilingPattern(response.getContentAsByteArray()); assertNull(tilingPattern);//from ww w .j a v a 2s.co m }