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

Side by Side Diff: test/WebRequest.cpp

Issue 29497591: Issue 5180 - remove synchronous WebRequest interface and stop exposing of DefaultWebRequest (Closed) Base URL: https://github.com/adblockplus/libadblockplus.git
Patch Set: Created July 25, 2017, 2:31 p.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/Notification.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 MockWebRequest : public AdblockPlus::WebRequest 28 class DefaultWebRequestTest : public ::testing::Test
29 { 29 {
30 public: 30 protected:
31 AdblockPlus::ServerResponse GET(const std::string& url, const AdblockPlus::H eaderList& requestHeaders) const 31 void SetUp()
32 { 32 {
33 std::set<std::string> headerNames; 33 JsEngineCreationParameters jsEngineParams;
34 for (auto header : requestHeaders) 34 jsEngineParams.timer.reset(new NoopTimer());
35 jsEngineParams.fileSystem.reset(new LazyFileSystem());
36 jsEngineParams.webRequest = CreateWebRequest();
37 jsEngine = CreateJsEngine(std::move(jsEngineParams));
38 }
39
40 virtual WebRequestPtr CreateWebRequest()
41 {
42 return CreateDefaultWebRequest();
43 }
44
45 JsEnginePtr jsEngine;
46 };
47
48 class MockWebRequestTest : public DefaultWebRequestTest
49 {
50 virtual WebRequestPtr CreateWebRequest() override
51 {
52 return DelayedWebRequest::New(webRequestTasks);
53 }
54
55 protected:
56 void ProcessPendingWebRequests()
57 {
58 for (auto iiWebTask = webRequestTasks->cbegin(); iiWebTask != webRequestTa sks->cend();
59 webRequestTasks->erase(iiWebTask++))
35 { 60 {
36 headerNames.insert(header.first); 61 const auto& webRequestTask = *iiWebTask;
62 std::set<std::string> headerNames;
63 for (const auto& header : webRequestTask.headers)
64 {
65 headerNames.insert(header.first);
66 }
67 // we currently ignore the result. We should check it actually gets inse rted.
68 requestHeaderNames.insert(std::make_pair(webRequestTask.url, std::move(h eaderNames)));
69
70 AdblockPlus::ServerResponse result;
71 result.status = IWebRequest::NS_OK;
72 result.responseStatus = 123;
73 result.responseHeaders.push_back(std::pair<std::string, std::string>("Fo o", "Bar"));
74 result.responseText = webRequestTask.url + "\n";
75 if (!webRequestTask.headers.empty())
76 {
77 result.responseText += webRequestTask.headers[0].first + "\n" + webReq uestTask.headers[0].second;
78 }
79 webRequestTask.getCallback(result);
37 } 80 }
38 {
39 std::lock_guard<std::mutex> lock(requestHeaderNamesMutex);
40 // we currently ignore the result. We should check it actually gets inse rted.
41 requestHeaderNames.insert(std::make_pair(url, std::move(headerNames)));
42 }
43
44 AdblockPlus::Sleep(50);
45
46 AdblockPlus::ServerResponse result;
47 result.status = IWebRequest::NS_OK;
48 result.responseStatus = 123;
49 result.responseHeaders.push_back(std::pair<std::string, std::string>("Foo" , "Bar"));
50 result.responseText = url + "\n";
51 if (!requestHeaders.empty())
52 {
53 result.responseText += requestHeaders[0].first + "\n" + requestHeaders[0 ].second;
54 }
55 return result;
56 } 81 }
57 82
58 // Testing method 83 // Testing method
59 // Get the headers for the request. Return a pair of a bool (found 84 // Get the headers for the request. Return a pair of a bool (found or not)
60 // or not) and the actual header names 85 // and the actual header names
61 std::pair<bool, std::set<std::string>> headersForRequest(const std::string& url) 86 std::pair<bool, std::set<std::string>> GetHeadersForRequest(const std::strin g& url)
62 { 87 {
63 std::lock_guard<std::mutex> lock(requestHeaderNamesMutex);
64 auto iter = requestHeaderNames.find(url); 88 auto iter = requestHeaderNames.find(url);
65 if (iter != requestHeaderNames.end()) 89 if (iter != requestHeaderNames.end())
66 { 90 {
67 auto result = std::make_pair(true, iter->second); 91 auto result = std::make_pair(true, iter->second);
68 requestHeaderNames.erase(iter); 92 requestHeaderNames.erase(iter);
69 return result; 93 return result;
70 } 94 }
71 return std::make_pair(false, std::set<std::string>()); 95 return std::make_pair(false, std::set<std::string>());
72 } 96 }
73 97
74 // mutable. Very Ugly. But we are testing and need to change this in GET whi ch is const. 98 DelayedWebRequest::SharedTasks webRequestTasks;
75 mutable std::mutex requestHeaderNamesMutex; 99 std::map<std::string, std::set<std::string>> requestHeaderNames;
76 mutable std::map<std::string, std::set<std::string>> requestHeaderNames;
77 }; 100 };
78 101
79 template<class T>
80 class WebRequestTest : public ::testing::Test
81 {
82 protected:
83 void SetUp()
84 {
85 JsEngineCreationParameters jsEngineParams;
86 jsEngineParams.timer.reset(new NoopTimer());
87 jsEngineParams.fileSystem.reset(new LazyFileSystem());
88 jsEngine = CreateJsEngine(std::move(jsEngineParams));
89 webRequest = std::make_shared<T>();
90 jsEngine->SetWebRequest(webRequest);
91 }
92
93 std::shared_ptr<T> webRequest;
94 JsEnginePtr jsEngine;
95 };
96
97 typedef WebRequestTest<MockWebRequest> MockWebRequestTest;
98 typedef WebRequestTest<AdblockPlus::DefaultWebRequestSync> DefaultWebRequestTe st;
99 typedef WebRequestTest<MockWebRequest> XMLHttpRequestTest;
100
101 // we return the url of the XHR. 102 // we return the url of the XHR.
102 std::string ResetTestXHR(const AdblockPlus::JsEnginePtr& jsEngine, const std:: string& defaultUrl = "") 103 std::string ResetTestXHR(const AdblockPlus::JsEnginePtr& jsEngine, const std:: string& defaultUrl = "")
103 { 104 {
104 std::string url = defaultUrl; 105 std::string url = defaultUrl;
105 // make up a unique URL if we don't have one. 106 // make up a unique URL if we don't have one.
106 if (url == "") 107 if (url == "")
107 { 108 {
108 url = "https://tests.adblockplus.org/easylist.txt-"; 109 url = "https://tests.adblockplus.org/easylist.txt-";
109 url += std::to_string(std::chrono::system_clock::to_time_t(std::chrono::sy stem_clock::now())); 110 url += std::to_string(std::chrono::system_clock::to_time_t(std::chrono::sy stem_clock::now()));
110 } 111 }
(...skipping 26 matching lines...) Expand all
137 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET({toString: false}, {}, fu nction(){})")); 138 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET({toString: false}, {}, fu nction(){})"));
138 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', nu ll, function(){})")); 139 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', nu ll, function(){})"));
139 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', {} , null)")); 140 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', {} , null)"));
140 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', {} , function(){}, 0)")); 141 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', {} , function(){}, 0)"));
141 } 142 }
142 143
143 TEST_F(MockWebRequestTest, SuccessfulRequest) 144 TEST_F(MockWebRequestTest, SuccessfulRequest)
144 { 145 {
145 jsEngine->Evaluate("_webRequest.GET('http://example.com/', {X: 'Y'}, function( result) {foo = result;} )"); 146 jsEngine->Evaluate("_webRequest.GET('http://example.com/', {X: 'Y'}, function( result) {foo = result;} )");
146 ASSERT_TRUE(jsEngine->Evaluate("this.foo").IsUndefined()); 147 ASSERT_TRUE(jsEngine->Evaluate("this.foo").IsUndefined());
147 AdblockPlus::Sleep(200); 148 ProcessPendingWebRequests();
148 ASSERT_EQ(IWebRequest::NS_OK, jsEngine->Evaluate("foo.status").AsInt()); 149 ASSERT_EQ(IWebRequest::NS_OK, jsEngine->Evaluate("foo.status").AsInt());
149 ASSERT_EQ(123, jsEngine->Evaluate("foo.responseStatus").AsInt()); 150 ASSERT_EQ(123, jsEngine->Evaluate("foo.responseStatus").AsInt());
150 ASSERT_EQ("http://example.com/\nX\nY", jsEngine->Evaluate("foo.responseText"). AsString()); 151 ASSERT_EQ("http://example.com/\nX\nY", jsEngine->Evaluate("foo.responseText"). AsString());
151 ASSERT_EQ("{\"Foo\":\"Bar\"}", jsEngine->Evaluate("JSON.stringify(foo.response Headers)").AsString()); 152 ASSERT_EQ("{\"Foo\":\"Bar\"}", jsEngine->Evaluate("JSON.stringify(foo.response Headers)").AsString());
152 } 153 }
153 154
154 #if defined(HAVE_CURL) || defined(_WIN32) 155 #if defined(HAVE_CURL) || defined(_WIN32)
155 TEST_F(DefaultWebRequestTest, RealWebRequest) 156 TEST_F(DefaultWebRequestTest, RealWebRequest)
156 { 157 {
157 // This URL should redirect to easylist-downloads.adblockplus.org and we 158 // This URL should redirect to easylist-downloads.adblockplus.org and we
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 void clear() 241 void clear()
241 { 242 {
242 lastLogLevel = AdblockPlus::LogSystem::LOG_LEVEL_TRACE; 243 lastLogLevel = AdblockPlus::LogSystem::LOG_LEVEL_TRACE;
243 lastMessage.clear(); 244 lastMessage.clear();
244 } 245 }
245 }; 246 };
246 247
247 typedef std::shared_ptr<CatchLogSystem> CatchLogSystemPtr; 248 typedef std::shared_ptr<CatchLogSystem> CatchLogSystemPtr;
248 } 249 }
249 250
250 TEST_F(XMLHttpRequestTest, RequestHeaderValidation) 251 TEST_F(MockWebRequestTest, RequestHeaderValidation)
251 { 252 {
252 auto catchLogSystem = CatchLogSystemPtr(new CatchLogSystem()); 253 auto catchLogSystem = CatchLogSystemPtr(new CatchLogSystem());
253 jsEngine->SetLogSystem(catchLogSystem); 254 jsEngine->SetLogSystem(catchLogSystem);
254 255
255 auto filterEngine = AdblockPlus::FilterEngine::Create(jsEngine); 256 auto filterEngine = AdblockPlus::FilterEngine::Create(jsEngine);
256 257
257 const std::string msg = "Attempt to set a forbidden header was denied: "; 258 const std::string msg = "Attempt to set a forbidden header was denied: ";
258 259
259 // The test will check that console.warn has been called when the 260 // The test will check that console.warn has been called when the
260 // header is rejected. While this is an implementation detail, we 261 // header is rejected. While this is an implementation detail, we
261 // have no other way to check this 262 // have no other way to check this
262 263
263 // test 'Accept-Encoding' is rejected 264 // test 'Accept-Encoding' is rejected
264 catchLogSystem->clear(); 265 catchLogSystem->clear();
265 std::string url = ResetTestXHR(jsEngine); 266 std::string url = ResetTestXHR(jsEngine);
266 jsEngine->Evaluate("\ 267 jsEngine->Evaluate("\
267 request.setRequestHeader('Accept-Encoding', 'gzip');\nrequest.send();"); 268 request.setRequestHeader('Accept-Encoding', 'gzip');\nrequest.send();");
268 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel ); 269 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel );
269 EXPECT_EQ(msg + "Accept-Encoding", catchLogSystem->lastMessage); 270 EXPECT_EQ(msg + "Accept-Encoding", catchLogSystem->lastMessage);
270 WaitForVariable("result", jsEngine); 271 ProcessPendingWebRequests();
271 { 272 {
272 auto headersRequest = webRequest->headersForRequest(url); 273 auto headersRequest = GetHeadersForRequest(url);
273 EXPECT_TRUE(headersRequest.first); 274 EXPECT_TRUE(headersRequest.first);
274 const auto& headers = headersRequest.second; 275 const auto& headers = headersRequest.second;
275 EXPECT_TRUE(headers.cend() == headers.find("Accept-Encoding")); 276 EXPECT_TRUE(headers.cend() == headers.find("Accept-Encoding"));
276 } 277 }
277 278
278 // test 'DNT' is rejected 279 // test 'DNT' is rejected
279 catchLogSystem->clear(); 280 catchLogSystem->clear();
280 url = ResetTestXHR(jsEngine); 281 url = ResetTestXHR(jsEngine);
281 jsEngine->Evaluate("\ 282 jsEngine->Evaluate("\
282 request.setRequestHeader('DNT', '1');\nrequest.send();"); 283 request.setRequestHeader('DNT', '1');\nrequest.send();");
283 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel ); 284 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel );
284 EXPECT_EQ(msg + "DNT", catchLogSystem->lastMessage); 285 EXPECT_EQ(msg + "DNT", catchLogSystem->lastMessage);
285 WaitForVariable("result", jsEngine); 286 ProcessPendingWebRequests();
286 { 287 {
287 auto headersRequest = webRequest->headersForRequest(url); 288 auto headersRequest = GetHeadersForRequest(url);
288 EXPECT_TRUE(headersRequest.first); 289 EXPECT_TRUE(headersRequest.first);
289 const auto& headers = headersRequest.second; 290 const auto& headers = headersRequest.second;
290 EXPECT_TRUE(headers.cend() == headers.find("DNT")); 291 EXPECT_TRUE(headers.cend() == headers.find("DNT"));
291 } 292 }
292 293
293 // test random 'X' header is accepted 294 // test random 'X' header is accepted
294 catchLogSystem->clear(); 295 catchLogSystem->clear();
295 url = ResetTestXHR(jsEngine); 296 url = ResetTestXHR(jsEngine);
296 jsEngine->Evaluate("\ 297 jsEngine->Evaluate("\
297 request.setRequestHeader('X', 'y');\nrequest.send();"); 298 request.setRequestHeader('X', 'y');\nrequest.send();");
298 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_TRACE, catchLogSystem->lastLogLeve l); 299 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_TRACE, catchLogSystem->lastLogLeve l);
299 EXPECT_EQ("", catchLogSystem->lastMessage); 300 EXPECT_EQ("", catchLogSystem->lastMessage);
300 WaitForVariable("result", jsEngine); 301 ProcessPendingWebRequests();
301 { 302 {
302 auto headersRequest = webRequest->headersForRequest(url); 303 auto headersRequest = GetHeadersForRequest(url);
303 EXPECT_TRUE(headersRequest.first); 304 EXPECT_TRUE(headersRequest.first);
304 const auto& headers = headersRequest.second; 305 const auto& headers = headersRequest.second;
305 EXPECT_FALSE(headers.cend() == headers.find("X")); 306 EXPECT_FALSE(headers.cend() == headers.find("X"));
306 } 307 }
307 308
308 // test /^Proxy-/ is rejected. 309 // test /^Proxy-/ is rejected.
309 catchLogSystem->clear(); 310 catchLogSystem->clear();
310 url = ResetTestXHR(jsEngine); 311 url = ResetTestXHR(jsEngine);
311 jsEngine->Evaluate("\ 312 jsEngine->Evaluate("\
312 request.setRequestHeader('Proxy-foo', 'bar');\nrequest.send();"); 313 request.setRequestHeader('Proxy-foo', 'bar');\nrequest.send();");
313 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel ); 314 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel );
314 EXPECT_EQ(msg + "Proxy-foo", catchLogSystem->lastMessage); 315 EXPECT_EQ(msg + "Proxy-foo", catchLogSystem->lastMessage);
315 WaitForVariable("result", jsEngine); 316 ProcessPendingWebRequests();
316 { 317 {
317 auto headersRequest = webRequest->headersForRequest(url); 318 auto headersRequest = GetHeadersForRequest(url);
318 EXPECT_TRUE(headersRequest.first); 319 EXPECT_TRUE(headersRequest.first);
319 const auto& headers = headersRequest.second; 320 const auto& headers = headersRequest.second;
320 EXPECT_TRUE(headers.cend() == headers.find("Proxy-foo")); 321 EXPECT_TRUE(headers.cend() == headers.find("Proxy-foo"));
321 } 322 }
322 323
323 // test /^Sec-/ is rejected. 324 // test /^Sec-/ is rejected.
324 catchLogSystem->clear(); 325 catchLogSystem->clear();
325 url = ResetTestXHR(jsEngine); 326 url = ResetTestXHR(jsEngine);
326 jsEngine->Evaluate("\ 327 jsEngine->Evaluate("\
327 request.setRequestHeader('Sec-foo', 'bar');\nrequest.send();"); 328 request.setRequestHeader('Sec-foo', 'bar');\nrequest.send();");
328 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel ); 329 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel );
329 EXPECT_EQ(msg + "Sec-foo", catchLogSystem->lastMessage); 330 EXPECT_EQ(msg + "Sec-foo", catchLogSystem->lastMessage);
330 WaitForVariable("result", jsEngine); 331 ProcessPendingWebRequests();
331 { 332 {
332 auto headersRequest = webRequest->headersForRequest(url); 333 auto headersRequest = GetHeadersForRequest(url);
333 EXPECT_TRUE(headersRequest.first); 334 EXPECT_TRUE(headersRequest.first);
334 const auto& headers = headersRequest.second; 335 const auto& headers = headersRequest.second;
335 EXPECT_TRUE(headers.cend() == headers.find("Sec-foo")); 336 EXPECT_TRUE(headers.cend() == headers.find("Sec-foo"));
336 } 337 }
337 338
338 // test 'Security' is accepted. 339 // test 'Security' is accepted.
339 catchLogSystem->clear(); 340 catchLogSystem->clear();
340 url = ResetTestXHR(jsEngine); 341 url = ResetTestXHR(jsEngine);
341 jsEngine->Evaluate("\ 342 jsEngine->Evaluate("\
342 request.setRequestHeader('Security', 'theater');\nrequest.send();"); 343 request.setRequestHeader('Security', 'theater');\nrequest.send();");
343 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_TRACE, catchLogSystem->lastLogLeve l); 344 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_TRACE, catchLogSystem->lastLogLeve l);
344 EXPECT_EQ("", catchLogSystem->lastMessage); 345 EXPECT_EQ("", catchLogSystem->lastMessage);
345 WaitForVariable("result", jsEngine); 346 ProcessPendingWebRequests();
346 { 347 {
347 auto headersRequest = webRequest->headersForRequest(url); 348 auto headersRequest = GetHeadersForRequest(url);
348 EXPECT_TRUE(headersRequest.first); 349 EXPECT_TRUE(headersRequest.first);
349 const auto& headers = headersRequest.second; 350 const auto& headers = headersRequest.second;
350 EXPECT_FALSE(headers.cend() == headers.find("Security")); 351 EXPECT_FALSE(headers.cend() == headers.find("Security"));
351 } 352 }
352 } 353 }
OLDNEW
« no previous file with comments | « test/Notification.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld