Search in sources :

Example 1 with HttpStatusCodeException

use of org.springframework.web.client.HttpStatusCodeException in project spring-security-oauth by spring-projects.

the class ClientCredentialsProviderTests method testHardCodedAuthenticationWrongClient.

@Test
public void testHardCodedAuthenticationWrongClient() {
    RestTemplate restTemplate = new RestTemplate();
    MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
    params.add("grant_type", "client_credentials");
    params.add("client_id", "my-trusted-client");
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    RequestEntity<MultiValueMap<String, String>> req = new RequestEntity<MultiValueMap<String, String>>(params, headers, HttpMethod.POST, tokenUri);
    try {
        restTemplate.exchange(req, Map.class);
        fail("Expected HTTP 401");
    } catch (HttpStatusCodeException e) {
        assertEquals(HttpStatus.UNAUTHORIZED, e.getStatusCode());
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) RestTemplate(org.springframework.web.client.RestTemplate) HttpStatusCodeException(org.springframework.web.client.HttpStatusCodeException) RequestEntity(org.springframework.http.RequestEntity) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)1 HttpHeaders (org.springframework.http.HttpHeaders)1 RequestEntity (org.springframework.http.RequestEntity)1 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)1 MultiValueMap (org.springframework.util.MultiValueMap)1 HttpStatusCodeException (org.springframework.web.client.HttpStatusCodeException)1 RestTemplate (org.springframework.web.client.RestTemplate)1