23
23
import org .springframework .http .HttpHeaders ;
24
24
import org .springframework .http .HttpStatus ;
25
25
import org .springframework .http .client .ClientHttpResponse ;
26
+ import org .springframework .http .converter .HttpMessageConverter ;
26
27
import org .springframework .mock .http .MockHttpInputMessage ;
27
28
import org .springframework .mock .http .client .MockClientHttpResponse ;
28
29
import org .springframework .security .oauth2 .core .OAuth2AuthorizationException ;
30
+ import org .springframework .security .oauth2 .core .OAuth2Error ;
29
31
import org .springframework .web .client .UnknownHttpStatusCodeException ;
30
32
31
33
import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
34
+ import static org .mockito .ArgumentMatchers .any ;
35
+ import static org .mockito .ArgumentMatchers .eq ;
36
+ import static org .mockito .BDDMockito .given ;
37
+ import static org .mockito .Mockito .mock ;
38
+ import static org .mockito .Mockito .verify ;
32
39
33
40
/**
34
41
* Tests for {@link OAuth2ErrorResponseErrorHandler}.
@@ -53,6 +60,26 @@ public void handleErrorWhenErrorResponseBodyThenHandled() {
53
60
.withMessage ("[unauthorized_client] The client is not authorized" );
54
61
}
55
62
63
+ @ Test
64
+ public void handleErrorWhenOAuth2ErrorConverterSetThenCalled () throws IOException {
65
+ HttpMessageConverter <OAuth2Error > oauth2ErrorConverter = mock (HttpMessageConverter .class );
66
+ this .errorHandler .setErrorConverter (oauth2ErrorConverter );
67
+ // @formatter:off
68
+ String errorResponse = "{\n "
69
+ + " \" errorCode\" : \" unauthorized_client\" ,\n "
70
+ + " \" errorSummary\" : \" The client is not authorized\" \n "
71
+ + "}\n " ;
72
+ // @formatter:on
73
+ MockClientHttpResponse response = new MockClientHttpResponse (errorResponse .getBytes (), HttpStatus .BAD_REQUEST );
74
+ given (oauth2ErrorConverter .read (any (), any ()))
75
+ .willReturn (new OAuth2Error ("unauthorized_client" , "The client is not authorized" , null ));
76
+
77
+ assertThatExceptionOfType (OAuth2AuthorizationException .class )
78
+ .isThrownBy (() -> this .errorHandler .handleError (response ))
79
+ .withMessage ("[unauthorized_client] The client is not authorized" );
80
+ verify (oauth2ErrorConverter ).read (eq (OAuth2Error .class ), eq (response ));
81
+ }
82
+
56
83
@ Test
57
84
public void handleErrorWhenErrorResponseWwwAuthenticateHeaderThenHandled () {
58
85
String wwwAuthenticateHeader = "Bearer realm=\" auth-realm\" error=\" insufficient_scope\" error_description=\" The access token expired\" " ;
0 commit comments