1212 */
1313package org .springframework .security .oauth2 .common .exception ;
1414
15+ import static org .junit .Assert .*;
16+
17+ import java .util .Collections ;
18+
1519import org .codehaus .jackson .map .ObjectMapper ;
1620import org .junit .BeforeClass ;
1721import org .junit .Test ;
2630import org .springframework .security .oauth2 .common .exceptions .UnsupportedGrantTypeException ;
2731import org .springframework .security .oauth2 .common .exceptions .UserDeniedAuthorizationException ;
2832
33+ /**
34+ *
35+ * @author Rob Winch
36+ *
37+ */
2938@ SuppressWarnings ("unused" )
30- public class TestOAuth2ExceptionDeserializer {
31-
39+ public class TestOAuth2ExceptionDeserializer {
40+ private static final String DETAILS = "some detail" ;
3241 private static ObjectMapper mapper ;
3342
3443 @ BeforeClass
@@ -37,67 +46,99 @@ public static void setUpClass() {
3746 }
3847
3948 @ Test
40- public void deserializeInvalidGrant () throws Exception {
49+ public void readValueInvalidGrant () throws Exception {
4150 String accessToken = createResponse (OAuth2Exception .INVALID_GRANT );
4251 InvalidGrantException result = (InvalidGrantException ) mapper .readValue (accessToken , OAuth2Exception .class );
52+ assertEquals (DETAILS ,result .getMessage ());
53+ assertEquals (null ,result .getAdditionalInformation ());
4354 }
4455
4556 @ Test
46- public void deserializeInvalidRequest () throws Exception {
57+ public void readValueInvalidRequest () throws Exception {
4758 String accessToken = createResponse (OAuth2Exception .INVALID_REQUEST );
4859 InvalidRequestException result = (InvalidRequestException ) mapper .readValue (accessToken , OAuth2Exception .class );
60+ assertEquals (DETAILS ,result .getMessage ());
61+ assertEquals (null ,result .getAdditionalInformation ());
4962 }
5063
5164 @ Test
52- public void deserializeInvalidScope () throws Exception {
65+ public void readValueInvalidScope () throws Exception {
5366 String accessToken = createResponse (OAuth2Exception .INVALID_SCOPE );
5467 InvalidScopeException result = (InvalidScopeException ) mapper .readValue (accessToken , OAuth2Exception .class );
68+ assertEquals (DETAILS ,result .getMessage ());
69+ assertEquals (null ,result .getAdditionalInformation ());
5570 }
5671
5772 @ Test
58- public void deserializeUnsupportedGrantType () throws Exception {
73+ public void readValueUnsupportedGrantType () throws Exception {
5974 String accessToken = createResponse (OAuth2Exception .UNSUPPORTED_GRANT_TYPE );
60- UnsupportedGrantTypeException result = (UnsupportedGrantTypeException ) mapper .readValue (accessToken , OAuth2Exception .class );
75+ UnsupportedGrantTypeException result = (UnsupportedGrantTypeException ) mapper .readValue (accessToken ,
76+ OAuth2Exception .class );
77+ assertEquals (DETAILS ,result .getMessage ());
78+ assertEquals (null ,result .getAdditionalInformation ());
6179 }
6280
6381 @ Test
64- public void deserializeUnauthorizedClient () throws Exception {
82+ public void readValueUnauthorizedClient () throws Exception {
6583 String accessToken = createResponse (OAuth2Exception .UNAUTHORIZED_CLIENT );
66- UnauthorizedClientException result = (UnauthorizedClientException ) mapper .readValue (accessToken , OAuth2Exception .class );
84+ UnauthorizedClientException result = (UnauthorizedClientException ) mapper .readValue (accessToken ,
85+ OAuth2Exception .class );
86+ assertEquals (DETAILS ,result .getMessage ());
87+ assertEquals (null ,result .getAdditionalInformation ());
6788 }
6889
6990 @ Test
70- public void deserializeAccessDenied () throws Exception {
91+ public void readValueAccessDenied () throws Exception {
7192 String accessToken = createResponse (OAuth2Exception .ACCESS_DENIED );
72- UserDeniedAuthorizationException result = (UserDeniedAuthorizationException ) mapper .readValue (accessToken , OAuth2Exception .class );
93+ UserDeniedAuthorizationException result = (UserDeniedAuthorizationException ) mapper .readValue (accessToken ,
94+ OAuth2Exception .class );
95+ assertEquals (DETAILS ,result .getMessage ());
96+ assertEquals (null ,result .getAdditionalInformation ());
7397 }
7498
7599 @ Test
76- public void deserializeRedirectUriMismatch () throws Exception {
100+ public void readValueRedirectUriMismatch () throws Exception {
77101 String accessToken = createResponse (OAuth2Exception .REDIRECT_URI_MISMATCH );
78- RedirectMismatchException result = (RedirectMismatchException )mapper .readValue (accessToken , OAuth2Exception .class );
102+ RedirectMismatchException result = (RedirectMismatchException ) mapper .readValue (accessToken ,
103+ OAuth2Exception .class );
104+ assertEquals (DETAILS ,result .getMessage ());
105+ assertEquals (null ,result .getAdditionalInformation ());
79106 }
80107
81108 @ Test
82- public void deserializeInvalidToken () throws Exception {
109+ public void readValueInvalidToken () throws Exception {
83110 String accessToken = createResponse (OAuth2Exception .INVALID_TOKEN );
84111 InvalidTokenException result = (InvalidTokenException ) mapper .readValue (accessToken , OAuth2Exception .class );
112+ assertEquals (DETAILS ,result .getMessage ());
113+ assertEquals (null ,result .getAdditionalInformation ());
85114 }
86115
87116 @ Test
88- public void deserializeUndefinedException () throws Exception {
117+ public void readValueUndefinedException () throws Exception {
89118 String accessToken = createResponse ("notdefinedcode" );
90119 OAuth2Exception result = mapper .readValue (accessToken , OAuth2Exception .class );
120+ assertEquals (DETAILS ,result .getMessage ());
121+ assertEquals (null ,result .getAdditionalInformation ());
91122 }
92123
93124 @ Test
94- public void deserializeInvalidClient () throws Exception {
125+ public void readValueInvalidClient () throws Exception {
95126 String accessToken = createResponse (OAuth2Exception .INVALID_CLIENT );
96127 InvalidClientException result = (InvalidClientException ) mapper .readValue (accessToken , OAuth2Exception .class );
128+ assertEquals (DETAILS ,result .getMessage ());
129+ assertEquals (null ,result .getAdditionalInformation ());
130+ }
131+
132+ @ Test
133+ public void readValueWithAdditionalDetails () throws Exception {
134+ String accessToken = "{\" error\" : \" invalid_client\" , \" error_description\" : \" some detail\" , \" foo\" : \" bar\" }" ;
135+ InvalidClientException result = (InvalidClientException ) mapper .readValue (accessToken , OAuth2Exception .class );
136+ assertEquals (DETAILS ,result .getMessage ());
137+ assertEquals ("{foo=bar}" ,result .getAdditionalInformation ().toString ());
97138 }
98139
99140 private String createResponse (String error ) {
100- return "{\" error\" :\" " + error + "\" ,\" error_description\" :\" some detail\" }" ;
141+ return "{\" error\" :\" " + error + "\" ,\" error_description\" :\" some detail\" }" ;
101142 }
102143
103144}
0 commit comments