OLD | NEW |
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 "../src/Thread.h" | |
21 | 20 |
22 namespace | 21 namespace |
23 { | 22 { |
24 class MockWebRequest : public AdblockPlus::WebRequest | 23 class MockWebRequest : public AdblockPlus::WebRequest |
25 { | 24 { |
26 public: | 25 public: |
27 AdblockPlus::ServerResponse GET(const std::string& url, const AdblockPlus::H
eaderList& requestHeaders) const | 26 AdblockPlus::ServerResponse GET(const std::string& url, const AdblockPlus::H
eaderList& requestHeaders) const |
28 { | 27 { |
29 AdblockPlus::Sleep(50); | 28 std::this_thread::sleep_for(std::chrono::milliseconds(50)); |
30 | 29 |
31 AdblockPlus::ServerResponse result; | 30 AdblockPlus::ServerResponse result; |
32 result.status = NS_OK; | 31 result.status = NS_OK; |
33 result.responseStatus = 123; | 32 result.responseStatus = 123; |
34 result.responseHeaders.push_back(std::pair<std::string, std::string>("Foo"
, "Bar")); | 33 result.responseHeaders.push_back(std::pair<std::string, std::string>("Foo"
, "Bar")); |
35 result.responseText = url + "\n" + requestHeaders[0].first + "\n" + reques
tHeaders[0].second; | 34 result.responseText = url + "\n" + requestHeaders[0].first + "\n" + reques
tHeaders[0].second; |
36 return result; | 35 return result; |
37 } | 36 } |
38 }; | 37 }; |
39 | 38 |
(...skipping 20 matching lines...) Expand all Loading... |
60 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET({toString: false}, {}, fu
nction(){})")); | 59 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET({toString: false}, {}, fu
nction(){})")); |
61 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', nu
ll, function(){})")); | 60 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', nu
ll, function(){})")); |
62 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', {}
, null)")); | 61 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', {}
, null)")); |
63 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', {}
, function(){}, 0)")); | 62 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', {}
, function(){}, 0)")); |
64 } | 63 } |
65 | 64 |
66 TEST_F(MockWebRequestTest, SuccessfulRequest) | 65 TEST_F(MockWebRequestTest, SuccessfulRequest) |
67 { | 66 { |
68 jsEngine->Evaluate("_webRequest.GET('http://example.com/', {X: 'Y'}, function(
result) {foo = result;} )"); | 67 jsEngine->Evaluate("_webRequest.GET('http://example.com/', {X: 'Y'}, function(
result) {foo = result;} )"); |
69 ASSERT_TRUE(jsEngine->Evaluate("this.foo")->IsUndefined()); | 68 ASSERT_TRUE(jsEngine->Evaluate("this.foo")->IsUndefined()); |
70 AdblockPlus::Sleep(200); | 69 std::this_thread::sleep_for(std::chrono::milliseconds(200)); |
71 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("foo.status")->As
Int()); | 70 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("foo.status")->As
Int()); |
72 ASSERT_EQ(123, jsEngine->Evaluate("foo.responseStatus")->AsInt()); | 71 ASSERT_EQ(123, jsEngine->Evaluate("foo.responseStatus")->AsInt()); |
73 ASSERT_EQ("http://example.com/\nX\nY", jsEngine->Evaluate("foo.responseText")-
>AsString()); | 72 ASSERT_EQ("http://example.com/\nX\nY", jsEngine->Evaluate("foo.responseText")-
>AsString()); |
74 ASSERT_EQ("{\"Foo\":\"Bar\"}", jsEngine->Evaluate("JSON.stringify(foo.response
Headers)")->AsString()); | 73 ASSERT_EQ("{\"Foo\":\"Bar\"}", jsEngine->Evaluate("JSON.stringify(foo.response
Headers)")->AsString()); |
75 } | 74 } |
76 | 75 |
77 #if defined(HAVE_CURL) || defined(_WIN32) | 76 #if defined(HAVE_CURL) || defined(_WIN32) |
78 TEST_F(DefaultWebRequestTest, RealWebRequest) | 77 TEST_F(DefaultWebRequestTest, RealWebRequest) |
79 { | 78 { |
80 // This URL should redirect to easylist-downloads.adblockplus.org and we | 79 // This URL should redirect to easylist-downloads.adblockplus.org and we |
81 // should get the actual filter list back. | 80 // should get the actual filter list back. |
82 jsEngine->Evaluate("_webRequest.GET('https://easylist-downloads.adblockplus.or
g/easylist.txt', {}, function(result) {foo = result;} )"); | 81 jsEngine->Evaluate("_webRequest.GET('https://easylist-downloads.adblockplus.or
g/easylist.txt', {}, function(result) {foo = result;} )"); |
83 do | 82 do |
84 { | 83 { |
85 AdblockPlus::Sleep(200); | 84 std::this_thread::sleep_for(std::chrono::milliseconds(200)); |
86 } while (jsEngine->Evaluate("this.foo")->IsUndefined()); | 85 } while (jsEngine->Evaluate("this.foo")->IsUndefined()); |
87 ASSERT_EQ("text/plain", jsEngine->Evaluate("foo.responseHeaders['content-type'
].substr(0, 10)")->AsString()); | 86 ASSERT_EQ("text/plain", jsEngine->Evaluate("foo.responseHeaders['content-type'
].substr(0, 10)")->AsString()); |
88 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("foo.status")->As
Int()); | 87 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("foo.status")->As
Int()); |
89 ASSERT_EQ(200, jsEngine->Evaluate("foo.responseStatus")->AsInt()); | 88 ASSERT_EQ(200, jsEngine->Evaluate("foo.responseStatus")->AsInt()); |
90 ASSERT_EQ("[Adblock Plus ", jsEngine->Evaluate("foo.responseText.substr(0, 14)
")->AsString()); | 89 ASSERT_EQ("[Adblock Plus ", jsEngine->Evaluate("foo.responseText.substr(0, 14)
")->AsString()); |
91 ASSERT_EQ("text/plain", jsEngine->Evaluate("foo.responseHeaders['content-type'
].substr(0, 10)")->AsString()); | 90 ASSERT_EQ("text/plain", jsEngine->Evaluate("foo.responseHeaders['content-type'
].substr(0, 10)")->AsString()); |
92 ASSERT_TRUE(jsEngine->Evaluate("foo.responseHeaders['location']")->IsUndefined
()); | 91 ASSERT_TRUE(jsEngine->Evaluate("foo.responseHeaders['location']")->IsUndefined
()); |
93 } | 92 } |
94 | 93 |
95 TEST_F(DefaultWebRequestTest, XMLHttpRequest) | 94 TEST_F(DefaultWebRequestTest, XMLHttpRequest) |
96 { | 95 { |
97 AdblockPlus::FilterEngine filterEngine(jsEngine); | 96 AdblockPlus::FilterEngine filterEngine(jsEngine); |
98 | 97 |
99 jsEngine->Evaluate("\ | 98 jsEngine->Evaluate("\ |
100 var result;\ | 99 var result;\ |
101 var request = new XMLHttpRequest();\ | 100 var request = new XMLHttpRequest();\ |
102 request.open('GET', 'https://easylist-downloads.adblockplus.org/easylist.txt
');\ | 101 request.open('GET', 'https://easylist-downloads.adblockplus.org/easylist.txt
');\ |
103 request.setRequestHeader('X', 'Y');\ | 102 request.setRequestHeader('X', 'Y');\ |
104 request.setRequestHeader('X2', 'Y2');\ | 103 request.setRequestHeader('X2', 'Y2');\ |
105 request.overrideMimeType('text/plain');\ | 104 request.overrideMimeType('text/plain');\ |
106 request.addEventListener('load', function() {result = request.responseText;}
, false);\ | 105 request.addEventListener('load', function() {result = request.responseText;}
, false);\ |
107 request.addEventListener('error', function() {result = 'error';}, false);\ | 106 request.addEventListener('error', function() {result = 'error';}, false);\ |
108 request.send(null);"); | 107 request.send(null);"); |
109 do | 108 do |
110 { | 109 { |
111 AdblockPlus::Sleep(200); | 110 std::this_thread::sleep_for(std::chrono::milliseconds(200)); |
112 } while (jsEngine->Evaluate("result")->IsUndefined()); | 111 } while (jsEngine->Evaluate("result")->IsUndefined()); |
113 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("request.channel.
status")->AsInt()); | 112 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("request.channel.
status")->AsInt()); |
114 ASSERT_EQ(200, jsEngine->Evaluate("request.status")->AsInt()); | 113 ASSERT_EQ(200, jsEngine->Evaluate("request.status")->AsInt()); |
115 ASSERT_EQ("[Adblock Plus ", jsEngine->Evaluate("result.substr(0, 14)")->AsStri
ng()); | 114 ASSERT_EQ("[Adblock Plus ", jsEngine->Evaluate("result.substr(0, 14)")->AsStri
ng()); |
116 ASSERT_EQ("text/plain", jsEngine->Evaluate("request.getResponseHeader('Content
-Type').substr(0, 10)")->AsString()); | 115 ASSERT_EQ("text/plain", jsEngine->Evaluate("request.getResponseHeader('Content
-Type').substr(0, 10)")->AsString()); |
117 ASSERT_TRUE(jsEngine->Evaluate("request.getResponseHeader('Location')")->IsNul
l()); | 116 ASSERT_TRUE(jsEngine->Evaluate("request.getResponseHeader('Location')")->IsNul
l()); |
118 } | 117 } |
119 #else | 118 #else |
120 TEST_F(DefaultWebRequestTest, DummyWebRequest) | 119 TEST_F(DefaultWebRequestTest, DummyWebRequest) |
121 { | 120 { |
(...skipping 25 matching lines...) Expand all Loading... |
147 { | 146 { |
148 AdblockPlus::Sleep(200); | 147 AdblockPlus::Sleep(200); |
149 } while (jsEngine->Evaluate("result")->IsUndefined()); | 148 } while (jsEngine->Evaluate("result")->IsUndefined()); |
150 ASSERT_EQ(AdblockPlus::WebRequest::NS_ERROR_FAILURE, jsEngine->Evaluate("reque
st.channel.status")->AsInt()); | 149 ASSERT_EQ(AdblockPlus::WebRequest::NS_ERROR_FAILURE, jsEngine->Evaluate("reque
st.channel.status")->AsInt()); |
151 ASSERT_EQ(0, jsEngine->Evaluate("request.status")->AsInt()); | 150 ASSERT_EQ(0, jsEngine->Evaluate("request.status")->AsInt()); |
152 ASSERT_EQ("error", jsEngine->Evaluate("result")->AsString()); | 151 ASSERT_EQ("error", jsEngine->Evaluate("result")->AsString()); |
153 ASSERT_TRUE(jsEngine->Evaluate("request.getResponseHeader('Content-Type')")->I
sNull()); | 152 ASSERT_TRUE(jsEngine->Evaluate("request.getResponseHeader('Content-Type')")->I
sNull()); |
154 } | 153 } |
155 | 154 |
156 #endif | 155 #endif |
OLD | NEW |