Index: test/WebRequest.cpp |
=================================================================== |
--- a/test/WebRequest.cpp |
+++ b/test/WebRequest.cpp |
@@ -25,20 +25,25 @@ |
namespace |
{ |
class MockWebRequest : public AdblockPlus::WebRequest |
{ |
public: |
AdblockPlus::ServerResponse GET(const std::string& url, const AdblockPlus::HeaderList& requestHeaders) const |
{ |
- lastRequestHeaders.clear(); |
+ std::set<std::string> r; |
sergei
2017/04/19 10:38:31
what about renaming here?
r -> headerNames
hub
2017/04/19 13:12:04
Done.
|
for (auto header : requestHeaders) |
{ |
- lastRequestHeaders.insert(header.first); |
+ r.insert(header.first); |
+ } |
+ { |
+ std::lock_guard<std::mutex> lock(lastRequestHeadersMutex); |
+ // XXX we currently ignore the result. We should check it actually gets inserted. |
sergei
2017/04/19 10:38:31
It would be better to remove XXX.
hub
2017/04/19 13:12:04
Acknowledged.
|
+ lastRequestHeaders.insert(std::make_pair(url, std::move(r))); |
} |
AdblockPlus::Sleep(50); |
AdblockPlus::ServerResponse result; |
result.status = NS_OK; |
result.responseStatus = 123; |
result.responseHeaders.push_back(std::pair<std::string, std::string>("Foo", "Bar")); |
@@ -46,17 +51,18 @@ |
if (!requestHeaders.empty()) |
{ |
result.responseText += requestHeaders[0].first + "\n" + requestHeaders[0].second; |
} |
return result; |
} |
// mutable. Very Ugly. But we are testing and need to change this in GET which is const. |
- mutable std::set<std::string> lastRequestHeaders; |
+ mutable std::mutex lastRequestHeadersMutex; |
+ mutable std::map<std::string, std::set<std::string>> lastRequestHeaders; |
sergei
2017/04/19 10:38:31
because now it's not only for last requests
lastR
hub
2017/04/19 13:12:04
Done.
|
}; |
template<class T> |
class WebRequestTest : public BaseJsTest |
{ |
protected: |
void SetUp() |
{ |
@@ -65,26 +71,36 @@ |
jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new LazyFileSystem())); |
} |
}; |
typedef WebRequestTest<MockWebRequest> MockWebRequestTest; |
typedef WebRequestTest<AdblockPlus::DefaultWebRequest> DefaultWebRequestTest; |
typedef WebRequestTest<MockWebRequest> XMLHttpRequestTest; |
- void ResetTestXHR(const AdblockPlus::JsEnginePtr& jsEngine) |
+ // we return the url of the XHR. |
+ std::string ResetTestXHR(const AdblockPlus::JsEnginePtr& jsEngine, const std::string& defaultUrl = "") |
{ |
- jsEngine->Evaluate("\ |
+ std::string url = defaultUrl; |
+ // make up a unique URL if we don't have one. |
+ if (url == "") |
+ { |
+ url = "https://tests.adblockplus.org/easylist.txt-"; |
+ url += std::to_string(std::chrono::system_clock::to_time_t(std::chrono::system_clock::now())); |
+ } |
+ |
+ jsEngine->Evaluate(std::string("\ |
var result;\ |
var request = new XMLHttpRequest();\ |
- request.open('GET', 'https://easylist-downloads.adblockplus.org/easylist.txt');\ |
+ request.open('GET', '") + url + "'); \ |
request.overrideMimeType('text/plain');\ |
request.addEventListener('load', function() {result = request.responseText;}, false);\ |
request.addEventListener('error', function() {result = 'error';}, false);\ |
"); |
+ return url; |
} |
void WaitForVariable(const std::string& variable, const AdblockPlus::JsEnginePtr& jsEngine) |
{ |
do |
{ |
AdblockPlus::Sleep(60); |
} while (jsEngine->Evaluate(variable)->IsUndefined()); |
@@ -130,17 +146,17 @@ |
#endif |
ASSERT_TRUE(jsEngine->Evaluate("foo.responseHeaders['location']")->IsUndefined()); |
} |
TEST_F(DefaultWebRequestTest, XMLHttpRequest) |
{ |
auto filterEngine = AdblockPlus::FilterEngine::Create(jsEngine); |
- ResetTestXHR(jsEngine); |
+ ResetTestXHR(jsEngine, "https://easylist-downloads.adblockplus.org/easylist.txt"); |
jsEngine->Evaluate("\ |
request.setRequestHeader('X', 'Y');\ |
request.setRequestHeader('X2', 'Y2');\ |
request.send(null);"); |
WaitForVariable("result", jsEngine); |
ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("request.channel.status")->AsInt()); |
ASSERT_EQ(200, jsEngine->Evaluate("request.status")->AsInt()); |
ASSERT_EQ("[Adblock Plus ", jsEngine->Evaluate("result.substr(0, 14)")->AsString()); |
@@ -223,72 +239,108 @@ |
const std::string msg = "Attempt to set a forbidden header was denied: "; |
// The test will check that console.warn has been called when the |
// header is rejected. While this is an implementation detail, we |
// have no other way to check this |
// test 'Accept-Encoding' is rejected |
catchLogSystem->clear(); |
- ResetTestXHR(jsEngine); |
+ std::string url = ResetTestXHR(jsEngine); |
jsEngine->Evaluate("\ |
request.setRequestHeader('Accept-Encoding', 'gzip');\nrequest.send();"); |
EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel); |
EXPECT_EQ(msg + "Accept-Encoding", catchLogSystem->lastMessage); |
WaitForVariable("result", jsEngine); |
- EXPECT_TRUE(webRequest->lastRequestHeaders.cend() == |
- webRequest->lastRequestHeaders.find("Accept-Encoding")); |
+ { |
+ std::lock_guard<std::mutex> lock(webRequest->lastRequestHeadersMutex); |
sergei
2017/04/19 10:38:31
any work with the mutex of webRequest should be mo
hub
2017/04/19 13:12:04
Done.
|
+ const auto& iter = webRequest->lastRequestHeaders.find(url); |
+ EXPECT_TRUE(iter != webRequest->lastRequestHeaders.end()); |
+ const auto& headers = iter->second; |
+ EXPECT_TRUE(headers.cend() == headers.find("Accept-Encoding")); |
+ webRequest->lastRequestHeaders.erase(iter); |
+ } |
// test 'DNT' is rejected |
catchLogSystem->clear(); |
- ResetTestXHR(jsEngine); |
+ url = ResetTestXHR(jsEngine); |
jsEngine->Evaluate("\ |
request.setRequestHeader('DNT', '1');\nrequest.send();"); |
EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel); |
EXPECT_EQ(msg + "DNT", catchLogSystem->lastMessage); |
WaitForVariable("result", jsEngine); |
- EXPECT_TRUE(webRequest->lastRequestHeaders.cend() == |
- webRequest->lastRequestHeaders.find("DNT")); |
+ { |
+ std::lock_guard<std::mutex> lock(webRequest->lastRequestHeadersMutex); |
+ const auto& iter = webRequest->lastRequestHeaders.find(url); |
+ EXPECT_TRUE(iter != webRequest->lastRequestHeaders.end()); |
+ const auto& headers = iter->second; |
+ EXPECT_TRUE(headers.cend() == headers.find("DNT")); |
+ webRequest->lastRequestHeaders.erase(iter); |
+ } |
// test random 'X' header is accepted |
catchLogSystem->clear(); |
- ResetTestXHR(jsEngine); |
+ url = ResetTestXHR(jsEngine); |
jsEngine->Evaluate("\ |
request.setRequestHeader('X', 'y');\nrequest.send();"); |
EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_TRACE, catchLogSystem->lastLogLevel); |
EXPECT_EQ("", catchLogSystem->lastMessage); |
WaitForVariable("result", jsEngine); |
- EXPECT_FALSE(webRequest->lastRequestHeaders.cend() == |
- webRequest->lastRequestHeaders.find("X")); |
+ { |
+ std::lock_guard<std::mutex> lock(webRequest->lastRequestHeadersMutex); |
+ const auto& iter = webRequest->lastRequestHeaders.find(url); |
+ EXPECT_TRUE(iter != webRequest->lastRequestHeaders.end()); |
+ const auto& headers = iter->second; |
+ EXPECT_FALSE(headers.cend() == headers.find("X")); |
+ webRequest->lastRequestHeaders.erase(iter); |
+ } |
// test /^Proxy-/ is rejected. |
catchLogSystem->clear(); |
- ResetTestXHR(jsEngine); |
+ url = ResetTestXHR(jsEngine); |
jsEngine->Evaluate("\ |
request.setRequestHeader('Proxy-foo', 'bar');\nrequest.send();"); |
EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel); |
EXPECT_EQ(msg + "Proxy-foo", catchLogSystem->lastMessage); |
WaitForVariable("result", jsEngine); |
- EXPECT_TRUE(webRequest->lastRequestHeaders.cend() == |
- webRequest->lastRequestHeaders.find("Proxy-foo")); |
+ { |
+ std::lock_guard<std::mutex> lock(webRequest->lastRequestHeadersMutex); |
+ const auto& iter = webRequest->lastRequestHeaders.find(url); |
+ EXPECT_TRUE(iter != webRequest->lastRequestHeaders.end()); |
+ const auto& headers = iter->second; |
+ EXPECT_TRUE(headers.cend() == headers.find("Proxy-foo")); |
+ webRequest->lastRequestHeaders.erase(iter); |
+ } |
// test /^Sec-/ is rejected. |
catchLogSystem->clear(); |
- ResetTestXHR(jsEngine); |
+ url = ResetTestXHR(jsEngine); |
jsEngine->Evaluate("\ |
request.setRequestHeader('Sec-foo', 'bar');\nrequest.send();"); |
EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_WARN, catchLogSystem->lastLogLevel); |
EXPECT_EQ(msg + "Sec-foo", catchLogSystem->lastMessage); |
WaitForVariable("result", jsEngine); |
- EXPECT_TRUE(webRequest->lastRequestHeaders.cend() == |
- webRequest->lastRequestHeaders.find("Sec-foo")); |
+ { |
+ std::lock_guard<std::mutex> lock(webRequest->lastRequestHeadersMutex); |
+ const auto& iter = webRequest->lastRequestHeaders.find(url); |
+ EXPECT_TRUE(iter != webRequest->lastRequestHeaders.end()); |
+ const auto& headers = iter->second; |
+ EXPECT_TRUE(headers.cend() == headers.find("Sec-foo")); |
+ webRequest->lastRequestHeaders.erase(iter); |
+ } |
// test 'Security' is accepted. |
catchLogSystem->clear(); |
- ResetTestXHR(jsEngine); |
+ url = ResetTestXHR(jsEngine); |
jsEngine->Evaluate("\ |
request.setRequestHeader('Security', 'theater');\nrequest.send();"); |
EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_TRACE, catchLogSystem->lastLogLevel); |
EXPECT_EQ("", catchLogSystem->lastMessage); |
WaitForVariable("result", jsEngine); |
- EXPECT_FALSE(webRequest->lastRequestHeaders.cend() == |
- webRequest->lastRequestHeaders.find("Security")); |
+ { |
+ std::lock_guard<std::mutex> lock(webRequest->lastRequestHeadersMutex); |
+ const auto& iter = webRequest->lastRequestHeaders.find(url); |
+ EXPECT_TRUE(iter != webRequest->lastRequestHeaders.end()); |
+ const auto& headers = iter->second; |
+ EXPECT_FALSE(headers.cend() == headers.find("Security")); |
+ webRequest->lastRequestHeaders.erase(iter); |
+ } |
} |