@Override public void sendRedirect(String url) throws IOException { Assert.state(!isCommitted(), "Cannot send redirect - response is already committed"); Assert.notNull(url, "Redirect URL must not be null"); setHeader(HttpHeaders.LOCATION, url); setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY); setCommitted(true); }
@Test public void buildStatusNotOk() throws Exception { this.response.setStatus(401); WebResponse webResponse = this.responseBuilder.build(); assertThat(webResponse.getStatusCode(), equalTo(401)); assertThat(webResponse.getStatusMessage(), equalTo("Unauthorized")); }
@Test public void testHttpStatusCodeResultMatchers() throws Exception { List<AssertionError> failures = new ArrayList<>(); for (HttpStatus status : HttpStatus.values()) { MockHttpServletResponse response = new MockHttpServletResponse(); response.setStatus(status.value()); MvcResult mvcResult = new StubMvcResult(request, null, null, null, null, null, response); try { Method method = getMethodForHttpStatus(status); ResultMatcher matcher = (ResultMatcher) ReflectionUtils.invokeMethod(method, this.matchers); try { matcher.match(mvcResult); } catch (AssertionError error) { failures.add(error); } } catch (Exception ex) { throw new Exception("Failed to obtain ResultMatcher for status " + status, ex); } } if (!failures.isEmpty()) { fail("Failed status codes: " + failures); } }
@Test // SPR-10414 @SuppressWarnings("deprecation") public void modifyStatusMessageAfterSendError() throws IOException { response.sendError(HttpServletResponse.SC_NOT_FOUND); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,"Server Error"); assertEquals(HttpServletResponse.SC_NOT_FOUND, response.getStatus()); }
public String performTwoSimpleRequests() throws ServletException, IOException { String path = "/authenticate/test"; setRequestData(path); for (int status : Arrays.asList(200, 500)) { response.setStatus(status); filter.doFilterInternal(request, response, chain); } return path; }
@Test // SPR-10414 public void modifyStatusAfterSendError() throws IOException { response.sendError(HttpServletResponse.SC_NOT_FOUND); response.setStatus(HttpServletResponse.SC_OK); assertEquals(HttpServletResponse.SC_NOT_FOUND, response.getStatus()); }
enigmaCookie.setSecure(true); this.response.setStatus(400, "error"); this.response.addHeader("header", "headerValue"); this.response.setContentType("text/plain");
@Test public void deserialize_summary() throws Exception { String path = "/some/path"; setRequestData(path); for (int status : Arrays.asList(200,500)) { response.setStatus(status); filter.doFilterInternal(request, response, chain); } Map<String, String> summary = filter.getSummary(); MetricsQueue metricSummary = readValue(summary.get(filter.getUriGroup(request).getGroup()), MetricsQueue.class); assertEquals(2, metricSummary.getTotals().getCount()); }
@Test public void statusRanges() throws Exception { for (HttpStatus status : HttpStatus.values()) { MockHttpServletResponse response = new MockHttpServletResponse(); response.setStatus(status.value()); MvcResult mvcResult = new StubMvcResult(request, null, null, null, null, null, response); switch (status.series().value()) { case 1: this.matchers.is1xxInformational().match(mvcResult); break; case 2: this.matchers.is2xxSuccessful().match(mvcResult); break; case 3: this.matchers.is3xxRedirection().match(mvcResult); break; case 4: this.matchers.is4xxClientError().match(mvcResult); break; case 5: this.matchers.is5xxServerError().match(mvcResult); break; default: fail("Unexpected range for status code value " + status); } } }
@Test public void basicResponse() { MockHttpServletResponse response = new MockHttpServletResponse(); response.setStatus(HttpServletResponse.SC_OK); OperationResponse operationResponse = this.factory.convert(response); assertThat(operationResponse.getStatus()).isEqualTo(HttpStatus.OK); }
@Test public void responseWithCookie() { MockHttpServletResponse response = new MockHttpServletResponse(); response.setStatus(HttpServletResponse.SC_OK); Cookie cookie = new Cookie("name", "value"); cookie.setDomain("localhost"); cookie.setHttpOnly(true); response.addCookie(cookie); OperationResponse operationResponse = this.factory.convert(response); assertThat(operationResponse.getHeaders()).hasSize(1); assertThat(operationResponse.getHeaders()).containsEntry(HttpHeaders.SET_COOKIE, Collections.singletonList("name=value; Domain=localhost; HttpOnly")); }
@Override public void sendRedirect(String url) throws IOException { Assert.state(!isCommitted(), "Cannot send redirect - response is already committed"); Assert.notNull(url, "Redirect URL must not be null"); setHeader(HttpHeaders.LOCATION, url); setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY); setCommitted(true); }
@Override public void sendRedirect(String url) throws IOException { if (isCommitted()) { throw new IllegalStateException("Cannot send redirect - response is already committed"); } Assert.notNull(url, "Redirect URL must not be null"); setHeader(LOCATION_HEADER, url); setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY); setCommitted(true); }
@Test public void afterCompletion_populatesTheCEFLogObject() { final Authentication authentication = mock(Authentication.class); when(authentication.getName()).thenReturn("foo"); request.setUserPrincipal(authentication); request.setAuthType(CLIENT_CERT_AUTH); response.setStatus(200); subject.afterCompletion(request, response, new Object(), null); assertThat(auditRecord.getUsername(), is(equalTo("foo"))); assertThat(auditRecord.getHttpStatusCode(), is(equalTo(200))); assertThat(auditRecord.getResult(), is(equalTo("success"))); assertThat(auditRecord.getAuthMechanism(), is(equalTo(CLIENT_CERT_AUTH))); }
response.setStatus(500);