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

Side by Side Diff: test/WebRequest.cpp

Issue 29417624: Issue 5034 - Part 4: JsEngine::Evaluate() return a JsValue (Closed) Base URL: https://hg.adblockplus.org/libadblockplus/
Patch Set: Rebased on top of part 3 Created April 20, 2017, 12:41 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « test/UpdateCheck.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2017 eyeo GmbH 3 * Copyright (C) 2006-2017 eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 request.addEventListener('error', function() {result = 'error';}, false);\ 115 request.addEventListener('error', function() {result = 'error';}, false);\
116 "); 116 ");
117 return url; 117 return url;
118 } 118 }
119 119
120 void WaitForVariable(const std::string& variable, const AdblockPlus::JsEngineP tr& jsEngine) 120 void WaitForVariable(const std::string& variable, const AdblockPlus::JsEngineP tr& jsEngine)
121 { 121 {
122 do 122 do
123 { 123 {
124 AdblockPlus::Sleep(60); 124 AdblockPlus::Sleep(60);
125 } while (jsEngine->Evaluate(variable)->IsUndefined()); 125 } while (jsEngine->Evaluate(variable).IsUndefined());
126 } 126 }
127 127
128 } 128 }
129 129
130 TEST_F(MockWebRequestTest, BadCall) 130 TEST_F(MockWebRequestTest, BadCall)
131 { 131 {
132 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET()")); 132 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET()"));
133 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('', {}, function(){})")); 133 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('', {}, function(){})"));
134 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET({toString: false}, {}, fu nction(){})")); 134 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET({toString: false}, {}, fu nction(){})"));
135 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', nu ll, function(){})")); 135 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', nu ll, function(){})"));
136 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', {} , null)")); 136 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', {} , null)"));
137 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', {} , function(){}, 0)")); 137 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', {} , function(){}, 0)"));
138 } 138 }
139 139
140 TEST_F(MockWebRequestTest, SuccessfulRequest) 140 TEST_F(MockWebRequestTest, SuccessfulRequest)
141 { 141 {
142 jsEngine->Evaluate("_webRequest.GET('http://example.com/', {X: 'Y'}, function( result) {foo = result;} )"); 142 jsEngine->Evaluate("_webRequest.GET('http://example.com/', {X: 'Y'}, function( result) {foo = result;} )");
143 ASSERT_TRUE(jsEngine->Evaluate("this.foo")->IsUndefined()); 143 ASSERT_TRUE(jsEngine->Evaluate("this.foo").IsUndefined());
144 AdblockPlus::Sleep(200); 144 AdblockPlus::Sleep(200);
145 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("foo.status")->As Int()); 145 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("foo.status").AsI nt());
146 ASSERT_EQ(123, jsEngine->Evaluate("foo.responseStatus")->AsInt()); 146 ASSERT_EQ(123, jsEngine->Evaluate("foo.responseStatus").AsInt());
147 ASSERT_EQ("http://example.com/\nX\nY", jsEngine->Evaluate("foo.responseText")- >AsString()); 147 ASSERT_EQ("http://example.com/\nX\nY", jsEngine->Evaluate("foo.responseText"). AsString());
148 ASSERT_EQ("{\"Foo\":\"Bar\"}", jsEngine->Evaluate("JSON.stringify(foo.response Headers)")->AsString()); 148 ASSERT_EQ("{\"Foo\":\"Bar\"}", jsEngine->Evaluate("JSON.stringify(foo.response Headers)").AsString());
149 } 149 }
150 150
151 #if defined(HAVE_CURL) || defined(_WIN32) 151 #if defined(HAVE_CURL) || defined(_WIN32)
152 TEST_F(DefaultWebRequestTest, RealWebRequest) 152 TEST_F(DefaultWebRequestTest, RealWebRequest)
153 { 153 {
154 // This URL should redirect to easylist-downloads.adblockplus.org and we 154 // This URL should redirect to easylist-downloads.adblockplus.org and we
155 // should get the actual filter list back. 155 // should get the actual filter list back.
156 jsEngine->Evaluate("_webRequest.GET('https://easylist-downloads.adblockplus.or g/easylist.txt', {}, function(result) {foo = result;} )"); 156 jsEngine->Evaluate("_webRequest.GET('https://easylist-downloads.adblockplus.or g/easylist.txt', {}, function(result) {foo = result;} )");
157 WaitForVariable("this.foo", jsEngine); 157 WaitForVariable("this.foo", jsEngine);
158 ASSERT_EQ("text/plain", jsEngine->Evaluate("foo.responseHeaders['content-type' ].substr(0, 10)")->AsString()); 158 ASSERT_EQ("text/plain", jsEngine->Evaluate("foo.responseHeaders['content-type' ].substr(0, 10)").AsString());
159 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("foo.status")->As Int()); 159 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("foo.status").AsI nt());
160 ASSERT_EQ(200, jsEngine->Evaluate("foo.responseStatus")->AsInt()); 160 ASSERT_EQ(200, jsEngine->Evaluate("foo.responseStatus").AsInt());
161 ASSERT_EQ("[Adblock Plus ", jsEngine->Evaluate("foo.responseText.substr(0, 14) ")->AsString()); 161 ASSERT_EQ("[Adblock Plus ", jsEngine->Evaluate("foo.responseText.substr(0, 14) ").AsString());
162 ASSERT_EQ("text/plain", jsEngine->Evaluate("foo.responseHeaders['content-type' ].substr(0, 10)")->AsString()); 162 ASSERT_EQ("text/plain", jsEngine->Evaluate("foo.responseHeaders['content-type' ].substr(0, 10)").AsString());
163 #if defined(HAVE_CURL) 163 #if defined(HAVE_CURL)
164 ASSERT_EQ("gzip", jsEngine->Evaluate("foo.responseHeaders['content-encoding']. substr(0, 4)")->AsString()); 164 ASSERT_EQ("gzip", jsEngine->Evaluate("foo.responseHeaders['content-encoding']. substr(0, 4)").AsString());
165 #endif 165 #endif
166 ASSERT_TRUE(jsEngine->Evaluate("foo.responseHeaders['location']")->IsUndefined ()); 166 ASSERT_TRUE(jsEngine->Evaluate("foo.responseHeaders['location']").IsUndefined( ));
167 } 167 }
168 168
169 TEST_F(DefaultWebRequestTest, XMLHttpRequest) 169 TEST_F(DefaultWebRequestTest, XMLHttpRequest)
170 { 170 {
171 auto filterEngine = AdblockPlus::FilterEngine::Create(jsEngine); 171 auto filterEngine = AdblockPlus::FilterEngine::Create(jsEngine);
172 172
173 ResetTestXHR(jsEngine, "https://easylist-downloads.adblockplus.org/easylist.tx t"); 173 ResetTestXHR(jsEngine, "https://easylist-downloads.adblockplus.org/easylist.tx t");
174 jsEngine->Evaluate("\ 174 jsEngine->Evaluate("\
175 request.setRequestHeader('X', 'Y');\ 175 request.setRequestHeader('X', 'Y');\
176 request.setRequestHeader('X2', 'Y2');\ 176 request.setRequestHeader('X2', 'Y2');\
177 request.send(null);"); 177 request.send(null);");
178 WaitForVariable("result", jsEngine); 178 WaitForVariable("result", jsEngine);
179 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("request.channel. status")->AsInt()); 179 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("request.channel. status").AsInt());
180 ASSERT_EQ(200, jsEngine->Evaluate("request.status")->AsInt()); 180 ASSERT_EQ(200, jsEngine->Evaluate("request.status").AsInt());
181 ASSERT_EQ("[Adblock Plus ", jsEngine->Evaluate("result.substr(0, 14)")->AsStri ng()); 181 ASSERT_EQ("[Adblock Plus ", jsEngine->Evaluate("result.substr(0, 14)").AsStrin g());
182 ASSERT_EQ("text/plain", jsEngine->Evaluate("request.getResponseHeader('Content -Type').substr(0, 10)")->AsString()); 182 ASSERT_EQ("text/plain", jsEngine->Evaluate("request.getResponseHeader('Content -Type').substr(0, 10)").AsString());
183 #if defined(HAVE_CURL) 183 #if defined(HAVE_CURL)
184 ASSERT_EQ("gzip", jsEngine->Evaluate("request.getResponseHeader('Content-Encod ing').substr(0, 4)")->AsString()); 184 ASSERT_EQ("gzip", jsEngine->Evaluate("request.getResponseHeader('Content-Encod ing').substr(0, 4)").AsString());
185 #endif 185 #endif
186 ASSERT_TRUE(jsEngine->Evaluate("request.getResponseHeader('Location')")->IsNul l()); 186 ASSERT_TRUE(jsEngine->Evaluate("request.getResponseHeader('Location')").IsNull ());
187 } 187 }
188 #else 188 #else
189 TEST_F(DefaultWebRequestTest, DummyWebRequest) 189 TEST_F(DefaultWebRequestTest, DummyWebRequest)
190 { 190 {
191 jsEngine->Evaluate("_webRequest.GET('https://easylist-downloads.adblockplus.or g/easylist.txt', {}, function(result) {foo = result;} )"); 191 jsEngine->Evaluate("_webRequest.GET('https://easylist-downloads.adblockplus.or g/easylist.txt', {}, function(result) {foo = result;} )");
192 WaitForVariable("this.foo", jsEngine); 192 WaitForVariable("this.foo", jsEngine);
193 ASSERT_EQ(AdblockPlus::WebRequest::NS_ERROR_FAILURE, jsEngine->Evaluate("foo.s tatus")->AsInt()); 193 ASSERT_EQ(AdblockPlus::WebRequest::NS_ERROR_FAILURE, jsEngine->Evaluate("foo.s tatus").AsInt());
194 ASSERT_EQ(0, jsEngine->Evaluate("foo.responseStatus")->AsInt()); 194 ASSERT_EQ(0, jsEngine->Evaluate("foo.responseStatus").AsInt());
195 ASSERT_EQ("", jsEngine->Evaluate("foo.responseText")->AsString()); 195 ASSERT_EQ("", jsEngine->Evaluate("foo.responseText").AsString());
196 ASSERT_EQ("{}", jsEngine->Evaluate("JSON.stringify(foo.responseHeaders)")->AsS tring()); 196 ASSERT_EQ("{}", jsEngine->Evaluate("JSON.stringify(foo.responseHeaders)").AsSt ring());
197 } 197 }
198 198
199 TEST_F(DefaultWebRequestTest, XMLHttpRequest) 199 TEST_F(DefaultWebRequestTest, XMLHttpRequest)
200 { 200 {
201 auto filterEngine = AdblockPlus::FilterEngine::Create(jsEngine); 201 auto filterEngine = AdblockPlus::FilterEngine::Create(jsEngine);
202 202
203 ResetTestXHR(jsEngine); 203 ResetTestXHR(jsEngine);
204 jsEngine->Evaluate("\ 204 jsEngine->Evaluate("\
205 request.setRequestHeader('X', 'Y');\ 205 request.setRequestHeader('X', 'Y');\
206 request.send(null);"); 206 request.send(null);");
207 WaitForVariable("result", jsEngine); 207 WaitForVariable("result", jsEngine);
208 ASSERT_EQ(AdblockPlus::WebRequest::NS_ERROR_FAILURE, jsEngine->Evaluate("reque st.channel.status")->AsInt()); 208 ASSERT_EQ(AdblockPlus::WebRequest::NS_ERROR_FAILURE, jsEngine->Evaluate("reque st.channel.status").AsInt());
209 ASSERT_EQ(0, jsEngine->Evaluate("request.status")->AsInt()); 209 ASSERT_EQ(0, jsEngine->Evaluate("request.status").AsInt());
210 ASSERT_EQ("error", jsEngine->Evaluate("result")->AsString()); 210 ASSERT_EQ("error", jsEngine->Evaluate("result").AsString());
211 ASSERT_TRUE(jsEngine->Evaluate("request.getResponseHeader('Content-Type')")->I sNull()); 211 ASSERT_TRUE(jsEngine->Evaluate("request.getResponseHeader('Content-Type')").Is Null());
212 } 212 }
213 213
214 #endif 214 #endif
215 215
216 namespace 216 namespace
217 { 217 {
218 class CatchLogSystem : public AdblockPlus::LogSystem 218 class CatchLogSystem : public AdblockPlus::LogSystem
219 { 219 {
220 public: 220 public:
221 AdblockPlus::LogSystem::LogLevel lastLogLevel; 221 AdblockPlus::LogSystem::LogLevel lastLogLevel;
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
340 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_TRACE, catchLogSystem->lastLogLeve l); 340 EXPECT_EQ(AdblockPlus::LogSystem::LOG_LEVEL_TRACE, catchLogSystem->lastLogLeve l);
341 EXPECT_EQ("", catchLogSystem->lastMessage); 341 EXPECT_EQ("", catchLogSystem->lastMessage);
342 WaitForVariable("result", jsEngine); 342 WaitForVariable("result", jsEngine);
343 { 343 {
344 auto headersRequest = webRequest->headersForRequest(url); 344 auto headersRequest = webRequest->headersForRequest(url);
345 EXPECT_TRUE(headersRequest.first); 345 EXPECT_TRUE(headersRequest.first);
346 const auto& headers = headersRequest.second; 346 const auto& headers = headersRequest.second;
347 EXPECT_FALSE(headers.cend() == headers.find("Security")); 347 EXPECT_FALSE(headers.cend() == headers.find("Security"));
348 } 348 }
349 } 349 }
OLDNEW
« no previous file with comments | « test/UpdateCheck.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld