|
1 | 1 | /* |
2 | | - * Copyright 2002-2017 the original author or authors. |
| 2 | + * Copyright 2002-2018 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
34 | 34 | import org.springframework.security.oauth2.core.user.OAuth2User; |
35 | 35 | import org.springframework.security.oauth2.core.user.OAuth2UserAuthority; |
36 | 36 |
|
| 37 | +import java.util.concurrent.TimeUnit; |
| 38 | + |
37 | 39 | import static org.assertj.core.api.Assertions.assertThat; |
38 | 40 | import static org.hamcrest.CoreMatchers.containsString; |
39 | 41 | import static org.mockito.Mockito.mock; |
@@ -204,4 +206,35 @@ public void loadUserWhenUserInfoUriInvalidThenThrowAuthenticationServiceExceptio |
204 | 206 |
|
205 | 207 | this.userService.loadUser(new OAuth2UserRequest(this.clientRegistration, this.accessToken)); |
206 | 208 | } |
| 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\": \"user1@example.com\"\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 | + } |
207 | 240 | } |
0 commit comments