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

Side by Side Diff: test/WebRequest.cpp

Issue 29428650: Issue 5180 - introduce asynchronous web request (Closed) Base URL: https://github.com/adblockplus/libadblockplus.git
Patch Set: Created May 3, 2017, 2:21 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 26 matching lines...) Expand all
37 } 37 }
38 { 38 {
39 std::lock_guard<std::mutex> lock(requestHeaderNamesMutex); 39 std::lock_guard<std::mutex> lock(requestHeaderNamesMutex);
40 // we currently ignore the result. We should check it actually gets inse rted. 40 // we currently ignore the result. We should check it actually gets inse rted.
41 requestHeaderNames.insert(std::make_pair(url, std::move(headerNames))); 41 requestHeaderNames.insert(std::make_pair(url, std::move(headerNames)));
42 } 42 }
43 43
44 AdblockPlus::Sleep(50); 44 AdblockPlus::Sleep(50);
45 45
46 AdblockPlus::ServerResponse result; 46 AdblockPlus::ServerResponse result;
47 result.status = NS_OK; 47 result.status = IWebRequest::NS_OK;
48 result.responseStatus = 123; 48 result.responseStatus = 123;
49 result.responseHeaders.push_back(std::pair<std::string, std::string>("Foo" , "Bar")); 49 result.responseHeaders.push_back(std::pair<std::string, std::string>("Foo" , "Bar"));
50 result.responseText = url + "\n"; 50 result.responseText = url + "\n";
51 if (!requestHeaders.empty()) 51 if (!requestHeaders.empty())
52 { 52 {
53 result.responseText += requestHeaders[0].first + "\n" + requestHeaders[0 ].second; 53 result.responseText += requestHeaders[0].first + "\n" + requestHeaders[0 ].second;
54 } 54 }
55 return result; 55 return result;
56 } 56 }
57 57
(...skipping 27 matching lines...) Expand all
85 BaseJsTest::SetUp(); 85 BaseJsTest::SetUp();
86 webRequest = std::make_shared<T>(); 86 webRequest = std::make_shared<T>();
87 jsEngine->SetWebRequest(webRequest); 87 jsEngine->SetWebRequest(webRequest);
88 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new LazyFileSystem())); 88 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new LazyFileSystem()));
89 } 89 }
90 90
91 std::shared_ptr<T> webRequest; 91 std::shared_ptr<T> webRequest;
92 }; 92 };
93 93
94 typedef WebRequestTest<MockWebRequest> MockWebRequestTest; 94 typedef WebRequestTest<MockWebRequest> MockWebRequestTest;
95 typedef WebRequestTest<AdblockPlus::DefaultWebRequest> DefaultWebRequestTest; 95 typedef WebRequestTest<AdblockPlus::DefaultWebRequestSync> DefaultWebRequestTe st;
96 typedef WebRequestTest<MockWebRequest> XMLHttpRequestTest; 96 typedef WebRequestTest<MockWebRequest> XMLHttpRequestTest;
97 97
98 // we return the url of the XHR. 98 // we return the url of the XHR.
99 std::string ResetTestXHR(const AdblockPlus::JsEnginePtr& jsEngine, const std:: string& defaultUrl = "") 99 std::string ResetTestXHR(const AdblockPlus::JsEnginePtr& jsEngine, const std:: string& defaultUrl = "")
100 { 100 {
101 std::string url = defaultUrl; 101 std::string url = defaultUrl;
102 // make up a unique URL if we don't have one. 102 // make up a unique URL if we don't have one.
103 if (url == "") 103 if (url == "")
104 { 104 {
105 url = "https://tests.adblockplus.org/easylist.txt-"; 105 url = "https://tests.adblockplus.org/easylist.txt-";
(...skipping 29 matching lines...) Expand all
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").AsI nt()); 145 ASSERT_EQ(IWebRequest::NS_OK, jsEngine->Evaluate("foo.status").AsInt());
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").AsI nt()); 159 ASSERT_EQ(IWebRequest::NS_OK, jsEngine->Evaluate("foo.status").AsInt());
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(IWebRequest::NS_OK, jsEngine->Evaluate("request.channel.status").AsI nt());
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)").AsStrin g()); 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')").IsNull ()); 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(IWebRequest::NS_ERROR_FAILURE, jsEngine->Evaluate("foo.status").AsIn t());
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)").AsSt ring()); 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(IWebRequest::NS_ERROR_FAILURE, jsEngine->Evaluate("request.channel.s tatus").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')").Is Null()); 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
(...skipping 121 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