List of usage examples for org.springframework.mock.web MockHttpServletResponse getHeader
@Override
@Nullable
public String getHeader(String name)
From source file:cherry.foundation.download.TableDownloadTemplateTest.java
@Test public void testDownloadXlsxNoHeader() throws InvalidFormatException, IOException { // // w w w . j a v a2s.c o 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:cherry.foundation.download.TableDownloadTemplateTest.java
@Test public void testDownloadXlsWithHeader() throws InvalidFormatException, IOException { // /*from w w w. ja v a 2 s. c o m*/ 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:com.tasktop.c2c.server.common.service.tests.ajp.AjpProtocolTest.java
private void assertResponseIsExpected(Payload expectedPayload, MockHttpServletResponse response) throws UnsupportedEncodingException { Assert.assertEquals(expectedPayload.responseCode, response.getStatus()); for (Entry<String, String> header : expectedPayload.getResponseHeaders().entrySet()) { Assert.assertEquals(header.getValue(), response.getHeader(header.getKey())); }//from ww w . jav a 2s .com if (expectedPayload.binaryContent != null) { Assert.assertArrayEquals(expectedPayload.binaryContent, response.getContentAsByteArray()); } else if (expectedPayload.characterContent != null) { Assert.assertEquals(expectedPayload.characterContent, response.getContentAsString()); } }
From source file:com.redblackit.web.server.EchoServletTest.java
/** * Do test//from w w w.ja v a2s . c o m * * @param method * @param hasBody if content should be expected */ private void doTest(String method, boolean hasBody) throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); request.setMethod(method); request.setRequestURI(this.requestURI); final String msg = "doTest:" + method + ":hasBody=" + hasBody; logger.debug(msg + ":this=" + this); for (String headerName : headersMap.keySet()) { List<String> values = headersMap.get(headerName); if (values.size() == 1) { request.addHeader(headerName, values.get(0)); } else { request.addHeader(headerName, values); } Enumeration<String> headerValues = request.getHeaders(headerName); int hi = 0; while (headerValues.hasMoreElements()) { logger.debug(msg + "request:header[" + headerName + "," + hi + "]=" + headerValues.nextElement()); ++hi; } Assert.assertTrue(msg + "TEST ERROR:request:header[" + headerName + "]=" + values + ":shouldn't be empty (" + values.getClass() + ")", hi > 0); } int expectedContentLength = 0; if (hasBody && body != null && body.length() > 0) { request.setContent(body.getBytes()); expectedContentLength = request.getContentLength(); } MockHttpServletResponse response = new MockHttpServletResponse(); echoServlet.service(request, response); String responseBody = response.getContentAsString(); Assert.assertEquals("response code:" + response, HttpServletResponse.SC_OK, response.getStatus()); Assert.assertEquals("requestURI and Location", requestURI, response.getHeader("Location")); Map<String, List<String>> responseHeadersMap = new TreeMap<String, List<String>>(); for (String headerName : response.getHeaderNames()) { List<String> values = response.getHeaders(headerName); int hi = 0; for (String value : values) { logger.debug(msg + ":response:header[" + headerName + "," + hi + "]=" + value); ++hi; } if (hi == 0) { logger.debug(msg + ":response:header[" + headerName + "]=" + values + ":is empty (" + values.getClass() + ")"); values = Arrays.asList(new String[] { "" }); } if (!(headerName.equals("Location") || headerName.equals("Content-Length"))) { responseHeadersMap.put(headerName, values); } } Assert.assertEquals("headers (excluding Location and Content-Length)", headersMap, responseHeadersMap); if (hasBody) { Assert.assertEquals("body", (body == null ? "" : body), responseBody); } else { Assert.assertEquals("body", "", responseBody); } Assert.assertEquals("contentLength", expectedContentLength, response.getContentLength()); }
From source file:com.google.api.server.spi.response.ServletResponseResultWriterTest.java
@Test public void testWriteErrorResponseHeaders() throws Exception { MockHttpServletResponse response = new MockHttpServletResponse(); ServletResponseResultWriter writer = new ServletResponseResultWriter(response, null); Map<String, String> headers = new LinkedHashMap<>(); headers.put("name0", "value0"); headers.put("name1", "value1"); writer.writeError(new UnauthorizedException("message", "schema", headers)); assertEquals("schema name0=value0, name1=value1", response.getHeader("WWW-Authenticate")); }
From source file:org.springframework.data.rest.tests.mongodb.MongoWebTests.java
/** * @see DATAREST-506/*from w ww . j a v a2 s .c o m*/ */ @Test public void supportsConditionalGetsOnItemResource() throws Exception { Receipt receipt = new Receipt(); receipt.amount = new BigDecimal(50); receipt.saleItem = "Springy Tacos"; Link receiptsLink = client.discoverUnique("receipts"); MockHttpServletResponse response = postAndGet(receiptsLink, mapper.writeValueAsString(receipt), MediaType.APPLICATION_JSON); Link receiptLink = client.getDiscoverer(response).findLinkWithRel("self", response.getContentAsString()); mvc.perform(get(receiptLink.getHref()).header(IF_MODIFIED_SINCE, response.getHeader(LAST_MODIFIED))).// andExpect(status().isNotModified()).// andExpect(header().string(ETAG, is(notNullValue()))); mvc.perform(get(receiptLink.getHref()).header(IF_NONE_MATCH, response.getHeader(ETAG))).// andExpect(status().isNotModified()).// andExpect(header().string(ETAG, is(notNullValue()))); }
From source file:cherry.foundation.download.TableDownloadTemplateTest.java
@Test public void testDownloadCsvNoHeader() throws IOException { // //from w ww . j a v a 2 s .c o m MockHttpServletResponse response = new MockHttpServletResponse(); // tableDownloadOperation.downloadCsv(response, StandardCharsets.UTF_8, "test_{0}.csv", new LocalDateTime(2015, 1, 23, 12, 34, 56), null, createCommonClause(), createOrderByClause(), constant("TEST00")); // assertEquals("text/csv", response.getContentType()); assertEquals("UTF-8", response.getCharacterEncoding()); assertEquals("text/csv;charset=UTF-8", response.getHeader("Content-Type")); assertEquals("attachment; filename=\"test_20150123123456.csv\"", response.getHeader("Content-Disposition")); assertEquals("\"TEST00\"\r\n", response.getContentAsString()); }
From source file:cherry.foundation.download.TableDownloadTemplateTest.java
@Test public void testDownloadXlsxWithHeader() throws InvalidFormatException, IOException { // //w ww . ja v 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:cherry.foundation.download.TableDownloadTemplateTest.java
@Test public void testDownloadCsvWithHeader() throws IOException { // /*from w w w . ja v a 2 s.c om*/ MockHttpServletResponse response = new MockHttpServletResponse(); // tableDownloadOperation.downloadCsv(response, StandardCharsets.UTF_8, "test_{0}.csv", new LocalDateTime(2015, 1, 23, 12, 34, 56), asList("HEAD0"), createCommonClause(), createOrderByClause(), constant("TEST00")); // assertEquals("text/csv", response.getContentType()); assertEquals("UTF-8", response.getCharacterEncoding()); assertEquals("text/csv;charset=UTF-8", response.getHeader("Content-Type")); assertEquals("attachment; filename=\"test_20150123123456.csv\"", response.getHeader("Content-Disposition")); assertEquals("\"HEAD0\"\r\n\"TEST00\"\r\n", response.getContentAsString()); }
From source file:org.springsource.restbucks.payment.web.PaymentProcessIntegrationTest.java
/** * Creates a new {@link Order} by looking up the orders link from the source and posting the content of * {@code orders.json} to it. Verifies we get receive a {@code 201 Created} and a {@code Location} header. Follows the * location header to retrieve the {@link Order} just created. * /* w w w . j a va 2s . c o m*/ * @param source * @return * @throws Exception */ private MockHttpServletResponse createNewOrder(MockHttpServletResponse source) throws Exception { String content = source.getContentAsString(); Link ordersLink = getDiscovererFor(source).findLinkWithRel(ORDERS_REL, content); ClassPathResource resource = new ClassPathResource("order.json"); byte[] data = Files.readAllBytes(resource.getFile().toPath()); MockHttpServletResponse result = mvc .perform(post(ordersLink.expand().getHref()).contentType(MediaType.APPLICATION_JSON).content(data)). // andExpect(status().isCreated()). // andExpect(header().string("Location", is(notNullValue()))). // andReturn().getResponse(); return mvc.perform(get(result.getHeader("Location"))).andReturn().getResponse(); }