Example usage for org.springframework.mock.web MockHttpServletResponse getStatus

List of usage examples for org.springframework.mock.web MockHttpServletResponse getStatus

Introduction

In this page you can find the example usage for org.springframework.mock.web MockHttpServletResponse getStatus.

Prototype

@Override
    public int getStatus() 

Source Link

Usage

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);/*w  ww .j  a  va2s . 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.jasig.cas.support.oauth.web.OAuth20TokenAuthorizationCodeControllerTests.java

@Test
public void verifyNoRedirectUri() 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.CLIENT_SECRET, CLIENT_SECRET);

    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

    final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController();
    oauth20WrapperController.afterPropertiesSet();

    final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
    assertNull(modelAndView);/*from  ww w .  java 2s  .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.REDIRECT_URI).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.openmrs.module.atomfeed.web.AtomFeedDownloadServletTest.java

/**
 * @see AtomFeedDownloadServlet#doHead(javax.servlet.http.HttpServletRequest,
 *      javax.servlet.http.HttpServletResponse)
 * @verifies send valid headers if atom feed has changed
 *//*w  w w  . ja v a2 s .  c o m*/
@Test
public void doHead_shouldSendValidHeadersIfAtomFeedHasChanged() throws Exception {
    // create servlet and corresponding request and response object to be sent
    AtomFeedDownloadServlet atomFeedDownloadServlet = new AtomFeedDownloadServlet();
    MockHttpServletRequest request = new MockHttpServletRequest("HEAD", "/atomfeed");
    request.setContextPath("/somecontextpath");
    MockHttpServletResponse response = new MockHttpServletResponse();

    // intentionally change atom feed in order to not depend from other tests
    AtomFeedUtil.objectCreated(new Encounter());

    String etagToken = "somevalue";
    Date lastModified = new Date();
    // set request headers 
    request.addHeader("If-None-Match", '"' + etagToken + '"');
    request.addHeader("If-Modified-Since", lastModified);

    atomFeedDownloadServlet.service(request, response);
    // check response headers
    Assert.assertNotSame(0, response.getContentLength());
    Assert.assertEquals("application/atom+xml", response.getContentType());
    Assert.assertNotSame(HttpServletResponse.SC_NOT_MODIFIED, response.getStatus());
    Assert.assertNotSame('"' + etagToken + '"', response.getHeader("Etag"));
    Assert.assertNotNull(response.getHeader("Last-Modified"));
}

From source file:org.springsource.restbucks.PaymentProcessIntegrationTest.java

/**
 * Polls the order resource every 2 seconds and uses an {@code If-None-Match} header alongside the {@code ETag} of the
 * first response to avoid sending the representation over and over again.
 * /*  w  w w .  ja v  a2 s  .  c o m*/
 * @param response
 * @return
 * @throws Exception
 */
private MockHttpServletResponse pollUntilOrderHasReceiptLink(MockHttpServletResponse response)
        throws Exception {

    // Grab
    String content = response.getContentAsString();
    Link orderLink = links.findLinkWithRel(ORDER_REL, content);

    // Poll order until receipt link is set
    Link receiptLink = null;
    String etag = null;
    MockHttpServletResponse pollResponse;

    do {

        HttpHeaders headers = new HttpHeaders();
        if (etag != null) {
            headers.setIfNoneMatch(etag);
        }

        log.info("Poll state of order until receipt is ready");

        ResultActions action = mvc.perform(get(orderLink.getHref()).headers(headers));
        pollResponse = action.andReturn().getResponse();

        int status = pollResponse.getStatus();
        etag = pollResponse.getHeader("ETag");

        log.info(String.format("Received %s with ETag of %s", status, etag));

        if (status == HttpStatus.OK.value()) {

            action.andExpect(linkWithRelIsPresent(Link.REL_SELF)). //
                    andExpect(linkWithRelIsNotPresent(UPDATE_REL)). //
                    andExpect(linkWithRelIsNotPresent(CANCEL_REL));

            receiptLink = links.findLinkWithRel(RECEIPT_REL, pollResponse.getContentAsString());

        } else if (status == HttpStatus.NO_CONTENT.value()) {
            action.andExpect(content().string(isEmptyOrNullString()));
        }

        if (receiptLink == null) {
            Thread.sleep(2000);
        }

    } while (receiptLink == null);

    return pollResponse;
}

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  w ww  .j a va  2  s .c  om
    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);//  w w w  .j a v  a2  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.thoughtworks.go.server.controller.GoConfigAdministrationControllerIntegrationTest.java

@Test
public void shouldGetTemplateAsPartialXmlOnlyIfUserHasAdminRights() throws Exception {
    //get the pipeline XML
    configHelper.addPipeline("pipeline", "dev", "linux", "windows");
    configHelper.addTemplate("new-template", "dev");
    controller.getPipelineAsXmlPartial(0, TemplatesConfig.PIPELINE_TEMPLATES_FAKE_GROUP_NAME, null, response);
    String xml = response.getContentAsString();
    assertThat(xml, containsString("new-template"));

    //save the pipeline XML
    MockHttpServletResponse postResponse = new MockHttpServletResponse();
    String modifiedXml = xml.replace("new-template", "new-name-for-template");
    controller.postPipelineAsXmlPartial(0, TemplatesConfig.PIPELINE_TEMPLATES_FAKE_GROUP_NAME, modifiedXml,
            goConfigFileDao.md5OfConfigFile(), postResponse);

    //get the pipeline XML again
    MockHttpServletResponse getResponse = new MockHttpServletResponse();
    controller.getPipelineAsXmlPartial(0, TemplatesConfig.PIPELINE_TEMPLATES_FAKE_GROUP_NAME, null,
            getResponse);//from www  .jav a  2 s .  co m
    assertThat(getResponse.getContentAsString(), containsString("new-name-for-template"));
    assertThat(getResponse.getContentAsString(), is(modifiedXml));

    setCurrentUser("user");
    MockHttpServletResponse nonAdminResponse = new MockHttpServletResponse();
    controller.getPipelineAsXmlPartial(0, TemplatesConfig.PIPELINE_TEMPLATES_FAKE_GROUP_NAME, null,
            nonAdminResponse);
    assertThat(nonAdminResponse.getStatus(), is(SC_UNAUTHORIZED));
    assertThat(nonAdminResponse.getContentAsString(),
            is("User 'user' does not have permission to administer pipeline templates"));
}

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);/*from  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 verifyNoRegisteredService() throws Exception {
    final AuthorizationCode authorizationCode = mock(AuthorizationCode.class);

    final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class);
    when(centralOAuthService.getToken(CODE, AuthorizationCode.class)).thenReturn(authorizationCode);
    when(centralOAuthService.getRegisteredService(CLIENT_ID)).thenReturn(null);

    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 ww .ja v a  2  s . com*/
    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_CLIENT_ID_OR_SECRET_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 verifyWrongSecret() throws Exception {
    final AuthorizationCode authorizationCode = mock(AuthorizationCode.class);

    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, WRONG_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);// ww 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_REQUEST + "\",\"error_description\":\""
            + OAuthConstants.INVALID_CLIENT_ID_OR_SECRET_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());
}