List of usage examples for org.springframework.mock.web MockHttpServletResponse getHeader
@Override
@Nullable
public String getHeader(String name)
From source file:ch.silviowangler.dox.web.DocumentControllerTest.java
@Test public void getTheDocumentContent() throws DocumentNotFoundException, DocumentNotInStoreException, UnsupportedEncodingException { MockHttpServletResponse response = new MockHttpServletResponse(); PhysicalDocument physicalDocument = new PhysicalDocument(new DocumentClass("hhh"), "hello".getBytes(), null, "hello.txt"); physicalDocument.setMimeType("aaa/bbb"); when(documentService.findPhysicalDocument(1L)).thenReturn(physicalDocument); controller.getDocument(1L, response); assertThat(response.getStatus(), is(SC_OK)); assertThat(response.getContentAsString(), is("hello")); assertThat(response.getContentType(), is("aaa/bbb")); assertThat(response.getHeader("Content-Disposition"), is("inline; filename=\"hello.txt\"")); }
From source file:org.springsource.restbucks.training.payment.web.PaymentProcessIntegrationTest.java
/** * Polls the order resource every 2 seconds and uses an {@code If-None-Match} header alongside the {@code ETag} of the * first response to avoid sending the representation over and over again. * //w w w . j a v a 2 s.co m * @param response * @return * @throws Exception */ private MockHttpServletResponse pollUntilOrderHasReceiptLink(MockHttpServletResponse response) throws Exception { // Grab String content = response.getContentAsString(); LinkDiscoverer discoverer = getDiscovererFor(response); Link orderLink = discoverer.findLinkWithRel(ORDER_REL, content); // Poll order until receipt link is set Link receiptLink = null; String etag = null; MockHttpServletResponse pollResponse; do { HttpHeaders headers = new HttpHeaders(); if (etag != null) { headers.setIfNoneMatch(etag); } log.info("Poll state of order until receipt is ready"); ResultActions action = mvc.perform(get(orderLink.getHref()).headers(headers)); pollResponse = action.andReturn().getResponse(); int status = pollResponse.getStatus(); etag = pollResponse.getHeader("ETag"); log.info(String.format("Received %s with ETag of %s", status, etag)); if (status == HttpStatus.OK.value()) { action.andExpect(linkWithRelIsPresent(Link.REL_SELF)). // andExpect(linkWithRelIsNotPresent(UPDATE_REL)). // andExpect(linkWithRelIsNotPresent(CANCEL_REL)); receiptLink = discoverer.findLinkWithRel(RECEIPT_REL, pollResponse.getContentAsString()); } else if (status == HttpStatus.NO_CONTENT.value()) { action.andExpect(content().string(isEmptyOrNullString())); } if (receiptLink == null) { Thread.sleep(2000); } } while (receiptLink == null); return pollResponse; }
From source file:org.springsource.restbucks.payment.web.PaymentProcessIntegrationTest.java
/** * Polls the order resource every 2 seconds and uses an {@code If-None-Match} header alongside the {@code ETag} of the * first response to avoid sending the representation over and over again. * /* w ww .j a va 2 s . c om*/ * @param response * @return * @throws Exception */ private MockHttpServletResponse pollUntilOrderHasReceiptLink(MockHttpServletResponse response) throws Exception { // Grab String content = response.getContentAsString(); LinkDiscoverer discoverer = getDiscovererFor(response); Link orderLink = discoverer.findLinkWithRel(ORDER_REL, content); // Poll order until receipt link is set Link receiptLink = null; String etag = null; MockHttpServletResponse pollResponse; do { HttpHeaders headers = new HttpHeaders(); if (etag != null) { headers.setIfNoneMatch(etag); } LOG.info("Poll state of order until receipt is ready"); ResultActions action = mvc.perform(get(orderLink.expand().getHref()).headers(headers)); pollResponse = action.andReturn().getResponse(); int status = pollResponse.getStatus(); etag = pollResponse.getHeader("ETag"); LOG.info(String.format("Received %s with ETag of %s", status, etag)); if (status == HttpStatus.OK.value()) { action.andExpect(linkWithRelIsPresent(Link.REL_SELF)). // andExpect(linkWithRelIsNotPresent(UPDATE_REL)). // andExpect(linkWithRelIsNotPresent(CANCEL_REL)); receiptLink = discoverer.findLinkWithRel(RECEIPT_REL, pollResponse.getContentAsString()); } else if (status == HttpStatus.NO_CONTENT.value()) { action.andExpect(content().string(isEmptyOrNullString())); } if (receiptLink == null) { Thread.sleep(2000); } } while (receiptLink == null); return pollResponse; }
From source file:com.liferay.document.library.webdav.test.BaseWebDAVTestCase.java
public Tuple service(String method, String path, Map<String, String> headers, byte[] data) { WebDAVServlet webDAVServlet = new WebDAVServlet(); String requestURI = _CONTEXT_PATH + _SERVLET_PATH + _PATH_INFO_PREFACE + path; MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest(method, requestURI); mockHttpServletRequest.setContextPath(_CONTEXT_PATH); mockHttpServletRequest.setServletPath(_SERVLET_PATH); mockHttpServletRequest.setPathInfo(_PATH_INFO_PREFACE + path); try {/*from w w w . j a v a 2 s .c o m*/ mockHttpServletRequest.setRemoteUser(String.valueOf(TestPropsValues.getUserId())); } catch (Exception e) { Assert.fail("User ID cannot be initialized"); } if (headers == null) { headers = new HashMap<>(); } headers.put(HttpHeaders.USER_AGENT, getUserAgent()); try { throw new Exception(); } catch (Exception e) { StackTraceElement[] stackTraceElements = e.getStackTrace(); for (StackTraceElement stackTraceElement : stackTraceElements) { String methodName = stackTraceElement.getMethodName(); if (methodName.equals("setUp") || methodName.equals("tearDown") || methodName.startsWith("test")) { String testName = StringUtil.extractLast(stackTraceElement.getClassName(), CharPool.PERIOD); testName = StringUtil.removeSubstrings(testName, "WebDAV", "Test"); headers.put("X-Litmus", testName + ": (" + stackTraceElement.getMethodName() + ":" + stackTraceElement.getLineNumber() + ")"); break; } } } if (data != null) { mockHttpServletRequest.setContent(data); String contentType = headers.remove(HttpHeaders.CONTENT_TYPE); if (contentType != null) { mockHttpServletRequest.setContentType(contentType); } else { mockHttpServletRequest.setContentType(ContentTypes.TEXT_PLAIN); } } for (Map.Entry<String, String> entry : headers.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); mockHttpServletRequest.addHeader(key, value); } try { MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse(); webDAVServlet.service(mockHttpServletRequest, mockHttpServletResponse); int statusCode = mockHttpServletResponse.getStatus(); byte[] responseBody = mockHttpServletResponse.getContentAsByteArray(); Map<String, String> responseHeaders = new HashMap<>(); for (String name : mockHttpServletResponse.getHeaderNames()) { responseHeaders.put(name, mockHttpServletResponse.getHeader(name)); } return new Tuple(statusCode, responseBody, responseHeaders); } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:com.enonic.cms.framework.util.HttpServletRangeUtilTest.java
@Test public void test_process_request_plain_last_range_minus() throws Exception { final MockHttpServletRequest httpServletRequest = new MockHttpServletRequest(); httpServletRequest.setMethod("GET"); httpServletRequest.setPathInfo("/input.dat"); httpServletRequest.addHeader(HttpHeaders.RANGE, "bytes=51-"); final MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse(); HttpServletRangeUtil.processRequest(httpServletRequest, mockHttpServletResponse, "input.dat", "application/pdf", INPUT_FILE, false); assertEquals("z", mockHttpServletResponse.getContentAsString()); assertEquals(HttpServletResponse.SC_PARTIAL_CONTENT, mockHttpServletResponse.getStatus()); assertEquals("application/pdf", mockHttpServletResponse.getContentType()); assertEquals("inline;filename=\"input.dat\"", mockHttpServletResponse.getHeader(HttpHeaders.CONTENT_DISPOSITION)); assertEquals("1", mockHttpServletResponse.getHeader(HttpHeaders.CONTENT_LENGTH)); }
From source file:com.enonic.cms.framework.util.HttpServletRangeUtilTest.java
@Test public void test_process_request_plain_range_minus() throws Exception { final MockHttpServletRequest httpServletRequest = new MockHttpServletRequest(); httpServletRequest.setMethod("GET"); httpServletRequest.setPathInfo("/input.dat"); httpServletRequest.addHeader(HttpHeaders.RANGE, "bytes=50-"); final MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse(); HttpServletRangeUtil.processRequest(httpServletRequest, mockHttpServletResponse, "input.dat", "application/pdf", INPUT_FILE, false); assertEquals("Zz", mockHttpServletResponse.getContentAsString()); assertEquals(HttpServletResponse.SC_PARTIAL_CONTENT, mockHttpServletResponse.getStatus()); assertEquals("application/pdf", mockHttpServletResponse.getContentType()); assertEquals("inline;filename=\"input.dat\"", mockHttpServletResponse.getHeader(HttpHeaders.CONTENT_DISPOSITION)); assertEquals("2", mockHttpServletResponse.getHeader(HttpHeaders.CONTENT_LENGTH)); }
From source file:com.enonic.cms.framework.util.HttpServletRangeUtilTest.java
@Test public void test_process_request_plain_one_range() throws Exception { final MockHttpServletRequest httpServletRequest = new MockHttpServletRequest(); httpServletRequest.setMethod("GET"); httpServletRequest.setPathInfo("/input.dat"); httpServletRequest.addHeader(HttpHeaders.RANGE, "bytes=0-5"); final MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse(); HttpServletRangeUtil.processRequest(httpServletRequest, mockHttpServletResponse, "input.dat", "application/pdf", INPUT_FILE, false); assertEquals("AaBbCc", mockHttpServletResponse.getContentAsString()); assertEquals(HttpServletResponse.SC_PARTIAL_CONTENT, mockHttpServletResponse.getStatus()); assertEquals("application/pdf", mockHttpServletResponse.getContentType()); assertEquals("inline;filename=\"input.dat\"", mockHttpServletResponse.getHeader(HttpHeaders.CONTENT_DISPOSITION)); assertEquals("6", mockHttpServletResponse.getHeader(HttpHeaders.CONTENT_LENGTH)); }
From source file:com.enonic.cms.framework.util.HttpServletRangeUtilTest.java
@Test public void test_gzip_plain() throws Exception { final MockHttpServletRequest httpServletRequest = new MockHttpServletRequest(); httpServletRequest.setMethod("GET"); httpServletRequest.setPathInfo("/input.js"); httpServletRequest.addHeader(HttpHeaders.ACCEPT_ENCODING, "gzip"); final MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse(); HttpServletRangeUtil.processRequest(httpServletRequest, mockHttpServletResponse, "input.js", "application/javascript", INPUT_FILE, false); assertTrue(mockHttpServletResponse.getContentAsByteArray().length > 0); assertEquals(HttpServletResponse.SC_OK, mockHttpServletResponse.getStatus()); assertEquals("application/javascript", mockHttpServletResponse.getContentType()); assertEquals("inline;filename=\"input.js\"", mockHttpServletResponse.getHeader(HttpHeaders.CONTENT_DISPOSITION)); assertEquals(null, mockHttpServletResponse.getHeader(HttpHeaders.CONTENT_LENGTH)); }
From source file:com.enonic.cms.framework.util.HttpServletRangeUtilTest.java
@Test public void test_process_request_plain_some_range() throws Exception { final MockHttpServletRequest httpServletRequest = new MockHttpServletRequest(); httpServletRequest.setMethod("GET"); httpServletRequest.setPathInfo("/input.dat"); httpServletRequest.addHeader(HttpHeaders.RANGE, "bytes=-48"); final MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse(); HttpServletRangeUtil.processRequest(httpServletRequest, mockHttpServletResponse, "input.dat", "application/pdf", INPUT_FILE, false); assertEquals("CcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz", mockHttpServletResponse.getContentAsString()); assertEquals(HttpServletResponse.SC_PARTIAL_CONTENT, mockHttpServletResponse.getStatus()); assertEquals("application/pdf", mockHttpServletResponse.getContentType()); assertEquals("inline;filename=\"input.dat\"", mockHttpServletResponse.getHeader(HttpHeaders.CONTENT_DISPOSITION)); assertEquals("48", mockHttpServletResponse.getHeader(HttpHeaders.CONTENT_LENGTH)); }
From source file:com.enonic.cms.framework.util.HttpServletRangeUtilTest.java
@Test public void test_process_request_plain_minus_range() throws Exception { final MockHttpServletRequest httpServletRequest = new MockHttpServletRequest(); httpServletRequest.setMethod("GET"); httpServletRequest.setPathInfo("/input.dat"); httpServletRequest.addHeader(HttpHeaders.RANGE, "bytes=-50"); final MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse(); HttpServletRangeUtil.processRequest(httpServletRequest, mockHttpServletResponse, "input.dat", "application/pdf", INPUT_FILE, false); assertEquals("BbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz", mockHttpServletResponse.getContentAsString()); assertEquals(HttpServletResponse.SC_PARTIAL_CONTENT, mockHttpServletResponse.getStatus()); assertEquals("application/pdf", mockHttpServletResponse.getContentType()); assertEquals("inline;filename=\"input.dat\"", mockHttpServletResponse.getHeader(HttpHeaders.CONTENT_DISPOSITION)); assertEquals("50", mockHttpServletResponse.getHeader(HttpHeaders.CONTENT_LENGTH)); }