15
15
*/
16
16
package sample ;
17
17
18
- import java .io .IOException ;
19
-
20
18
import org .htmlunit .Page ;
21
19
import org .htmlunit .WebClient ;
22
20
import org .htmlunit .WebResponse ;
27
25
import org .junit .jupiter .api .BeforeEach ;
28
26
import org .junit .jupiter .api .Test ;
29
27
import org .junit .jupiter .api .extension .ExtendWith ;
30
-
31
28
import org .springframework .beans .factory .annotation .Autowired ;
32
29
import org .springframework .boot .test .autoconfigure .web .servlet .AutoConfigureMockMvc ;
33
30
import org .springframework .boot .test .context .SpringBootTest ;
34
31
import org .springframework .http .HttpStatus ;
35
32
import org .springframework .test .context .junit .jupiter .SpringExtension ;
33
+ import org .springframework .test .web .servlet .MockMvc ;
36
34
import org .springframework .web .util .UriComponentsBuilder ;
37
35
36
+ import java .io .IOException ;
37
+ import java .net .URL ;
38
+
38
39
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 ;
39
45
40
46
/**
41
47
* Integration tests for the sample Authorization Server.
@@ -59,12 +65,14 @@ public class DefaultAuthorizationServerApplicationTests {
59
65
60
66
@ Autowired
61
67
private WebClient webClient ;
68
+ @ Autowired
69
+ private MockMvc mockMvc ;
62
70
63
71
@ BeforeEach
64
72
public void setUp () {
65
73
this .webClient .getOptions ().setThrowExceptionOnFailingStatusCode (true );
66
74
this .webClient .getOptions ().setRedirectEnabled (true );
67
- this .webClient .getCookieManager ().clearCookies (); // log out
75
+ this .webClient .getCookieManager ().clearCookies (); // log out
68
76
}
69
77
70
78
@ Test
@@ -75,7 +83,7 @@ public void whenLoginSuccessfulThenDisplayNotFoundError() throws IOException {
75
83
76
84
this .webClient .getOptions ().setThrowExceptionOnFailingStatusCode (false );
77
85
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
79
87
}
80
88
81
89
@ Test
@@ -97,7 +105,7 @@ public void whenNotLoggedInAndRequestingTokenThenRedirectsToLogin() throws IOExc
97
105
}
98
106
99
107
@ Test
100
- public void whenLoggingInAndRequestingTokenThenRedirectsToClientApplication () throws IOException {
108
+ public void whenLoggingInAndRequestingTokenThenRedirectsToClientApplication () throws Exception {
101
109
// Log in
102
110
this .webClient .getOptions ().setThrowExceptionOnFailingStatusCode (false );
103
111
this .webClient .getOptions ().setRedirectEnabled (false );
@@ -110,6 +118,37 @@ public void whenLoggingInAndRequestingTokenThenRedirectsToClientApplication() th
110
118
String location = response .getResponseHeaderValue ("location" );
111
119
assertThat (location ).startsWith (REDIRECT_URI );
112
120
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 ());
113
152
}
114
153
115
154
private static <P extends Page > P signIn (HtmlPage page , String username , String password ) throws IOException {
0 commit comments