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

Unified Diff: test/WebRequest.cpp

Issue 10310030: Convert references to FileSystem & Co. into shared pointers (avoid use after free) (Closed)
Patch Set: Created April 18, 2013, 11:59 a.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
« src/JsEngine.cpp ('K') | « test/JsValue.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,18 +1,14 @@
#include <sstream>
#include <AdblockPlus.h>
#include <gtest/gtest.h>
#include "../src/Thread.h"
-#ifdef HAVE_WININET
-#include "../src/WebRequestWinInet.h"
-#endif
-
class TestWebRequest : public AdblockPlus::WebRequest
{
public:
AdblockPlus::ServerResponse GET(const std::string& url, const AdblockPlus::HeaderList& requestHeaders) const
{
AdblockPlus::Sleep(50);
AdblockPlus::ServerResponse result;
@@ -28,79 +24,61 @@ std::string ToString(unsigned int i)
{
std::stringstream stream;
stream << i;
return stream.str();
}
TEST(WebRequestTest, BadCall)
{
- AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, 0, 0);
+ AdblockPlus::JsEngine jsEngine;
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)
{
- TestWebRequest webRequest;
- AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, &webRequest, 0);
+ AdblockPlus::JsEngine jsEngine;
+ 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)
+#if defined(HAVE_CURL) or defined(HAVE_WININET)
TEST(WebRequestTest, RealWebRequest)
{
- AdblockPlus::DefaultWebRequest webRequest;
- AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, &webRequest, 0);
+ AdblockPlus::JsEngine jsEngine;
// 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(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());
}
-#elif defined(HAVE_WININET)
-TEST(WebRequestTest, RealWebRequest)
-{
- AdblockPlus::WebRequestWinInet webRequest;
- AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, &webRequest, 0);
- jsEngine.Evaluate("_webRequest.GET('https://easylist.adblockplus.org/easylist.txt', {}, function(result) {foo = result;} )");
- do
- {
- AdblockPlus::Sleep(200);
- } while (jsEngine.Evaluate("typeof foo") == "undefined");
- ASSERT_EQ(ToString(AdblockPlus::WebRequest::NS_OK), jsEngine.Evaluate("foo.status"));
- ASSERT_EQ("200", jsEngine.Evaluate("foo.responseStatus"));
- ASSERT_EQ("[Adblock Plus ", jsEngine.Evaluate("foo.responseText.substr(0, 14)"));
- //TODO: Shall we be strict with letter casing here? Content-Type vs content-type
- ASSERT_EQ("text/plain", jsEngine.Evaluate("foo.responseHeaders['Content-Type'].substr(0, 10)"));
- ASSERT_EQ("undefined", jsEngine.Evaluate("typeof foo.responseHeaders['location']"));
-}
+#if !FILTER_ENGINE_STUBS
TEST(WebRequestTest, XMLHttpRequest)
{
- AdblockPlus::DefaultWebRequest webRequest;
- AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, &webRequest, 0);
+ AdblockPlus::JsEngine jsEngine;
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');\
@@ -112,36 +90,37 @@ TEST(WebRequestTest, XMLHttpRequest)
AdblockPlus::Sleep(200);
} 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());
}
+#endif
+
#else
TEST(WebRequestTest, DummyWebRequest)
{
- AdblockPlus::DefaultWebRequest webRequest;
- AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, &webRequest, 0);
+ AdblockPlus::JsEngine jsEngine;
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());
}
+#if !FILTER_ENGINE_STUBS
TEST(WebRequestTest, XMLHttpRequest)
{
- AdblockPlus::DefaultWebRequest webRequest;
- AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, &webRequest, 0);
+ AdblockPlus::JsEngine jsEngine;
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');\
@@ -153,8 +132,10 @@ TEST(WebRequestTest, XMLHttpRequest)
AdblockPlus::Sleep(200);
} while (jsEngine.Evaluate("result")->IsUndefined());
ASSERT_EQ(AdblockPlus::WebRequest::NS_ERROR_FAILURE, jsEngine.Evaluate("request.channel.status")->AsInt());
ASSERT_EQ(0, jsEngine.Evaluate("request.status")->AsInt());
ASSERT_EQ("error", jsEngine.Evaluate("result")->AsString());
ASSERT_TRUE(jsEngine.Evaluate("request.getResponseHeader('Content-Type')")->IsNull());
}
#endif
+
+#endif
« src/JsEngine.cpp ('K') | « test/JsValue.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld