@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()); }
@Test public void sendRedirect() throws IOException { String redirectUrl = "/redirect"; response.sendRedirect(redirectUrl); assertEquals(HttpServletResponse.SC_MOVED_TEMPORARILY, response.getStatus()); assertEquals(redirectUrl, response.getHeader("Location")); assertEquals(redirectUrl, response.getRedirectedUrl()); assertTrue(response.isCommitted()); }
@Test public void doFilter() throws Exception { SecurityContext context = SecurityContextHolder.getContext(); request.getSession().setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, context); springSecurityFilterChain.doFilter(request, response, chain); assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_OK); }
@Test public void testNullRequest() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest("GET", "/test-get"); MockHttpServletResponse response = new MockHttpServletResponse(); jsonRpcServer.handle(request, response); assertTrue(MockHttpServletResponse.SC_BAD_REQUEST == response.getStatus()); }
@Test public void user_config_is_returning_error() throws Exception { when(googleAuthenticator.getUserGoogleMfaCredentials(anyString(), anyString())).thenReturn(null); filter.doFilterInternal(request, response, chain); assertThat(response.getStatus(), equalTo(400)); assertThat(response.getHeader(HttpHeaders.CONTENT_TYPE), equalTo(MediaType.APPLICATION_JSON_VALUE)); assertNotNull(response.getContentAsString()); JsonAssert.with(response.getContentAsString()) .assertThat("error", equalTo("invalid_request")) .assertThat("error_description", equalTo("User must register a multi-factor authentication token")); verify(publisher, times(1)).publishEvent(any(MfaAuthenticationFailureEvent.class)); verify(publisher, times(1)).publishEvent(any(ApplicationEvent.class)); }
private void validateView(View view, HttpStatus status) { MockHttpServletResponse response = new MockHttpServletResponse(); try { view.render(new HashMap<String, Object>(), new MockHttpServletRequest(), response); assertNotNull(response.getContentAsString()); } catch (Exception e) { fail("view should render correct status and body"); } assertEquals(status.value(), response.getStatus()); }
private void checkSuccessfulResponse(MockHttpServletResponse response) throws IOException { assertTrue(HttpServletResponse.SC_OK == response.getStatus()); JsonNode responseEnvelope = decodeAnswer(toByteArrayOutputStream(response.getContentAsByteArray())); assertTrue(responseEnvelope.get(ID).isIntegralNumber()); assertEquals(responseEnvelope.get(ID).asLong(), 123L); assertTrue(responseEnvelope.get(RESULT).isTextual()); assertEquals(responseEnvelope.get(RESULT).asText(), "For?est"); }
@Test public void test_redirect_contains_error() throws Exception { request.setParameter(OAuth2Utils.REDIRECT_URI, redirectUrl); uaaAuthorizationEndpoint.commence(request, response, authException); assertEquals(HttpStatus.FOUND.value(), response.getStatus()); assertTrue(response.getHeader("Location").contains("error=login_required")); }
@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()); }
@Test public void doFilter() throws Exception { SecurityContext context = SecurityContextHolder.getContext(); request.getSession().setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, context); springSecurityFilterChain.doFilter(request, response, chain); assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_OK); }
@Test public void testFailedAddWithCommandExecutionException() { // Arrange controller.commandGateway = gateway; //cheating a bit here, but mocking all the axon framework's beans is a pain. when(gateway.sendAndWait(any())).thenThrow(CommandExecutionException.class); //Act controller.add(UUID.randomUUID().toString(), "Test Add Product", mockHttpServletResponse); //Assert verify(gateway).sendAndWait(any()); assertTrue(mockHttpServletResponse.getStatus() == HttpServletResponse.SC_BAD_REQUEST); }
@Test public void testHandleWhenNotLoggedInAndNoCsrf() throws Exception { AccessDeniedException ex = new MissingCsrfTokenException("something"); handler.handle(request, response, ex); assertEquals(HttpServletResponse.SC_FOUND, response.getStatus()); assertSame(request.getAttribute(WebAttributes.ACCESS_DENIED_403), ex); assertTrue(response.isCommitted()); assertEquals("http://localhost/login", response.getHeader("Location")); assertEquals(HttpServletResponse.SC_MOVED_TEMPORARILY, response.getStatus()); }
@Test public void testEncoding() throws Exception { for (String[] path : paths) { MockHttpServletResponse response = request(path, null); assertEquals(Arrays.toString(path), 200, response.getStatus()); assertEquals(path[path.length - 1], response.getContentAsString()); } }
@Test public void doFilterDenied() throws Exception { setup("DENIED"); SecurityContext context = SecurityContextHolder.getContext(); request.getSession().setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, context); springSecurityFilterChain.doFilter(request, response, chain); assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_FORBIDDEN); }
@Test public void testAddWithGoodRequestParams() { // Arrange controller.commandGateway = gateway; //cheating a bit here, but mocking all the axon framework's beans is a pain. when(gateway.sendAndWait(any())).thenReturn(null); //Act controller.add(UUID.randomUUID().toString(), "Test Add Product", mockHttpServletResponse); //Assert verify(gateway).sendAndWait(any()); assertTrue(mockHttpServletResponse.getStatus() == HttpServletResponse.SC_CREATED); }
@Test public void service_when_failure() throws Exception { Mockito.doThrow(new RuntimeException("some app error", new SQLException("db error"))).when(delegate).init(any()); servlet.setDelegate(delegate); servlet.init(mock(ServletConfig.class)); servlet.service(request, response); assertEquals(HttpServletResponse.SC_SERVICE_UNAVAILABLE, response.getStatus()); verify(delegate, times(1)).init(any()); verify(delegate, times(0)).service(any(), any()); assertNotNull(response.getHeader(RecognizeFailureDispatcherServlet.HEADER)); assertEquals(RecognizeFailureDispatcherServlet.HEADER_MSG, response.getHeader(RecognizeFailureDispatcherServlet.HEADER)); } @Test
@Test public void test_client_not_found() throws Exception { reset(clientDetailsService); when(clientDetailsService.loadClientByClientId(anyString(), anyString())).thenThrow(new NoSuchClientException("not found")); uaaAuthorizationEndpoint.commence(request, response, authException); assertEquals(HttpStatus.BAD_REQUEST.value(), response.getStatus()); }
@Test public void doFilterDenied() throws Exception { setup("DENIED"); SecurityContext context = SecurityContextHolder.getContext(); request.getSession().setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, context); springSecurityFilterChain.doFilter(request, response, chain); assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_FORBIDDEN); }
@Test public void testFailedAddWithAssertionError() { // Arrange controller.commandGateway = gateway; //cheating a bit here, but mocking all the axon framework's beans is a pain. when(gateway.sendAndWait(any())).thenThrow(AssertionError.class); //Act controller.add(UUID.randomUUID().toString(), "Test Add Product", mockHttpServletResponse); //Assert verify(gateway).sendAndWait(any()); assertTrue(mockHttpServletResponse.getStatus() == HttpServletResponse.SC_BAD_REQUEST); }
@Test public void testGetStaticResource() throws Exception { request.setRequestURI("/stagemonitor/static/test.html"); fileServlet.service(request, response); assertEquals(200, response.getStatus()); assertEquals("test", response.getContentAsString()); assertTrue(response.getContentType().equals("text/html") || response.getContentType().equals("application/octet-stream")); }