Example usage for org.springframework.mock.web MockHttpServletRequest setParameter

List of usage examples for org.springframework.mock.web MockHttpServletRequest setParameter

Introduction

In this page you can find the example usage for org.springframework.mock.web MockHttpServletRequest setParameter.

Prototype

public void setParameter(String name, String... values) 

Source Link

Document

Set an array of values for the specified HTTP parameter.

Usage

From source file:org.openmrs.web.controller.OptionsFormControllerTest.java

@Test
public void shouldRejectEmptySecretAnswerWhenSecretQuestionPasswordIsNotSet() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
    request.setParameter("secretQuestionPassword", "");
    request.setParameter("secretQuestionNew", "test_question");

    String emptyAnswer = "";
    request.setParameter("secretAnswerNew", emptyAnswer);
    request.setParameter("secretAnswerConfirm", emptyAnswer);

    HttpServletResponse response = new MockHttpServletResponse();
    controller.handleRequest(request, response);

    LoginCredential loginCredential = userDao.getLoginCredential(user);
    assertNull(loginCredential.getSecretAnswer());
}

From source file:com.gisgraphy.servlet.AbstractAddressServletTest.java

@Test
public void doGetWithRequiredParameter() throws AddressParserException, IOException {
    MockHttpServletResponse response = new MockHttpServletResponse();

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setParameter(AbstractAddressServlet.ADDRESS_PARAMETER, "address");
    request.setParameter(AbstractAddressServlet.COUNTRY_PARAMETER, "us");
    servlet.doGet(request, response);/*ww  w  . j  av  a 2  s  .com*/
    Assert.assertFalse(customErrorSended);
    Assert.assertTrue(processRequest);
    EasyMock.verify(mockAddressQueryHttpBuilder);

}

From source file:org.openmrs.web.controller.OptionsFormControllerTest.java

@Test
public void shouldChangeSecretQuestionAndAnswer() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
    request.setParameter("secretQuestionPassword", "test");
    request.setParameter("secretQuestionNew", "test_question");

    String answer = "test_answer";
    String hashedAnswer = Security.encodeString(answer);
    request.setParameter("secretAnswerNew", answer);
    request.setParameter("secretAnswerConfirm", answer);

    HttpServletResponse response = new MockHttpServletResponse();
    controller.handleRequest(request, response);

    LoginCredential loginCredential = userDao.getLoginCredential(user);
    assertEquals(Security.encodeString(answer + loginCredential.getSalt()), loginCredential.getSecretAnswer());
}

From source file:org.openmrs.web.controller.OptionsFormControllerTest.java

@Test
public void shouldRejectInvalidNotificationAddress() throws Exception {
    final String incorrectAddress = "gayan@gmail";
    MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
    request.setParameter("notification", "email");
    request.setParameter("notificationAddress", incorrectAddress);

    HttpServletResponse response = new MockHttpServletResponse();
    ModelAndView modelAndView = controller.handleRequest(request, response);

    OptionsForm optionsForm = (OptionsForm) controller.formBackingObject(request);
    assertEquals(incorrectAddress, optionsForm.getNotificationAddress());

    BeanPropertyBindingResult bindingResult = (BeanPropertyBindingResult) modelAndView.getModel()
            .get("org.springframework.validation.BindingResult.opts");
    Assert.assertTrue(bindingResult.hasErrors());
}

From source file:org.openmrs.web.controller.OptionsFormControllerTest.java

@Test
public void shouldRejectEmptySecretQuestion() throws Exception {
    LoginCredential loginCredential = userDao.getLoginCredential(user);
    String originalQuestion = loginCredential.getSecretQuestion();

    MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
    request.setParameter("secretQuestionPassword", "test");
    request.setParameter("secretQuestionNew", "");

    String emptyAnswer = "test_answer";
    request.setParameter("secretAnswerNew", emptyAnswer);
    request.setParameter("secretAnswerConfirm", emptyAnswer);

    HttpServletResponse response = new MockHttpServletResponse();
    controller.handleRequest(request, response);

    loginCredential = userDao.getLoginCredential(user);
    assertEquals(originalQuestion, loginCredential.getSecretQuestion());
}

From source file:org.openmrs.web.controller.OptionsFormControllerTest.java

@Test
public void shouldAcceptValidNotificationAddress() throws Exception {
    String notificationTypes[] = { "internal", "internalProtected", "email" };
    String correctAddress = "gayan@gmail.com";

    for (String notifyType : notificationTypes) {
        MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
        request.setParameter("notification", notifyType);
        request.setParameter("notificationAddress", correctAddress);

        HttpServletResponse response = new MockHttpServletResponse();
        controller.handleRequest(request, response);

        OptionsForm optionsForm = (OptionsForm) controller.formBackingObject(request);
        assertEquals(correctAddress, optionsForm.getNotificationAddress());
    }//ww  w  .ja  v a 2 s.c o m
}

