1
2
3
4
5
6
7
8
9
10
11
12
13
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
35
36
37
38 public class FalsifyingWebConnectionTest extends SimpleWebTestCase {
39
40
41
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
63
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);
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
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
104 webClient.getPage(URL_FIRST);
105 final String[] expectedAlerts = {"hello"};
106 assertEquals(expectedAlerts, collectedAlerts);
107
108
109
110
111
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
128 }
129 }
130 }
131 }