1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit;
16
17 import java.net.URL;
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import org.htmlunit.junit.annotation.Alerts;
22 import org.htmlunit.junit.annotation.HtmlUnitNYI;
23 import org.htmlunit.util.NameValuePair;
24 import org.junit.jupiter.api.Assertions;
25 import org.junit.jupiter.api.Test;
26 import org.openqa.selenium.By;
27 import org.openqa.selenium.WebDriver;
28
29
30
31
32
33
34
35
36 public class WebClient6Test extends WebDriverTestCase {
37
38
39
40
41 @Test
42 @Alerts({"§§URL§§page2.html", "3", "§§URL§§page2.html"})
43 public void redirect301() throws Exception {
44 redirectGet(301, HttpMethod.GET, "/page2.html");
45 redirectPost(301, HttpMethod.GET, "/page2.html", false);
46 }
47
48
49
50
51 @Test
52 @Alerts({"§§URL§§page2.html", "3", "§§URL§§page2.html"})
53 public void redirectAbsolute301() throws Exception {
54 redirectGet(301, HttpMethod.GET, new URL(URL_FIRST, "/page2.html").toExternalForm());
55 redirectPost(301, HttpMethod.GET, new URL(URL_FIRST, "/page2.html").toExternalForm(), false);
56 }
57
58
59
60
61 @Test
62 @Alerts({"§§URL§§page2.html?test=foo", "3", "§§URL§§page2.html?test=foo"})
63 public void redirect301WithQuery() throws Exception {
64 redirectGet(301, HttpMethod.GET, "/page2.html?test=foo");
65 redirectPost(301, HttpMethod.GET, "/page2.html?test=foo", true);
66 }
67
68
69
70
71 @Test
72 @Alerts({"§§URL§§page2.html", "3", "§§URL§§page2.html#hash"})
73 public void redirect301WithHash() throws Exception {
74 redirectGet(301, HttpMethod.GET, "/page2.html#hash");
75 redirectPost(301, HttpMethod.GET, "/page2.html#hash", false);
76 }
77
78
79
80
81 @Test
82 @Alerts({"§§URL§§page2.html?test=foo", "3", "§§URL§§page2.html?test=foo#hash"})
83 public void redirect301WithQueryAndHash() throws Exception {
84 redirectGet(301, HttpMethod.GET, "/page2.html?test=foo#hash");
85 redirectPost(301, HttpMethod.GET, "/page2.html?test=foo#hash", true);
86 }
87
88
89
90
91 @Test
92 @Alerts({"§§URL§§page2.html?ignorefrom=pwr&", "3",
93 "§§URL§§page2.html?ignorefrom=pwr&ignorenai=1&ignoresearch_submit=Get%20Resumes&mne=4"})
94 @HtmlUnitNYI(CHROME = {"§§URL§§page2.html?ignorefrom=pwr&", "3",
95 "§§URL§§page2.html?ignorefrom=pwr&ignorenai=1&"
96 + "%23x26;ignoresearch_submit=Get%20Resumes&%23x26;mne=4"},
97 EDGE = {"§§URL§§page2.html?ignorefrom=pwr&", "3",
98 "§§URL§§page2.html?ignorefrom=pwr&ignorenai=1&"
99 + "%23x26;ignoresearch_submit=Get%20Resumes&%23x26;mne=4"},
100 FF = {"§§URL§§page2.html?ignorefrom=pwr&", "3",
101 "§§URL§§page2.html?ignorefrom=pwr&ignorenai=1&"
102 + "%23x26;ignoresearch_submit=Get%20Resumes&%23x26;mne=4"},
103 FF_ESR = {"§§URL§§page2.html?ignorefrom=pwr&", "3",
104 "§§URL§§page2.html?ignorefrom=pwr&ignorenai=1&"
105 + "%23x26;ignoresearch_submit=Get%20Resumes&%23x26;mne=4"})
106 public void redirect301WithQueryAndHashSpecialChars() throws Exception {
107 redirectGet(301, HttpMethod.GET,
108 "/page2.html?ignorefrom=pwr&ignorenai=1&ignoresearch_submit=Get%20Resumes&mne=4");
109 redirectPost(301, HttpMethod.GET,
110 "/page2.html?ignorefrom=pwr&ignorenai=1&ignoresearch_submit=Get%20Resumes&mne=4",
111 true);
112 }
113
114
115
116
117 @Test
118 @Alerts({"§§URL§§page2.html?test=foo", "3", "§§URL§§page2.html?test=foo#hash"})
119 public void redirectAbsolute301WithQueryAndHash() throws Exception {
120 redirectGet(301, HttpMethod.GET, new URL(URL_FIRST,
121 "/page2.html?test=foo#hash").toExternalForm());
122 redirectPost(301, HttpMethod.GET, new URL(URL_FIRST,
123 "/page2.html?test=foo#hash").toExternalForm(), true);
124 }
125
126
127
128
129 @Test
130 @Alerts({"§§URL§§page2.html?ignorefrom=pwr&", "3",
131 "§§URL§§page2.html?ignorefrom=pwr&ignorenai=1&"
132 + "ignoresearch_submit=Get%20Resumes&mne=4"})
133 @HtmlUnitNYI(CHROME = {"§§URL§§page2.html?ignorefrom=pwr&", "3",
134 "§§URL§§page2.html?ignorefrom=pwr&ignorenai=1&"
135 + "%23x26;ignoresearch_submit=Get%20Resumes&%23x26;mne=4"},
136 EDGE = {"§§URL§§page2.html?ignorefrom=pwr&", "3",
137 "§§URL§§page2.html?ignorefrom=pwr&ignorenai=1&"
138 + "%23x26;ignoresearch_submit=Get%20Resumes&%23x26;mne=4"},
139 FF = {"§§URL§§page2.html?ignorefrom=pwr&", "3",
140 "§§URL§§page2.html?ignorefrom=pwr&ignorenai=1&"
141 + "%23x26;ignoresearch_submit=Get%20Resumes&%23x26;mne=4"},
142 FF_ESR = {"§§URL§§page2.html?ignorefrom=pwr&", "3",
143 "§§URL§§page2.html?ignorefrom=pwr&ignorenai=1&"
144 + "%23x26;ignoresearch_submit=Get%20Resumes&%23x26;mne=4"})
145 public void redirectAbsolute301WithQueryAndHashSpecialChars() throws Exception {
146 redirectGet(301, HttpMethod.GET, new URL(URL_FIRST,
147 "/page2.html?ignorefrom=pwr&ignorenai=1&ignoresearch_submit=Get%20Resumes&mne=4")
148 .toExternalForm());
149 redirectPost(301, HttpMethod.GET, new URL(URL_FIRST,
150 "/page2.html?ignorefrom=pwr&ignorenai=1&ignoresearch_submit=Get%20Resumes&mne=4")
151 .toExternalForm(), true);
152 }
153
154
155
156
157 @Test
158 @Alerts({"§§URL§§page2.html", "3", "§§URL§§page2.html"})
159 public void redirect302() throws Exception {
160 redirectGet(302, HttpMethod.GET, "/page2.html");
161 redirectPost(302, HttpMethod.GET, "/page2.html", false);
162 }
163
164
165
166
167 @Test
168 @Alerts({"§§URL§§page2.html", "3", "§§URL§§page2.html"})
169 public void redirectAbsolute302() throws Exception {
170 redirectGet(302, HttpMethod.GET, new URL(URL_FIRST, "/page2.html").toExternalForm());
171 redirectPost(302, HttpMethod.GET, new URL(URL_FIRST, "/page2.html").toExternalForm(), false);
172 }
173
174
175
176
177 @Test
178 @Alerts({"§§URL§§page2.html?test=foo", "3", "§§URL§§page2.html?test=foo"})
179 public void redirect302WithQuery() throws Exception {
180 redirectGet(302, HttpMethod.GET, "/page2.html?test=foo");
181 redirectPost(302, HttpMethod.GET, "/page2.html?test=foo", true);
182 }
183
184
185
186
187 @Test
188 @Alerts({"§§URL§§page2.html", "3", "§§URL§§page2.html#hash"})
189 public void redirect302WithHash() throws Exception {
190 redirectGet(302, HttpMethod.GET, "/page2.html#hash");
191 redirectPost(302, HttpMethod.GET, "/page2.html#hash", false);
192 }
193
194
195
196
197 @Test
198 @Alerts({"§§URL§§page2.html?test=foo", "3", "§§URL§§page2.html?test=foo#hash"})
199 public void redirect302WithQueryAndHash() throws Exception {
200 redirectGet(302, HttpMethod.GET, "/page2.html?test=foo#hash");
201 redirectPost(302, HttpMethod.GET, "/page2.html?test=foo#hash", true);
202 }
203
204
205
206
207 @Test
208 @Alerts({"§§URL§§page2.html", "3", "§§URL§§page2.html"})
209 public void redirect303() throws Exception {
210 redirectGet(303, HttpMethod.GET, "/page2.html");
211 redirectPost(303, HttpMethod.GET, "/page2.html", false);
212 }
213
214
215
216
217 @Test
218 @Alerts({"§§URL§§page2.html", "3", "§§URL§§page2.html"})
219 public void redirectAbsolute303() throws Exception {
220 redirectGet(303, HttpMethod.GET, new URL(URL_FIRST, "/page2.html").toExternalForm());
221 redirectPost(303, HttpMethod.GET, new URL(URL_FIRST, "/page2.html").toExternalForm(), false);
222 }
223
224
225
226
227 @Test
228 @Alerts({"§§URL§§page2.html?test=foo", "3", "§§URL§§page2.html?test=foo"})
229 public void redirect303WithQuery() throws Exception {
230 redirectGet(303, HttpMethod.GET, "/page2.html?test=foo");
231 redirectPost(303, HttpMethod.GET, "/page2.html?test=foo", true);
232 }
233
234
235
236
237 @Test
238 @Alerts({"§§URL§§page2.html", "3", "§§URL§§page2.html#hash"})
239 public void redirect303WithHash() throws Exception {
240 redirectGet(303, HttpMethod.GET, "/page2.html#hash");
241 redirectPost(303, HttpMethod.GET, "/page2.html#hash", false);
242 }
243
244
245
246
247 @Test
248 @Alerts({"§§URL§§page2.html?test=foo", "3", "§§URL§§page2.html?test=foo#hash"})
249 public void redirect303WithQueryAndHash() throws Exception {
250 redirectGet(303, HttpMethod.GET, "/page2.html?test=foo#hash");
251 redirectPost(303, HttpMethod.GET, "/page2.html?test=foo#hash", true);
252 }
253
254
255
256
257 @Test
258 @Alerts({"§§URL§§page2.html", "3", "§§URL§§page2.html"})
259 public void redirect307() throws Exception {
260 redirectGet(307, HttpMethod.GET, "/page2.html");
261 redirectPost(307, HttpMethod.POST, "/page2.html", true);
262 }
263
264
265
266
267 @Test
268 @Alerts({"§§URL§§page2.html?test=foo", "3", "§§URL§§page2.html?test=foo"})
269 public void redirect307WithQuery() throws Exception {
270 redirectGet(307, HttpMethod.GET, "/page2.html?test=foo");
271 redirectPost(307, HttpMethod.POST, "/page2.html?test=foo", true);
272 }
273
274
275
276
277 @Test
278 @Alerts({"§§URL§§page2.html", "3", "§§URL§§page2.html"})
279 public void redirectAbsolute307() throws Exception {
280 redirectGet(307, HttpMethod.GET, new URL(URL_FIRST, "/page2.html").toExternalForm());
281 redirectPost(307, HttpMethod.POST, new URL(URL_FIRST, "/page2.html").toExternalForm(), true);
282 }
283
284
285
286
287 @Test
288 @Alerts({"§§URL§§page2.html", "3", "§§URL§§page2.html#hash"})
289 public void redirect307WithHash() throws Exception {
290 redirectGet(307, HttpMethod.GET, "/page2.html#hash");
291 redirectPost(307, HttpMethod.POST, "/page2.html#hash", true);
292 }
293
294
295
296
297 @Test
298 @Alerts({"§§URL§§page2.html?test=foo", "3", "§§URL§§page2.html?test=foo#hash"})
299 public void redirect307WithQueryAndHash() throws Exception {
300 redirectGet(307, HttpMethod.GET, "/page2.html?test=foo#hash");
301 redirectPost(307, HttpMethod.POST, "/page2.html?test=foo#hash", true);
302 }
303
304
305
306
307 @Test
308 @Alerts({"§§URL§§page2.html", "3", "§§URL§§page2.html"})
309 public void redirect308() throws Exception {
310 redirectGet(308, HttpMethod.GET, "/page2.html");
311 redirectPost(308, HttpMethod.POST, "/page2.html", true);
312 }
313
314
315
316
317 @Test
318 @Alerts({"§§URL§§page2.html?test=foo", "3", "§§URL§§page2.html?test=foo"})
319 public void redirect308WithQuery() throws Exception {
320 redirectGet(308, HttpMethod.GET, "/page2.html?test=foo");
321 redirectPost(308, HttpMethod.POST, "/page2.html?test=foo", true);
322 }
323
324
325
326
327 @Test
328 @Alerts({"§§URL§§page2.html", "3", "§§URL§§page2.html"})
329 public void redirectAbsolute308() throws Exception {
330 redirectGet(308, HttpMethod.GET, new URL(URL_FIRST, "/page2.html").toExternalForm());
331 redirectPost(308, HttpMethod.POST, new URL(URL_FIRST, "/page2.html").toExternalForm(), true);
332 }
333
334
335
336
337 @Test
338 @Alerts({"§§URL§§page2.html", "3", "§§URL§§page2.html#hash"})
339 public void redirect308WithHash() throws Exception {
340 redirectGet(308, HttpMethod.GET, "/page2.html#hash");
341 redirectPost(308, HttpMethod.POST, "/page2.html#hash", true);
342 }
343
344
345
346
347 @Test
348 @Alerts({"§§URL§§page2.html?test=foo", "3", "§§URL§§page2.html?test=foo#hash"})
349 public void redirect308WithQueryAndHash() throws Exception {
350 redirectGet(308, HttpMethod.GET, "/page2.html?test=foo#hash");
351 redirectPost(308, HttpMethod.POST, "/page2.html?test=foo#hash", true);
352 }
353
354
355
356
357 @Test
358 @Alerts({"§§URL§§page2.html?ignorefrom=pwr&", "3",
359 "§§URL§§page2.html?ignorefrom=pwr&ignorenai=1&ignoresearch_submit=Get%20Resumes&mne=4"})
360 @HtmlUnitNYI(CHROME = {"§§URL§§page2.html?ignorefrom=pwr&", "3",
361 "§§URL§§page2.html?ignorefrom=pwr&ignorenai=1&"
362 + "%23x26;ignoresearch_submit=Get%20Resumes&%23x26;mne=4"},
363 EDGE = {"§§URL§§page2.html?ignorefrom=pwr&", "3",
364 "§§URL§§page2.html?ignorefrom=pwr&ignorenai=1&"
365 + "%23x26;ignoresearch_submit=Get%20Resumes&%23x26;mne=4"},
366 FF = {"§§URL§§page2.html?ignorefrom=pwr&", "3",
367 "§§URL§§page2.html?ignorefrom=pwr&ignorenai=1&"
368 + "%23x26;ignoresearch_submit=Get%20Resumes&%23x26;mne=4"},
369 FF_ESR = {"§§URL§§page2.html?ignorefrom=pwr&", "3",
370 "§§URL§§page2.html?ignorefrom=pwr&ignorenai=1&"
371 + "%23x26;ignoresearch_submit=Get%20Resumes&%23x26;mne=4"})
372 public void redirect302WithQueryAndHashSpecialChars() throws Exception {
373 redirectGet(302, HttpMethod.GET,
374 "/page2.html?ignorefrom=pwr&ignorenai=1&ignoresearch_submit=Get%20Resumes&mne=4");
375 redirectPost(302, HttpMethod.GET,
376 "/page2.html?ignorefrom=pwr&ignorenai=1&ignoresearch_submit=Get%20Resumes&mne=4",
377 true);
378 }
379
380 private void redirectGet(final int code, final HttpMethod httpMethod, final String redirectUrl) throws Exception {
381 final String html = DOCTYPE_HTML
382 + "<html><body><a href='redirect.html'>redirect</a></body></html>";
383 final MockWebConnection webConn = getMockWebConnection();
384 final int reqCount = webConn.getRequestCount();
385
386 final URL url = new URL(URL_FIRST, "page2.html");
387 webConn.setResponse(url, html);
388
389 final List<NameValuePair> headers = new ArrayList<>();
390 headers.add(new NameValuePair("Location", redirectUrl));
391 webConn.setDefaultResponse("", code, "Found", null, headers);
392
393 expandExpectedAlertsVariables(URL_FIRST);
394 final WebDriver driver = loadPage2(html);
395 driver.findElement(By.tagName("a")).click();
396 if (useRealBrowser()) {
397 Thread.sleep(400);
398 }
399
400 assertEquals(getExpectedAlerts()[0], webConn.getLastWebRequest().getUrl().toString());
401 assertEquals(reqCount + Integer.parseInt(getExpectedAlerts()[1]), webConn.getRequestCount());
402 assertEquals(httpMethod, webConn.getLastWebRequest().getHttpMethod());
403 assertEquals(getExpectedAlerts()[2], driver.getCurrentUrl());
404 }
405
406 private void redirectPost(final int code, final HttpMethod httpMethod,
407 final String redirectUrl, final boolean resendParams) throws Exception {
408 final String html = DOCTYPE_HTML
409 + "<html><body><form action='redirect.html' method='POST'>\n"
410 + " <input type='hidden' name='param1' value='paramValue'>\n"
411 + " <input type='submit' id='postBtn' value='Submit'>\n"
412 + "</form></body></html>";
413 final MockWebConnection webConn = getMockWebConnection();
414 final int reqCount = webConn.getRequestCount();
415
416 final URL url = new URL(URL_FIRST, "page2.html");
417 webConn.setResponse(url, html);
418
419 final List<NameValuePair> headers = new ArrayList<>();
420 headers.add(new NameValuePair("Location", redirectUrl));
421 webConn.setDefaultResponse("", code, "Found", null, headers);
422
423 expandExpectedAlertsVariables(URL_FIRST);
424 final WebDriver driver = loadPage2(html);
425 driver.findElement(By.id("postBtn")).click();
426 if (useRealBrowser()) {
427 Thread.sleep(400);
428 }
429
430 assertEquals(reqCount + Integer.parseInt(getExpectedAlerts()[1]), webConn.getRequestCount());
431 assertEquals(httpMethod, webConn.getLastWebRequest().getHttpMethod());
432
433 if (resendParams) {
434 assertTrue(webConn.getLastWebRequest().getRequestParameters().size() > 0);
435
436 final NameValuePair param = webConn.getLastWebRequest().getRequestParameters().get(0);
437 if ("param1".equals(param.getName())) {
438 assertEquals("paramValue", param.getValue());
439 }
440 else if ("test".equals(param.getName())) {
441 assertEquals("foo", param.getValue());
442 }
443 else if (!param.getName().startsWith("ignore")) {
444 Assertions.fail("unexpected param '" + param.getName() + "'");
445 }
446 }
447 else {
448 assertEquals(0, webConn.getLastWebRequest().getRequestParameters().size());
449 }
450
451 assertEquals(getExpectedAlerts()[0], webConn.getLastWebRequest().getUrl().toString());
452 assertEquals(getExpectedAlerts()[2], driver.getCurrentUrl());
453 }
454
455
456
457
458
459 @Test
460 public void redirect302WithoutLocation() throws Exception {
461 final String html = DOCTYPE_HTML + "<html><body><a href='page2'>to redirect</a></body></html>";
462 getMockWebConnection().setDefaultResponse("", 302, "Found", null);
463
464 final WebDriver driver = loadPage2(html);
465 driver.findElement(By.tagName("a")).click();
466 assertEquals(URL_FIRST + "page2", driver.getCurrentUrl());
467 }
468
469
470
471
472
473 @Test
474 public void redirect302ChangePageUrl() throws Exception {
475 final String html = DOCTYPE_HTML + "<html><body><a href='redirect.html'>redirect</a></body></html>";
476
477 final URL url = new URL(URL_FIRST, "page2.html");
478 getMockWebConnection().setResponse(url, html);
479
480 final List<NameValuePair> headers = new ArrayList<>();
481 headers.add(new NameValuePair("Location", "/page2.html"));
482 getMockWebConnection().setDefaultResponse("", 302, "Found", null, headers);
483
484 final WebDriver driver = loadPage2(html);
485 driver.findElement(By.tagName("a")).click();
486 assertEquals(url.toString(), driver.getCurrentUrl());
487 }
488
489
490
491
492
493
494 @Test
495 public void redirect302UrlsInQuery() throws Exception {
496 final String html = DOCTYPE_HTML + "<html><body><a href='redirect.html'>redirect</a></body></html>";
497
498 final URL url = new URL(URL_FIRST, "page2.html");
499 getMockWebConnection().setResponse(url, html);
500
501 final List<NameValuePair> headers = new ArrayList<>();
502 headers.add(new NameValuePair("Location", "/page2.html?param=http%3A//somwhere.org"));
503 getMockWebConnection().setDefaultResponse("", 302, "Found", null, headers);
504
505 final WebDriver driver = loadPage2(html);
506 driver.findElement(By.tagName("a")).click();
507 assertEquals(url + "?param=http%3A//somwhere.org", driver.getCurrentUrl());
508 }
509 }