39
39
import org .springframework .security .oauth2 .core .endpoint .OAuth2ParameterNames ;
40
40
import org .springframework .security .oauth2 .server .authorization .OAuth2AuthorizationServerMetadata ;
41
41
import org .springframework .security .oauth2 .server .authorization .oidc .OidcProviderConfiguration ;
42
- import org .springframework .web .util .UriComponentsBuilder ;
42
+ import org .springframework .util .LinkedMultiValueMap ;
43
+ import org .springframework .util .MultiValueMap ;
43
44
44
45
import static org .assertj .core .api .Assertions .assertThat ;
45
46
@@ -103,13 +104,13 @@ void anonymousShouldRedirectToLogin() {
103
104
void validTokenRequestShouldReturnTokenResponse () {
104
105
HttpHeaders headers = new HttpHeaders ();
105
106
headers .setBasicAuth ("messaging-client" , "secret" );
106
- HttpEntity < Object > request = new HttpEntity <>( headers );
107
- String requestUri = UriComponentsBuilder . fromUriString ( "/token" )
108
- . queryParam (OAuth2ParameterNames .CLIENT_ID , "messaging-client" )
109
- . queryParam (OAuth2ParameterNames .GRANT_TYPE , AuthorizationGrantType .CLIENT_CREDENTIALS .getValue ())
110
- . queryParam (OAuth2ParameterNames .SCOPE , "message.read+ message.write" )
111
- . toUriString ( );
112
- ResponseEntity <Map <String , Object >> entity = this .restTemplate .exchange (requestUri , HttpMethod .POST , request ,
107
+ headers . setContentType ( MediaType . APPLICATION_FORM_URLENCODED );
108
+ MultiValueMap < String , Object > body = new LinkedMultiValueMap <>();
109
+ body . add (OAuth2ParameterNames .CLIENT_ID , "messaging-client" );
110
+ body . add (OAuth2ParameterNames .GRANT_TYPE , AuthorizationGrantType .CLIENT_CREDENTIALS .getValue ());
111
+ body . add (OAuth2ParameterNames .SCOPE , "message.read message.write" );
112
+ HttpEntity < Object > request = new HttpEntity <>( body , headers );
113
+ ResponseEntity <Map <String , Object >> entity = this .restTemplate .exchange ("/token" , HttpMethod .POST , request ,
113
114
MAP_TYPE_REFERENCE );
114
115
assertThat (entity .getStatusCode ()).isEqualTo (HttpStatus .OK );
115
116
Map <String , Object > tokenResponse = Objects .requireNonNull (entity .getBody ());
@@ -123,43 +124,43 @@ void validTokenRequestShouldReturnTokenResponse() {
123
124
@ Test
124
125
void anonymousTokenRequestShouldReturnUnauthorized () {
125
126
HttpHeaders headers = new HttpHeaders ();
126
- HttpEntity < Object > request = new HttpEntity <>( headers );
127
- String requestUri = UriComponentsBuilder . fromUriString ( "/token" )
128
- . queryParam (OAuth2ParameterNames .CLIENT_ID , "messaging-client" )
129
- . queryParam (OAuth2ParameterNames .GRANT_TYPE , AuthorizationGrantType .CLIENT_CREDENTIALS .getValue ())
130
- . queryParam (OAuth2ParameterNames .SCOPE , "message.read+ message.write" )
131
- . toUriString ( );
132
- ResponseEntity <Map <String , Object >> entity = this .restTemplate .exchange (requestUri , HttpMethod .POST , request ,
127
+ headers . setContentType ( MediaType . APPLICATION_FORM_URLENCODED );
128
+ MultiValueMap < String , Object > body = new LinkedMultiValueMap <>();
129
+ body . add (OAuth2ParameterNames .CLIENT_ID , "messaging-client" );
130
+ body . add (OAuth2ParameterNames .GRANT_TYPE , AuthorizationGrantType .CLIENT_CREDENTIALS .getValue ());
131
+ body . add (OAuth2ParameterNames .SCOPE , "message.read message.write" );
132
+ HttpEntity < Object > request = new HttpEntity <>( body , headers );
133
+ ResponseEntity <Map <String , Object >> entity = this .restTemplate .exchange ("/token" , HttpMethod .POST , request ,
133
134
MAP_TYPE_REFERENCE );
134
135
assertThat (entity .getStatusCode ()).isEqualTo (HttpStatus .UNAUTHORIZED );
135
136
}
136
137
137
138
@ Test
138
139
void anonymousTokenRequestWithAcceptHeaderAllShouldReturnUnauthorized () {
139
140
HttpHeaders headers = new HttpHeaders ();
141
+ headers .setContentType (MediaType .APPLICATION_FORM_URLENCODED );
140
142
headers .setAccept (List .of (MediaType .ALL ));
141
- HttpEntity <Object > request = new HttpEntity <>(headers );
142
- String requestUri = UriComponentsBuilder .fromUriString ("/token" )
143
- .queryParam (OAuth2ParameterNames .CLIENT_ID , "messaging-client" )
144
- .queryParam (OAuth2ParameterNames .GRANT_TYPE , AuthorizationGrantType .CLIENT_CREDENTIALS .getValue ())
145
- .queryParam (OAuth2ParameterNames .SCOPE , "message.read+message.write" )
146
- .toUriString ();
147
- ResponseEntity <Map <String , Object >> entity = this .restTemplate .exchange (requestUri , HttpMethod .POST , request ,
143
+ MultiValueMap <String , Object > body = new LinkedMultiValueMap <>();
144
+ body .add (OAuth2ParameterNames .CLIENT_ID , "messaging-client" );
145
+ body .add (OAuth2ParameterNames .GRANT_TYPE , AuthorizationGrantType .CLIENT_CREDENTIALS .getValue ());
146
+ body .add (OAuth2ParameterNames .SCOPE , "message.read message.write" );
147
+ HttpEntity <Object > request = new HttpEntity <>(body , headers );
148
+ ResponseEntity <Map <String , Object >> entity = this .restTemplate .exchange ("/token" , HttpMethod .POST , request ,
148
149
MAP_TYPE_REFERENCE );
149
150
assertThat (entity .getStatusCode ()).isEqualTo (HttpStatus .UNAUTHORIZED );
150
151
}
151
152
152
153
@ Test
153
154
void anonymousTokenRequestWithAcceptHeaderTextHtmlShouldRedirectToLogin () {
154
155
HttpHeaders headers = new HttpHeaders ();
156
+ headers .setContentType (MediaType .APPLICATION_FORM_URLENCODED );
155
157
headers .setAccept (List .of (MediaType .TEXT_HTML ));
156
- HttpEntity <Object > request = new HttpEntity <>(headers );
157
- String requestUri = UriComponentsBuilder .fromUriString ("/token" )
158
- .queryParam (OAuth2ParameterNames .CLIENT_ID , "messaging-client" )
159
- .queryParam (OAuth2ParameterNames .GRANT_TYPE , AuthorizationGrantType .CLIENT_CREDENTIALS .getValue ())
160
- .queryParam (OAuth2ParameterNames .SCOPE , "message.read+message.write" )
161
- .toUriString ();
162
- ResponseEntity <Map <String , Object >> entity = this .restTemplate .exchange (requestUri , HttpMethod .POST , request ,
158
+ MultiValueMap <String , Object > body = new LinkedMultiValueMap <>();
159
+ body .add (OAuth2ParameterNames .CLIENT_ID , "messaging-client" );
160
+ body .add (OAuth2ParameterNames .GRANT_TYPE , AuthorizationGrantType .CLIENT_CREDENTIALS .getValue ());
161
+ body .add (OAuth2ParameterNames .SCOPE , "message.read message.write" );
162
+ HttpEntity <Object > request = new HttpEntity <>(body , headers );
163
+ ResponseEntity <Map <String , Object >> entity = this .restTemplate .exchange ("/token" , HttpMethod .POST , request ,
163
164
MAP_TYPE_REFERENCE );
164
165
assertThat (entity .getStatusCode ()).isEqualTo (HttpStatus .FOUND );
165
166
assertThat (entity .getHeaders ().getLocation ()).isEqualTo (URI .create ("http://localhost:" + this .port + "/login" ));
0 commit comments