Example usage for org.springframework.http ResponseEntity getStatusCode

List of usage examples for org.springframework.http ResponseEntity getStatusCode

Introduction

In this page you can find the example usage for org.springframework.http ResponseEntity getStatusCode.

Prototype

public HttpStatus getStatusCode() 

Source Link

Document

Return the HTTP status code of the response.

Usage

From source file:org.cloudfoundry.identity.uaa.integration.TokenAdminEndpointsIntegrationTests.java

@Test
@OAuth2ContextConfiguration(resource = OAuth2ContextConfiguration.ClientCredentials.class)
public void testRevokeTokenByClient() throws Exception {

    OAuth2AccessToken token = context.getAccessToken();
    String hash = new StandardPasswordEncoder().encode(token.getValue());

    HttpEntity<?> request = new HttpEntity<String>(token.getValue());
    assertEquals(HttpStatus.OK,//ww  w  . j av  a2 s  .co m
            serverRunning.getRestTemplate().exchange(serverRunning.getUrl("/oauth/clients/scim/tokens/" + hash),
                    HttpMethod.DELETE, request, Void.class).getStatusCode());

    // The token was revoked so if we trya nd use it again it should come back unauthorized
    ResponseEntity<String> result = serverRunning.getForString("/oauth/clients/scim/tokens/");
    assertEquals(HttpStatus.UNAUTHORIZED, result.getStatusCode());
    String body = result.getBody();
    assertTrue("Wrong body: " + body, body.contains("invalid_token"));

}

From source file:org.kurento.repository.test.util.HttpRepositoryTest.java

protected void uploadFileWithMultiparts(String uploadURL, File fileToUpload) {

    RestTemplate template = getRestTemplate();

    MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
    parts.add("file", new FileSystemResource(fileToUpload));

    ResponseEntity<String> entity = postWithRetries(uploadURL, template, parts);

    assertEquals("Returned response: " + entity.getBody(), HttpStatus.OK, entity.getStatusCode());
}

From source file:com.cloudbees.jenkins.plugins.demo.actuator.ShutdownSampleActuatorApplicationTests.java

@Test
public void testShutdown() throws Exception {
    @SuppressWarnings("rawtypes")
    ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword())
            .postForEntity("http://localhost:" + this.port + "/shutdown", null, Map.class);
    assertEquals(HttpStatus.OK, entity.getStatusCode());
    @SuppressWarnings("unchecked")
    Map<String, Object> body = entity.getBody();
    assertTrue("Wrong body: " + body, ((String) body.get("message")).contains("Shutting down"));
}

From source file:com.salmon.security.xacml.demo.springmvc.rest.HTTPPopulatorsTest.java

@Test
public void testCreateVehicle() {

    ResponseEntity<Vehicle> entity = this.createOneVehicle();

    String path = entity.getHeaders().getLocation().getPath();

    assertEquals(HttpStatus.CREATED, entity.getStatusCode());
    Vehicle vehicle = entity.getBody();/*from ww  w .  j  av  a 2s.  co m*/

    LOG.info("The Vehicle PLATE is " + vehicle.getPlate());
    LOG.info("The Location is " + entity.getHeaders().getLocation());

    assertEquals("RE 11 ODH", vehicle.getPlate());
    assertTrue(path.startsWith("/xacml/aggregators/dvla/vehicles/"));

}

From source file:de.pentasys.playground.springbootexample.ManagementPortSampleActuatorApplicationTests.java

@Test
public void testHome() throws Exception {
    @SuppressWarnings("rawtypes")
    ResponseEntity<Map> entity = new TestRestTemplate("admin", getPassword())
            .getForEntity("http://localhost:" + this.port, Map.class);
    assertEquals(HttpStatus.OK, entity.getStatusCode());
    @SuppressWarnings("unchecked")
    Map<String, Object> body = entity.getBody();
    assertEquals("Hello Phil", body.get("message"));
}

From source file:org.appverse.web.framework.backend.frontfacade.rest.MvcExceptionHandlerTests.java

@Test
public void testNotFound() throws Exception {
    // Login first
    TestLoginInfo loginInfo = login();//from   w w w .j  ava 2 s.c o  m
    HttpHeaders headers = new HttpHeaders();
    headers.set("Cookie", loginInfo.getJsessionid());
    HttpEntity<String> entity = new HttpEntity<String>(headers);

    // Calling protected resource - requires CSRF token
    UriComponentsBuilder builder = UriComponentsBuilder
            .fromHttpUrl("http://localhost:" + port + baseApiPath + "/badurl")
            .queryParam(DEFAULT_CSRF_PARAMETER_NAME, loginInfo.getXsrfToken());

    ResponseEntity<String> responseEntity = restTemplate.exchange(builder.build().encode().toUri(),
            HttpMethod.GET, entity, String.class);
    assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
}

From source file:org.cloudfoundry.identity.uaa.integration.AuthorizationCodeGrantIntegrationTests.java

