Skip to content

Commit f524da3

Browse files
committed
Add test NimbusUserInfoResponseClient sets Accept header to JSON
Issue gh-5294
1 parent e1e4643 commit f524da3

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/oidc/userinfo/OidcUserServiceTests.java

+32-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -44,6 +44,7 @@
4444
import java.util.LinkedHashSet;
4545
import java.util.Map;
4646
import java.util.Set;
47+
import java.util.concurrent.TimeUnit;
4748

4849
import static org.assertj.core.api.Assertions.assertThat;
4950
import static org.hamcrest.CoreMatchers.containsString;
@@ -290,4 +291,34 @@ public void loadUserWhenCustomUserNameAttributeNameThenGetNameReturnsCustomUserN
290291

291292
assertThat(user.getName()).isEqualTo("[email protected]");
292293
}
294+
295+
// gh-5294
296+
@Test
297+
public void loadUserWhenUserInfoSuccessResponseThenAcceptHeaderJson() throws Exception {
298+
MockWebServer server = new MockWebServer();
299+
300+
String userInfoResponse = "{\n" +
301+
" \"sub\": \"subject1\",\n" +
302+
" \"name\": \"first last\",\n" +
303+
" \"given_name\": \"first\",\n" +
304+
" \"family_name\": \"last\",\n" +
305+
" \"preferred_username\": \"user1\",\n" +
306+
" \"email\": \"[email protected]\"\n" +
307+
"}\n";
308+
server.enqueue(new MockResponse()
309+
.setHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
310+
.setBody(userInfoResponse));
311+
312+
server.start();
313+
314+
String userInfoUri = server.url("/user").toString();
315+
316+
when(this.userInfoEndpoint.getUri()).thenReturn(userInfoUri);
317+
when(this.accessToken.getTokenValue()).thenReturn("access-token");
318+
319+
this.userService.loadUser(new OidcUserRequest(this.clientRegistration, this.accessToken, this.idToken));
320+
server.shutdown();
321+
assertThat(server.takeRequest(1, TimeUnit.SECONDS).getHeader(HttpHeaders.ACCEPT))
322+
.isEqualTo(MediaType.APPLICATION_JSON_VALUE);
323+
}
293324
}

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/userinfo/DefaultOAuth2UserServiceTests.java

+34-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,6 +34,8 @@
3434
import org.springframework.security.oauth2.core.user.OAuth2User;
3535
import org.springframework.security.oauth2.core.user.OAuth2UserAuthority;
3636

37+
import java.util.concurrent.TimeUnit;
38+
3739
import static org.assertj.core.api.Assertions.assertThat;
3840
import static org.hamcrest.CoreMatchers.containsString;
3941
import static org.mockito.Mockito.mock;
@@ -204,4 +206,35 @@ public void loadUserWhenUserInfoUriInvalidThenThrowAuthenticationServiceExceptio
204206

205207
this.userService.loadUser(new OAuth2UserRequest(this.clientRegistration, this.accessToken));
206208
}
209+
210+
// gh-5294
211+
@Test
212+
public void loadUserWhenUserInfoSuccessResponseThenAcceptHeaderJson() throws Exception {
213+
MockWebServer server = new MockWebServer();
214+
215+
String userInfoResponse = "{\n" +
216+
" \"user-name\": \"user1\",\n" +
217+
" \"first-name\": \"first\",\n" +
218+
" \"last-name\": \"last\",\n" +
219+
" \"middle-name\": \"middle\",\n" +
220+
" \"address\": \"address\",\n" +
221+
" \"email\": \"[email protected]\"\n" +
222+
"}\n";
223+
server.enqueue(new MockResponse()
224+
.setHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
225+
.setBody(userInfoResponse));
226+
227+
server.start();
228+
229+
String userInfoUri = server.url("/user").toString();
230+
231+
when(this.userInfoEndpoint.getUri()).thenReturn(userInfoUri);
232+
when(this.userInfoEndpoint.getUserNameAttributeName()).thenReturn("user-name");
233+
when(this.accessToken.getTokenValue()).thenReturn("access-token");
234+
235+
this.userService.loadUser(new OAuth2UserRequest(this.clientRegistration, this.accessToken));
236+
server.shutdown();
237+
assertThat(server.takeRequest(1, TimeUnit.SECONDS).getHeader(HttpHeaders.ACCEPT))
238+
.isEqualTo(MediaType.APPLICATION_JSON_VALUE);
239+
}
207240
}

0 commit comments

Comments
 (0)