Left: | ||
Right: |
OLD | NEW |
---|---|
1 #include <sstream> | 1 #include <sstream> |
2 #include <AdblockPlus.h> | 2 #include "BaseJsTest.h" |
3 #include <gtest/gtest.h> | |
4 | |
5 #include "../src/Thread.h" | 3 #include "../src/Thread.h" |
6 | 4 |
7 // TODO: Use a fixture here | |
8 | |
9 namespace | 5 namespace |
10 { | 6 { |
11 class TestWebRequest : public AdblockPlus::WebRequest | 7 class MockWebRequest : public AdblockPlus::WebRequest |
12 { | 8 { |
13 public: | 9 public: |
14 AdblockPlus::ServerResponse GET(const std::string& url, const AdblockPlus::H eaderList& requestHeaders) const | 10 AdblockPlus::ServerResponse GET(const std::string& url, const AdblockPlus::H eaderList& requestHeaders) const |
15 { | 11 { |
16 AdblockPlus::Sleep(50); | 12 AdblockPlus::Sleep(50); |
17 | 13 |
18 AdblockPlus::ServerResponse result; | 14 AdblockPlus::ServerResponse result; |
19 result.status = NS_OK; | 15 result.status = NS_OK; |
20 result.responseStatus = 123; | 16 result.responseStatus = 123; |
21 result.responseHeaders.push_back(std::pair<std::string, std::string>("Foo" , "Bar")); | 17 result.responseHeaders.push_back(std::pair<std::string, std::string>("Foo" , "Bar")); |
22 result.responseText = url + "\n" + requestHeaders[0].first + "\n" + reques tHeaders[0].second; | 18 result.responseText = url + "\n" + requestHeaders[0].first + "\n" + reques tHeaders[0].second; |
23 return result; | 19 return result; |
24 } | 20 } |
25 }; | 21 }; |
26 | 22 |
27 std::string ToString(unsigned int i) | 23 template<class T> |
Oleksandr
2013/04/26 13:10:01
Miht be a good idea to make this even more generic
Wladimir Palant
2013/04/26 13:12:58
I tend to resist making code more generic "for fut
| |
24 class WebRequestTest : public BaseJsTest | |
28 { | 25 { |
29 std::stringstream stream; | 26 protected: |
30 stream << i; | 27 void SetUp() |
31 return stream.str(); | 28 { |
32 } | 29 BaseJsTest::SetUp(); |
30 jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(new T)); | |
31 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new LazyFileSystem)); | |
32 } | |
33 }; | |
34 | |
35 typedef WebRequestTest<MockWebRequest> MockWebRequestTest; | |
36 typedef WebRequestTest<AdblockPlus::DefaultWebRequest> DefaultWebRequestTest; | |
33 } | 37 } |
34 | 38 |
35 TEST(WebRequestTest, BadCall) | 39 TEST_F(MockWebRequestTest, BadCall) |
36 { | 40 { |
37 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New()); | |
38 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET()")); | 41 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET()")); |
39 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('', {}, function(){})")); | 42 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('', {}, function(){})")); |
40 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET({toString: false}, {}, fu nction(){})")); | 43 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET({toString: false}, {}, fu nction(){})")); |
41 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', nu ll, function(){})")); | 44 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', nu ll, function(){})")); |
42 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', {} , null)")); | 45 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', {} , null)")); |
43 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', {} , function(){}, 0)")); | 46 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', {} , function(){}, 0)")); |
44 } | 47 } |
45 | 48 |
46 TEST(WebRequestTest, TestWebRequest) | 49 TEST_F(MockWebRequestTest, SuccessfulRequest) |
47 { | 50 { |
48 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New()); | |
49 jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(new TestWebRequest())); | |
50 jsEngine->Evaluate("_webRequest.GET('http://example.com/', {X: 'Y'}, function( result) {foo = result;} )"); | 51 jsEngine->Evaluate("_webRequest.GET('http://example.com/', {X: 'Y'}, function( result) {foo = result;} )"); |
51 ASSERT_TRUE(jsEngine->Evaluate("this.foo")->IsUndefined()); | 52 ASSERT_TRUE(jsEngine->Evaluate("this.foo")->IsUndefined()); |
52 AdblockPlus::Sleep(200); | 53 AdblockPlus::Sleep(200); |
53 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("foo.status")->As Int()); | 54 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("foo.status")->As Int()); |
54 ASSERT_EQ(123, jsEngine->Evaluate("foo.responseStatus")->AsInt()); | 55 ASSERT_EQ(123, jsEngine->Evaluate("foo.responseStatus")->AsInt()); |
55 ASSERT_EQ("http://example.com/\nX\nY", jsEngine->Evaluate("foo.responseText")- >AsString()); | 56 ASSERT_EQ("http://example.com/\nX\nY", jsEngine->Evaluate("foo.responseText")- >AsString()); |
56 ASSERT_EQ("{\"Foo\":\"Bar\"}", jsEngine->Evaluate("JSON.stringify(foo.response Headers)")->AsString()); | 57 ASSERT_EQ("{\"Foo\":\"Bar\"}", jsEngine->Evaluate("JSON.stringify(foo.response Headers)")->AsString()); |
57 } | 58 } |
58 | 59 |
59 #if defined(HAVE_CURL) || defined(_WIN32) | 60 #if defined(HAVE_CURL) || defined(_WIN32) |
60 TEST(WebRequestTest, RealWebRequest) | 61 TEST_F(DefaultWebRequestTest, RealWebRequest) |
61 { | 62 { |
62 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New()); | |
63 | |
64 // This URL should redirect to easylist-downloads.adblockplus.org and we | 63 // This URL should redirect to easylist-downloads.adblockplus.org and we |
65 // should get the actual filter list back. | 64 // should get the actual filter list back. |
66 jsEngine->Evaluate("_webRequest.GET('https://easylist.adblockplus.org/easylist .txt', {}, function(result) {foo = result;} )"); | 65 jsEngine->Evaluate("_webRequest.GET('https://easylist.adblockplus.org/easylist .txt', {}, function(result) {foo = result;} )"); |
67 do | 66 do |
68 { | 67 { |
69 AdblockPlus::Sleep(200); | 68 AdblockPlus::Sleep(200); |
70 } while (jsEngine->Evaluate("this.foo")->IsUndefined()); | 69 } while (jsEngine->Evaluate("this.foo")->IsUndefined()); |
71 ASSERT_EQ("text/plain", jsEngine->Evaluate("foo.responseHeaders['content-type' ].substr(0, 10)")->AsString()); | 70 ASSERT_EQ("text/plain", jsEngine->Evaluate("foo.responseHeaders['content-type' ].substr(0, 10)")->AsString()); |
72 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("foo.status")->As Int()); | 71 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("foo.status")->As Int()); |
73 ASSERT_EQ(200, jsEngine->Evaluate("foo.responseStatus")->AsInt()); | 72 ASSERT_EQ(200, jsEngine->Evaluate("foo.responseStatus")->AsInt()); |
74 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()); |
75 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()); |
76 ASSERT_TRUE(jsEngine->Evaluate("foo.responseHeaders['location']")->IsUndefined ()); | 75 ASSERT_TRUE(jsEngine->Evaluate("foo.responseHeaders['location']")->IsUndefined ()); |
77 } | 76 } |
78 | 77 |
79 TEST(WebRequestTest, XMLHttpRequest) | 78 TEST_F(DefaultWebRequestTest, XMLHttpRequest) |
80 { | 79 { |
81 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New()); | |
82 AdblockPlus::FilterEngine filterEngine(jsEngine); | 80 AdblockPlus::FilterEngine filterEngine(jsEngine); |
83 | 81 |
84 jsEngine->Evaluate("\ | 82 jsEngine->Evaluate("\ |
85 var result;\ | 83 var result;\ |
86 var request = new XMLHttpRequest();\ | 84 var request = new XMLHttpRequest();\ |
87 request.open('GET', 'https://easylist.adblockplus.org/easylist.txt');\ | 85 request.open('GET', 'https://easylist.adblockplus.org/easylist.txt');\ |
88 request.setRequestHeader('X', 'Y');\ | 86 request.setRequestHeader('X', 'Y');\ |
89 request.overrideMimeType('text/plain');\ | 87 request.overrideMimeType('text/plain');\ |
90 request.addEventListener('load', function() {result = request.responseText;} , false);\ | 88 request.addEventListener('load', function() {result = request.responseText;} , false);\ |
91 request.addEventListener('error', function() {result = 'error';}, false);\ | 89 request.addEventListener('error', function() {result = 'error';}, false);\ |
92 request.send(null);"); | 90 request.send(null);"); |
93 do | 91 do |
94 { | 92 { |
95 AdblockPlus::Sleep(200); | 93 AdblockPlus::Sleep(200); |
96 } while (jsEngine->Evaluate("result")->IsUndefined()); | 94 } while (jsEngine->Evaluate("result")->IsUndefined()); |
97 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("request.channel. status")->AsInt()); | 95 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("request.channel. status")->AsInt()); |
98 ASSERT_EQ(200, jsEngine->Evaluate("request.status")->AsInt()); | 96 ASSERT_EQ(200, jsEngine->Evaluate("request.status")->AsInt()); |
99 ASSERT_EQ("[Adblock Plus ", jsEngine->Evaluate("result.substr(0, 14)")->AsStri ng()); | 97 ASSERT_EQ("[Adblock Plus ", jsEngine->Evaluate("result.substr(0, 14)")->AsStri ng()); |
100 ASSERT_EQ("text/plain", jsEngine->Evaluate("request.getResponseHeader('Content -Type').substr(0, 10)")->AsString()); | 98 ASSERT_EQ("text/plain", jsEngine->Evaluate("request.getResponseHeader('Content -Type').substr(0, 10)")->AsString()); |
101 ASSERT_TRUE(jsEngine->Evaluate("request.getResponseHeader('Location')")->IsNul l()); | 99 ASSERT_TRUE(jsEngine->Evaluate("request.getResponseHeader('Location')")->IsNul l()); |
102 } | 100 } |
103 #else | 101 #else |
104 TEST(WebRequestTest, DummyWebRequest) | 102 TEST_F(DefaultWebRequestTest, DummyWebRequest) |
105 { | 103 { |
106 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New()); | |
107 jsEngine->Evaluate("_webRequest.GET('https://easylist.adblockplus.org/easylist .txt', {}, function(result) {foo = result;} )"); | 104 jsEngine->Evaluate("_webRequest.GET('https://easylist.adblockplus.org/easylist .txt', {}, function(result) {foo = result;} )"); |
108 do | 105 do |
109 { | 106 { |
110 AdblockPlus::Sleep(200); | 107 AdblockPlus::Sleep(200); |
111 } while (jsEngine->Evaluate("this.foo")->IsUndefined()); | 108 } while (jsEngine->Evaluate("this.foo")->IsUndefined()); |
112 ASSERT_EQ(AdblockPlus::WebRequest::NS_ERROR_FAILURE, jsEngine->Evaluate("foo.s tatus")->AsInt()); | 109 ASSERT_EQ(AdblockPlus::WebRequest::NS_ERROR_FAILURE, jsEngine->Evaluate("foo.s tatus")->AsInt()); |
113 ASSERT_EQ(0, jsEngine->Evaluate("foo.responseStatus")->AsInt()); | 110 ASSERT_EQ(0, jsEngine->Evaluate("foo.responseStatus")->AsInt()); |
114 ASSERT_EQ("", jsEngine->Evaluate("foo.responseText")->AsString()); | 111 ASSERT_EQ("", jsEngine->Evaluate("foo.responseText")->AsString()); |
115 ASSERT_EQ("{}", jsEngine->Evaluate("JSON.stringify(foo.responseHeaders)")->AsS tring()); | 112 ASSERT_EQ("{}", jsEngine->Evaluate("JSON.stringify(foo.responseHeaders)")->AsS tring()); |
116 } | 113 } |
117 | 114 |
118 TEST(WebRequestTest, XMLHttpRequest) | 115 TEST_F(DefaultWebRequestTest, XMLHttpRequest) |
119 { | 116 { |
120 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New()); | |
121 AdblockPlus::FilterEngine filterEngine(jsEngine); | 117 AdblockPlus::FilterEngine filterEngine(jsEngine); |
122 | 118 |
123 jsEngine->Evaluate("\ | 119 jsEngine->Evaluate("\ |
124 var result;\ | 120 var result;\ |
125 var request = new XMLHttpRequest();\ | 121 var request = new XMLHttpRequest();\ |
126 request.open('GET', 'https://easylist.adblockplus.org/easylist.txt');\ | 122 request.open('GET', 'https://easylist.adblockplus.org/easylist.txt');\ |
127 request.setRequestHeader('X', 'Y');\ | 123 request.setRequestHeader('X', 'Y');\ |
128 request.overrideMimeType('text/plain');\ | 124 request.overrideMimeType('text/plain');\ |
129 request.addEventListener('load', function() {result = request.responseText;} , false);\ | 125 request.addEventListener('load', function() {result = request.responseText;} , false);\ |
130 request.addEventListener('error', function() {result = 'error';}, false);\ | 126 request.addEventListener('error', function() {result = 'error';}, false);\ |
131 request.send(null);"); | 127 request.send(null);"); |
132 do | 128 do |
133 { | 129 { |
134 AdblockPlus::Sleep(200); | 130 AdblockPlus::Sleep(200); |
135 } while (jsEngine->Evaluate("result")->IsUndefined()); | 131 } while (jsEngine->Evaluate("result")->IsUndefined()); |
136 ASSERT_EQ(AdblockPlus::WebRequest::NS_ERROR_FAILURE, jsEngine->Evaluate("reque st.channel.status")->AsInt()); | 132 ASSERT_EQ(AdblockPlus::WebRequest::NS_ERROR_FAILURE, jsEngine->Evaluate("reque st.channel.status")->AsInt()); |
137 ASSERT_EQ(0, jsEngine->Evaluate("request.status")->AsInt()); | 133 ASSERT_EQ(0, jsEngine->Evaluate("request.status")->AsInt()); |
138 ASSERT_EQ("error", jsEngine->Evaluate("result")->AsString()); | 134 ASSERT_EQ("error", jsEngine->Evaluate("result")->AsString()); |
139 ASSERT_TRUE(jsEngine->Evaluate("request.getResponseHeader('Content-Type')")->I sNull()); | 135 ASSERT_TRUE(jsEngine->Evaluate("request.getResponseHeader('Content-Type')")->I sNull()); |
140 } | 136 } |
141 | 137 |
142 #endif | 138 #endif |
OLD | NEW |