View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
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   * Tests for {@link FailingHttpStatusCodeException}.
31   *
32   * @author Marc Guillemot
33   * @author Ronald Brill
34   */
35  @RunWith(BrowserRunner.class)
36  public final class FailingHttpStatusCodeExceptionTest extends SimpleWebTestCase {
37  
38      /**
39       * @throws Exception if the test fails
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       * @throws Exception if the test fails
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              // expected
67          }
68          assertEquals("Error: not found",
69                  client.getCurrentWindow().getEnclosedPage().getWebResponse().getContentAsString());
70      }
71  
72      /**
73       * @throws Exception if the test fails
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              // expected
88          }
89          assertEquals("Error: not found",
90                  client.getCurrentWindow().getEnclosedPage().getWebResponse().getContentAsString());
91      }
92  
93      /**
94       * @throws Exception if the test fails
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             // expected
113         }
114         assertEquals("Error: not found",
115                 client.getCurrentWindow().getEnclosedPage().getWebResponse().getContentAsString());
116     }
117 }