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