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