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