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.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   * Tests for {@link FailingHttpStatusCodeException}.
29   *
30   * @author Marc Guillemot
31   * @author Ronald Brill
32   */
33  public final class FailingHttpStatusCodeExceptionTest extends SimpleWebTestCase {
34  
35      /**
36       * @throws Exception if the test fails
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       * @throws Exception if the test fails
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              // expected
64          }
65          assertEquals("Error: not found",
66                  client.getCurrentWindow().getEnclosedPage().getWebResponse().getContentAsString());
67      }
68  
69      /**
70       * @throws Exception if the test fails
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              // expected
85          }
86          assertEquals("Error: not found",
87                  client.getCurrentWindow().getEnclosedPage().getWebResponse().getContentAsString());
88      }
89  
90      /**
91       * @throws Exception if the test fails
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             // expected
110         }
111         assertEquals("Error: not found",
112                 client.getCurrentWindow().getEnclosedPage().getWebResponse().getContentAsString());
113     }
114 }