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.util;
16  
17  import static org.junit.Assert.fail;
18  
19  import java.io.IOException;
20  import java.net.URL;
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import org.htmlunit.CollectingAlertHandler;
25  import org.htmlunit.FailingHttpStatusCodeException;
26  import org.htmlunit.MockWebConnection;
27  import org.htmlunit.SimpleWebTestCase;
28  import org.htmlunit.WebClient;
29  import org.htmlunit.WebRequest;
30  import org.htmlunit.WebResponse;
31  import org.htmlunit.junit.BrowserRunner;
32  import org.junit.Test;
33  import org.junit.runner.RunWith;
34  
35  /**
36   * Tests for {@link FalsifyingWebConnection}.
37   *
38   * @author Marc Guillemot
39   */
40  @RunWith(BrowserRunner.class)
41  public class FalsifyingWebConnectionTest extends SimpleWebTestCase {
42  
43      /**
44       * @throws Exception if the test fails
45       */
46      @Test
47      public void blockSomeRequests() throws Exception {
48          final WebClient webClient = getWebClient();
49  
50          final String html = "<html><head>\n"
51              + "<script src='http://www.google-analytics.com/ga.js'></script>\n"
52              + "<script src='myJs.js'></script>\n"
53              + "</head><body>\n"
54              + "hello world!"
55              + "<body></html>";
56  
57          final MockWebConnection mockConnection = new MockWebConnection();
58          mockConnection.setResponse(URL_FIRST, html);
59          mockConnection.setResponse(new URL(URL_FIRST, "myJs.js"), "alert('hello');");
60          webClient.setWebConnection(mockConnection);
61  
62          final List<String> collectedAlerts = new ArrayList<>();
63          webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
64  
65          // create a WebConnection that filters google-analytics scripts
66          // c'tor configures connection on the web client
67          try (FalsifyingWebConnection connection = new FalsifyingWebConnection(webClient) {
68              @Override
69              public WebResponse getResponse(final WebRequest request) throws IOException {
70                  if ("www.google-analytics.com".equals(request.getUrl().getHost())) {
71                      return createWebResponse(request, "", MimeType.TEXT_JAVASCRIPT); // -> empty script
72                  }
73                  return super.getResponse(request);
74              }
75          }) {
76  
77              webClient.getPage(URL_FIRST);
78  
79              assertEquals(2, mockConnection.getRequestCount());
80              final String[] expectedAlerts = {"hello"};
81              assertEquals(expectedAlerts, collectedAlerts);
82          }
83      }
84  
85      /**
86       * @throws Exception if the test fails
87       */
88      @Test
89      public void simulateHttpError() throws Exception {
90          final WebClient webClient = getWebClient();
91  
92          final String html = "<html><head>\n"
93              + "<script src='myJs.js'></script>\n"
94              + "</head><body>\n"
95              + "hello world!"
96              + "<body></html>";
97  
98          final MockWebConnection mockConnection = new MockWebConnection();
99          mockConnection.setResponse(URL_FIRST, html);
100         mockConnection.setResponse(new URL(URL_FIRST, "myJs.js"), "alert('hello');");
101         webClient.setWebConnection(mockConnection);
102 
103         final List<String> collectedAlerts = new ArrayList<>();
104         webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
105 
106         // first test this "site" when everything is ok
107         webClient.getPage(URL_FIRST);
108         final String[] expectedAlerts = {"hello"};
109         assertEquals(expectedAlerts, collectedAlerts);
110 
111         // now simulate some server problems
112 
113         // create a WebConnection that filters google-analytics scripts
114         // c'tor configures connection on the web client
115         try (FalsifyingWebConnection connection = new FalsifyingWebConnection(webClient) {
116             @Override
117             public WebResponse getResponse(final WebRequest request) throws IOException {
118                 if (request.getUrl().getPath().endsWith(".js")) {
119                     return createWebResponse(request, "", MimeType.TEXT_HTML, 500, "Application Error");
120                 }
121                 return super.getResponse(request);
122             }
123         }) {
124 
125             try {
126                 webClient.getPage(URL_FIRST);
127                 fail("HTTP Exception expected!");
128             }
129             catch (final FailingHttpStatusCodeException e) {
130                 // that's fine
131             }
132         }
133     }
134 }