@Test
public void testSuccessfulAuthorizationCodeFlow() throws Exception {

    HttpHeaders headers = new HttpHeaders();
    // TODO: should be able to handle just TEXT_HTML
    headers.setAccept(Arrays.asList(MediaType.TEXT_HTML, MediaType.ALL));

    AuthorizationCodeResourceDetails resource = testAccounts.getDefaultAuthorizationCodeResource();

    URI uri = serverRunning.buildUri("/oauth/authorize").queryParam("response_type", "code")
            .queryParam("state", "mystateid").queryParam("client_id", resource.getClientId())
            .queryParam("redirect_uri", resource.getPreEstablishedRedirectUri()).build();
    ResponseEntity<Void> result = serverRunning.getForResponse(uri.toString(), headers);
    assertEquals(HttpStatus.FOUND, result.getStatusCode());
    String location = result.getHeaders().getLocation().toString();

    if (result.getHeaders().containsKey("Set-Cookie")) {
        String cookie = result.getHeaders().getFirst("Set-Cookie");
        headers.set("Cookie", cookie);
    }/*from   ww w.  j  a  v a 2  s.  c o  m*/

    ResponseEntity<String> response = serverRunning.getForString(location, headers);
    // should be directed to the login screen...
    assertTrue(response.getBody().contains("/login.do"));
    assertTrue(response.getBody().contains("auth_key"));
    assertTrue(response.getBody().contains("password"));

    MultiValueMap<String, String> formData = new LinkedMultiValueMap<String, String>();
    formData.add("auth_key", testAccounts.getUserName());
    formData.add("password", testAccounts.getPassword());

    // Should be redirected to the original URL, but now authenticated
    result = serverRunning.postForResponse("/login.do", headers, formData);
    assertEquals(HttpStatus.FOUND, result.getStatusCode());

    if (result.getHeaders().containsKey("Set-Cookie")) {
        String cookie = result.getHeaders().getFirst("Set-Cookie");
        headers.set("Cookie", cookie);
    }

    response = serverRunning.getForString(result.getHeaders().getLocation().toString(), headers);
    if (response.getStatusCode() == HttpStatus.OK) {
        // The grant access page should be returned
        assertTrue(response.getBody().contains("Do you authorize"));

        formData.clear();
        formData.add("user_oauth_approval", "true");
        result = serverRunning.postForResponse("/oauth/authorize", headers, formData);
        assertEquals(HttpStatus.FOUND, result.getStatusCode());
        location = result.getHeaders().getLocation().toString();
    } else {
        // Token cached so no need for second approval
        assertEquals(HttpStatus.FOUND, response.getStatusCode());
        location = response.getHeaders().getLocation().toString();
    }
    assertTrue("Wrong location: " + location,
            location.matches(resource.getPreEstablishedRedirectUri() + ".*code=.+"));

    formData.clear();
    formData.add("client_id", resource.getClientId());
    formData.add("redirect_uri", resource.getPreEstablishedRedirectUri());
    formData.add("grant_type", "authorization_code");
    formData.add("code", location.split("code=")[1].split("&")[0]);
    HttpHeaders tokenHeaders = new HttpHeaders();
    tokenHeaders.set("Authorization",
            testAccounts.getAuthorizationHeader(resource.getClientId(), resource.getClientSecret()));
    @SuppressWarnings("rawtypes")
    ResponseEntity<Map> tokenResponse = serverRunning.postForMap("/oauth/token", formData, tokenHeaders);
    assertEquals(HttpStatus.OK, tokenResponse.getStatusCode());
    @SuppressWarnings("unchecked")
    Map<String, String> body = tokenResponse.getBody();
    Jwt token = JwtHelper.decode(body.get("access_token"));
    assertTrue("Wrong claims: " + token.getClaims(), token.getClaims().contains("\"aud\""));
    assertTrue("Wrong claims: " + token.getClaims(), token.getClaims().contains("\"user_id\""));
}

From source file:comsat.sample.ui.method.SampleMethodSecurityApplicationTests.java

@Test
public void testManagementProtected() throws Exception {
    ResponseEntity<String> entity = new TestRestTemplate()
            .getForEntity("http://localhost:" + this.port + "/beans", String.class);
    assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
}

From source file:de.pentasys.playground.springbootexample.ShutdownSampleActuatorApplicationTests.java

@Test
public void testShutdown() throws Exception {
    @SuppressWarnings("rawtypes")
    ResponseEntity<Map> entity = new TestRestTemplate("admin", getPassword())
            .postForEntity("http://localhost:" + this.port + "/shutdown", null, Map.class);
    assertEquals(HttpStatus.OK, entity.getStatusCode());
    @SuppressWarnings("unchecked")
    Map<String, Object> body = entity.getBody();
    assertTrue("Wrong body: " + body, ((String) body.get("message")).contains("Shutting down"));
}

From source file:de.qaware.cloud.nativ.zwitscher.board.ZwitscherBoardApplicationTests.java

@Test
public void zwitscherBoardUiIsRunning() {
    ResponseEntity<String> responseEntity = testRestTemplate.getForEntity(HOST + DEFINED_PORT, String.class);
    assertNotNull("responseEntity is null", responseEntity);
    assertEquals("wrong status code", HttpStatus.OK, responseEntity.getStatusCode());
    assertTrue(responseEntity.getBody().contains("Zwitscher Messages on Topic"));
}