use of org.springframework.core.convert.converter.Converter in project spring-security by spring-projects.
the class OAuth2AccessTokenResponseHttpMessageConverterTests method writeInternalWhenConversionFailsThenThrowHttpMessageNotWritableException.
@Test
public void writeInternalWhenConversionFailsThenThrowHttpMessageNotWritableException() {
Converter tokenResponseParametersConverter = mock(Converter.class);
given(tokenResponseParametersConverter.convert(any())).willThrow(RuntimeException.class);
this.messageConverter.setTokenResponseParametersConverter(tokenResponseParametersConverter);
// @formatter:off
OAuth2AccessTokenResponse accessTokenResponse = OAuth2AccessTokenResponse.withToken("access-token-1234").tokenType(OAuth2AccessToken.TokenType.BEARER).expiresIn(Instant.now().plusSeconds(3600).toEpochMilli()).build();
// @formatter:on
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
assertThatExceptionOfType(HttpMessageNotWritableException.class).isThrownBy(() -> this.messageConverter.writeInternal(accessTokenResponse, outputMessage)).withMessageContaining("An error occurred writing the OAuth 2.0 Access Token Response");
}
use of org.springframework.core.convert.converter.Converter in project spring-security by spring-projects.
the class OAuth2ErrorHttpMessageConverterTests method readInternalWhenConversionFailsThenThrowHttpMessageNotReadableException.
@Test
public void readInternalWhenConversionFailsThenThrowHttpMessageNotReadableException() {
Converter errorConverter = mock(Converter.class);
given(errorConverter.convert(any())).willThrow(RuntimeException.class);
this.messageConverter.setErrorConverter(errorConverter);
String errorResponse = "{}";
MockClientHttpResponse response = new MockClientHttpResponse(errorResponse.getBytes(), HttpStatus.BAD_REQUEST);
assertThatExceptionOfType(HttpMessageNotReadableException.class).isThrownBy(() -> this.messageConverter.readInternal(OAuth2Error.class, response)).withMessageContaining("An error occurred reading the OAuth 2.0 Error");
}
use of org.springframework.core.convert.converter.Converter in project spring-security by spring-projects.
the class OAuth2ErrorHttpMessageConverterTests method writeInternalWhenConversionFailsThenThrowHttpMessageNotWritableException.
@Test
public void writeInternalWhenConversionFailsThenThrowHttpMessageNotWritableException() {
Converter errorParametersConverter = mock(Converter.class);
given(errorParametersConverter.convert(any())).willThrow(RuntimeException.class);
this.messageConverter.setErrorParametersConverter(errorParametersConverter);
OAuth2Error oauth2Error = new OAuth2Error("unauthorized_client", "The client is not authorized", "https://tools.ietf.org/html/rfc6749#section-5.2");
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
assertThatExceptionOfType(HttpMessageNotWritableException.class).isThrownBy(() -> this.messageConverter.writeInternal(oauth2Error, outputMessage)).withMessageContaining("An error occurred writing the OAuth 2.0 Error");
}
use of org.springframework.core.convert.converter.Converter in project spring-security by spring-projects.
the class DelegatingJwtGrantedAuthoritiesConverterTests method convertWhenMultipleConvertersThenDuplicatesRemoved.
@Test
public void convertWhenMultipleConvertersThenDuplicatesRemoved() {
Converter<Jwt, Collection<GrantedAuthority>> one = (jwt) -> AuthorityUtils.createAuthorityList("one", "two");
Converter<Jwt, Collection<GrantedAuthority>> two = (jwt) -> AuthorityUtils.createAuthorityList("one", "three");
DelegatingJwtGrantedAuthoritiesConverter composite = new DelegatingJwtGrantedAuthoritiesConverter(one, two);
Jwt jwt = TestJwts.jwt().build();
Collection<GrantedAuthority> authorities = composite.convert(jwt);
assertThat(authorityListToOrderedSet(authorities)).containsExactly("one", "two", "three");
}
use of org.springframework.core.convert.converter.Converter in project spring-security by spring-projects.
the class ReactiveJwtAuthenticationConverterTests method convertWithOverriddenGrantedAuthoritiesConverter.
@Test
public void convertWithOverriddenGrantedAuthoritiesConverter() {
Jwt jwt = TestJwts.jwt().claim("scope", "message:read message:write").build();
Converter<Jwt, Flux<GrantedAuthority>> grantedAuthoritiesConverter = (token) -> Flux.just(new SimpleGrantedAuthority("blah"));
this.jwtAuthenticationConverter.setJwtGrantedAuthoritiesConverter(grantedAuthoritiesConverter);
AbstractAuthenticationToken authentication = this.jwtAuthenticationConverter.convert(jwt).block();
Collection<GrantedAuthority> authorities = authentication.getAuthorities();
assertThat(authorities).containsExactly(new SimpleGrantedAuthority("blah"));
}
Aggregations