Skip to content

Commit 7035739

Browse files
committed
Add request token with code example to DefaultAuthorizationServerApplicationTests
Signed-off-by: DevDengChao <[email protected]>
1 parent 2dff088 commit 7035739

File tree

1 file changed

+45
-6
lines changed

1 file changed

+45
-6
lines changed

samples/default-authorizationserver/src/test/java/sample/DefaultAuthorizationServerApplicationTests.java

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
package sample;
1717

18-
import java.io.IOException;
19-
2018
import org.htmlunit.Page;
2119
import org.htmlunit.WebClient;
2220
import org.htmlunit.WebResponse;
@@ -27,15 +25,23 @@
2725
import org.junit.jupiter.api.BeforeEach;
2826
import org.junit.jupiter.api.Test;
2927
import org.junit.jupiter.api.extension.ExtendWith;
30-
3128
import org.springframework.beans.factory.annotation.Autowired;
3229
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
3330
import org.springframework.boot.test.context.SpringBootTest;
3431
import org.springframework.http.HttpStatus;
3532
import org.springframework.test.context.junit.jupiter.SpringExtension;
33+
import org.springframework.test.web.servlet.MockMvc;
3634
import org.springframework.web.util.UriComponentsBuilder;
3735

36+
import java.io.IOException;
37+
import java.net.URL;
38+
3839
import static org.assertj.core.api.Assertions.assertThat;
40+
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic;
41+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
42+
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
43+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
44+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
3945

4046
/**
4147
* Integration tests for the sample Authorization Server.
@@ -59,12 +65,14 @@ public class DefaultAuthorizationServerApplicationTests {
5965

6066
@Autowired
6167
private WebClient webClient;
68+
@Autowired
69+
private MockMvc mockMvc;
6270

6371
@BeforeEach
6472
public void setUp() {
6573
this.webClient.getOptions().setThrowExceptionOnFailingStatusCode(true);
6674
this.webClient.getOptions().setRedirectEnabled(true);
67-
this.webClient.getCookieManager().clearCookies(); // log out
75+
this.webClient.getCookieManager().clearCookies(); // log out
6876
}
6977

7078
@Test
@@ -75,7 +83,7 @@ public void whenLoginSuccessfulThenDisplayNotFoundError() throws IOException {
7583

7684
this.webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
7785
WebResponse signInResponse = signIn(page, "user1", "password").getWebResponse();
78-
assertThat(signInResponse.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND.value()); // there is no "default" index page
86+
assertThat(signInResponse.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND.value()); // there is no "default" index page
7987
}
8088

8189
@Test
@@ -97,7 +105,7 @@ public void whenNotLoggedInAndRequestingTokenThenRedirectsToLogin() throws IOExc
97105
}
98106

99107
@Test
100-
public void whenLoggingInAndRequestingTokenThenRedirectsToClientApplication() throws IOException {
108+
public void whenLoggingInAndRequestingTokenThenRedirectsToClientApplication() throws Exception {
101109
// Log in
102110
this.webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
103111
this.webClient.getOptions().setRedirectEnabled(false);
@@ -110,6 +118,37 @@ public void whenLoggingInAndRequestingTokenThenRedirectsToClientApplication() th
110118
String location = response.getResponseHeaderValue("location");
111119
assertThat(location).startsWith(REDIRECT_URI);
112120
assertThat(location).contains("code=");
121+
122+
123+
// ==============================================================================================
124+
// The following token request should be performed by the client application,
125+
// eg: a web application, a mobile app, etc.
126+
// ==============================================================================================
127+
128+
// get code parameter value form location
129+
String query = new URL(location).getQuery();
130+
String[] kAndV = query.split("&");
131+
String code = null;
132+
for (String kv : kAndV) {
133+
if (kv.startsWith("code=")) {
134+
code = kv.replace("code=", "");
135+
break;
136+
}
137+
}
138+
assertThat(code).isNotNull();
139+
140+
// Request token with code
141+
mockMvc.perform(post("/oauth2/token")
142+
// for OAuth2AuthorizationCodeAuthenticationConverter
143+
.formField("grant_type", "authorization_code")
144+
.formField("client_id", "messaging-client")
145+
.formField("code", code)
146+
.formField("redirect_uri", REDIRECT_URI)
147+
// for BasicAuthenticationFilter
148+
.with(httpBasic("messaging-client","secret")))
149+
.andDo(print())
150+
.andExpect(status().isOk())
151+
.andExpect(jsonPath("$.access_token").exists());
113152
}
114153

115154
private static <P extends Page> P signIn(HtmlPage page, String username, String password) throws IOException {

0 commit comments

Comments
 (0)