List of usage examples for org.springframework.mock.web MockHttpServletResponse getStatus
@Override public int getStatus()
From source file:org.jasig.cas.support.oauth.web.OAuth20MetadataPrincipalControllerTests.java
@Test public void verifyNoTokenOrAuthHeader() throws Exception { final MockHttpServletRequest mockRequest = new MockHttpServletRequest("POST", CONTEXT + OAuthConstants.METADATA_URL); final MockHttpServletResponse mockResponse = new MockHttpServletResponse(); final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController(); oauth20WrapperController.afterPropertiesSet(); final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse); assertNull(modelAndView);/*from ww w .ja v a 2 s .co m*/ assertEquals(HttpStatus.SC_BAD_REQUEST, mockResponse.getStatus()); assertEquals(CONTENT_TYPE, mockResponse.getContentType()); final String expected = "{\"error\":\"" + OAuthConstants.MISSING_ACCESS_TOKEN + "\",\"error_description\":\"" + OAuthConstants.MISSING_ACCESS_TOKEN_DESCRIPTION + "\"}"; final ObjectMapper mapper = new ObjectMapper(); final JsonNode expectedObj = mapper.readTree(expected); final JsonNode receivedObj = mapper.readTree(mockResponse.getContentAsString()); assertEquals(expectedObj.get("error").asText(), receivedObj.get("error").asText()); assertEquals(expectedObj.get("error_description").asText(), receivedObj.get("error_description").asText()); }
From source file:org.jasig.cas.support.oauth.web.OAuth20MetadataPrincipalControllerTests.java
@Test public void verifyNoTokenAndAuthHeaderIsBlank() throws Exception { final MockHttpServletRequest mockRequest = new MockHttpServletRequest("POST", CONTEXT + OAuthConstants.METADATA_URL); mockRequest.addHeader("Authorization", ""); final MockHttpServletResponse mockResponse = new MockHttpServletResponse(); final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController(); oauth20WrapperController.afterPropertiesSet(); final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse); assertNull(modelAndView);/*from w w w.j a va2 s. c o m*/ assertEquals(HttpStatus.SC_BAD_REQUEST, mockResponse.getStatus()); assertEquals(CONTENT_TYPE, mockResponse.getContentType()); final String expected = "{\"error\":\"" + OAuthConstants.MISSING_ACCESS_TOKEN + "\",\"error_description\":\"" + OAuthConstants.MISSING_ACCESS_TOKEN_DESCRIPTION + "\"}"; final ObjectMapper mapper = new ObjectMapper(); final JsonNode expectedObj = mapper.readTree(expected); final JsonNode receivedObj = mapper.readTree(mockResponse.getContentAsString()); assertEquals(expectedObj.get("error").asText(), receivedObj.get("error").asText()); assertEquals(expectedObj.get("error_description").asText(), receivedObj.get("error_description").asText()); }
From source file:org.jasig.cas.support.oauth.web.OAuth20MetadataPrincipalControllerTests.java
@Test public void verifyNoTokenAndAuthHeaderIsMalformed() throws Exception { final MockHttpServletRequest mockRequest = new MockHttpServletRequest("POST", CONTEXT + OAuthConstants.METADATA_URL); mockRequest.addHeader("Authorization", "Let me in i am authorized"); final MockHttpServletResponse mockResponse = new MockHttpServletResponse(); final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController(); oauth20WrapperController.afterPropertiesSet(); final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse); assertNull(modelAndView);/*from w ww .j a va 2 s .c o m*/ assertEquals(HttpStatus.SC_BAD_REQUEST, mockResponse.getStatus()); assertEquals(CONTENT_TYPE, mockResponse.getContentType()); final String expected = "{\"error\":\"" + OAuthConstants.MISSING_ACCESS_TOKEN + "\",\"error_description\":\"" + OAuthConstants.MISSING_ACCESS_TOKEN_DESCRIPTION + "\"}"; final ObjectMapper mapper = new ObjectMapper(); final JsonNode expectedObj = mapper.readTree(expected); final JsonNode receivedObj = mapper.readTree(mockResponse.getContentAsString()); assertEquals(expectedObj.get("error").asText(), receivedObj.get("error").asText()); assertEquals(expectedObj.get("error_description").asText(), receivedObj.get("error_description").asText()); }
From source file:com.doitnext.http.router.responsehandlers.DefaultErrorHandlerTest.java
@Test public void testHandleResponseWithExceptionHandlerAnnotation() throws Exception { DefaultErrorHandler h = new DefaultErrorHandler(); MockHttpServletResponse response = new MockHttpServletResponse(); PathTemplate pt = null;// w w w . j a v a 2 s. co m Path path = new Path("a/b/c", "nice=winds", pt); Method method = this.getClass().getMethod("exceptionGeneratingMethod"); Route route = new Route(HttpMethod.GET, null, null, null, "application/json", pt, this.getClass(), method, null, this, null, h, false); PathMatch pm = new PathMatch(route, path); boolean handled = h.handleResponse(pm, null, response, new IllegalArgumentException()); Assert.assertTrue(handled); Assert.assertEquals(409, response.getStatus()); response.setStatus(0); handled = h.handleResponse(pm, null, response, new IOException()); Assert.assertTrue(handled); Assert.assertEquals(402, response.getStatus()); response.setStatus(0); handled = h.handleResponse(pm, null, response, new NullPointerException()); Assert.assertTrue(handled); Assert.assertEquals(500, response.getStatus()); }
From source file:org.jasig.cas.support.oauth.web.OAuth20RevokeClientPrincipalTokensControllerTests.java
@Test public void verifyOKWithAccessToken() throws Exception { final AccessToken accessToken = mock(AccessToken.class); when(accessToken.getId()).thenReturn(AT_ID); final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class); when(centralOAuthService.getToken(AT_ID, AccessToken.class)).thenReturn(accessToken); when(centralOAuthService.revokeClientPrincipalTokens(accessToken, CLIENT_ID)).thenReturn(true); final MockHttpServletRequest mockRequest = new MockHttpServletRequest("POST", CONTEXT + OAuthConstants.REVOKE_URL); mockRequest.setParameter(OAuthConstants.ACCESS_TOKEN, AT_ID); mockRequest.setParameter(OAuthConstants.CLIENT_ID, CLIENT_ID); final MockHttpServletResponse mockResponse = new MockHttpServletResponse(); final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController(); oauth20WrapperController.setCentralOAuthService(centralOAuthService); oauth20WrapperController.afterPropertiesSet(); final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse); assertNull(modelAndView);// w w w. jav a2 s. c om assertEquals(HttpStatus.SC_NO_CONTENT, mockResponse.getStatus()); assertNull(mockResponse.getContentType()); assertEquals("null", mockResponse.getContentAsString()); }
From source file:org.jasig.cas.support.oauth.web.OAuth20RevokeClientPrincipalTokensControllerTests.java
@Test public void verifyOKWithAuthHeader() throws Exception { final AccessToken accessToken = mock(AccessToken.class); when(accessToken.getId()).thenReturn(AT_ID); final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class); when(centralOAuthService.getToken(AT_ID, AccessToken.class)).thenReturn(accessToken); when(centralOAuthService.revokeClientPrincipalTokens(accessToken, CLIENT_ID)).thenReturn(true); final MockHttpServletRequest mockRequest = new MockHttpServletRequest("POST", CONTEXT + OAuthConstants.REVOKE_URL); mockRequest.addHeader("Authorization", OAuthConstants.BEARER_TOKEN + " " + AT_ID); mockRequest.setParameter(OAuthConstants.CLIENT_ID, CLIENT_ID); final MockHttpServletResponse mockResponse = new MockHttpServletResponse(); final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController(); oauth20WrapperController.setCentralOAuthService(centralOAuthService); oauth20WrapperController.afterPropertiesSet(); final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse); assertNull(modelAndView);//from w ww . jav a 2 s . c o m assertEquals(HttpStatus.SC_NO_CONTENT, mockResponse.getStatus()); assertNull(mockResponse.getContentType()); assertEquals("null", mockResponse.getContentAsString()); }
From source file:com.enonic.cms.framework.util.HttpServletRangeUtilTest.java
@Test public void test_process_request_multipart_three_ranges_with_spaces() throws Exception { final MockHttpServletRequest httpServletRequest = new MockHttpServletRequest(); httpServletRequest.setMethod("GET"); httpServletRequest.setPathInfo("/input.dat"); httpServletRequest.addHeader(HttpHeaders.RANGE, "bytes=0-5, 6-10, 11-20"); final MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse(); HttpServletRangeUtil.processRequest(httpServletRequest, mockHttpServletResponse, "input.dat", "application/pdf", INPUT_FILE, false); assertEquals(readFromFile("response3.dat"), mockHttpServletResponse.getContentAsString()); assertEquals(HttpServletResponse.SC_PARTIAL_CONTENT, mockHttpServletResponse.getStatus()); assertEquals("multipart/byteranges; boundary=THIS_STRING_SEPARATES", mockHttpServletResponse.getContentType()); assertEquals("attachment;filename=\"input.dat\"", mockHttpServletResponse.getHeader(HttpHeaders.CONTENT_DISPOSITION)); }
From source file:org.jasig.cas.support.oauth.web.OAuth20ServiceValidateControllerTests.java
@Test public void verifyHandleInternal() throws Exception { final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET", URL); final MockHttpServletResponse mockResponse = new MockHttpServletResponse(); final OAuth20ServiceValidateController oauth20ServiceValidateController = new OAuth20ServiceValidateController(); final ModelAndView modelAndView = oauth20ServiceValidateController.handleRequestInternal(mockRequest, mockResponse);// w w w .j av a 2s. co m assertNull(modelAndView); assertEquals(HttpStatus.SC_OK, mockResponse.getStatus()); assertEquals("", mockResponse.getContentAsString()); }
From source file:com.enonic.cms.framework.util.HttpServletRangeUtilTest.java
@Test public void test_process_request_multipart_zero_to_zero() throws Exception { final MockHttpServletRequest httpServletRequest = new MockHttpServletRequest(); httpServletRequest.setMethod("GET"); httpServletRequest.setPathInfo("/input.dat"); httpServletRequest.addHeader(HttpHeaders.RANGE, "bytes=0-0,-1"); final MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse(); HttpServletRangeUtil.processRequest(httpServletRequest, mockHttpServletResponse, "input.dat", "application/pdf", INPUT_FILE, false); assertEquals(readFromFile("response1.dat"), mockHttpServletResponse.getContentAsString()); assertEquals(HttpServletResponse.SC_PARTIAL_CONTENT, mockHttpServletResponse.getStatus()); assertEquals("multipart/byteranges; boundary=THIS_STRING_SEPARATES", mockHttpServletResponse.getContentType()); assertEquals("attachment;filename=\"input.dat\"", mockHttpServletResponse.getHeader(HttpHeaders.CONTENT_DISPOSITION)); }
From source file:com.enonic.cms.framework.util.HttpServletRangeUtilTest.java
@Test public void test_process_request_multipart_three_ranges() throws Exception { final MockHttpServletRequest httpServletRequest = new MockHttpServletRequest(); httpServletRequest.setMethod("GET"); httpServletRequest.setPathInfo("/input.dat"); httpServletRequest.addHeader(HttpHeaders.RANGE, "bytes=0-5,6-10,11-20"); final MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse(); HttpServletRangeUtil.processRequest(httpServletRequest, mockHttpServletResponse, "input.dat", "application/pdf", INPUT_FILE, false); assertEquals(readFromFile("response3.dat"), mockHttpServletResponse.getContentAsString()); assertEquals(HttpServletResponse.SC_PARTIAL_CONTENT, mockHttpServletResponse.getStatus()); assertEquals("multipart/byteranges; boundary=THIS_STRING_SEPARATES", mockHttpServletResponse.getContentType()); assertEquals("attachment;filename=\"input.dat\"", mockHttpServletResponse.getHeader(HttpHeaders.CONTENT_DISPOSITION)); }