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 java.util.ArrayList;
18  import java.util.Arrays;
19  import java.util.List;
20  
21  import org.htmlunit.util.MimeType;
22  import org.htmlunit.util.NameValuePair;
23  import org.junit.jupiter.api.Test;
24  import org.openqa.selenium.WebDriver;
25  
26  /**
27   * Tests methods in {@link HttpWebConnection}.
28   *
29   * @author Marc Guillemot
30   * @author Ahmed Ashour
31   * @author Frank Danek
32   * @author Ronald Brill
33   */
34  public class HttpWebConnection2Test extends WebDriverTestCase {
35  
36      /**
37       * Test for broken gzip content.
38       * @throws Exception if the test fails
39       */
40      @Test
41      public void brokenGzip() throws Exception {
42          final byte[] content = new byte[] {-1};
43          final List<NameValuePair> headers = new ArrayList<>();
44          headers.add(new NameValuePair("Content-Encoding", "gzip"));
45          headers.add(new NameValuePair(HttpHeader.CONTENT_LENGTH, String.valueOf(content.length)));
46  
47          final MockWebConnection conn = getMockWebConnection();
48          conn.setResponse(URL_FIRST, content, 404, "OK", MimeType.TEXT_HTML, headers);
49  
50          // only check that no exception is thrown
51          final WebDriver driver = loadPageWithAlerts2(URL_FIRST);
52          assertTrue(driver.getPageSource().length() > 100);
53      }
54  
55      /**
56       * @throws Exception if the test fails
57       */
58      @Test
59      public void redirectBrokenGzip() throws Exception {
60          final String html = DOCTYPE_HTML + "<html></html>";
61  
62          final List<NameValuePair> headers = Arrays.asList(new NameValuePair("Location", URL_SECOND.toString()),
63                  new NameValuePair("Content-Encoding", "gzip"));
64          final MockWebConnection conn = getMockWebConnection();
65          conn.setResponse(URL_FIRST, "12", 302, "Some error", MimeType.TEXT_HTML, headers);
66          conn.setResponse(URL_SECOND, html);
67  
68          final WebDriver driver = loadPageWithAlerts2(URL_FIRST);
69          assertEquals(URL_SECOND.toString(), driver.getCurrentUrl());
70      }
71  
72  }