List of usage examples for org.springframework.mock.web MockHttpServletResponse getContentAsString
public String getContentAsString() throws UnsupportedEncodingException
From source file:org.jasig.cas.support.oauth.web.OAuth20TokenAuthorizationCodeControllerTests.java
@Test public void verifyNoClientSecret() throws Exception { final MockHttpServletRequest mockRequest = new MockHttpServletRequest("POST", CONTEXT + OAuthConstants.TOKEN_URL); mockRequest.setParameter(OAuthConstants.GRANT_TYPE, OAuthConstants.AUTHORIZATION_CODE); mockRequest.setParameter(OAuthConstants.CODE, CODE); mockRequest.setParameter(OAuthConstants.CLIENT_ID, CLIENT_ID); mockRequest.setParameter(OAuthConstants.REDIRECT_URI, REDIRECT_URI); final MockHttpServletResponse mockResponse = new MockHttpServletResponse(); final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController(); oauth20WrapperController.afterPropertiesSet(); final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse); assertNull(modelAndView);//from www.java 2 s .c o m assertEquals(HttpStatus.SC_BAD_REQUEST, mockResponse.getStatus()); assertEquals("application/json", mockResponse.getContentType()); final ObjectMapper mapper = new ObjectMapper(); final String expected = "{\"error\":\"" + OAuthConstants.INVALID_REQUEST + "\",\"error_description\":\"" + new InvalidParameterException(OAuthConstants.CLIENT_SECRET).getMessage() + "\"}"; 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.springframework.data.rest.webmvc.jpa.JpaWebTests.java
/** * @see DATAREST-317// w ww . j av a 2 s. c om */ @Test public void rendersExcerptProjectionsCorrectly() throws Exception { Link authorsLink = client.discoverUnique("authors"); MockHttpServletResponse response = client.request(authorsLink); String firstAuthorPath = "$._embedded.authors[0]"; // Has main content assertHasJsonPathValue(firstAuthorPath.concat(".name"), response); // Embeddes content of related entity, self link and keeps relation link assertHasJsonPathValue(firstAuthorPath.concat("._embedded.books[0].title"), response); assertHasJsonPathValue(firstAuthorPath.concat("._embedded.books[0]._links.self"), response); assertHasJsonPathValue(firstAuthorPath.concat("._links.books"), response); // Access item resource and expect link to related resource present String content = response.getContentAsString(); String href = JsonPath.read(content, firstAuthorPath.concat("._links.self.href")); client.follow(new Link(href)).andExpect(client.hasLinkWithRel("books")); }
From source file:com.doitnext.http.router.DefaultInvokerTest.java
@Test public void testStoresCall() throws Exception { Route route = routesByName.get("getFavoritesForUser"); Path path = route.getPathTemplate().match("/sports-api/teams/favorites/user1/a/b/c?query=String"); Assert.assertNotNull(path);//from w ww .j ava2s.co m PathMatch pm = new PathMatch(route, path); HttpMethod method = route.getHttpMethod(); MockHttpServletRequest req = (MockHttpServletRequest) createHappyMockRequest(method, pm); req.setContentType("application/json; model=hashmap"); MockHttpServletResponse resp = new MockHttpServletResponse(); //executePathMatchTest(createPathMatchTestCaseId(pm),pm, req, resp); String testCaseId = createPathMatchTestCaseId(pm); HttpMethod httpmethod = pm.getRoute().getHttpMethod(); InvokeResult invokeresult = invoker.invokeMethod(httpmethod, pm, req, resp); Assert.assertTrue(invokeresult.handled); if (invokeresult.success) { Assert.assertFalse(testCaseId, StringUtils.isEmpty(testCollectionImpl.getLastHttpMethodCalled())); HttpMethod methodCalled = HttpMethod.valueOf(testCollectionImpl.getLastHttpMethodCalled()); Assert.assertEquals(testCaseId, pm.getRoute().getHttpMethod(), methodCalled); Assert.assertEquals(testCaseId, pm.getRoute().getImplMethod().getName(), testCollectionImpl.getLastMethodCalled()); } else { verify(errorHandlerJson).handleResponse(eq(pm), eq(req), eq(resp), any(Throwable.class)); } String content = resp.getContentAsString(); Assert.assertEquals(200, resp.getStatus()); Assert.assertEquals("{\"storePath\":\"a/b/c\",\"userId\":\"user1\"}", content); }
From source file:org.jasig.cas.support.oauth.web.OAuth20ProfileControllerTests.java
@Test public void verifyNoAccessToken() throws Exception { final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET", CONTEXT + OAuthConstants.PROFILE_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. com*/ assertEquals(HttpStatus.SC_BAD_REQUEST, mockResponse.getStatus()); assertEquals(CONTENT_TYPE, mockResponse.getContentType()); final ObjectMapper mapper = new ObjectMapper(); final String expected = "{\"error\":\"" + OAuthConstants.MISSING_ACCESS_TOKEN + "\",\"error_description\":\"" + OAuthConstants.MISSING_ACCESS_TOKEN_DESCRIPTION + "\"}"; 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.OAuth20ProfileControllerTests.java
@Test public void verifyNoTokenAndAuthHeaderIsMalformed() throws Exception { final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET", CONTEXT + OAuthConstants.PROFILE_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 ww w . ja va 2 s .com*/ 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.OAuth20TokenAuthorizationCodeControllerTests.java
@Test public void verifyNoAuthorizationCode() throws Exception { final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class); when(centralOAuthService.getToken(CODE, AuthorizationCode.class)) .thenThrow(new InvalidTokenException("error")); final MockHttpServletRequest mockRequest = new MockHttpServletRequest("POST", CONTEXT + OAuthConstants.TOKEN_URL); mockRequest.setParameter(OAuthConstants.GRANT_TYPE, OAuthConstants.AUTHORIZATION_CODE); mockRequest.setParameter(OAuthConstants.CODE, CODE); mockRequest.setParameter(OAuthConstants.CLIENT_ID, CLIENT_ID); mockRequest.setParameter(OAuthConstants.CLIENT_SECRET, CLIENT_SECRET); mockRequest.setParameter(OAuthConstants.REDIRECT_URI, REDIRECT_URI); 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. j a v a 2 s . c om assertEquals(HttpStatus.SC_BAD_REQUEST, mockResponse.getStatus()); assertEquals("application/json", mockResponse.getContentType()); final ObjectMapper mapper = new ObjectMapper(); final String expected = "{\"error\":\"" + OAuthConstants.INVALID_REQUEST + "\",\"error_description\":\"" + OAuthConstants.INVALID_CODE_DESCRIPTION + "\"}"; 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.OAuth20TokenAuthorizationCodeControllerTests.java
@Test public void verifyInvalidGrantType() throws Exception { final AuthorizationCode authorizationCode = mock(AuthorizationCode.class); when(authorizationCode.getType()).thenReturn(TokenType.PERSONAL); final OAuthRegisteredService service = getRegisteredService(REDIRECT_URI, CLIENT_SECRET); final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class); when(centralOAuthService.getToken(CODE, AuthorizationCode.class)).thenReturn(authorizationCode); when(centralOAuthService.getRegisteredService(CLIENT_ID)).thenReturn(service); final MockHttpServletRequest mockRequest = new MockHttpServletRequest("POST", CONTEXT + OAuthConstants.TOKEN_URL); mockRequest.setParameter(OAuthConstants.GRANT_TYPE, OAuthConstants.AUTHORIZATION_CODE); mockRequest.setParameter(OAuthConstants.CODE, CODE); mockRequest.setParameter(OAuthConstants.CLIENT_ID, CLIENT_ID); mockRequest.setParameter(OAuthConstants.CLIENT_SECRET, CLIENT_SECRET); mockRequest.setParameter(OAuthConstants.REDIRECT_URI, REDIRECT_URI); 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 w w .j av a 2 s. c o m assertEquals(HttpStatus.SC_BAD_REQUEST, mockResponse.getStatus()); assertEquals("application/json", mockResponse.getContentType()); final ObjectMapper mapper = new ObjectMapper(); final String expected = "{\"error\":\"" + OAuthConstants.INVALID_GRANT + "\",\"error_description\":\"" + OAuthConstants.INVALID_GRANT_TYPE_DESCRIPTION + "\"}"; 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 verifyOKWithAccessToken() throws Exception { final AccessToken accessToken = mock(AccessToken.class); when(accessToken.getId()).thenReturn(AT_ID); final List<PrincipalMetadata> principalMetas = Arrays.asList( new PrincipalMetadata(CLIENT_ID, PRINC_NAME_ONE, PRINC_DESCR_ONE), new PrincipalMetadata(CLIENT_ID, PRINC_NAME_TWO, PRINC_DESCR_TWO)); final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class); when(centralOAuthService.getToken(AT_ID, AccessToken.class)).thenReturn(accessToken); when(centralOAuthService.getPrincipalMetadata(accessToken)).thenReturn(principalMetas); final MockHttpServletRequest mockRequest = new MockHttpServletRequest("POST", CONTEXT + OAuthConstants.METADATA_URL); mockRequest.setParameter(OAuthConstants.ACCESS_TOKEN, AT_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 w w . j av a2 s . c o m assertEquals(HttpStatus.SC_OK, mockResponse.getStatus()); assertEquals(CONTENT_TYPE, mockResponse.getContentType()); final ObjectMapper mapper = new ObjectMapper(); final String expected = "{\"data\":[" + "{\"client_id\":\"" + CLIENT_ID + "\",\"name\":\"" + PRINC_NAME_ONE + "\",\"description\":\"" + PRINC_DESCR_ONE + "\",\"scope\":[]}," + "{\"client_id\":\"" + CLIENT_ID + "\",\"name\":\"" + PRINC_NAME_TWO + "\",\"description\":\"" + PRINC_DESCR_TWO + "\",\"scope\":[]}]}"; final JsonNode expectedObj = mapper.readTree(expected); final JsonNode receivedObj = mapper.readTree(mockResponse.getContentAsString()); final JsonNode expectedData = expectedObj.get("data"); final JsonNode receivedData = receivedObj.get("data"); assertEquals(expectedData.size(), receivedData.size()); final JsonNode expectedPrincipalOne = expectedData.get(0); final JsonNode receivedPrincipalOne = receivedData.get(0); assertEquals(expectedPrincipalOne.get("client_id"), receivedPrincipalOne.get("client_id")); assertEquals(expectedPrincipalOne.get("name"), receivedPrincipalOne.get("name")); assertEquals(expectedPrincipalOne.get("description"), receivedPrincipalOne.get("description")); assertEquals(expectedPrincipalOne.get("scope").size(), receivedPrincipalOne.get("scope").size()); final JsonNode expectedPrincipalTwo = expectedData.get(1); final JsonNode receivedPrincipalTwo = receivedData.get(1); assertEquals(expectedPrincipalTwo.get("client_id"), receivedPrincipalTwo.get("client_id")); assertEquals(expectedPrincipalTwo.get("name"), receivedPrincipalTwo.get("name")); assertEquals(expectedPrincipalTwo.get("description"), receivedPrincipalTwo.get("description")); assertEquals(expectedPrincipalTwo.get("scope").size(), receivedPrincipalTwo.get("scope").size()); }
From source file:org.jasig.cas.support.oauth.web.OAuth20MetadataPrincipalControllerTests.java
@Test public void verifyOKWithAuthHeader() throws Exception { final AccessToken accessToken = mock(AccessToken.class); when(accessToken.getId()).thenReturn(AT_ID); final List<PrincipalMetadata> principalMetas = Arrays.asList( new PrincipalMetadata(CLIENT_ID, PRINC_NAME_ONE, PRINC_DESCR_ONE), new PrincipalMetadata(CLIENT_ID, PRINC_NAME_TWO, PRINC_DESCR_TWO)); final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class); when(centralOAuthService.getToken(AT_ID, AccessToken.class)).thenReturn(accessToken); when(centralOAuthService.getPrincipalMetadata(accessToken)).thenReturn(principalMetas); final MockHttpServletRequest mockRequest = new MockHttpServletRequest("POST", CONTEXT + OAuthConstants.METADATA_URL); mockRequest.addHeader("Authorization", OAuthConstants.BEARER_TOKEN + " " + AT_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);/* ww w. ja v a2 s. com*/ assertEquals(HttpStatus.SC_OK, mockResponse.getStatus()); assertEquals(CONTENT_TYPE, mockResponse.getContentType()); final ObjectMapper mapper = new ObjectMapper(); final String expected = "{\"data\":[" + "{\"client_id\":\"" + CLIENT_ID + "\",\"name\":\"" + PRINC_NAME_ONE + "\",\"description\":\"" + PRINC_DESCR_ONE + "\",\"scope\":[]}," + "{\"client_id\":\"" + CLIENT_ID + "\",\"name\":\"" + PRINC_NAME_TWO + "\",\"description\":\"" + PRINC_DESCR_TWO + "\",\"scope\":[]}]}"; final JsonNode expectedObj = mapper.readTree(expected); final JsonNode receivedObj = mapper.readTree(mockResponse.getContentAsString()); final JsonNode expectedData = expectedObj.get("data"); final JsonNode receivedData = receivedObj.get("data"); assertEquals(expectedData.size(), receivedData.size()); final JsonNode expectedPrincipalOne = expectedData.get(0); final JsonNode receivedPrincipalOne = receivedData.get(0); assertEquals(expectedPrincipalOne.get("client_id"), receivedPrincipalOne.get("client_id")); assertEquals(expectedPrincipalOne.get("name"), receivedPrincipalOne.get("name")); assertEquals(expectedPrincipalOne.get("description"), receivedPrincipalOne.get("description")); assertEquals(expectedPrincipalOne.get("scope").size(), receivedPrincipalOne.get("scope").size()); final JsonNode expectedPrincipalTwo = expectedData.get(1); final JsonNode receivedPrincipalTwo = receivedData.get(1); assertEquals(expectedPrincipalTwo.get("client_id"), receivedPrincipalTwo.get("client_id")); assertEquals(expectedPrincipalTwo.get("name"), receivedPrincipalTwo.get("name")); assertEquals(expectedPrincipalTwo.get("description"), receivedPrincipalTwo.get("description")); assertEquals(expectedPrincipalTwo.get("scope").size(), receivedPrincipalTwo.get("scope").size()); }