List of usage examples for org.springframework.mock.web MockHttpServletResponse getContentType
@Override
@Nullable
public String getContentType()
From source file:org.cloudifysource.rest.AttributesContollerTest.java
private void testRequest(final MockHttpServletRequest reqeust, final HandlerMethod expectedHandlerMethod, final String expectedResponseContent) throws Exception { final MockHttpServletResponse response = new MockHttpServletResponse(); final Object handler = getHandlerToRequest(reqeust); Assert.assertEquals("Wrong handler selected for request uri: " + reqeust.getRequestURI(), expectedHandlerMethod.toString(), handler.toString()); // handle the request handlerAdapter.handle(reqeust, response, handler); // validate the response Assert.assertTrue("Wrong response status: " + response.getStatus(), response.getStatus() == HttpStatus.OK.value()); Assert.assertEquals("Wrong content type in response: " + response.getContentType(), JSON_CONTENT_TYPE, response.getContentType());//from w w w. j av a 2 s . c o m Assert.assertEquals("Wrong response content: " + response.getContentAsString(), expectedResponseContent, response.getContentAsString()); }
From source file:org.cloudifysource.rest.ControllerTest.java
private MockHttpServletResponse testRequest(final MockHttpServletRequest request, final HandlerMethod expectedHandlerMethod) throws Exception { final MockHttpServletResponse response = new MockHttpServletResponse(); final HandlerExecutionChain handlerExecutionChain = getHandlerToRequest(request); Object handler = handlerExecutionChain.getHandler(); Assert.assertEquals("Wrong handler selected for request uri: " + request.getRequestURI(), expectedHandlerMethod.toString(), handler.toString()); HandlerInterceptor[] interceptors = handlerExecutionChain.getInterceptors(); // pre handle for (HandlerInterceptor handlerInterceptor : interceptors) { handlerInterceptor.preHandle(request, response, handler); }//from w w w .jav a 2s. c o m // handle the request ModelAndView modelAndView = handlerAdapter.handle(request, response, handler); // post handle for (HandlerInterceptor handlerInterceptor : interceptors) { handlerInterceptor.postHandle(request, response, handler, modelAndView); } // validate the response Assert.assertTrue("Wrong response status: " + response.getStatus(), response.getStatus() == HttpStatus.OK.value()); Assert.assertTrue(response.getContentType().contains(MediaType.APPLICATION_JSON)); return response; }
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 w w w . j a v a 2s .c o m*/ 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
/** * Test attempt to retrieve a good file by an admin, where a good file is one that exists and is retrievable by * authorized user.//from w ww . ja v a 2 s .c o m * * Expected: Status 200 * Etag header * Content-Disposition header * Content-Type header * Content-Lenth header * Last-modified header * File bytestream * * @throws IOException */ @Test public void testGetFileRequestByAdmin() throws IOException { MockHttpServletRequest req = new MockHttpServletRequest("GET", REQUEST_STRING); MockHttpServletResponse res = new MockHttpServletResponse(); final int lowContentLength = 4; final int highContentLength = 8; fileController.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.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 null "Accept" header and null "If-Modified-Since" header * Expected: Status 200//from w w w . j a v a 2 s . 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// w w w . jav a 2 s. c o 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 2 s. com * 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 *//*ww w. java2s. c o m*/ @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.importer.ImporterTestSupport.java
protected void runChecks(String layerName) throws Exception { LayerInfo layer = getCatalog().getLayerByName(layerName); assertNotNull(layer);// ww w.ja va 2 s .c o m assertNotNull(layer.getDefaultStyle()); assertNotNull(layer.getResource().getProjectionPolicy()); if (layer.getType() == PublishedType.VECTOR) { FeatureTypeInfo featureType = (FeatureTypeInfo) layer.getResource(); FeatureSource source = featureType.getFeatureSource(null, null); assertTrue(source.getCount(Query.ALL) > 0); //do a wfs request Document dom = getAsDOM("wfs?request=getFeature&typename=" + featureType.getPrefixedName()); assertEquals("wfs:FeatureCollection", dom.getDocumentElement().getNodeName()); assertEquals(source.getCount(Query.ALL), dom.getElementsByTagName(featureType.getPrefixedName()).getLength()); } //do a wms request MockHttpServletResponse response = getAsServletResponse( "wms/reflect?layers=" + layer.getResource().getPrefixedName()); assertEquals("image/png", response.getContentType()); }