List of usage examples for org.springframework.mock.web MockHttpServletResponse getContentAsByteArray
public byte[] getContentAsByteArray()
From source file:cherry.foundation.download.TableDownloadTemplateTest.java
@Test public void testDownloadXlsNoHeader() throws InvalidFormatException, IOException { // //from w ww.ja v a 2s . co m MockHttpServletResponse response = new MockHttpServletResponse(); // tableDownloadOperation.downloadXls(response, "test_{0}.xls", new LocalDateTime(2015, 1, 23, 12, 34, 56), null, createCommonClause(), createOrderByClause(), constant("TEST00")); // assertEquals("application/vnd.ms-excel", response.getContentType()); assertEquals("application/vnd.ms-excel", response.getHeader("Content-Type")); assertEquals("attachment; filename=\"test_20150123123456.xls\"", response.getHeader("Content-Disposition")); try (InputStream in = new ByteArrayInputStream(response.getContentAsByteArray()); Workbook workbook = WorkbookFactory.create(in); ExcelReader reader = new ExcelReader(workbook)) { String[] record; record = reader.read(); assertEquals(1, record.length); assertEquals("TEST00", record[0]); assertNull(reader.read()); } }
From source file:cherry.foundation.download.TableDownloadTemplateTest.java
@Test public void testDownloadXlsWithHeader() throws InvalidFormatException, IOException { // // w ww.ja v a 2 s . com MockHttpServletResponse response = new MockHttpServletResponse(); // tableDownloadOperation.downloadXls(response, "test_{0}.xls", new LocalDateTime(2015, 1, 23, 12, 34, 56), asList("HEAD0"), createCommonClause(), createOrderByClause(), constant("TEST00")); // assertEquals("application/vnd.ms-excel", response.getContentType()); assertEquals("application/vnd.ms-excel", response.getHeader("Content-Type")); assertEquals("attachment; filename=\"test_20150123123456.xls\"", response.getHeader("Content-Disposition")); try (InputStream in = new ByteArrayInputStream(response.getContentAsByteArray()); Workbook workbook = WorkbookFactory.create(in); ExcelReader reader = new ExcelReader(workbook)) { String[] record; record = reader.read(); assertEquals(1, record.length); assertEquals("HEAD0", record[0]); record = reader.read(); assertEquals(1, record.length); assertEquals("TEST00", record[0]); assertNull(reader.read()); } }
From source file:cherry.foundation.download.TableDownloadTemplateTest.java
@Test public void testDownloadXlsxNoHeader() throws InvalidFormatException, IOException { // /*from ww w. ja v a 2s . co m*/ MockHttpServletResponse response = new MockHttpServletResponse(); // tableDownloadOperation.downloadXlsx(response, "test_{0}.xlsx", new LocalDateTime(2015, 1, 23, 12, 34, 56), null, createCommonClause(), createOrderByClause(), constant("TEST00")); // assertEquals("application/vnd.ms-excel", response.getContentType()); assertEquals("application/vnd.ms-excel", response.getHeader("Content-Type")); assertEquals("attachment; filename=\"test_20150123123456.xlsx\"", response.getHeader("Content-Disposition")); try (InputStream in = new ByteArrayInputStream(response.getContentAsByteArray()); Workbook workbook = WorkbookFactory.create(in); ExcelReader reader = new ExcelReader(workbook)) { String[] record; record = reader.read(); assertEquals(1, record.length); assertEquals("TEST00", record[0]); assertNull(reader.read()); } }
From source file:org.eclipse.virgo.apps.repository.web.RepositoryControllerTests.java
@Test public void getArtefact() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); request.setRequestURI(//from ww w . ja v a2 s .c o m "http://localhost:8080/org.eclipse.virgo.server.repository/my-repo/bundle/com.foo/1.0.0"); request.setMethod("GET"); byte[] artefactBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; expect(this.repositoryManager.getArtifact("my-repo", "bundle", "com.foo", "1.0.0")) .andReturn(new ByteArrayInputStream(artefactBytes)); replay(this.repositoryManager); repositoryController.getArtifact(request, response); verify(this.repositoryManager); assertEquals("application/octet-stream", response.getContentType()); assertArrayEquals(artefactBytes, response.getContentAsByteArray()); }
From source file:com.comcast.video.dawg.show.video.VideoSnapTest.java
@Test public void testSaveSnappedImage() throws IOException { String deviceId = "000000000001"; MockHttpSession session = new MockHttpSession(); MockHttpServletResponse response = new MockHttpServletResponse(); UniqueIndexedCache<BufferedImage> imgCache = ClientCache.getClientCache(session).getImgCache(); String imageId = imgCache.storeItem(PC_IMG); VideoSnap videoSnap = new VideoSnap(); videoSnap.saveSnappedImage(imageId, deviceId, response, session); String header = response.getHeader("Content-Disposition"); Assert.assertTrue(header.contains("attachment"), "Response header does not indicates attachment"); Assert.assertTrue(header.contains(".jpg"), "Attached file is not in jpg format"); ByteArrayOutputStream bao = new ByteArrayOutputStream(); ImageIO.write(PC_IMG, "jpg", bao); Assert.assertEquals(response.getContentAsByteArray(), bao.toByteArray(), "Saved image size is not matching with expected size"); }
From source file:cherry.foundation.download.TableDownloadTemplateTest.java
@Test public void testDownloadXlsxWithHeader() throws InvalidFormatException, IOException { // /*w w w. jav a 2s . com*/ MockHttpServletResponse response = new MockHttpServletResponse(); // tableDownloadOperation.downloadXls(response, "test_{0}.xlsx", new LocalDateTime(2015, 1, 23, 12, 34, 56), asList("HEAD0"), createCommonClause(), createOrderByClause(), constant("TEST00"), constantAs("TEST01", path(String.class, "head1"))); // assertEquals("application/vnd.ms-excel", response.getContentType()); assertEquals("application/vnd.ms-excel", response.getHeader("Content-Type")); assertEquals("attachment; filename=\"test_20150123123456.xlsx\"", response.getHeader("Content-Disposition")); try (InputStream in = new ByteArrayInputStream(response.getContentAsByteArray()); Workbook workbook = WorkbookFactory.create(in); ExcelReader reader = new ExcelReader(workbook)) { String[] record; record = reader.read(); assertEquals(2, record.length); assertEquals("HEAD0", record[0]); assertEquals("HEAD1", record[1]); record = reader.read(); assertEquals(2, record.length); assertEquals("TEST00", record[0]); assertEquals("TEST01", record[1]); assertNull(reader.read()); } }
From source file:ch.ralscha.extdirectspring.util.ExtDirectSpringUtilTest.java
@Test public void testHandleCacheableResponseWithIfNoneMatch() throws IOException { byte[] data = "the response data".getBytes(); String etag = "\"0" + DigestUtils.md5DigestAsHex(data) + '"'; String contentType = "application/javascript;charset=UTF-8"; MockHttpServletRequest request = new MockHttpServletRequest(); request.addHeader("If-None-Match", etag); MockHttpServletResponse response = new MockHttpServletResponse(); ExtDirectSpringUtil.handleCacheableResponse(request, response, data, contentType); assertThat(response.getStatus()).isEqualTo(304); request = new MockHttpServletRequest(); request.addHeader("If-None-Match", etag); response = new MockHttpServletResponse(); data = "new response data".getBytes(); etag = "\"0" + DigestUtils.md5DigestAsHex(data) + '"'; ExtDirectSpringUtil.handleCacheableResponse(request, response, data, contentType); assertThat(response.getStatus()).isEqualTo(200); assertResponse(response, 5, etag, 6); assertThat(response.getContentLength()).isEqualTo(data.length); assertThat(response.getContentType()).isEqualTo(contentType); assertThat(response.getContentAsByteArray()).isEqualTo(data); }
From source file:com.nebhale.cyclinglibrary.web.GzipFilterTest.java
@Test public void gzipResponse() throws ServletException, IOException { MockHttpServletRequest request = new MockHttpServletRequest(); request.addHeader("Accept-Encoding", "gzip"); request.setContent("test-request-content".getBytes("UTF-8")); MockHttpServletResponse response = new MockHttpServletResponse(); FilterChain filterChain = mock(FilterChain.class); doAnswer(new Answer<Void>() { @Override/*from w w w.j a v a2s .com*/ public Void answer(InvocationOnMock invocation) throws Throwable { HttpServletRequest request = (HttpServletRequest) invocation.getArguments()[0]; assertEquals("test-request-content", readContent(request.getInputStream())); HttpServletResponse response = (HttpServletResponse) invocation.getArguments()[1]; writeContent("test-response-content", response.getOutputStream()); return null; } }).when(filterChain).doFilter(any(ServletRequest.class), any(ServletResponse.class)); this.filter.doFilterInternal(request, response, filterChain); assertEquals("test-response-content", gunzipContent(response.getContentAsByteArray())); }
From source file:com.comcast.video.dawg.show.video.VideoSnapTest.java
@Test public void testAddImagesToZipFile() throws IOException { String imageIds[] = { "", "", "" }; MockHttpSession session = new MockHttpSession(); MockHttpServletResponse response = new MockHttpServletResponse(); UniqueIndexedCache<BufferedImage> imgCache = ClientCache.getClientCache(session).getImgCache(); imageIds[0] = imgCache.storeItem(PC_IMG); imageIds[1] = imgCache.storeItem(PC_IMG); imageIds[2] = null;/* w w w .j av a2 s .c o m*/ String deviceIds[] = { "000000000001", "000000000002", "000000000003" }; int expectedZipSizeInBytes = 28954; VideoSnap videoSnap = new VideoSnap(); videoSnap.addImagesToZipFile(imageIds, deviceIds, response, session); Assert.assertEquals(response.getContentType(), "application/zip", "Content type is not in zip format"); String header = response.getHeader("Content-Disposition"); Assert.assertTrue(header.contains("attachment"), "Response header does not indicates attachment"); Assert.assertTrue(header.contains(".zip"), "Attached file is not in zip format"); Assert.assertEquals(response.getContentAsByteArray().length, expectedZipSizeInBytes, "Zip file size in http response is not matching with expected zip size."); }
From source file:io.spring.initializr.web.test.MockMvcClientHttpRequestFactory.java
@Override public ClientHttpRequest createRequest(final URI uri, final HttpMethod httpMethod) throws IOException { return new MockClientHttpRequest(httpMethod, uri) { @Override//from www . j a va 2s . c o m public ClientHttpResponse executeInternal() throws IOException { try { MockHttpServletRequestBuilder requestBuilder = request(httpMethod, uri.toString()); requestBuilder.content(getBodyAsBytes()); requestBuilder.headers(getHeaders()); MockHttpServletResponse servletResponse = actions(requestBuilder).andReturn().getResponse(); HttpStatus status = HttpStatus.valueOf(servletResponse.getStatus()); if (status.value() >= 400) { requestBuilder = request(HttpMethod.GET, "/error") .requestAttr(RequestDispatcher.ERROR_STATUS_CODE, status.value()) .requestAttr(RequestDispatcher.ERROR_REQUEST_URI, uri.toString()); if (servletResponse.getErrorMessage() != null) { requestBuilder.requestAttr(RequestDispatcher.ERROR_MESSAGE, servletResponse.getErrorMessage()); } // Overwrites the snippets from the first request servletResponse = actions(requestBuilder).andReturn().getResponse(); } byte[] body = servletResponse.getContentAsByteArray(); HttpHeaders headers = getResponseHeaders(servletResponse); MockClientHttpResponse clientResponse = new MockClientHttpResponse(body, status); clientResponse.getHeaders().putAll(headers); return clientResponse; } catch (Exception ex) { throw new IllegalStateException(ex); } } }; }