List of usage examples for org.springframework.mock.web MockHttpServletRequest setParameter
public void setParameter(String name, String... values)
From source file:com.gisgraphy.addressparser.web.AddressQueryHttpBuilderTest.java
@Test public void buildAddressFromRequest() { AddressQueryHttpBuilder builder = AddressQueryHttpBuilder.getInstance(); MockHttpServletRequest request = new MockHttpServletRequest(); String city = "city"; String houseNumber = "1"; String streetName = "california street"; request.setParameter(city, city); request.setParameter("houseNumber", houseNumber); request.setParameter("streetName", streetName); request.setParameter("NotExistingFieldName", "foo"); request.setParameter("country", "france"); Address address = builder.buildAddressFromRequest(request); Assert.assertEquals(city, address.getCity()); Assert.assertEquals(houseNumber, address.getHouseNumber()); Assert.assertEquals(streetName, address.getStreetName()); Assert.assertNull(address.getCountry()); }
From source file:com.ideabase.repository.test.webservice.RESTfulControllerTest.java
public void testListOfRelatedItems() throws Exception { // Create dummy user object final Integer dummyUserId = TestCaseRepositoryHelper.fixCreateUser(mUserService, "hasan", "hasankhan"); // authenticate user. final Subject subject = getSubjectFromASuccessfulRequest(); // create fixed items final List<Integer> fixedItems = TestCaseRepositoryHelper.fixCreateItems(mRepositoryService, 11); // update object relation final Integer baseItemId = fixedItems.get(0); // attach related items final String relationType = "category"; mRepositoryService.addRelatedItems(relationType, baseItemId, fixedItems.subList(1, fixedItems.size())); // send restful request final MockHttpServletRequest request = new MockHttpServletRequest(); request.setRequestURI(/* ww w. jav a2s. c o m*/ "/service/find-related-items/" + relationType + "&" + String.valueOf(baseItemId) + ".xml"); request.setParameter(WebConstants.PARAM_MAX, 4 + ""); request.setMethod(METHOD_POST); request.getSession().setAttribute(WebConstants.SESSION_ATTR_USER_SUBJECT, subject); final MockHttpServletResponse response = new MockHttpServletResponse(); // execute restful controller mRestfulController.handleRequest(request, response); final String responseString = response.getContentAsString(); LOG.debug("Response content - " + responseString); assertFalse("No response found", responseString == null || responseString.length() == 0); assertFalse("Response state false", responseString.indexOf("false") != -1); }
From source file:com.ideabase.repository.test.webservice.RESTfulControllerTest.java
public void testGetTermTagCloudByQuery() throws Exception { // create some dummy data TestCaseRepositoryHelper.fixCreateItems(mRepositoryService, 5); final Integer userId = TestCaseRepositoryHelper.fixCreateUser(mUserService, "hasan", "hasankhan"); final MockHttpServletRequest request = new MockHttpServletRequest(); final MockHttpServletResponse response = new MockHttpServletResponse(); // set http request parameters request.setRequestURI("/service/tagcloud/query.xml"); request.setMethod(METHOD_GET);//from w w w . jav a 2 s . c o m request.setParameter("query", "name:fox"); request.setParameter("max", "50"); request.setParameter("select", "name"); // fix login state final Subject subject = getSubjectFromASuccessfulRequest(); final MockHttpSession session = new MockHttpSession(); session.setAttribute(WebConstants.SESSION_ATTR_USER_SUBJECT, subject); request.setSession(session); final ModelAndView modelAndView = mRestfulController.handleRequest(request, response); // verify response final String responseContent = response.getContentAsString(); assertNotNull(responseContent); LOG.debug("Response content - " + responseContent); }
From source file:org.openmrs.module.webservices.rest.web.v1_0.controller.PersonControllerTest.java
@Test public void shouldRespectStartIndexAndLimit() throws Exception { MockHttpServletRequest hsr = new MockHttpServletRequest("GET", "http://localhost:8080/openmrs/ws/rest/patient?q=Test"); SimpleObject wrapper = new PersonController().search("Test", hsr, new MockHttpServletResponse()); Util.log("Everything", wrapper); List<Object> results = (List<Object>) wrapper.get("results"); int fullCount = results.size(); Assert.assertTrue("This test assumes >2 matching patients", fullCount > 2); hsr.removeAllParameters();//from ww w .j a v a2 s . co m hsr.setParameter(RestConstants.REQUEST_PROPERTY_FOR_LIMIT, "2"); wrapper = new PersonController().search("Test", hsr, new MockHttpServletResponse()); Util.log("First 2", wrapper); results = (List<Object>) wrapper.get("results"); int firstCount = results.size(); Assert.assertEquals(2, firstCount); hsr.removeAllParameters(); hsr.setParameter(RestConstants.REQUEST_PROPERTY_FOR_START_INDEX, "2"); wrapper = new PersonController().search("Test", hsr, new MockHttpServletResponse()); Util.log("The rest", wrapper); results = (List<Object>) wrapper.get("results"); int restCount = results.size(); Assert.assertEquals(fullCount, firstCount + restCount); }
From source file:org.jasig.cas.support.oauth.web.OAuth20AuthorizeCallbackActionControllerTests.java
@Test public void verifyResponseIsCodeWithoutState() throws Exception { final AuthorizationCode authorizationCode = mock(AuthorizationCode.class); when(authorizationCode.getId()).thenReturn(AC_ID); final Set<String> scopes = new HashSet<>(); scopes.add(NAME1);//from w w w . j a va2 s. c o m scopes.add(NAME2); final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class); when(centralOAuthService.grantAuthorizationCode(TokenType.OFFLINE, CLIENT_ID, TICKET_GRANTING_TICKET_ID, REDIRECT_URI, scopes)).thenReturn(authorizationCode); final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET", CONTEXT + OAuthConstants.CALLBACK_AUTHORIZE_ACTION_URL); final MockHttpSession mockSession = new MockHttpSession(); mockSession.putValue(OAuthConstants.OAUTH20_CLIENT_ID, CLIENT_ID); mockSession.putValue(OAuthConstants.OAUTH20_REDIRECT_URI, REDIRECT_URI); mockSession.putValue(OAuthConstants.OAUTH20_TOKEN_TYPE, TokenType.OFFLINE); mockSession.putValue(OAuthConstants.OAUTH20_LOGIN_TICKET_ID, TICKET_GRANTING_TICKET_ID); mockSession.putValue(OAuthConstants.OAUTH20_SCOPE_SET, scopes); mockRequest.setSession(mockSession); mockRequest.setParameter(OAuthConstants.OAUTH20_APPROVAL_PROMPT_ACTION, OAuthConstants.OAUTH20_APPROVAL_PROMPT_ACTION_ALLOW); final MockHttpServletResponse mockResponse = new MockHttpServletResponse(); final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController(); oauth20WrapperController.setCentralOAuthService(centralOAuthService); oauth20WrapperController.afterPropertiesSet(); final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse); assertTrue(modelAndView.getView() instanceof RedirectView); final RedirectView redirectView = (RedirectView) modelAndView.getView(); assertEquals(redirectView.getUrl(), REDIRECT_URI + "?" + OAuthConstants.CODE + "=" + AC_ID); assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_RESPONSE_TYPE)); assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_CLIENT_ID)); assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_STATE)); assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_REDIRECT_URI)); assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_TOKEN_TYPE)); assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_LOGIN_TICKET_ID)); assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_SCOPE_SET)); }
From source file:org.jasig.cas.support.oauth.web.OAuth20AuthorizeCallbackActionControllerTests.java
@Test public void verifyResponseIsTokenWithoutState() throws Exception { final AuthorizationCode authorizationCode = mock(AuthorizationCode.class); final TicketGrantingTicket ticketGrantingTicket = mock(TicketGrantingTicket.class); when(ticketGrantingTicket.getCreationTime()).thenReturn(new Date().getTime()); final AccessToken accessToken = mock(AccessToken.class); when(accessToken.getId()).thenReturn(AT_ID); when(accessToken.getTicket()).thenReturn(ticketGrantingTicket); final Set<String> scopes = new HashSet<>(); scopes.add(NAME1);/*ww w. j a v a 2 s . c om*/ scopes.add(NAME2); final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class); when(centralOAuthService.grantAuthorizationCode(TokenType.ONLINE, CLIENT_ID, TICKET_GRANTING_TICKET_ID, REDIRECT_URI, scopes)).thenReturn(authorizationCode); when(centralOAuthService.grantOnlineAccessToken(authorizationCode)).thenReturn(accessToken); final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET", CONTEXT + OAuthConstants.CALLBACK_AUTHORIZE_ACTION_URL); final MockHttpSession mockSession = new MockHttpSession(); mockSession.putValue(OAuthConstants.OAUTH20_RESPONSE_TYPE, RESPONSE_TYPE); mockSession.putValue(OAuthConstants.OAUTH20_CLIENT_ID, CLIENT_ID); mockSession.putValue(OAuthConstants.OAUTH20_REDIRECT_URI, REDIRECT_URI); mockSession.putValue(OAuthConstants.OAUTH20_LOGIN_TICKET_ID, TICKET_GRANTING_TICKET_ID); mockSession.putValue(OAuthConstants.OAUTH20_SCOPE_SET, scopes); mockRequest.setSession(mockSession); mockRequest.setParameter(OAuthConstants.OAUTH20_APPROVAL_PROMPT_ACTION, OAuthConstants.OAUTH20_APPROVAL_PROMPT_ACTION_ALLOW); final MockHttpServletResponse mockResponse = new MockHttpServletResponse(); final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController(); oauth20WrapperController.setCentralOAuthService(centralOAuthService); oauth20WrapperController.setTimeout(TIMEOUT); oauth20WrapperController.afterPropertiesSet(); final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse); assertTrue(modelAndView.getView() instanceof RedirectView); final RedirectView redirectView = (RedirectView) modelAndView.getView(); assertEquals(redirectView.getUrl(), REDIRECT_URI + "#" + OAuthConstants.ACCESS_TOKEN + "=" + accessToken.getId() + "&" + OAuthConstants.EXPIRES_IN + '=' + TIMEOUT + "&" + OAuthConstants.TOKEN_TYPE + '=' + OAuthConstants.BEARER_TOKEN); assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_RESPONSE_TYPE)); assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_CLIENT_ID)); assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_STATE)); assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_REDIRECT_URI)); assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_TOKEN_TYPE)); assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_LOGIN_TICKET_ID)); assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_SCOPE_SET)); }
From source file:org.jasig.cas.support.oauth.web.OAuth20AuthorizeCallbackActionControllerTests.java
@Test public void verifyNoRedirectError() throws Exception { final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET", CONTEXT + OAuthConstants.CALLBACK_AUTHORIZE_ACTION_URL); final MockHttpSession mockSession = new MockHttpSession(); mockSession.putValue(OAuthConstants.OAUTH20_RESPONSE_TYPE, RESPONSE_TYPE); mockSession.putValue(OAuthConstants.OAUTH20_STATE, STATE); mockSession.putValue(OAuthConstants.OAUTH20_CLIENT_ID, CLIENT_ID); mockSession.putValue(OAuthConstants.OAUTH20_TOKEN_TYPE, TokenType.OFFLINE); mockSession.putValue(OAuthConstants.OAUTH20_LOGIN_TICKET_ID, TICKET_GRANTING_TICKET_ID); mockSession.putValue(OAuthConstants.OAUTH20_SCOPE, SCOPE); mockRequest.setSession(mockSession); mockRequest.setParameter(OAuthConstants.OAUTH20_APPROVAL_PROMPT_ACTION, OAuthConstants.OAUTH20_APPROVAL_PROMPT_ACTION_ALLOW); final MockHttpServletResponse mockResponse = new MockHttpServletResponse(); final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController(); oauth20WrapperController.afterPropertiesSet(); final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse); assertEquals(OAuthConstants.ERROR_VIEW, modelAndView.getViewName()); assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_RESPONSE_TYPE)); assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_CLIENT_ID)); assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_STATE)); assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_REDIRECT_URI)); assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_TOKEN_TYPE)); assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_LOGIN_TICKET_ID)); assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_SCOPE_SET)); }
From source file:org.jasig.cas.support.oauth.web.OAuth20AuthorizeCallbackActionControllerTests.java
@Test public void verifyNoClientIdError() throws Exception { final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET", CONTEXT + OAuthConstants.CALLBACK_AUTHORIZE_ACTION_URL); final MockHttpSession mockSession = new MockHttpSession(); mockSession.putValue(OAuthConstants.OAUTH20_RESPONSE_TYPE, RESPONSE_TYPE); mockSession.putValue(OAuthConstants.OAUTH20_STATE, STATE); mockSession.putValue(OAuthConstants.OAUTH20_REDIRECT_URI, REDIRECT_URI); mockSession.putValue(OAuthConstants.OAUTH20_TOKEN_TYPE, TokenType.OFFLINE); mockSession.putValue(OAuthConstants.OAUTH20_LOGIN_TICKET_ID, TICKET_GRANTING_TICKET_ID); mockSession.putValue(OAuthConstants.OAUTH20_SCOPE, SCOPE); mockRequest.setSession(mockSession); mockRequest.setParameter(OAuthConstants.OAUTH20_APPROVAL_PROMPT_ACTION, OAuthConstants.OAUTH20_APPROVAL_PROMPT_ACTION_ALLOW); final MockHttpServletResponse mockResponse = new MockHttpServletResponse(); final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController(); oauth20WrapperController.afterPropertiesSet(); final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse); assertEquals(OAuthConstants.ERROR_VIEW, modelAndView.getViewName()); assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_RESPONSE_TYPE)); assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_CLIENT_ID)); assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_STATE)); assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_REDIRECT_URI)); assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_TOKEN_TYPE)); assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_LOGIN_TICKET_ID)); assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_SCOPE_SET)); }
From source file:org.jasig.cas.support.oauth.web.OAuth20AuthorizeCallbackActionControllerTests.java
@Test public void verifyResponseIsCodeWithState() throws Exception { final AuthorizationCode authorizationCode = mock(AuthorizationCode.class); when(authorizationCode.getId()).thenReturn(AC_ID); final Set<String> scopes = new HashSet<>(); scopes.add(NAME1);//from w ww . j ava2 s .c om scopes.add(NAME2); final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class); when(centralOAuthService.grantAuthorizationCode(TokenType.OFFLINE, CLIENT_ID, TICKET_GRANTING_TICKET_ID, REDIRECT_URI, scopes)).thenReturn(authorizationCode); final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET", CONTEXT + OAuthConstants.CALLBACK_AUTHORIZE_ACTION_URL); final MockHttpSession mockSession = new MockHttpSession(); mockSession.putValue(OAuthConstants.OAUTH20_CLIENT_ID, CLIENT_ID); mockSession.putValue(OAuthConstants.OAUTH20_STATE, STATE); mockSession.putValue(OAuthConstants.OAUTH20_REDIRECT_URI, REDIRECT_URI); mockSession.putValue(OAuthConstants.OAUTH20_TOKEN_TYPE, TokenType.OFFLINE); mockSession.putValue(OAuthConstants.OAUTH20_LOGIN_TICKET_ID, TICKET_GRANTING_TICKET_ID); mockSession.putValue(OAuthConstants.OAUTH20_SCOPE_SET, scopes); mockRequest.setSession(mockSession); mockRequest.setParameter(OAuthConstants.OAUTH20_APPROVAL_PROMPT_ACTION, OAuthConstants.OAUTH20_APPROVAL_PROMPT_ACTION_ALLOW); final MockHttpServletResponse mockResponse = new MockHttpServletResponse(); final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController(); oauth20WrapperController.setCentralOAuthService(centralOAuthService); oauth20WrapperController.afterPropertiesSet(); final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse); assertTrue(modelAndView.getView() instanceof RedirectView); final RedirectView redirectView = (RedirectView) modelAndView.getView(); assertEquals(redirectView.getUrl(), REDIRECT_URI + "?" + OAuthConstants.CODE + "=" + AC_ID + "&" + OAuthConstants.STATE + '=' + STATE); assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_RESPONSE_TYPE)); assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_CLIENT_ID)); assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_STATE)); assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_REDIRECT_URI)); assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_TOKEN_TYPE)); assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_LOGIN_TICKET_ID)); assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_SCOPE_SET)); }
From source file:org.jasig.cas.support.oauth.web.OAuth20AuthorizeCallbackActionControllerTests.java
@Test public void verifyResponseIsTokenWithState() throws Exception { final AuthorizationCode authorizationCode = mock(AuthorizationCode.class); final TicketGrantingTicket ticketGrantingTicket = mock(TicketGrantingTicket.class); when(ticketGrantingTicket.getCreationTime()).thenReturn(new Date().getTime()); final AccessToken accessToken = mock(AccessToken.class); when(accessToken.getId()).thenReturn(AT_ID); when(accessToken.getTicket()).thenReturn(ticketGrantingTicket); final Set<String> scopes = new HashSet<>(); scopes.add(NAME1);/*from ww w .j a v a 2s .c om*/ scopes.add(NAME2); final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class); when(centralOAuthService.grantAuthorizationCode(TokenType.ONLINE, CLIENT_ID, TICKET_GRANTING_TICKET_ID, REDIRECT_URI, scopes)).thenReturn(authorizationCode); when(centralOAuthService.grantOnlineAccessToken(authorizationCode)).thenReturn(accessToken); final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET", CONTEXT + OAuthConstants.CALLBACK_AUTHORIZE_ACTION_URL); final MockHttpSession mockSession = new MockHttpSession(); mockSession.putValue(OAuthConstants.OAUTH20_RESPONSE_TYPE, RESPONSE_TYPE); mockSession.putValue(OAuthConstants.OAUTH20_CLIENT_ID, CLIENT_ID); mockSession.putValue(OAuthConstants.OAUTH20_STATE, STATE); mockSession.putValue(OAuthConstants.OAUTH20_REDIRECT_URI, REDIRECT_URI); mockSession.putValue(OAuthConstants.OAUTH20_LOGIN_TICKET_ID, TICKET_GRANTING_TICKET_ID); mockSession.putValue(OAuthConstants.OAUTH20_SCOPE_SET, scopes); mockRequest.setSession(mockSession); mockRequest.setParameter(OAuthConstants.OAUTH20_APPROVAL_PROMPT_ACTION, OAuthConstants.OAUTH20_APPROVAL_PROMPT_ACTION_ALLOW); final MockHttpServletResponse mockResponse = new MockHttpServletResponse(); final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController(); oauth20WrapperController.setCentralOAuthService(centralOAuthService); oauth20WrapperController.setTimeout(TIMEOUT); oauth20WrapperController.afterPropertiesSet(); final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse); assertTrue(modelAndView.getView() instanceof RedirectView); final RedirectView redirectView = (RedirectView) modelAndView.getView(); assertEquals(redirectView.getUrl(), REDIRECT_URI + "#" + OAuthConstants.ACCESS_TOKEN + "=" + accessToken.getId() + "&" + OAuthConstants.EXPIRES_IN + '=' + TIMEOUT + "&" + OAuthConstants.TOKEN_TYPE + '=' + OAuthConstants.BEARER_TOKEN + "&" + OAuthConstants.STATE + '=' + STATE); assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_RESPONSE_TYPE)); assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_CLIENT_ID)); assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_STATE)); assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_REDIRECT_URI)); assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_TOKEN_TYPE)); assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_LOGIN_TICKET_ID)); assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_SCOPE_SET)); }