From source file:org.jasig.cas.support.oauth.web.OAuth20MetadataClientControllerTests.java

@Test
public void verifyNoClientId() throws Exception {
    final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class);
    when(centralOAuthService.getClientMetadata(CLIENT_ID, CLIENT_SECRET)).thenReturn(METADATA);

    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("POST",
            CONTEXT + OAuthConstants.METADATA_URL);
    mockRequest.setParameter(OAuthConstants.CLIENT_ID, "");
    mockRequest.setParameter(OAuthConstants.CLIENT_SECRET, CLIENT_SECRET);
    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  2s  . c  o  m
    assertEquals(HttpStatus.SC_BAD_REQUEST, mockResponse.getStatus());
    assertEquals(CONTENT_TYPE, mockResponse.getContentType());

    final String expected = "{\"error\":\"" + OAuthConstants.INVALID_REQUEST + "\",\"error_description\":\""
            + "Invalid or missing parameter 'client_id'\"}";

    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.OAuth20MetadataClientControllerTests.java

@Test
public void verifyNoClientSecret() throws Exception {
    final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class);
    when(centralOAuthService.getClientMetadata(CLIENT_ID, CLIENT_SECRET)).thenReturn(METADATA);

    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("POST",
            CONTEXT + OAuthConstants.METADATA_URL);
    mockRequest.setParameter(OAuthConstants.CLIENT_ID, CLIENT_ID);
    mockRequest.setParameter(OAuthConstants.CLIENT_SECRET, "");
    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   www.  j  a v  a  2  s.  c  o m*/
    assertEquals(HttpStatus.SC_BAD_REQUEST, mockResponse.getStatus());
    assertEquals(CONTENT_TYPE, mockResponse.getContentType());

    final String expected = "{\"error\":\"" + OAuthConstants.INVALID_REQUEST + "\",\"error_description\":\""
            + "Invalid or missing parameter 'client_secret'\"}";

    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.OAuth20MetadataClientControllerTests.java

@Test
public void verifyNoSuchClientId() throws Exception {
    final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class);
    when(centralOAuthService.getClientMetadata(NO_SUCH_CLIENT_ID, CLIENT_SECRET)).thenReturn(null);
    when(centralOAuthService.getClientMetadata(CLIENT_ID, WRONG_CLIENT_SECRET)).thenReturn(null);
    when(centralOAuthService.getClientMetadata(CLIENT_ID, CLIENT_SECRET)).thenReturn(METADATA);

    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("POST",
            CONTEXT + OAuthConstants.METADATA_URL);
    mockRequest.setParameter(OAuthConstants.CLIENT_ID, NO_SUCH_CLIENT_ID);
    mockRequest.setParameter(OAuthConstants.CLIENT_SECRET, CLIENT_SECRET);
    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  a v a2 s. com*/
    assertEquals(HttpStatus.SC_BAD_REQUEST, mockResponse.getStatus());
    assertEquals(CONTENT_TYPE, mockResponse.getContentType());

    final String expected = "{\"error\":\"" + OAuthConstants.INVALID_REQUEST + "\",\"error_description\":\""
            + OAuthConstants.INVALID_CLIENT_ID_OR_SECRET_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.OAuth20MetadataClientControllerTests.java

@Test
public void verifyWrongClientSecret() throws Exception {
    final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class);
    when(centralOAuthService.getClientMetadata(NO_SUCH_CLIENT_ID, CLIENT_SECRET)).thenReturn(null);
    when(centralOAuthService.getClientMetadata(CLIENT_ID, WRONG_CLIENT_SECRET)).thenReturn(null);
    when(centralOAuthService.getClientMetadata(CLIENT_ID, CLIENT_SECRET)).thenReturn(METADATA);

    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("POST",
            CONTEXT + OAuthConstants.METADATA_URL);
    mockRequest.setParameter(OAuthConstants.CLIENT_ID, CLIENT_ID);
    mockRequest.setParameter(OAuthConstants.CLIENT_SECRET, WRONG_CLIENT_SECRET);
    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  a  v  a2 s  .co m*/
    assertEquals(HttpStatus.SC_BAD_REQUEST, mockResponse.getStatus());
    assertEquals(CONTENT_TYPE, mockResponse.getContentType());

    final String expected = "{\"error\":\"" + OAuthConstants.INVALID_REQUEST + "\",\"error_description\":\""
            + OAuthConstants.INVALID_CLIENT_ID_OR_SECRET_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());
}