OLD | NEW |
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 | 6 |
7 #ifdef HAVE_WININET | 7 #ifdef HAVE_WININET |
8 #include "../src/WebRequestWinInet.h" | 8 #include "../src/WebRequestWinInet.h" |
9 #endif | 9 #endif |
10 | 10 |
(...skipping 15 matching lines...) Expand all Loading... |
26 | 26 |
27 std::string ToString(unsigned int i) | 27 std::string ToString(unsigned int i) |
28 { | 28 { |
29 std::stringstream stream; | 29 std::stringstream stream; |
30 stream << i; | 30 stream << i; |
31 return stream.str(); | 31 return stream.str(); |
32 } | 32 } |
33 | 33 |
34 TEST(WebRequestTest, BadCall) | 34 TEST(WebRequestTest, BadCall) |
35 { | 35 { |
36 AdblockPlus::JsEngine jsEngine(0, 0, 0); | 36 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, 0, 0); |
37 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET()")); | 37 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET()")); |
38 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET('', {}, function(){})")); | 38 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET('', {}, function(){})")); |
39 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET({toString: false}, {}, fun
ction(){})")); | 39 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET({toString: false}, {}, fun
ction(){})")); |
40 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(){})")); |
41 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET('http://example.com/', {},
null)")); | 41 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET('http://example.com/', {},
null)")); |
42 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)")); |
43 } | 43 } |
44 | 44 |
45 TEST(WebRequestTest, TestWebRequest) | 45 TEST(WebRequestTest, TestWebRequest) |
46 { | 46 { |
47 TestWebRequest webRequest; | 47 TestWebRequest webRequest; |
48 AdblockPlus::JsEngine jsEngine(0, &webRequest, 0); | 48 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, &webRequest, 0); |
49 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;} )"); |
50 ASSERT_TRUE(jsEngine.Evaluate("this.foo")->IsUndefined()); | 50 ASSERT_TRUE(jsEngine.Evaluate("this.foo")->IsUndefined()); |
51 AdblockPlus::Sleep(200); | 51 AdblockPlus::Sleep(200); |
52 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()); |
53 ASSERT_EQ(123, jsEngine.Evaluate("foo.responseStatus")->AsInt()); | 53 ASSERT_EQ(123, jsEngine.Evaluate("foo.responseStatus")->AsInt()); |
54 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()); |
55 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()); |
56 } | 56 } |
57 | 57 |
58 #if defined(HAVE_CURL) | 58 #if defined(HAVE_CURL) |
59 TEST(WebRequestTest, RealWebRequest) | 59 TEST(WebRequestTest, RealWebRequest) |
60 { | 60 { |
61 AdblockPlus::DefaultWebRequest webRequest; | 61 AdblockPlus::DefaultWebRequest webRequest; |
62 AdblockPlus::JsEngine jsEngine(0, &webRequest, 0); | 62 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, &webRequest, 0); |
63 | 63 |
64 // This URL should redirect to easylist-downloads.adblockplus.org and we | 64 // This URL should redirect to easylist-downloads.adblockplus.org and we |
65 // should get the actual filter list back. | 65 // should get the actual filter list back. |
66 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;} )"); |
67 do | 67 do |
68 { | 68 { |
69 AdblockPlus::Sleep(200); | 69 AdblockPlus::Sleep(200); |
70 } while (jsEngine.Evaluate("this.foo")->IsUndefined()); | 70 } while (jsEngine.Evaluate("this.foo")->IsUndefined()); |
71 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()); |
72 ASSERT_EQ(200, jsEngine.Evaluate("foo.responseStatus")->AsInt()); | 72 ASSERT_EQ(200, jsEngine.Evaluate("foo.responseStatus")->AsInt()); |
73 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()); |
74 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()); |
75 ASSERT_TRUE(jsEngine.Evaluate("foo.responseHeaders['location']")->IsUndefined(
)); | 75 ASSERT_TRUE(jsEngine.Evaluate("foo.responseHeaders['location']")->IsUndefined(
)); |
76 } | 76 } |
77 #elif defined(HAVE_WININET) | 77 #elif defined(HAVE_WININET) |
78 TEST(WebRequestTest, RealWebRequest) | 78 TEST(WebRequestTest, RealWebRequest) |
79 { | 79 { |
80 AdblockPlus::WebRequestWinInet webRequest; | 80 AdblockPlus::WebRequestWinInet webRequest; |
81 AdblockPlus::JsEngine jsEngine(0, &webRequest, 0); | 81 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, &webRequest, 0); |
82 jsEngine.Evaluate("_webRequest.GET('https://easylist.adblockplus.org/easylist.
txt', {}, function(result) {foo = result;} )"); | 82 jsEngine.Evaluate("_webRequest.GET('https://easylist.adblockplus.org/easylist.
txt', {}, function(result) {foo = result;} )"); |
83 do | 83 do |
84 { | 84 { |
85 AdblockPlus::Sleep(200); | 85 AdblockPlus::Sleep(200); |
86 } while (jsEngine.Evaluate("typeof foo") == "undefined"); | 86 } while (jsEngine.Evaluate("typeof foo") == "undefined"); |
87 ASSERT_EQ(ToString(AdblockPlus::WebRequest::NS_OK), jsEngine.Evaluate("foo.sta
tus")); | 87 ASSERT_EQ(ToString(AdblockPlus::WebRequest::NS_OK), jsEngine.Evaluate("foo.sta
tus")); |
88 ASSERT_EQ("200", jsEngine.Evaluate("foo.responseStatus")); | 88 ASSERT_EQ("200", jsEngine.Evaluate("foo.responseStatus")); |
89 ASSERT_EQ("[Adblock Plus ", jsEngine.Evaluate("foo.responseText.substr(0, 14)"
)); | 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 | 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)")); | 91 ASSERT_EQ("text/plain", jsEngine.Evaluate("foo.responseHeaders['Content-Type']
.substr(0, 10)")); |
92 ASSERT_EQ("undefined", jsEngine.Evaluate("typeof foo.responseHeaders['location
']")); | 92 ASSERT_EQ("undefined", jsEngine.Evaluate("typeof foo.responseHeaders['location
']")); |
93 } | 93 } |
94 | 94 |
95 TEST(WebRequestTest, XMLHttpRequest) | 95 TEST(WebRequestTest, XMLHttpRequest) |
96 { | 96 { |
97 AdblockPlus::DefaultWebRequest webRequest; | 97 AdblockPlus::DefaultWebRequest webRequest; |
98 AdblockPlus::JsEngine jsEngine(0, &webRequest, 0); | 98 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, &webRequest, 0); |
99 AdblockPlus::FilterEngine filterEngine(jsEngine); | 99 AdblockPlus::FilterEngine filterEngine(jsEngine); |
100 | 100 |
101 jsEngine.Evaluate("\ | 101 jsEngine.Evaluate("\ |
102 var result;\ | 102 var result;\ |
103 var request = new XMLHttpRequest();\ | 103 var request = new XMLHttpRequest();\ |
104 request.open('GET', 'https://easylist.adblockplus.org/easylist.txt');\ | 104 request.open('GET', 'https://easylist.adblockplus.org/easylist.txt');\ |
105 request.setRequestHeader('X', 'Y');\ | 105 request.setRequestHeader('X', 'Y');\ |
106 request.overrideMimeType('text/plain');\ | 106 request.overrideMimeType('text/plain');\ |
107 request.addEventListener('load', function() {result = request.responseText;}
, false);\ | 107 request.addEventListener('load', function() {result = request.responseText;}
, false);\ |
108 request.addEventListener('error', function() {result = 'error';}, false);\ | 108 request.addEventListener('error', function() {result = 'error';}, false);\ |
109 request.send(null);"); | 109 request.send(null);"); |
110 do | 110 do |
111 { | 111 { |
112 AdblockPlus::Sleep(200); | 112 AdblockPlus::Sleep(200); |
113 } while (jsEngine.Evaluate("result")->IsUndefined()); | 113 } while (jsEngine.Evaluate("result")->IsUndefined()); |
114 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine.Evaluate("request.channel.s
tatus")->AsInt()); | 114 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine.Evaluate("request.channel.s
tatus")->AsInt()); |
115 ASSERT_EQ(200, jsEngine.Evaluate("request.status")->AsInt()); | 115 ASSERT_EQ(200, jsEngine.Evaluate("request.status")->AsInt()); |
116 ASSERT_EQ("[Adblock Plus ", jsEngine.Evaluate("result.substr(0, 14)")->AsStrin
g()); | 116 ASSERT_EQ("[Adblock Plus ", jsEngine.Evaluate("result.substr(0, 14)")->AsStrin
g()); |
117 ASSERT_EQ("text/plain", jsEngine.Evaluate("request.getResponseHeader('Content-
Type').substr(0, 10)")->AsString()); | 117 ASSERT_EQ("text/plain", jsEngine.Evaluate("request.getResponseHeader('Content-
Type').substr(0, 10)")->AsString()); |
118 ASSERT_TRUE(jsEngine.Evaluate("request.getResponseHeader('Location')")->IsNull
()); | 118 ASSERT_TRUE(jsEngine.Evaluate("request.getResponseHeader('Location')")->IsNull
()); |
119 } | 119 } |
120 #else | 120 #else |
121 TEST(WebRequestTest, DummyWebRequest) | 121 TEST(WebRequestTest, DummyWebRequest) |
122 { | 122 { |
123 AdblockPlus::DefaultWebRequest webRequest; | 123 AdblockPlus::DefaultWebRequest webRequest; |
124 AdblockPlus::JsEngine jsEngine(0, &webRequest, 0); | 124 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, &webRequest, 0); |
125 jsEngine.Evaluate("_webRequest.GET('https://easylist.adblockplus.org/easylist.
txt', {}, function(result) {foo = result;} )"); | 125 jsEngine.Evaluate("_webRequest.GET('https://easylist.adblockplus.org/easylist.
txt', {}, function(result) {foo = result;} )"); |
126 do | 126 do |
127 { | 127 { |
128 AdblockPlus::Sleep(200); | 128 AdblockPlus::Sleep(200); |
129 } while (jsEngine.Evaluate("this.foo")->IsUndefined()); | 129 } while (jsEngine.Evaluate("this.foo")->IsUndefined()); |
130 ASSERT_EQ(AdblockPlus::WebRequest::NS_ERROR_FAILURE, jsEngine.Evaluate("foo.st
atus")->AsInt()); | 130 ASSERT_EQ(AdblockPlus::WebRequest::NS_ERROR_FAILURE, jsEngine.Evaluate("foo.st
atus")->AsInt()); |
131 ASSERT_EQ(0, jsEngine.Evaluate("foo.responseStatus")->AsInt()); | 131 ASSERT_EQ(0, jsEngine.Evaluate("foo.responseStatus")->AsInt()); |
132 ASSERT_EQ("", jsEngine.Evaluate("foo.responseText")->AsString()); | 132 ASSERT_EQ("", jsEngine.Evaluate("foo.responseText")->AsString()); |
133 ASSERT_EQ("{}", jsEngine.Evaluate("JSON.stringify(foo.responseHeaders)")->AsSt
ring()); | 133 ASSERT_EQ("{}", jsEngine.Evaluate("JSON.stringify(foo.responseHeaders)")->AsSt
ring()); |
134 } | 134 } |
135 | 135 |
136 TEST(WebRequestTest, XMLHttpRequest) | 136 TEST(WebRequestTest, XMLHttpRequest) |
137 { | 137 { |
138 AdblockPlus::DefaultWebRequest webRequest; | 138 AdblockPlus::DefaultWebRequest webRequest; |
139 AdblockPlus::JsEngine jsEngine(0, &webRequest, 0); | 139 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, &webRequest, 0); |
140 AdblockPlus::FilterEngine filterEngine(jsEngine); | 140 AdblockPlus::FilterEngine filterEngine(jsEngine); |
141 | 141 |
142 jsEngine.Evaluate("\ | 142 jsEngine.Evaluate("\ |
143 var result;\ | 143 var result;\ |
144 var request = new XMLHttpRequest();\ | 144 var request = new XMLHttpRequest();\ |
145 request.open('GET', 'https://easylist.adblockplus.org/easylist.txt');\ | 145 request.open('GET', 'https://easylist.adblockplus.org/easylist.txt');\ |
146 request.setRequestHeader('X', 'Y');\ | 146 request.setRequestHeader('X', 'Y');\ |
147 request.overrideMimeType('text/plain');\ | 147 request.overrideMimeType('text/plain');\ |
148 request.addEventListener('load', function() {result = request.responseText;}
, false);\ | 148 request.addEventListener('load', function() {result = request.responseText;}
, false);\ |
149 request.addEventListener('error', function() {result = 'error';}, false);\ | 149 request.addEventListener('error', function() {result = 'error';}, false);\ |
150 request.send(null);"); | 150 request.send(null);"); |
151 do | 151 do |
152 { | 152 { |
153 AdblockPlus::Sleep(200); | 153 AdblockPlus::Sleep(200); |
154 } while (jsEngine.Evaluate("result")->IsUndefined()); | 154 } while (jsEngine.Evaluate("result")->IsUndefined()); |
155 ASSERT_EQ(AdblockPlus::WebRequest::NS_ERROR_FAILURE, jsEngine.Evaluate("reques
t.channel.status")->AsInt()); | 155 ASSERT_EQ(AdblockPlus::WebRequest::NS_ERROR_FAILURE, jsEngine.Evaluate("reques
t.channel.status")->AsInt()); |
156 ASSERT_EQ(0, jsEngine.Evaluate("request.status")->AsInt()); | 156 ASSERT_EQ(0, jsEngine.Evaluate("request.status")->AsInt()); |
157 ASSERT_EQ("error", jsEngine.Evaluate("result")->AsString()); | 157 ASSERT_EQ("error", jsEngine.Evaluate("result")->AsString()); |
158 ASSERT_TRUE(jsEngine.Evaluate("request.getResponseHeader('Content-Type')")->Is
Null()); | 158 ASSERT_TRUE(jsEngine.Evaluate("request.getResponseHeader('Content-Type')")->Is
Null()); |
159 } | 159 } |
160 #endif | 160 #endif |
OLD | NEW |