Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: test/WebRequest.cpp

Issue 29508569: Issue 5450 - don't expose std::shared_ptr<JsEngine> (Closed) Base URL: https://github.com/adblockplus/libadblockplus.git
Patch Set: Created Aug. 7, 2017, 8:39 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « test/UpdateCheck.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2017 eyeo GmbH 3 * Copyright (C) 2006-2017 eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 #include <sstream> 18 #include <sstream>
19 #include "BaseJsTest.h" 19 #include "BaseJsTest.h"
20 #include "../src/Thread.h" 20 #include "../src/Thread.h"
21 #include <atomic> 21 #include <atomic>
22 #include <mutex> 22 #include <mutex>
23 23
24 using namespace AdblockPlus; 24 using namespace AdblockPlus;
25 25
26 namespace 26 namespace
27 { 27 {
28 class BaseWebRequestTest : public ::testing::Test 28 class BaseWebRequestTest : public BaseJsTest
29 { 29 {
30 protected: 30 protected:
31 void SetUp() 31 void SetUp()
32 { 32 {
33 ThrowingPlatformCreationParameters platformParams; 33 ThrowingPlatformCreationParameters platformParams;
34 platformParams.logSystem = CreateLogSystem(); 34 platformParams.logSystem = CreateLogSystem();
35 platformParams.timer.reset(new NoopTimer()); 35 platformParams.timer.reset(new NoopTimer());
36 platformParams.fileSystem.reset(fileSystem = new LazyFileSystem()); 36 platformParams.fileSystem.reset(fileSystem = new LazyFileSystem());
37 platformParams.webRequest = CreateWebRequest(); 37 platformParams.webRequest = CreateWebRequest();
38 platform.reset(new Platform(std::move(platformParams))); 38 platform.reset(new Platform(std::move(platformParams)));
39 jsEngine = platform->GetJsEngine();
40 } 39 }
41 40
42 virtual WebRequestPtr CreateWebRequest() = 0; 41 virtual WebRequestPtr CreateWebRequest() = 0;
43 42
44 virtual LogSystemPtr CreateLogSystem() 43 virtual LogSystemPtr CreateLogSystem()
45 { 44 {
46 return LogSystemPtr(new ThrowingLogSystem()); 45 return LogSystemPtr(new ThrowingLogSystem());
47 } 46 }
48 47
49 std::unique_ptr<Platform> platform;
50 JsEnginePtr jsEngine;
51 LazyFileSystem* fileSystem; 48 LazyFileSystem* fileSystem;
52 }; 49 };
53 50
54 class DefaultWebRequestTest : public BaseWebRequestTest 51 class DefaultWebRequestTest : public BaseWebRequestTest
55 { 52 {
56 WebRequestPtr CreateWebRequest() override 53 WebRequestPtr CreateWebRequest() override
57 { 54 {
58 return CreateDefaultWebRequest([this](const SchedulerTask& task) 55 return CreateDefaultWebRequest([this](const SchedulerTask& task)
59 { 56 {
60 webRequestTasks.emplace_back(task); 57 webRequestTasks.emplace_back(task);
61 }); 58 });
62 } 59 }
63 std::list<SchedulerTask> webRequestTasks; 60 std::list<SchedulerTask> webRequestTasks;
64 protected: 61 protected:
65 void WaitForVariable(const std::string& variable, const AdblockPlus::JsEngin ePtr& jsEngine) 62 void WaitForVariable(const std::string& variable, AdblockPlus::JsEngine& jsE ngine)
66 { 63 {
67 while (jsEngine->Evaluate(variable).IsUndefined() && !webRequestTasks.empt y()) 64 while (jsEngine.Evaluate(variable).IsUndefined() && !webRequestTasks.empty ())
68 { 65 {
69 (*webRequestTasks.begin())(); 66 (*webRequestTasks.begin())();
70 webRequestTasks.pop_front(); 67 webRequestTasks.pop_front();
71 } 68 }
72 } 69 }
73 }; 70 };
74 71
75 class MockWebRequestTest : public BaseWebRequestTest 72 class MockWebRequestTest : public BaseWebRequestTest
76 { 73 {
77 WebRequestPtr CreateWebRequest() override 74 WebRequestPtr CreateWebRequest() override
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 return result; 117 return result;
121 } 118 }
122 return std::make_pair(false, std::set<std::string>()); 119 return std::make_pair(false, std::set<std::string>());
123 } 120 }
124 121
125 DelayedWebRequest::SharedTasks webRequestTasks; 122 DelayedWebRequest::SharedTasks webRequestTasks;
126 std::map<std::string, std::set<std::string>> requestHeaderNames; 123 std::map<std::string, std::set<std::string>> requestHeaderNames;
127 }; 124 };
128 125
129 // we return the url of the XHR. 126 // we return the url of the XHR.
130 std::string ResetTestXHR(const AdblockPlus::JsEnginePtr& jsEngine, const std:: string& defaultUrl = "") 127 std::string ResetTestXHR(AdblockPlus::JsEngine& jsEngine, const std::string& d efaultUrl = "")
131 { 128 {
132 std::string url = defaultUrl; 129 std::string url = defaultUrl;
133 // make up a unique URL if we don't have one. 130 // make up a unique URL if we don't have one.
134 if (url == "") 131 if (url == "")
135 { 132 {
136 url = "https://tests.adblockplus.org/easylist.txt-"; 133 url = "https://tests.adblockplus.org/easylist.txt-";
137 url += std::to_string(std::chrono::system_clock::to_time_t(std::chrono::sy stem_clock::now())); 134 url += std::to_string(std::chrono::system_clock::to_time_t(std::chrono::sy stem_clock::now()));
138 } 135 }
139 136
140 jsEngine->Evaluate(std::string("\ 137 jsEngine.Evaluate(std::string("\
141 var result;\ 138 var result;\
142 var request = new XMLHttpRequest();\ 139 var request = new XMLHttpRequest();\
143 request.open('GET', '") + url + "'); \ 140 request.open('GET', '") + url + "'); \
144 request.overrideMimeType('text/plain');\ 141 request.overrideMimeType('text/plain');\
145 request.addEventListener('load', function() {result = request.responseText ;}, false);\ 142 request.addEventListener('load', function() {result = request.responseText ;}, false);\
146 request.addEventListener('error', function() {result = 'error';}, false);\ 143 request.addEventListener('error', function() {result = 'error';}, false);\
147 "); 144 ");
148 return url; 145 return url;
149 } 146 }
150 } 147 }
151 148
152 TEST_F(MockWebRequestTest, BadCall) 149 TEST_F(MockWebRequestTest, BadCall)
153 { 150 {
154 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET()")); 151 auto& jsEngine = GetJsEngine();
155 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('', {}, function(){})")); 152 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET()"));
156 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET({toString: false}, {}, fu nction(){})")); 153 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET('', {}, function(){})"));
157 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', nu ll, function(){})")); 154 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET({toString: false}, {}, fun ction(){})"));
158 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', {} , null)")); 155 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET('http://example.com/', nul l, function(){})"));
159 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', {} , function(){}, 0)")); 156 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET('http://example.com/', {}, null)"));
157 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET('http://example.com/', {}, function(){}, 0)"));
160 } 158 }
161 159
162 TEST_F(MockWebRequestTest, SuccessfulRequest) 160 TEST_F(MockWebRequestTest, SuccessfulRequest)
163 { 161 {
164 jsEngine->Evaluate("_webRequest.GET('http://example.com/', {X: 'Y'}, function( result) {foo = result;} )"); 162 auto& jsEngine = GetJsEngine();
165 ASSERT_TRUE(jsEngine->Evaluate("this.foo").IsUndefined()); 163 jsEngine.Evaluate("_webRequest.GET('http://example.com/', {X: 'Y'}, function(r esult) {foo = result;} )");
164 ASSERT_TRUE(jsEngine.Evaluate("this.foo").IsUndefined());
166 ProcessPendingWebRequests(); 165 ProcessPendingWebRequests();
167 ASSERT_EQ(IWebRequest::NS_OK, jsEngine->Evaluate("foo.status").AsInt()); 166 ASSERT_EQ(IWebRequest::NS_OK, jsEngine.Evaluate("foo.status").AsInt());
168 ASSERT_EQ(123, jsEngine->Evaluate("foo.responseStatus").AsInt()); 167 ASSERT_EQ(123, jsEngine.Evaluate("foo.responseStatus").AsInt());
169 ASSERT_EQ("http://example.com/\nX\nY", jsEngine->Evaluate("foo.responseText"). AsString()); 168 ASSERT_EQ("http://example.com/\nX\nY", jsEngine.Evaluate("foo.responseText").A sString());
170 ASSERT_EQ("{\"Foo\":\"Bar\"}", jsEngine->Evaluate("JSON.stringify(foo.response Headers)").AsString()); 169 ASSERT_EQ("{\"Foo\":\"Bar\"}", jsEngine.Evaluate("JSON.stringify(foo.responseH eaders)").AsString());
171 } 170 }
172 171
173 #if defined(HAVE_CURL) || defined(_WIN32) 172 #if defined(HAVE_CURL) || defined(_WIN32)
174 TEST_F(DefaultWebRequestTest, RealWebRequest) 173 TEST_F(DefaultWebRequestTest, RealWebRequest)
175 { 174 {
175 auto& jsEngine = GetJsEngine();
176 // This URL should redirect to easylist-downloads.adblockplus.org and we 176 // This URL should redirect to easylist-downloads.adblockplus.org and we
177 // should get the actual filter list back. 177 // should get the actual filter list back.
178 jsEngine->Evaluate("_webRequest.GET('https://easylist-downloads.adblockplus.or g/easylist.txt', {}, function(result) {foo = result;} )"); 178 jsEngine.Evaluate("_webRequest.GET('https://easylist-downloads.adblockplus.org /easylist.txt', {}, function(result) {foo = result;} )");
179 WaitForVariable("this.foo", jsEngine); 179 WaitForVariable("this.foo", jsEngine);
180 ASSERT_EQ("text/plain", jsEngine->Evaluate("foo.responseHeaders['content-type' ].substr(0, 10)").AsString()); 180 ASSERT_EQ("text/plain", jsEngine.Evaluate("foo.responseHeaders['content-type'] .substr(0, 10)").AsString());
181 ASSERT_EQ(IWebRequest::NS_OK, jsEngine->Evaluate("foo.status").AsInt()); 181 ASSERT_EQ(IWebRequest::NS_OK, jsEngine.Evaluate("foo.status").AsInt());
182 ASSERT_EQ(200, jsEngine->Evaluate("foo.responseStatus").AsInt()); 182 ASSERT_EQ(200, jsEngine.Evaluate("foo.responseStatus").AsInt());
183 ASSERT_EQ("[Adblock Plus ", jsEngine->Evaluate("foo.responseText.substr(0, 14) ").AsString()); 183 ASSERT_EQ("[Adblock Plus ", jsEngine.Evaluate("foo.responseText.substr(0, 14)" ).AsString());
184 ASSERT_EQ("text/plain", jsEngine->Evaluate("foo.responseHeaders['content-type' ].substr(0, 10)").AsString()); 184 ASSERT_EQ("text/plain", jsEngine.Evaluate("foo.responseHeaders['content-type'] .substr(0, 10)").AsString());
185 #if defined(HAVE_CURL) 185 #if defined(HAVE_CURL)
186 ASSERT_EQ("gzip", jsEngine->Evaluate("foo.responseHeaders['content-encoding']. substr(0, 4)").AsString()); 186 ASSERT_EQ("gzip", jsEngine.Evaluate("foo.responseHeaders['content-encoding'].s ubstr(0, 4)").AsString());
187 #endif 187 #endif
188 ASSERT_TRUE(jsEngine->Evaluate("foo.responseHeaders['location']").IsUndefined( )); 188 ASSERT_TRUE(jsEngine.Evaluate("foo.responseHeaders['location']").IsUndefined() );
189 } 189 }
190 190
191 TEST_F(DefaultWebRequestTest, XMLHttpRequest) 191 TEST_F(DefaultWebRequestTest, XMLHttpRequest)
192 { 192 {
193 auto& jsEngine = GetJsEngine();
193 auto filterEngine = CreateFilterEngine(*fileSystem, *platform); 194 auto filterEngine = CreateFilterEngine(*fileSystem, *platform);
194 195
195 ResetTestXHR(jsEngine, "https://easylist-downloads.adblockplus.org/easylist.tx t"); 196 ResetTestXHR(jsEngine, "https://easylist-downloads.adblockplus.org/easylist.tx t");
196 jsEngine->Evaluate("\ 197 jsEngine.Evaluate("\
197 request.setRequestHeader('X', 'Y');\ 198 request.setRequestHeader('X', 'Y');\
198 request.setRequestHeader('X2', 'Y2');\ 199 request.setRequestHeader('X2', 'Y2');\
199 request.send(null);"); 200 request.send(null);");
200 WaitForVariable("result", jsEngine); 201 WaitForVariable("result", jsEngine);
201 ASSERT_EQ(IWebRequest::NS_OK, jsEngine->Evaluate("request.channel.status").AsI nt()); 202 ASSERT_EQ(IWebRequest::NS_OK, jsEngine.Evaluate("request.channel.status").AsIn t());
202 ASSERT_EQ(200, jsEngine->Evaluate("request.status").AsInt()); 203 ASSERT_EQ(200, jsEngine.Evaluate("request.status").AsInt());
203 ASSERT_EQ("[Adblock Plus ", jsEngine->Evaluate("result.substr(0, 14)").AsStrin g()); 204 ASSERT_EQ("[Adblock Plus ", jsEngine.Evaluate("result.substr(0, 14)").AsString ());
204 ASSERT_EQ("text/plain", jsEngine->Evaluate("request.getResponseHeader('Content -Type').substr(0, 10)").AsString()); 205 ASSERT_EQ("text/plain", jsEngine.Evaluate("request.getResponseHeader('Content- Type').substr(0, 10)").AsString());
205 #if defined(HAVE_CURL) 206 #if defined(HAVE_CURL)
206 ASSERT_EQ("gzip", jsEngine->Evaluate("request.getResponseHeader('Content-Encod ing').substr(0, 4)").AsString()); 207 ASSERT_EQ("gzip", jsEngine.Evaluate("request.getResponseHeader('Content-Encodi ng').substr(0, 4)").AsString());
207 #endif 208 #endif
208 ASSERT_TRUE(jsEngine->Evaluate("request.getResponseHeader('Location')").IsNull ()); 209 ASSERT_TRUE(jsEngine.Evaluate("request.getResponseHeader('Location')").IsNull( ));
209 } 210 }
210 #else 211 #else
211 TEST_F(DefaultWebRequestTest, DummyWebRequest) 212 TEST_F(DefaultWebRequestTest, DummyWebRequest)
212 { 213 {
213 jsEngine->Evaluate("_webRequest.GET('https://easylist-downloads.adblockplus.or g/easylist.txt', {}, function(result) {foo = result;} )"); 214 auto& jsEngine = GetJsEngine();
215 jsEngine.Evaluate("_webRequest.GET('https://easylist-downloads.adblockplus.org /easylist.txt', {}, function(result) {foo = result;} )");
214 WaitForVariable("this.foo", jsEngine); 216 WaitForVariable("this.foo", jsEngine);
215 ASSERT_EQ(IWebRequest::NS_ERROR_FAILURE, jsEngine->Evaluate("foo.status").AsIn t()); 217 ASSERT_EQ(IWebRequest::NS_ERROR_FAILURE, jsEngine.Evaluate("foo.status").AsInt ());
216 ASSERT_EQ(0, jsEngine->Evaluate("foo.responseStatus").AsInt()); 218 ASSERT_EQ(0, jsEngine.Evaluate("foo.responseStatus").AsInt());
217 ASSERT_EQ("", jsEngine->Evaluate("foo.responseText").AsString()); 219 ASSERT_EQ("", jsEngine.Evaluate("foo.responseText").AsString());
218 ASSERT_EQ("{}", jsEngine->Evaluate("JSON.stringify(foo.responseHeaders)").AsSt ring()); 220 ASSERT_EQ("{}", jsEngine.Evaluate("JSON.stringify(foo.responseHeaders)").AsStr ing());
219 } 221 }
220 222
221 TEST_F(DefaultWebRequestTest, XMLHttpRequest) 223 TEST_F(DefaultWebRequestTest, XMLHttpRequest)
222 { 224 {
225 auto& jsEngine = GetJsEngine();
223 auto filterEngine = CreateFilterEngine(*fileSystem, *platform); 226 auto filterEngine = CreateFilterEngine(*fileSystem, *platform);
224 227
225 ResetTestXHR(jsEngine); 228 ResetTestXHR(jsEngine);
226 jsEngine->Evaluate("\ 229 jsEngine.Evaluate("\
227 request.setRequestHeader('X', 'Y');\ 230 request.setRequestHeader('X', 'Y');\
228 request.send(null);"); 231 request.send(null);");
229 WaitForVariable("result", jsEngine); 232 WaitForVariable("result", jsEngine);
230 ASSERT_EQ(IWebRequest::NS_ERROR_FAILURE, jsEngine->Evaluate("request.channel.s tatus").AsInt()); 233 ASSERT_EQ(IWebRequest::NS_ERROR_FAILURE, jsEngine.Evaluate("request.channel.st atus").AsInt());
231 ASSERT_EQ(0, jsEngine->Evaluate("request.status").AsInt()); 234 ASSERT_EQ(0, jsEngine.Evaluate("request.status").AsInt());
232 ASSERT_EQ("error", jsEngine->Evaluate("result").AsString()); 235 ASSERT_EQ("error", jsEngine.Evaluate("result").AsString());
233 ASSERT_TRUE(jsEngine->Evaluate("request.getResponseHeader('Content-Type')").Is Null()); 236 ASSERT_TRUE(jsEngine.Evaluate("request.getResponseHeader('Content-Type')").IsN ull());
234 } 237 }
235 238
236 #endif 239 #endif
237 240
238 namespace 241 namespace
239 { 242 {
240 class CatchLogSystem : public AdblockPlus::LogSystem 243 class CatchLogSystem : public AdblockPlus::LogSystem
241 { 244 {
242 public: 245 public:
243 AdblockPlus::LogSystem::LogLevel lastLogLevel; 246 AdblockPlus::LogSystem::LogLevel lastLogLevel;
(...skipping 25 matching lines...) Expand all
269 { 272 {
270 return LogSystemPtr(catchLogSystem = new CatchLogSystem()); 273 return LogSystemPtr(catchLogSystem = new CatchLogSystem());
271 } 274 }
272 protected: 275 protected:
273 CatchLogSystem* catchLogSystem; 276 CatchLogSystem* catchLogSystem;
274 }; 277 };
275 } 278 }
276 279
277 TEST_F(MockWebRequestAndLogSystemTest, RequestHeaderValidation) 280 TEST_F(MockWebRequestAndLogSystemTest, RequestHeaderValidation)
278 { 281 {
282 auto& jsEngine = GetJsEngine();
279 auto filterEngine = CreateFilterEngine(*fileSystem, *platform); 283 auto filterEngine = CreateFilterEngine(*fileSystem, *platform);
280 284
281 const std::string msg = "Attempt to set a forbidden header was denied: "; 285 const std::string msg = "Attempt to set a forbidden header was denied: ";
282 286
283 // The test will check that console.warn has been called when the 287 // The test will check that console.warn has been called when the
284 // header is rejected. While this is an implementation detail, we 288 // header is rejected. While this is an implementation detail, we
285 // have no other way to check this 289 // have no other way to check this
286 290
287 // test 'Accept-Encoding' is rejected 291 // test 'Accept-Encoding' is rejected
288 catchLogSystem->clear(); 292 catchLogSystem->clear();
289 std::string url = ResetTestXHR(jsEngine); 293 std::string url = ResetTestXHR(jsEngine);
290 jsEngine->Evaluate("\ 294 jsEngine.Evaluate("\
291 request.setRequestHeader('Accept-Encoding', 'gzip');\nrequest.send();"); 295 request.setRequestHeader('Accept-Encoding', 'gzip');\nrequest.send();");
292 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel ); 296 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel );
293 EXPECT_EQ(msg + "Accept-Encoding", catchLogSystem->lastMessage); 297 EXPECT_EQ(msg + "Accept-Encoding", catchLogSystem->lastMessage);
294 ProcessPendingWebRequests(); 298 ProcessPendingWebRequests();
295 { 299 {
296 auto headersRequest = GetHeadersForRequest(url); 300 auto headersRequest = GetHeadersForRequest(url);
297 EXPECT_TRUE(headersRequest.first); 301 EXPECT_TRUE(headersRequest.first);
298 const auto& headers = headersRequest.second; 302 const auto& headers = headersRequest.second;
299 EXPECT_TRUE(headers.cend() == headers.find("Accept-Encoding")); 303 EXPECT_TRUE(headers.cend() == headers.find("Accept-Encoding"));
300 } 304 }
301 305
302 // test 'DNT' is rejected 306 // test 'DNT' is rejected
303 catchLogSystem->clear(); 307 catchLogSystem->clear();
304 url = ResetTestXHR(jsEngine); 308 url = ResetTestXHR(jsEngine);
305 jsEngine->Evaluate("\ 309 jsEngine.Evaluate("\
306 request.setRequestHeader('DNT', '1');\nrequest.send();"); 310 request.setRequestHeader('DNT', '1');\nrequest.send();");
307 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel ); 311 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel );
308 EXPECT_EQ(msg + "DNT", catchLogSystem->lastMessage); 312 EXPECT_EQ(msg + "DNT", catchLogSystem->lastMessage);
309 ProcessPendingWebRequests(); 313 ProcessPendingWebRequests();
310 { 314 {
311 auto headersRequest = GetHeadersForRequest(url); 315 auto headersRequest = GetHeadersForRequest(url);
312 EXPECT_TRUE(headersRequest.first); 316 EXPECT_TRUE(headersRequest.first);
313 const auto& headers = headersRequest.second; 317 const auto& headers = headersRequest.second;
314 EXPECT_TRUE(headers.cend() == headers.find("DNT")); 318 EXPECT_TRUE(headers.cend() == headers.find("DNT"));
315 } 319 }
316 320
317 // test random 'X' header is accepted 321 // test random 'X' header is accepted
318 catchLogSystem->clear(); 322 catchLogSystem->clear();
319 url = ResetTestXHR(jsEngine); 323 url = ResetTestXHR(jsEngine);
320 jsEngine->Evaluate("\ 324 jsEngine.Evaluate("\
321 request.setRequestHeader('X', 'y');\nrequest.send();"); 325 request.setRequestHeader('X', 'y');\nrequest.send();");
322 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_TRACE, catchLogSystem->lastLogLeve l); 326 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_TRACE, catchLogSystem->lastLogLeve l);
323 EXPECT_EQ("", catchLogSystem->lastMessage); 327 EXPECT_EQ("", catchLogSystem->lastMessage);
324 ProcessPendingWebRequests(); 328 ProcessPendingWebRequests();
325 { 329 {
326 auto headersRequest = GetHeadersForRequest(url); 330 auto headersRequest = GetHeadersForRequest(url);
327 EXPECT_TRUE(headersRequest.first); 331 EXPECT_TRUE(headersRequest.first);
328 const auto& headers = headersRequest.second; 332 const auto& headers = headersRequest.second;
329 EXPECT_FALSE(headers.cend() == headers.find("X")); 333 EXPECT_FALSE(headers.cend() == headers.find("X"));
330 } 334 }
331 335
332 // test /^Proxy-/ is rejected. 336 // test /^Proxy-/ is rejected.
333 catchLogSystem->clear(); 337 catchLogSystem->clear();
334 url = ResetTestXHR(jsEngine); 338 url = ResetTestXHR(jsEngine);
335 jsEngine->Evaluate("\ 339 jsEngine.Evaluate("\
336 request.setRequestHeader('Proxy-foo', 'bar');\nrequest.send();"); 340 request.setRequestHeader('Proxy-foo', 'bar');\nrequest.send();");
337 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel ); 341 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel );
338 EXPECT_EQ(msg + "Proxy-foo", catchLogSystem->lastMessage); 342 EXPECT_EQ(msg + "Proxy-foo", catchLogSystem->lastMessage);
339 ProcessPendingWebRequests(); 343 ProcessPendingWebRequests();
340 { 344 {
341 auto headersRequest = GetHeadersForRequest(url); 345 auto headersRequest = GetHeadersForRequest(url);
342 EXPECT_TRUE(headersRequest.first); 346 EXPECT_TRUE(headersRequest.first);
343 const auto& headers = headersRequest.second; 347 const auto& headers = headersRequest.second;
344 EXPECT_TRUE(headers.cend() == headers.find("Proxy-foo")); 348 EXPECT_TRUE(headers.cend() == headers.find("Proxy-foo"));
345 } 349 }
346 350
347 // test /^Sec-/ is rejected. 351 // test /^Sec-/ is rejected.
348 catchLogSystem->clear(); 352 catchLogSystem->clear();
349 url = ResetTestXHR(jsEngine); 353 url = ResetTestXHR(jsEngine);
350 jsEngine->Evaluate("\ 354 jsEngine.Evaluate("\
351 request.setRequestHeader('Sec-foo', 'bar');\nrequest.send();"); 355 request.setRequestHeader('Sec-foo', 'bar');\nrequest.send();");
352 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel ); 356 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel );
353 EXPECT_EQ(msg + "Sec-foo", catchLogSystem->lastMessage); 357 EXPECT_EQ(msg + "Sec-foo", catchLogSystem->lastMessage);
354 ProcessPendingWebRequests(); 358 ProcessPendingWebRequests();
355 { 359 {
356 auto headersRequest = GetHeadersForRequest(url); 360 auto headersRequest = GetHeadersForRequest(url);
357 EXPECT_TRUE(headersRequest.first); 361 EXPECT_TRUE(headersRequest.first);
358 const auto& headers = headersRequest.second; 362 const auto& headers = headersRequest.second;
359 EXPECT_TRUE(headers.cend() == headers.find("Sec-foo")); 363 EXPECT_TRUE(headers.cend() == headers.find("Sec-foo"));
360 } 364 }
361 365
362 // test 'Security' is accepted. 366 // test 'Security' is accepted.
363 catchLogSystem->clear(); 367 catchLogSystem->clear();
364 url = ResetTestXHR(jsEngine); 368 url = ResetTestXHR(jsEngine);
365 jsEngine->Evaluate("\ 369 jsEngine.Evaluate("\
366 request.setRequestHeader('Security', 'theater');\nrequest.send();"); 370 request.setRequestHeader('Security', 'theater');\nrequest.send();");
367 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_TRACE, catchLogSystem->lastLogLeve l); 371 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_TRACE, catchLogSystem->lastLogLeve l);
368 EXPECT_EQ("", catchLogSystem->lastMessage); 372 EXPECT_EQ("", catchLogSystem->lastMessage);
369 ProcessPendingWebRequests(); 373 ProcessPendingWebRequests();
370 { 374 {
371 auto headersRequest = GetHeadersForRequest(url); 375 auto headersRequest = GetHeadersForRequest(url);
372 EXPECT_TRUE(headersRequest.first); 376 EXPECT_TRUE(headersRequest.first);
373 const auto& headers = headersRequest.second; 377 const auto& headers = headersRequest.second;
374 EXPECT_FALSE(headers.cend() == headers.find("Security")); 378 EXPECT_FALSE(headers.cend() == headers.find("Security"));
375 } 379 }
376 } 380 }
OLDNEW
« no previous file with comments | « test/UpdateCheck.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld