LEFT | RIGHT |
1 #include <sstream> | 1 #include <sstream> |
2 #include <AdblockPlus.h> | 2 #include <AdblockPlus.h> |
3 #include <gtest/gtest.h> | 3 #include <gtest/gtest.h> |
4 | 4 |
5 #include "../src/Thread.h" | 5 #include "../src/Thread.h" |
| 6 |
| 7 #ifdef HAVE_WININET |
| 8 #include "../src/WebRequestWinInet.h" |
| 9 #endif |
6 | 10 |
7 class TestWebRequest : public AdblockPlus::WebRequest | 11 class TestWebRequest : public AdblockPlus::WebRequest |
8 { | 12 { |
9 public: | 13 public: |
10 AdblockPlus::ServerResponse GET(const std::string& url, const AdblockPlus::Hea
derList& requestHeaders) const | 14 AdblockPlus::ServerResponse GET(const std::string& url, const AdblockPlus::Hea
derList& requestHeaders) const |
11 { | 15 { |
12 AdblockPlus::Sleep(50); | 16 AdblockPlus::Sleep(50); |
13 | 17 |
14 AdblockPlus::ServerResponse result; | 18 AdblockPlus::ServerResponse result; |
15 result.status = NS_OK; | 19 result.status = NS_OK; |
(...skipping 20 matching lines...) Expand all Loading... |
36 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET('http://example.com/', nul
l, function(){})")); | 40 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET('http://example.com/', nul
l, function(){})")); |
37 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET('http://example.com/', {},
null)")); | 41 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET('http://example.com/', {},
null)")); |
38 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET('http://example.com/', {},
function(){}, 0)")); | 42 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET('http://example.com/', {},
function(){}, 0)")); |
39 } | 43 } |
40 | 44 |
41 TEST(WebRequestTest, TestWebRequest) | 45 TEST(WebRequestTest, TestWebRequest) |
42 { | 46 { |
43 TestWebRequest webRequest; | 47 TestWebRequest webRequest; |
44 AdblockPlus::JsEngine jsEngine(0, &webRequest, 0); | 48 AdblockPlus::JsEngine jsEngine(0, &webRequest, 0); |
45 jsEngine.Evaluate("_webRequest.GET('http://example.com/', {X: 'Y'}, function(r
esult) {foo = result;} )"); | 49 jsEngine.Evaluate("_webRequest.GET('http://example.com/', {X: 'Y'}, function(r
esult) {foo = result;} )"); |
46 ASSERT_EQ(true, jsEngine.Evaluate("this.foo")->IsUndefined()); | 50 ASSERT_TRUE(jsEngine.Evaluate("this.foo")->IsUndefined()); |
47 AdblockPlus::Sleep(200); | 51 AdblockPlus::Sleep(200); |
48 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine.Evaluate("foo.status")->AsI
nt()); | 52 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine.Evaluate("foo.status")->AsI
nt()); |
49 ASSERT_EQ(123, jsEngine.Evaluate("foo.responseStatus")->AsInt()); | 53 ASSERT_EQ(123, jsEngine.Evaluate("foo.responseStatus")->AsInt()); |
50 ASSERT_EQ("http://example.com/\nX\nY", jsEngine.Evaluate("foo.responseText")->
AsString()); | 54 ASSERT_EQ("http://example.com/\nX\nY", jsEngine.Evaluate("foo.responseText")->
AsString()); |
51 ASSERT_EQ("{\"Foo\":\"Bar\"}", jsEngine.Evaluate("JSON.stringify(foo.responseH
eaders)")->AsString()); | 55 ASSERT_EQ("{\"Foo\":\"Bar\"}", jsEngine.Evaluate("JSON.stringify(foo.responseH
eaders)")->AsString()); |
52 } | 56 } |
53 | 57 |
54 #if defined(HAVE_CURL) | 58 #if defined(HAVE_CURL) |
55 TEST(WebRequestTest, RealWebRequest) | 59 TEST(WebRequestTest, RealWebRequest) |
56 { | 60 { |
57 AdblockPlus::DefaultWebRequest webRequest; | 61 AdblockPlus::DefaultWebRequest webRequest; |
58 AdblockPlus::JsEngine jsEngine(0, &webRequest, 0); | 62 AdblockPlus::JsEngine jsEngine(0, &webRequest, 0); |
59 | 63 |
60 // This URL should redirect to easylist-downloads.adblockplus.org and we | 64 // This URL should redirect to easylist-downloads.adblockplus.org and we |
61 // should get the actual filter list back. | 65 // should get the actual filter list back. |
62 jsEngine.Evaluate("_webRequest.GET('https://easylist.adblockplus.org/easylist.
txt', {}, function(result) {foo = result;} )"); | 66 jsEngine.Evaluate("_webRequest.GET('https://easylist.adblockplus.org/easylist.
txt', {}, function(result) {foo = result;} )"); |
63 do | 67 do |
64 { | 68 { |
65 AdblockPlus::Sleep(200); | 69 AdblockPlus::Sleep(200); |
66 } while (jsEngine.Evaluate("this.foo")->IsUndefined()); | 70 } while (jsEngine.Evaluate("this.foo")->IsUndefined()); |
67 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine.Evaluate("foo.status")->AsI
nt()); | 71 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine.Evaluate("foo.status")->AsI
nt()); |
68 ASSERT_EQ(200, jsEngine.Evaluate("foo.responseStatus")->AsInt()); | 72 ASSERT_EQ(200, jsEngine.Evaluate("foo.responseStatus")->AsInt()); |
69 ASSERT_EQ("[Adblock Plus ", jsEngine.Evaluate("foo.responseText.substr(0, 14)"
)->AsString()); | 73 ASSERT_EQ("[Adblock Plus ", jsEngine.Evaluate("foo.responseText.substr(0, 14)"
)->AsString()); |
70 ASSERT_EQ("text/plain", jsEngine.Evaluate("foo.responseHeaders['content-type']
.substr(0, 10)")->AsString()); | 74 ASSERT_EQ("text/plain", jsEngine.Evaluate("foo.responseHeaders['content-type']
.substr(0, 10)")->AsString()); |
71 ASSERT_EQ(true, jsEngine.Evaluate("foo.responseHeaders['location']")->IsUndefi
ned()); | 75 ASSERT_TRUE(jsEngine.Evaluate("foo.responseHeaders['location']")->IsUndefined(
)); |
| 76 } |
| 77 #elif defined(HAVE_WININET) |
| 78 TEST(WebRequestTest, RealWebRequest) |
| 79 { |
| 80 AdblockPlus::WebRequestWinInet webRequest; |
| 81 AdblockPlus::JsEngine jsEngine(0, &webRequest, 0); |
| 82 jsEngine.Evaluate("_webRequest.GET('https://easylist.adblockplus.org/easylist.
txt', {}, function(result) {foo = result;} )"); |
| 83 do |
| 84 { |
| 85 AdblockPlus::Sleep(200); |
| 86 } while (jsEngine.Evaluate("typeof foo") == "undefined"); |
| 87 ASSERT_EQ(ToString(AdblockPlus::WebRequest::NS_OK), jsEngine.Evaluate("foo.sta
tus")); |
| 88 ASSERT_EQ("200", jsEngine.Evaluate("foo.responseStatus")); |
| 89 ASSERT_EQ("[Adblock Plus ", jsEngine.Evaluate("foo.responseText.substr(0, 14)"
)); |
| 90 //TODO: Shall we be strict with letter casing here? Content-Type vs content-ty
pe |
| 91 ASSERT_EQ("text/plain", jsEngine.Evaluate("foo.responseHeaders['Content-Type']
.substr(0, 10)")); |
| 92 ASSERT_EQ("undefined", jsEngine.Evaluate("typeof foo.responseHeaders['location
']")); |
72 } | 93 } |
73 #else | 94 #else |
74 TEST(WebRequestTest, DummyWebRequest) | 95 TEST(WebRequestTest, DummyWebRequest) |
75 { | 96 { |
76 AdblockPlus::DefaultWebRequest webRequest; | 97 AdblockPlus::DefaultWebRequest webRequest; |
77 AdblockPlus::JsEngine jsEngine(0, &webRequest, 0); | 98 AdblockPlus::JsEngine jsEngine(0, &webRequest, 0); |
78 jsEngine.Evaluate("_webRequest.GET('https://easylist.adblockplus.org/easylist.
txt', {}, function(result) {foo = result;} )"); | 99 jsEngine.Evaluate("_webRequest.GET('https://easylist.adblockplus.org/easylist.
txt', {}, function(result) {foo = result;} )"); |
79 do | 100 do |
80 { | 101 { |
81 AdblockPlus::Sleep(200); | 102 AdblockPlus::Sleep(200); |
82 } while (jsEngine.Evaluate("this.foo")->IsUndefined()); | 103 } while (jsEngine.Evaluate("this.foo")->IsUndefined()); |
83 ASSERT_EQ(AdblockPlus::WebRequest::NS_ERROR_FAILURE, jsEngine.Evaluate("foo.st
atus")->AsInt()); | 104 ASSERT_EQ(AdblockPlus::WebRequest::NS_ERROR_FAILURE, jsEngine.Evaluate("foo.st
atus")->AsInt()); |
84 ASSERT_EQ(0, jsEngine.Evaluate("foo.responseStatus")->AsInt()); | 105 ASSERT_EQ(0, jsEngine.Evaluate("foo.responseStatus")->AsInt()); |
85 ASSERT_EQ("", jsEngine.Evaluate("foo.responseText")->AsString()); | 106 ASSERT_EQ("", jsEngine.Evaluate("foo.responseText")->AsString()); |
86 ASSERT_EQ("{}", jsEngine.Evaluate("JSON.stringify(foo.responseHeaders)")->AsSt
ring()); | 107 ASSERT_EQ("{}", jsEngine.Evaluate("JSON.stringify(foo.responseHeaders)")->AsSt
ring()); |
87 } | 108 } |
88 #endif | 109 #endif |
LEFT | RIGHT |