OLD | NEW |
| 1 #include <sstream> |
1 #include <AdblockPlus.h> | 2 #include <AdblockPlus.h> |
2 #include <gtest/gtest.h> | 3 #include <gtest/gtest.h> |
3 | 4 |
4 #include "../src/Thread.h" | 5 #include "../src/Thread.h" |
5 | 6 |
6 class TestWebRequest : public AdblockPlus::WebRequest | 7 class TestWebRequest : public AdblockPlus::WebRequest |
7 { | 8 { |
8 public: | 9 public: |
9 AdblockPlus::ServerResponse GET(const std::string& url, const AdblockPlus::Hea
dersList& requestHeaders) const | 10 AdblockPlus::ServerResponse GET(const std::string& url, const AdblockPlus::Hea
dersList& requestHeaders) const |
10 { | 11 { |
11 AdblockPlus::Sleep(50); | 12 AdblockPlus::Sleep(50); |
12 | 13 |
13 AdblockPlus::ServerResponse result; | 14 AdblockPlus::ServerResponse result; |
| 15 result.status = NS_OK; |
14 result.responseStatus = 123; | 16 result.responseStatus = 123; |
15 result.responseHeaders.push_back(std::pair<std::string, std::string>("Foo",
"Bar")); | 17 result.responseHeaders.push_back(std::pair<std::string, std::string>("Foo",
"Bar")); |
16 result.responseText = url + "\n" + requestHeaders[0].first + "\n" + requestH
eaders[0].second; | 18 result.responseText = url + "\n" + requestHeaders[0].first + "\n" + requestH
eaders[0].second; |
17 return result; | 19 return result; |
18 } | 20 } |
19 }; | 21 }; |
20 | 22 |
| 23 std::string ToString(unsigned int i) |
| 24 { |
| 25 std::stringstream stream; |
| 26 stream << i; |
| 27 return stream.str(); |
| 28 } |
| 29 |
21 TEST(WebRequestTest, BadCall) | 30 TEST(WebRequestTest, BadCall) |
22 { | 31 { |
23 AdblockPlus::JsEngine jsEngine(0, 0, 0); | 32 AdblockPlus::JsEngine jsEngine(0, 0, 0); |
24 ASSERT_ANY_THROW(jsEngine.Evaluate("_request.GET()")); | 33 ASSERT_ANY_THROW(jsEngine.Evaluate("_request.GET()")); |
25 ASSERT_ANY_THROW(jsEngine.Evaluate("_request.GET('', {}, function(){})")); | 34 ASSERT_ANY_THROW(jsEngine.Evaluate("_request.GET('', {}, function(){})")); |
26 ASSERT_ANY_THROW(jsEngine.Evaluate("_request.GET({toString: false}, {}, functi
on(){})")); | 35 ASSERT_ANY_THROW(jsEngine.Evaluate("_request.GET({toString: false}, {}, functi
on(){})")); |
27 ASSERT_ANY_THROW(jsEngine.Evaluate("_request.GET('http://example.com/', null,
function(){})")); | 36 ASSERT_ANY_THROW(jsEngine.Evaluate("_request.GET('http://example.com/', null,
function(){})")); |
28 ASSERT_ANY_THROW(jsEngine.Evaluate("_request.GET('http://example.com/', {}, nu
ll)")); | 37 ASSERT_ANY_THROW(jsEngine.Evaluate("_request.GET('http://example.com/', {}, nu
ll)")); |
29 ASSERT_ANY_THROW(jsEngine.Evaluate("_request.GET('http://example.com/', {}, fu
nction(){}, 0)")); | 38 ASSERT_ANY_THROW(jsEngine.Evaluate("_request.GET('http://example.com/', {}, fu
nction(){}, 0)")); |
30 } | 39 } |
31 | 40 |
32 TEST(WebRequestTest, UrlLoad) | 41 TEST(WebRequestTest, TestWebRequest) |
33 { | 42 { |
34 TestWebRequest webRequest; | 43 TestWebRequest webRequest; |
35 AdblockPlus::JsEngine jsEngine(0, &webRequest, 0); | 44 AdblockPlus::JsEngine jsEngine(0, &webRequest, 0); |
36 jsEngine.Evaluate("_request.GET('http://example.com/', {X: 'Y'}, function(resu
lt) {foo = result;} )"); | 45 jsEngine.Evaluate("_request.GET('http://example.com/', {X: 'Y'}, function(resu
lt) {foo = result;} )"); |
37 ASSERT_EQ("undefined", jsEngine.Evaluate("typeof foo")); | 46 ASSERT_EQ("undefined", jsEngine.Evaluate("typeof foo")); |
38 AdblockPlus::Sleep(200); | 47 AdblockPlus::Sleep(200); |
| 48 ASSERT_EQ(ToString(NS_OK), jsEngine.Evaluate("foo.status")); |
39 ASSERT_EQ("123", jsEngine.Evaluate("foo.responseStatus")); | 49 ASSERT_EQ("123", jsEngine.Evaluate("foo.responseStatus")); |
40 ASSERT_EQ("http://example.com/\nX\nY", jsEngine.Evaluate("foo.responseText")); | 50 ASSERT_EQ("http://example.com/\nX\nY", jsEngine.Evaluate("foo.responseText")); |
41 ASSERT_EQ("{\"Foo\":\"Bar\"}", jsEngine.Evaluate("JSON.stringify(foo.responseH
eaders)")); | 51 ASSERT_EQ("{\"Foo\":\"Bar\"}", jsEngine.Evaluate("JSON.stringify(foo.responseH
eaders)")); |
42 } | 52 } |
| 53 |
| 54 #if defined(HAVE_CURL) |
| 55 TEST(WebRequestTest, RealWebRequest) |
| 56 { |
| 57 AdblockPlus::DefaultWebRequest webRequest; |
| 58 AdblockPlus::JsEngine jsEngine(0, &webRequest, 0); |
| 59 |
| 60 // This URL should redirect to easylist-downloads.adblockplus.org and we |
| 61 // should get the actual filter list back. |
| 62 jsEngine.Evaluate("_request.GET('https://easylist.adblockplus.org/easylist.txt
', {}, function(result) {foo = result;} )"); |
| 63 do |
| 64 { |
| 65 AdblockPlus::Sleep(200); |
| 66 } while (jsEngine.Evaluate("typeof foo") == "undefined"); |
| 67 ASSERT_EQ(ToString(NS_OK), jsEngine.Evaluate("foo.status")); |
| 68 ASSERT_EQ("200", jsEngine.Evaluate("foo.responseStatus")); |
| 69 ASSERT_EQ("[Adblock Plus ", jsEngine.Evaluate("foo.responseText.substr(0, 14)"
)); |
| 70 ASSERT_EQ("text/plain", jsEngine.Evaluate("foo.responseHeaders['content-type']
.substr(0, 10)")); |
| 71 ASSERT_EQ("undefined", jsEngine.Evaluate("typeof foo.responseHeaders['location
']")); |
| 72 } |
| 73 #else |
| 74 TEST(WebRequestTest, DummyWebRequest) |
| 75 { |
| 76 AdblockPlus::DefaultWebRequest webRequest; |
| 77 AdblockPlus::JsEngine jsEngine(0, &webRequest, 0); |
| 78 jsEngine.Evaluate("_request.GET('https://easylist.adblockplus.org/easylist.txt
', {}, function(result) {foo = result;} )"); |
| 79 do |
| 80 { |
| 81 AdblockPlus::Sleep(200); |
| 82 } while (jsEngine.Evaluate("typeof foo") == "undefined"); |
| 83 ASSERT_EQ(ToString(NS_ERROR_FAILURE), jsEngine.Evaluate("foo.status")); |
| 84 ASSERT_EQ("0", jsEngine.Evaluate("foo.responseStatus")); |
| 85 ASSERT_EQ("", jsEngine.Evaluate("foo.responseText")); |
| 86 ASSERT_EQ("{}", jsEngine.Evaluate("JSON.stringify(foo.responseHeaders)")); |
| 87 } |
| 88 #endif |
OLD | NEW |