1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit;
16
17 import static org.junit.jupiter.api.Assertions.fail;
18
19 import java.util.Collections;
20
21 import org.apache.commons.lang3.ArrayUtils;
22 import org.htmlunit.html.HtmlPage;
23 import org.htmlunit.http.HttpStatus;
24 import org.htmlunit.util.MimeType;
25 import org.junit.jupiter.api.Test;
26
27
28
29
30
31
32
33 public final class FailingHttpStatusCodeExceptionTest extends SimpleWebTestCase {
34
35
36
37
38 @Test
39 public void constructorWithWebResponse() throws Exception {
40 final WebResponseData webResponseData = new WebResponseData(ArrayUtils.EMPTY_BYTE_ARRAY,
41 HttpStatus.NOT_FOUND_404, HttpStatus.NOT_FOUND_404_MSG, Collections.emptyList());
42 final WebResponse webResponse = new WebResponse(webResponseData, URL_FIRST, HttpMethod.GET, 10);
43 final FailingHttpStatusCodeException e = new FailingHttpStatusCodeException(webResponse);
44
45 assertEquals(webResponse, e.getResponse());
46 assertEquals(webResponse.getStatusMessage(), e.getStatusMessage());
47 assertEquals(webResponse.getStatusCode(), e.getStatusCode());
48 assertTrue("message doesn't contain failing url", e.getMessage().indexOf(URL_FIRST.toExternalForm()) > -1);
49 }
50
51
52
53
54 @Test
55 public void failureByGetPage() throws Exception {
56 getMockWebConnection().setDefaultResponse("Error: not found", 404, "Not Found", MimeType.TEXT_HTML);
57 final WebClient client = getWebClientWithMockWebConnection();
58 try {
59 client.getPage(URL_FIRST);
60 fail("FailingHttpStatusCodeException expected");
61 }
62 catch (final FailingHttpStatusCodeException e) {
63
64 }
65 assertEquals("Error: not found",
66 client.getCurrentWindow().getEnclosedPage().getWebResponse().getContentAsString());
67 }
68
69
70
71
72 @Test
73 public void failureByClickLink() throws Exception {
74 final String html = DOCTYPE_HTML
75 + "<html><body><a href='doesntExist'>go</a></body></html>";
76 getMockWebConnection().setDefaultResponse("Error: not found", 404, "Not Found", MimeType.TEXT_HTML);
77
78 final WebClient client = getWebClientWithMockWebConnection();
79 try {
80 final HtmlPage page = loadPageWithAlerts(html);
81 page.getAnchors().get(0).click();
82 }
83 catch (final FailingHttpStatusCodeException e) {
84
85 }
86 assertEquals("Error: not found",
87 client.getCurrentWindow().getEnclosedPage().getWebResponse().getContentAsString());
88 }
89
90
91
92
93 @Test
94 public void failureBySubmit() throws Exception {
95 final String html = DOCTYPE_HTML
96 + "<html><body>\n"
97 + "<form name='form1' method='get' action='foo.html'>\n"
98 + " <input name='button' type='submit' id='mySubmit'/>\n"
99 + "</form>\n"
100 + "</body></html>";
101 getMockWebConnection().setDefaultResponse("Error: not found", 404, "Not Found", MimeType.TEXT_HTML);
102
103 final WebClient client = getWebClientWithMockWebConnection();
104 try {
105 final HtmlPage page = loadPageWithAlerts(html);
106 page.getElementById("mySubmit").click();
107 }
108 catch (final FailingHttpStatusCodeException e) {
109
110 }
111 assertEquals("Error: not found",
112 client.getCurrentWindow().getEnclosedPage().getWebResponse().getContentAsString());
113 }
114 }