OLD | NEW |
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 DefaultWebRequestTest : public ::testing::Test | 28 class BaseWebRequestTest : public ::testing::Test |
29 { | 29 { |
30 protected: | 30 protected: |
31 void SetUp() | 31 void SetUp() |
32 { | 32 { |
33 JsEngineCreationParameters jsEngineParams; | 33 JsEngineCreationParameters jsEngineParams; |
34 jsEngineParams.logSystem = CreateLogSystem(); | 34 jsEngineParams.logSystem = CreateLogSystem(); |
35 jsEngineParams.timer.reset(new NoopTimer()); | 35 jsEngineParams.timer.reset(new NoopTimer()); |
36 jsEngineParams.fileSystem.reset(fileSystem = new LazyFileSystem()); | 36 jsEngineParams.fileSystem.reset(fileSystem = new LazyFileSystem()); |
37 jsEngineParams.webRequest = CreateWebRequest(); | 37 jsEngineParams.webRequest = CreateWebRequest(); |
38 jsEngine = CreateJsEngine(std::move(jsEngineParams)); | 38 jsEngine = CreateJsEngine(std::move(jsEngineParams)); |
39 } | 39 } |
40 | 40 |
41 virtual WebRequestPtr CreateWebRequest() | 41 virtual WebRequestPtr CreateWebRequest() = 0; |
42 { | |
43 return CreateDefaultWebRequest(); | |
44 } | |
45 | 42 |
46 virtual LogSystemPtr CreateLogSystem() | 43 virtual LogSystemPtr CreateLogSystem() |
47 { | 44 { |
48 return LogSystemPtr(new ThrowingLogSystem()); | 45 return LogSystemPtr(new ThrowingLogSystem()); |
49 } | 46 } |
50 | 47 |
51 JsEnginePtr jsEngine; | 48 JsEnginePtr jsEngine; |
52 LazyFileSystem* fileSystem; | 49 LazyFileSystem* fileSystem; |
53 }; | 50 }; |
54 | 51 |
55 class MockWebRequestTest : public DefaultWebRequestTest | 52 class DefaultWebRequestTest : public BaseWebRequestTest |
56 { | 53 { |
57 virtual WebRequestPtr CreateWebRequest() override | 54 WebRequestPtr CreateWebRequest() override |
| 55 { |
| 56 return CreateDefaultWebRequest([this](const SchedulerTask& task) |
| 57 { |
| 58 webRequestTasks.emplace_back(task); |
| 59 }); |
| 60 } |
| 61 std::list<SchedulerTask> webRequestTasks; |
| 62 protected: |
| 63 void WaitForVariable(const std::string& variable, const AdblockPlus::JsEngin
ePtr& jsEngine) |
| 64 { |
| 65 while (jsEngine->Evaluate(variable).IsUndefined() && !webRequestTasks.empt
y()) |
| 66 { |
| 67 (*webRequestTasks.begin())(); |
| 68 webRequestTasks.pop_front(); |
| 69 } |
| 70 } |
| 71 }; |
| 72 |
| 73 class MockWebRequestTest : public BaseWebRequestTest |
| 74 { |
| 75 WebRequestPtr CreateWebRequest() override |
58 { | 76 { |
59 return DelayedWebRequest::New(webRequestTasks); | 77 return DelayedWebRequest::New(webRequestTasks); |
60 } | 78 } |
61 | 79 |
62 protected: | 80 protected: |
63 void ProcessPendingWebRequests() | 81 void ProcessPendingWebRequests() |
64 { | 82 { |
65 for (auto iiWebTask = webRequestTasks->cbegin(); iiWebTask != webRequestTa
sks->cend(); | 83 for (auto iiWebTask = webRequestTasks->cbegin(); iiWebTask != webRequestTa
sks->cend(); |
66 webRequestTasks->erase(iiWebTask++)) | 84 webRequestTasks->erase(iiWebTask++)) |
67 { | 85 { |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 jsEngine->Evaluate(std::string("\ | 138 jsEngine->Evaluate(std::string("\ |
121 var result;\ | 139 var result;\ |
122 var request = new XMLHttpRequest();\ | 140 var request = new XMLHttpRequest();\ |
123 request.open('GET', '") + url + "'); \ | 141 request.open('GET', '") + url + "'); \ |
124 request.overrideMimeType('text/plain');\ | 142 request.overrideMimeType('text/plain');\ |
125 request.addEventListener('load', function() {result = request.responseText
;}, false);\ | 143 request.addEventListener('load', function() {result = request.responseText
;}, false);\ |
126 request.addEventListener('error', function() {result = 'error';}, false);\ | 144 request.addEventListener('error', function() {result = 'error';}, false);\ |
127 "); | 145 "); |
128 return url; | 146 return url; |
129 } | 147 } |
130 | |
131 void WaitForVariable(const std::string& variable, const AdblockPlus::JsEngineP
tr& jsEngine) | |
132 { | |
133 do | |
134 { | |
135 AdblockPlus::Sleep(60); | |
136 } while (jsEngine->Evaluate(variable).IsUndefined()); | |
137 } | |
138 | |
139 } | 148 } |
140 | 149 |
141 TEST_F(MockWebRequestTest, BadCall) | 150 TEST_F(MockWebRequestTest, BadCall) |
142 { | 151 { |
143 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET()")); | 152 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET()")); |
144 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('', {}, function(){})")); | 153 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('', {}, function(){})")); |
145 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET({toString: false}, {}, fu
nction(){})")); | 154 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET({toString: false}, {}, fu
nction(){})")); |
146 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', nu
ll, function(){})")); | 155 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', nu
ll, function(){})")); |
147 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', {}
, null)")); | 156 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', {}
, null)")); |
148 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', {}
, function(){}, 0)")); | 157 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', {}
, function(){}, 0)")); |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
356 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_TRACE, catchLogSystem->lastLogLeve
l); | 365 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_TRACE, catchLogSystem->lastLogLeve
l); |
357 EXPECT_EQ("", catchLogSystem->lastMessage); | 366 EXPECT_EQ("", catchLogSystem->lastMessage); |
358 ProcessPendingWebRequests(); | 367 ProcessPendingWebRequests(); |
359 { | 368 { |
360 auto headersRequest = GetHeadersForRequest(url); | 369 auto headersRequest = GetHeadersForRequest(url); |
361 EXPECT_TRUE(headersRequest.first); | 370 EXPECT_TRUE(headersRequest.first); |
362 const auto& headers = headersRequest.second; | 371 const auto& headers = headersRequest.second; |
363 EXPECT_FALSE(headers.cend() == headers.find("Security")); | 372 EXPECT_FALSE(headers.cend() == headers.find("Security")); |
364 } | 373 } |
365 } | 374 } |
OLD | NEW |