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.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
37
38
39
40 @RunWith(BrowserRunner.class)
41 public class FalsifyingWebConnectionTest extends SimpleWebTestCase {
42
43
44
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
66
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);
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
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
107 webClient.getPage(URL_FIRST);
108 final String[] expectedAlerts = {"hello"};
109 assertEquals(expectedAlerts, collectedAlerts);
110
111
112
113
114
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
131 }
132 }
133 }
134 }