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

Side by Side Diff: test/WebRequest.cpp

Issue 29372702: Issue #4826 - Use latch to replace thread-sleeping in tests
Patch Set: stray comments Created Jan. 20, 2017, 1:29 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-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 #include <sstream> 18 #include <sstream>
19 #include "BaseJsTest.h" 19 #include "BaseJsTest.h"
20 #include "JsLatch.h"
20 21
21 namespace 22 namespace
22 { 23 {
23 class MockWebRequest : public AdblockPlus::WebRequest 24 class MockWebRequest : public AdblockPlus::WebRequest
24 { 25 {
25 public: 26 public:
26 AdblockPlus::ServerResponse GET(const std::string& url, const AdblockPlus::H eaderList& requestHeaders) const 27 AdblockPlus::ServerResponse GET(const std::string& url, const AdblockPlus::H eaderList& requestHeaders) const
27 { 28 {
28 std::this_thread::sleep_for(std::chrono::milliseconds(50));
29
30 AdblockPlus::ServerResponse result; 29 AdblockPlus::ServerResponse result;
31 result.status = NS_OK; 30 result.status = NS_OK;
32 result.responseStatus = 123; 31 result.responseStatus = 123;
33 result.responseHeaders.push_back(std::pair<std::string, std::string>("Foo" , "Bar")); 32 result.responseHeaders.push_back(std::pair<std::string, std::string>("Foo" , "Bar"));
34 result.responseText = url + "\n" + requestHeaders[0].first + "\n" + reques tHeaders[0].second; 33 result.responseText = url + "\n" + requestHeaders[0].first + "\n" + reques tHeaders[0].second;
35 return result; 34 return result;
36 } 35 }
37 }; 36 };
38 37
39 template<class T> 38 template<class T>
(...skipping 17 matching lines...) Expand all
57 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET()")); 56 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET()"));
58 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('', {}, function(){})")); 57 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('', {}, function(){})"));
59 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET({toString: false}, {}, fu nction(){})")); 58 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET({toString: false}, {}, fu nction(){})"));
60 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', nu ll, function(){})")); 59 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', nu ll, function(){})"));
61 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', {} , null)")); 60 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', {} , null)"));
62 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', {} , function(){}, 0)")); 61 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', {} , function(){}, 0)"));
63 } 62 }
64 63
65 TEST_F(MockWebRequestTest, SuccessfulRequest) 64 TEST_F(MockWebRequestTest, SuccessfulRequest)
66 { 65 {
67 jsEngine->Evaluate("_webRequest.GET('http://example.com/', {X: 'Y'}, function( result) {foo = result;} )"); 66 JsTestingLatch latch(engine, "latch");
68 ASSERT_TRUE(jsEngine->Evaluate("this.foo")->IsUndefined()); 67 jsEngine->Evaluate("_webRequest.GET('http://example.com/', {X: 'Y'}, function( result) {foo = result; latch.Arrive();} )");
69 std::this_thread::sleep_for(std::chrono::milliseconds(200)); 68 latch.Wait();
70 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("foo.status")->As Int()); 69 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("foo.status")->As Int());
71 ASSERT_EQ(123, jsEngine->Evaluate("foo.responseStatus")->AsInt()); 70 ASSERT_EQ(123, jsEngine->Evaluate("foo.responseStatus")->AsInt());
72 ASSERT_EQ("http://example.com/\nX\nY", jsEngine->Evaluate("foo.responseText")- >AsString()); 71 ASSERT_EQ("http://example.com/\nX\nY", jsEngine->Evaluate("foo.responseText")- >AsString());
73 ASSERT_EQ("{\"Foo\":\"Bar\"}", jsEngine->Evaluate("JSON.stringify(foo.response Headers)")->AsString()); 72 ASSERT_EQ("{\"Foo\":\"Bar\"}", jsEngine->Evaluate("JSON.stringify(foo.response Headers)")->AsString());
74 } 73 }
75 74
76 #if defined(HAVE_CURL) || defined(_WIN32) 75 #if defined(HAVE_CURL) || defined(_WIN32)
77 TEST_F(DefaultWebRequestTest, RealWebRequest) 76 TEST_F(DefaultWebRequestTest, RealWebRequest)
78 { 77 {
79 // This URL should redirect to easylist-downloads.adblockplus.org and we 78 // This URL should redirect to easylist-downloads.adblockplus.org and we
80 // should get the actual filter list back. 79 // should get the actual filter list back.
81 jsEngine->Evaluate("_webRequest.GET('https://easylist-downloads.adblockplus.or g/easylist.txt', {}, function(result) {foo = result;} )"); 80 JsTestingLatch latch(engine, "latch");
82 do 81 jsEngine->Evaluate("_webRequest.GET('https://easylist-downloads.adblockplus.or g/easylist.txt', {}, function(result) {foo = result; latch.Arrive();} )");
83 { 82 latch.Wait();
84 std::this_thread::sleep_for(std::chrono::milliseconds(200));
85 } while (jsEngine->Evaluate("this.foo")->IsUndefined());
86 ASSERT_EQ("text/plain", jsEngine->Evaluate("foo.responseHeaders['content-type' ].substr(0, 10)")->AsString()); 83 ASSERT_EQ("text/plain", jsEngine->Evaluate("foo.responseHeaders['content-type' ].substr(0, 10)")->AsString());
87 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("foo.status")->As Int()); 84 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("foo.status")->As Int());
88 ASSERT_EQ(200, jsEngine->Evaluate("foo.responseStatus")->AsInt()); 85 ASSERT_EQ(200, jsEngine->Evaluate("foo.responseStatus")->AsInt());
89 ASSERT_EQ("[Adblock Plus ", jsEngine->Evaluate("foo.responseText.substr(0, 14) ")->AsString()); 86 ASSERT_EQ("[Adblock Plus ", jsEngine->Evaluate("foo.responseText.substr(0, 14) ")->AsString());
90 ASSERT_EQ("text/plain", jsEngine->Evaluate("foo.responseHeaders['content-type' ].substr(0, 10)")->AsString()); 87 ASSERT_EQ("text/plain", jsEngine->Evaluate("foo.responseHeaders['content-type' ].substr(0, 10)")->AsString());
91 ASSERT_TRUE(jsEngine->Evaluate("foo.responseHeaders['location']")->IsUndefined ()); 88 ASSERT_TRUE(jsEngine->Evaluate("foo.responseHeaders['location']")->IsUndefined ());
92 } 89 }
93 90
94 TEST_F(DefaultWebRequestTest, XMLHttpRequest) 91 TEST_F(DefaultWebRequestTest, XMLHttpRequest)
95 { 92 {
96 AdblockPlus::FilterEngine filterEngine(jsEngine); 93 AdblockPlus::FilterEngine filterEngine(jsEngine);
97 94 JsTestingLatch latch(engine, "latch");
98 jsEngine->Evaluate("\ 95 jsEngine->Evaluate("\
99 var result;\ 96 var result;\
100 var request = new XMLHttpRequest();\ 97 var request = new XMLHttpRequest();\
101 request.open('GET', 'https://easylist-downloads.adblockplus.org/easylist.txt ');\ 98 request.open('GET', 'https://easylist-downloads.adblockplus.org/easylist.txt ');\
102 request.setRequestHeader('X', 'Y');\ 99 request.setRequestHeader('X', 'Y');\
103 request.setRequestHeader('X2', 'Y2');\ 100 request.setRequestHeader('X2', 'Y2');\
104 request.overrideMimeType('text/plain');\ 101 request.overrideMimeType('text/plain');\
105 request.addEventListener('load', function() {result = request.responseText;} , false);\ 102 request.addEventListener('load', function() {result = request.responseText; latch.Arrive();}, false);\
106 request.addEventListener('error', function() {result = 'error';}, false);\ 103 request.addEventListener('error', function() {result = 'error'; latch.Arrive ();}, false);\
107 request.send(null);"); 104 request.send(null);");
108 do 105 latch.Wait();
109 {
110 std::this_thread::sleep_for(std::chrono::milliseconds(200));
111 } while (jsEngine->Evaluate("result")->IsUndefined());
112 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("request.channel. status")->AsInt()); 106 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("request.channel. status")->AsInt());
113 ASSERT_EQ(200, jsEngine->Evaluate("request.status")->AsInt()); 107 ASSERT_EQ(200, jsEngine->Evaluate("request.status")->AsInt());
114 ASSERT_EQ("[Adblock Plus ", jsEngine->Evaluate("result.substr(0, 14)")->AsStri ng()); 108 ASSERT_EQ("[Adblock Plus ", jsEngine->Evaluate("result.substr(0, 14)")->AsStri ng());
115 ASSERT_EQ("text/plain", jsEngine->Evaluate("request.getResponseHeader('Content -Type').substr(0, 10)")->AsString()); 109 ASSERT_EQ("text/plain", jsEngine->Evaluate("request.getResponseHeader('Content -Type').substr(0, 10)")->AsString());
116 ASSERT_TRUE(jsEngine->Evaluate("request.getResponseHeader('Location')")->IsNul l()); 110 ASSERT_TRUE(jsEngine->Evaluate("request.getResponseHeader('Location')")->IsNul l());
117 } 111 }
118 #else 112 #else
119 TEST_F(DefaultWebRequestTest, DummyWebRequest) 113 TEST_F(DefaultWebRequestTest, DummyWebRequest)
120 { 114 {
121 jsEngine->Evaluate("_webRequest.GET('https://easylist-downloads.adblockplus.or g/easylist.txt', {}, function(result) {foo = result;} )"); 115 jsEngine->Evaluate("_webRequest.GET('https://easylist-downloads.adblockplus.or g/easylist.txt', {}, function(result) {foo = result;} )");
(...skipping 24 matching lines...) Expand all
146 { 140 {
147 AdblockPlus::Sleep(200); 141 AdblockPlus::Sleep(200);
148 } while (jsEngine->Evaluate("result")->IsUndefined()); 142 } while (jsEngine->Evaluate("result")->IsUndefined());
149 ASSERT_EQ(AdblockPlus::WebRequest::NS_ERROR_FAILURE, jsEngine->Evaluate("reque st.channel.status")->AsInt()); 143 ASSERT_EQ(AdblockPlus::WebRequest::NS_ERROR_FAILURE, jsEngine->Evaluate("reque st.channel.status")->AsInt());
150 ASSERT_EQ(0, jsEngine->Evaluate("request.status")->AsInt()); 144 ASSERT_EQ(0, jsEngine->Evaluate("request.status")->AsInt());
151 ASSERT_EQ("error", jsEngine->Evaluate("result")->AsString()); 145 ASSERT_EQ("error", jsEngine->Evaluate("result")->AsString());
152 ASSERT_TRUE(jsEngine->Evaluate("request.getResponseHeader('Content-Type')")->I sNull()); 146 ASSERT_TRUE(jsEngine->Evaluate("request.getResponseHeader('Content-Type')")->I sNull());
153 } 147 }
154 148
155 #endif 149 #endif
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