List of usage examples for org.springframework.mock.web MockHttpServletResponse getHeader
@Override
@Nullable
public String getHeader(String name)
From source file:org.terasoluna.gfw.web.logging.mdc.XTrackMDCPutFilterTest.java
@Test public void testGetMDCValue_changed_by_initParam_set_in_http_request() throws ServletException { mockFilterConfig.addInitParameter("attributeName", "X-Hoge"); xTrackMDCPutFilter.init(mockFilterConfig); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); request.addHeader("X-Hoge", "12345678901234567890123456789012"); String xTrack = xTrackMDCPutFilter.getMDCValue(request, response); assertThat(xTrack, is(notNullValue())); assertThat(xTrack, is("12345678901234567890123456789012")); assertThat(response.getHeader("X-Hoge"), is("12345678901234567890123456789012")); assertThat((String) request.getAttribute("X-Hoge"), is("12345678901234567890123456789012")); }
From source file:org.sventon.export.DefaultExportExecutorTest.java
@Test public void testPrepareResponse() throws Exception { final ConfigDirectory configDirectoryMock = createMock(ConfigDirectory.class); final DefaultExportExecutor exportExecutor = new DefaultExportExecutor(configDirectoryMock); final MockHttpServletRequest request = new MockHttpServletRequest(); final MockHttpServletResponse response = new MockHttpServletResponse(); assertEquals(0, response.getHeaderNames().size()); assertNull(response.getContentType()); exportExecutor.prepareResponse(request, response, new File("testfile")); assertEquals(1, response.getHeaderNames().size()); assertEquals("attachment; filename=\"testfile\"", response.getHeader(WebUtils.CONTENT_DISPOSITION_HEADER)); assertEquals(WebUtils.APPLICATION_OCTET_STREAM, response.getContentType()); }
From source file:org.terasoluna.gfw.web.logging.mdc.XTrackMDCPutFilterTest.java
@Test public void testGetMDCValue_default_attributeName_set_in_http_request_too_long_length() throws ServletException { xTrackMDCPutFilter.init(mockFilterConfig); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); request.addHeader("X-Track", "12345678901234567890123456789012345678901234567890"); String xTrack = xTrackMDCPutFilter.getMDCValue(request, response); assertThat(xTrack, is(notNullValue())); assertThat(xTrack, is("12345678901234567890123456789012")); assertThat(response.getHeader("X-Track"), is("12345678901234567890123456789012")); assertThat((String) request.getAttribute("X-Track"), is("12345678901234567890123456789012")); }
From source file:org.sventon.web.ctrl.template.GetFileControllerTest.java
@Test public void testSvnHandleGetImageAsInline() throws Exception { final BaseCommand command = new BaseCommand(); command.setName(repositoryName);/*from ww w . j a va2 s. com*/ command.setPath("/testimage.gif"); final GetFileController ctrl = new GetFileController(); ctrl.setApplication(application); ctrl.setRepositoryService(mock(RepositoryService.class)); final ConfigurableMimeFileTypeMap mftm = new ConfigurableMimeFileTypeMap(); mftm.afterPropertiesSet(); ctrl.setMimeFileTypeMap(mftm); final ModelAndView modelAndView; request.addParameter(GetFileController.DISPLAY_REQUEST_PARAMETER, GetFileController.CONTENT_DISPOSITION_INLINE); final MockHttpServletResponse res = new MockHttpServletResponse(); modelAndView = ctrl.svnHandle(null, command, 100, null, request, res, null); assertNull(modelAndView); assertEquals("image/gif", res.getContentType()); assertTrue(((String) res.getHeader(WebUtils.CONTENT_DISPOSITION_HEADER)).startsWith("inline")); }
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:org.sventon.web.ctrl.template.GetFileControllerTest.java
@Test public void testSvnHandleGetFile() throws Exception { final BaseCommand command = new BaseCommand(); command.setName(repositoryName);// w w w .j a v a2 s .c om command.setPath("/testfile.txt"); final GetFileController ctrl = new GetFileController(); ctrl.setApplication(application); ctrl.setRepositoryService(mock(RepositoryService.class)); final ConfigurableMimeFileTypeMap mftm = new ConfigurableMimeFileTypeMap(); mftm.afterPropertiesSet(); ctrl.setMimeFileTypeMap(mftm); final ModelAndView modelAndView; final MockHttpServletResponse res = new MockHttpServletResponse(); ctrl.setServletContext(mockServletContext); modelAndView = ctrl.svnHandle(null, command, 100, null, request, res, null); assertNull(modelAndView); assertEquals("text/plain", res.getContentType()); assertTrue(((String) res.getHeader(WebUtils.CONTENT_DISPOSITION_HEADER)).startsWith("attachment")); }
From source file:org.sventon.web.ctrl.template.GetFileControllerTest.java
@Test public void testSvnHandleGetFileAsAttachment() throws Exception { final BaseCommand command = new BaseCommand(); command.setName(repositoryName);//w w w. j a va 2 s . c om command.setPath("/testimage.gif"); final GetFileController ctrl = new GetFileController(); ctrl.setApplication(application); ctrl.setRepositoryService(mock(RepositoryService.class)); request.addParameter(GetFileController.DISPLAY_REQUEST_PARAMETER, (String) null); final MockHttpServletResponse mockResponse = new MockHttpServletResponse(); final ModelAndView modelAndView = ctrl.svnHandle(null, command, 100, null, request, mockResponse, null); assertNull(modelAndView); assertEquals(WebUtils.APPLICATION_OCTET_STREAM, mockResponse.getContentType()); assertTrue(((String) mockResponse.getHeader(WebUtils.CONTENT_DISPOSITION_HEADER)).startsWith("attachment")); }
From source file:test.com.tsc9526.monalisa.service.actions.AbstractActionTest.java
protected Response getRespone(MockHttpServletRequest req) throws Exception { MockHttpServletResponse resp = new MockHttpServletResponse(); req.addHeader("DEV_TEST", "true"); DbQueryHttpServlet ms = new DbQueryHttpServlet(); ms.service(req, resp);//from ww w . j a v a 2 s. co m String type = resp.getContentType(); Assert.assertTrue(type.indexOf("json") > 0 && type.indexOf("utf-8") > 0); String body = resp.getContentAsString(); Response r = Response.fromJson(body); if (req.getParameter("page") != null && r.getStatus() == 200) { int total = Integer.parseInt(resp.getHeader("X-Total-Page")); Assert.assertTrue(total >= 0); } return r; }
From source file:com.xemantic.tadedon.guice.servlet.mock.FakeServletContainerTest.java
/** * Tests {@link SessionIdReturningServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)}. * * @throws IOException if an input or output error is detected during request handling * @throws ServletException if container cannot handle request. *///from w w w . j a v a 2 s . c o m @Test public void shouldPreserveSessionAcrossRequest() throws IOException, ServletException { // given servletContainer.newSession("42"); // magic number MockHttpServletRequest request1 = servletContainer.newRequest("GET", "/SessionIdReturningServlet"); MockHttpServletResponse response1 = new MockHttpServletResponse(); MockHttpServletRequest request2 = servletContainer.newRequest("GET", "/SessionIdReturningServlet"); MockHttpServletResponse response2 = new MockHttpServletResponse(); // when servletContainer.service(request1, response1); servletContainer.service(request2, response2); // then assertThat(response1.getContentAsString(), is("42")); assertThat((String) response1.getHeader("Foo"), is("foo")); assertThat(response2.getContentAsString(), is("42")); assertThat((String) response2.getHeader("Foo"), is("foo")); }
From source file:cherry.foundation.download.TableDownloadTemplateTest.java
@Test public void testDownloadXlsNoHeader() throws InvalidFormatException, IOException { // /*w w w . j av a2 s .c o 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()); } }