1515 */
1616package sample ;
1717
18- import java .io .IOException ;
19-
2018import org .htmlunit .Page ;
2119import org .htmlunit .WebClient ;
2220import org .htmlunit .WebResponse ;
2725import org .junit .jupiter .api .BeforeEach ;
2826import org .junit .jupiter .api .Test ;
2927import org .junit .jupiter .api .extension .ExtendWith ;
30-
3128import org .springframework .beans .factory .annotation .Autowired ;
3229import org .springframework .boot .test .autoconfigure .web .servlet .AutoConfigureMockMvc ;
3330import org .springframework .boot .test .context .SpringBootTest ;
3431import org .springframework .http .HttpStatus ;
3532import org .springframework .test .context .junit .jupiter .SpringExtension ;
33+ import org .springframework .test .web .servlet .MockMvc ;
3634import org .springframework .web .util .UriComponentsBuilder ;
3735
36+ import java .io .IOException ;
37+ import java .net .URL ;
38+
3839import 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