5
5
import static org .junit .jupiter .api .Assertions .assertNull ;
6
6
import static org .junit .jupiter .api .Assertions .fail ;
7
7
8
+ import java .util .Base64 ;
9
+
8
10
import org .junit .jupiter .api .Test ;
9
11
10
12
import com .gargoylesoftware .htmlunit .FailingHttpStatusCodeException ;
@@ -24,7 +26,7 @@ public class CsrfReactiveTest {
24
26
@ Test
25
27
public void testCsrfTokenInForm () throws Exception {
26
28
try (final WebClient webClient = createWebClient ()) {
27
-
29
+ webClient . addRequestHeader ( "Authorization" , basicAuth ( "alice" , "alice" ));
28
30
HtmlPage htmlPage = webClient .getPage ("http://localhost:8081/service/csrfTokenForm" );
29
31
30
32
assertEquals ("CSRF Token Form Test" , htmlPage .getTitleText ());
@@ -74,7 +76,7 @@ public void testCsrfTokenWithFormRead() throws Exception {
74
76
@ Test
75
77
public void testCsrfTokenInFormButNoCookie () throws Exception {
76
78
try (final WebClient webClient = createWebClient ()) {
77
-
79
+ webClient . addRequestHeader ( "Authorization" , basicAuth ( "alice" , "alice" ));
78
80
HtmlPage htmlPage = webClient .getPage ("http://localhost:8081/service/csrfTokenForm" );
79
81
80
82
assertEquals ("CSRF Token Form Test" , htmlPage .getTitleText ());
@@ -98,6 +100,21 @@ public void testCsrfTokenInFormButNoCookie() throws Exception {
98
100
}
99
101
}
100
102
103
+ public void testCsrfFailedAuthentication () throws Exception {
104
+ try (final WebClient webClient = createWebClient ()) {
105
+ webClient .addRequestHeader ("Authorization" , basicAuth ("alice" , "password" ));
106
+ try {
107
+ webClient .getPage ("http://localhost:8081/service/csrfTokenForm" );
108
+ fail ("401 status error is expected" );
109
+ } catch (FailingHttpStatusCodeException ex ) {
110
+ assertEquals (401 , ex .getStatusCode ());
111
+ assertEquals ("true" , ex .getResponse ().getResponseHeaderValue ("test-mapper" ));
112
+ assertNull (webClient .getCookieManager ().getCookie ("csrftoken" ));
113
+ }
114
+ webClient .getCookieManager ().clearCookies ();
115
+ }
116
+ }
117
+
101
118
@ Test
102
119
public void testCsrfTokenInMultipart () throws Exception {
103
120
try (final WebClient webClient = createWebClient ()) {
@@ -127,7 +144,7 @@ public void testCsrfTokenInMultipart() throws Exception {
127
144
@ Test
128
145
public void testWrongCsrfTokenCookieValue () throws Exception {
129
146
try (final WebClient webClient = createWebClient ()) {
130
-
147
+ webClient . addRequestHeader ( "Authorization" , basicAuth ( "alice" , "alice" ));
131
148
HtmlPage htmlPage = webClient .getPage ("http://localhost:8081/service/csrfTokenForm" );
132
149
133
150
assertEquals ("CSRF Token Form Test" , htmlPage .getTitleText ());
@@ -157,7 +174,7 @@ public void testWrongCsrfTokenCookieValue() throws Exception {
157
174
@ Test
158
175
public void testWrongCsrfTokenFormValue () throws Exception {
159
176
try (final WebClient webClient = createWebClient ()) {
160
-
177
+ webClient . addRequestHeader ( "Authorization" , basicAuth ( "alice" , "alice" ));
161
178
HtmlPage htmlPage = webClient .getPage ("http://localhost:8081/service/csrfTokenForm" );
162
179
163
180
assertEquals ("CSRF Token Form Test" , htmlPage .getTitleText ());
@@ -197,4 +214,8 @@ private WebClient createWebClient() {
197
214
webClient .setCssErrorHandler (new SilentCssErrorHandler ());
198
215
return webClient ;
199
216
}
217
+
218
+ private String basicAuth (String user , String password ) {
219
+ return "Basic " + Base64 .getEncoder ().encodeToString ((user + ":" + password ).getBytes ());
220
+ }
200
221
}
0 commit comments