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

Unified Diff: test/WebRequest.cpp

Issue 10291009: Disable side-effects in filter engine tests (Closed)
Patch Set: Fixed the other TODO in tests Created April 26, 2013, 12:44 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « test/FilterEngine.cpp ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/WebRequest.cpp
===================================================================
--- a/test/WebRequest.cpp
+++ b/test/WebRequest.cpp
@@ -1,89 +1,87 @@
#include <sstream>
-#include <AdblockPlus.h>
-#include <gtest/gtest.h>
-
+#include "BaseJsTest.h"
#include "../src/Thread.h"
-// TODO: Use a fixture here
-
namespace
{
- class TestWebRequest : public AdblockPlus::WebRequest
+ class MockWebRequest : public AdblockPlus::WebRequest
{
public:
AdblockPlus::ServerResponse GET(const std::string& url, const AdblockPlus::HeaderList& requestHeaders) const
{
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"));
result.responseText = url + "\n" + requestHeaders[0].first + "\n" + requestHeaders[0].second;
return result;
}
};
- std::string ToString(unsigned int i)
+ template<class T>
Oleksandr 2013/04/26 13:10:01 Miht be a good idea to make this even more generic
Wladimir Palant 2013/04/26 13:12:58 I tend to resist making code more generic "for fut
+ class WebRequestTest : public BaseJsTest
{
- std::stringstream stream;
- stream << i;
- return stream.str();
- }
+ protected:
+ void SetUp()
+ {
+ BaseJsTest::SetUp();
+ jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(new T));
+ jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new LazyFileSystem));
+ }
+ };
+
+ typedef WebRequestTest<MockWebRequest> MockWebRequestTest;
+ typedef WebRequestTest<AdblockPlus::DefaultWebRequest> DefaultWebRequestTest;
}
-TEST(WebRequestTest, BadCall)
+TEST_F(MockWebRequestTest, BadCall)
{
- AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET()"));
ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('', {}, function(){})"));
ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET({toString: false}, {}, function(){})"));
ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', null, function(){})"));
ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', {}, null)"));
ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', {}, function(){}, 0)"));
}
-TEST(WebRequestTest, TestWebRequest)
+TEST_F(MockWebRequestTest, SuccessfulRequest)
{
- AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
- jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(new TestWebRequest()));
jsEngine->Evaluate("_webRequest.GET('http://example.com/', {X: 'Y'}, function(result) {foo = result;} )");
ASSERT_TRUE(jsEngine->Evaluate("this.foo")->IsUndefined());
AdblockPlus::Sleep(200);
ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("foo.status")->AsInt());
ASSERT_EQ(123, jsEngine->Evaluate("foo.responseStatus")->AsInt());
ASSERT_EQ("http://example.com/\nX\nY", jsEngine->Evaluate("foo.responseText")->AsString());
ASSERT_EQ("{\"Foo\":\"Bar\"}", jsEngine->Evaluate("JSON.stringify(foo.responseHeaders)")->AsString());
}
#if defined(HAVE_CURL) || defined(_WIN32)
-TEST(WebRequestTest, RealWebRequest)
+TEST_F(DefaultWebRequestTest, RealWebRequest)
{
- AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
-
// This URL should redirect to easylist-downloads.adblockplus.org and we
// should get the actual filter list back.
jsEngine->Evaluate("_webRequest.GET('https://easylist.adblockplus.org/easylist.txt', {}, function(result) {foo = result;} )");
do
{
AdblockPlus::Sleep(200);
} while (jsEngine->Evaluate("this.foo")->IsUndefined());
ASSERT_EQ("text/plain", jsEngine->Evaluate("foo.responseHeaders['content-type'].substr(0, 10)")->AsString());
ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("foo.status")->AsInt());
ASSERT_EQ(200, jsEngine->Evaluate("foo.responseStatus")->AsInt());
ASSERT_EQ("[Adblock Plus ", jsEngine->Evaluate("foo.responseText.substr(0, 14)")->AsString());
ASSERT_EQ("text/plain", jsEngine->Evaluate("foo.responseHeaders['content-type'].substr(0, 10)")->AsString());
ASSERT_TRUE(jsEngine->Evaluate("foo.responseHeaders['location']")->IsUndefined());
}
-TEST(WebRequestTest, XMLHttpRequest)
+TEST_F(DefaultWebRequestTest, XMLHttpRequest)
{
- AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
AdblockPlus::FilterEngine filterEngine(jsEngine);
jsEngine->Evaluate("\
var result;\
var request = new XMLHttpRequest();\
request.open('GET', 'https://easylist.adblockplus.org/easylist.txt');\
request.setRequestHeader('X', 'Y');\
request.overrideMimeType('text/plain');\
@@ -96,33 +94,31 @@ TEST(WebRequestTest, XMLHttpRequest)
} while (jsEngine->Evaluate("result")->IsUndefined());
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());
ASSERT_EQ("text/plain", jsEngine->Evaluate("request.getResponseHeader('Content-Type').substr(0, 10)")->AsString());
ASSERT_TRUE(jsEngine->Evaluate("request.getResponseHeader('Location')")->IsNull());
}
#else
-TEST(WebRequestTest, DummyWebRequest)
+TEST_F(DefaultWebRequestTest, DummyWebRequest)
{
- AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
jsEngine->Evaluate("_webRequest.GET('https://easylist.adblockplus.org/easylist.txt', {}, function(result) {foo = result;} )");
do
{
AdblockPlus::Sleep(200);
} while (jsEngine->Evaluate("this.foo")->IsUndefined());
ASSERT_EQ(AdblockPlus::WebRequest::NS_ERROR_FAILURE, jsEngine->Evaluate("foo.status")->AsInt());
ASSERT_EQ(0, jsEngine->Evaluate("foo.responseStatus")->AsInt());
ASSERT_EQ("", jsEngine->Evaluate("foo.responseText")->AsString());
ASSERT_EQ("{}", jsEngine->Evaluate("JSON.stringify(foo.responseHeaders)")->AsString());
}
-TEST(WebRequestTest, XMLHttpRequest)
+TEST_F(DefaultWebRequestTest, XMLHttpRequest)
{
- AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
AdblockPlus::FilterEngine filterEngine(jsEngine);
jsEngine->Evaluate("\
var result;\
var request = new XMLHttpRequest();\
request.open('GET', 'https://easylist.adblockplus.org/easylist.txt');\
request.setRequestHeader('X', 'Y');\
request.overrideMimeType('text/plain');\
« no previous file with comments | « test/FilterEngine.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld