Search in sources :

Example 31 with HttpHeaders

use of org.springframework.http.HttpHeaders in project spring-security-oauth by spring-projects.

the class ResourceOwnerPasswordProviderTests method testUnauthenticatedErrorMessage.

@Test
public void testUnauthenticatedErrorMessage() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    ResponseEntity<Void> response = serverRunning.getForResponse("/sparklr2/photos?format=json", headers);
    assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode());
    String authenticate = response.getHeaders().getFirst("WWW-Authenticate");
    assertTrue("Wrong header: " + authenticate, authenticate.contains("error=\"unauthorized\""));
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) Test(org.junit.Test)

Example 32 with HttpHeaders

use of org.springframework.http.HttpHeaders in project spring-security-oauth by spring-projects.

the class ServerRunning method postForString.

public ResponseEntity<String> postForString(String path, HttpHeaders headers, MultiValueMap<String, String> formData) {
    HttpHeaders actualHeaders = new HttpHeaders();
    actualHeaders.putAll(headers);
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    return client.exchange(getUrl(path), HttpMethod.POST, new HttpEntity<MultiValueMap<String, String>>(formData, actualHeaders), String.class);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap)

Example 33 with HttpHeaders

use of org.springframework.http.HttpHeaders 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)

Example 34 with HttpHeaders

use of org.springframework.http.HttpHeaders in project spring-security-oauth by spring-projects.

the class ClientCredentialsProviderTests method testHardCodedAuthenticationFineClient.

/**
	 * No Basic authentication provided, only the hard coded client_id.
	 */
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void testHardCodedAuthenticationFineClient() {
    RestTemplate restTemplate = new RestTemplate();
    MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
    params.add("grant_type", "client_credentials");
    params.add("client_id", "my-client-with-secret");
    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);
    ResponseEntity<Map> response = restTemplate.exchange(req, Map.class);
    assertEquals(HttpStatus.OK, response.getStatusCode());
    Map<String, String> body = response.getBody();
    String accessToken = body.get("access_token");
    assertNotNull(accessToken);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) RestTemplate(org.springframework.web.client.RestTemplate) RequestEntity(org.springframework.http.RequestEntity) MultiValueMap(org.springframework.util.MultiValueMap) Map(java.util.Map) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Test(org.junit.Test)

Example 35 with HttpHeaders

use of org.springframework.http.HttpHeaders in project spring-security-oauth by spring-projects.

the class CustomProviderTests method invalidGrant.

@Test
public void invalidGrant() throws Exception {
    LinkedMultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>();
    form.set("grant_type", "foo");
    HttpHeaders headers = new HttpHeaders();
    headers.set("Authorization", "Basic " + new String(Base64.encode(("my-trusted-client:").getBytes())));
    @SuppressWarnings("rawtypes") ResponseEntity<Map> response = http.postForMap("/oauth/token", headers, form);
    assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Map(java.util.Map) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Test(org.junit.Test)

Aggregations

HttpHeaders (org.springframework.http.HttpHeaders)484 Test (org.junit.Test)209 ResponseEntity (org.springframework.http.ResponseEntity)90 HttpEntity (org.springframework.http.HttpEntity)81 URI (java.net.URI)53 MediaType (org.springframework.http.MediaType)50 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)42 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)36 ByteArrayInputStream (java.io.ByteArrayInputStream)34 HttpStatus (org.springframework.http.HttpStatus)30 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)30 Map (java.util.Map)25 IOException (java.io.IOException)24 ArrayList (java.util.ArrayList)22 List (java.util.List)20 RestTemplate (org.springframework.web.client.RestTemplate)20 ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)19 MultiValueMap (org.springframework.util.MultiValueMap)18 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)18 HttpInputMessage (org.springframework.http.HttpInputMessage)17