OLD | NEW |
1 #include <sstream> | 1 #include <sstream> |
2 #include <AdblockPlus.h> | 2 #include <AdblockPlus.h> |
3 #include <gtest/gtest.h> | 3 #include <gtest/gtest.h> |
4 | 4 |
5 #include "../src/Thread.h" | 5 #include "../src/Thread.h" |
6 | 6 |
7 #ifdef HAVE_WININET | 7 #ifdef HAVE_WININET |
8 #include "../src/WebRequestWinInet.h" | 8 #include "../src/WebRequestWinInet.h" |
9 #endif | 9 #endif |
10 | 10 |
(...skipping 29 matching lines...) Expand all Loading... |
40 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET('http://example.com/', nul
l, function(){})")); | 40 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET('http://example.com/', nul
l, function(){})")); |
41 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET('http://example.com/', {},
null)")); | 41 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET('http://example.com/', {},
null)")); |
42 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET('http://example.com/', {},
function(){}, 0)")); | 42 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET('http://example.com/', {},
function(){}, 0)")); |
43 } | 43 } |
44 | 44 |
45 TEST(WebRequestTest, TestWebRequest) | 45 TEST(WebRequestTest, TestWebRequest) |
46 { | 46 { |
47 TestWebRequest webRequest; | 47 TestWebRequest webRequest; |
48 AdblockPlus::JsEngine jsEngine(0, &webRequest, 0); | 48 AdblockPlus::JsEngine jsEngine(0, &webRequest, 0); |
49 jsEngine.Evaluate("_webRequest.GET('http://example.com/', {X: 'Y'}, function(r
esult) {foo = result;} )"); | 49 jsEngine.Evaluate("_webRequest.GET('http://example.com/', {X: 'Y'}, function(r
esult) {foo = result;} )"); |
50 ASSERT_EQ("undefined", jsEngine.Evaluate("typeof foo")); | 50 ASSERT_TRUE(jsEngine.Evaluate("this.foo")->IsUndefined()); |
51 AdblockPlus::Sleep(200); | 51 AdblockPlus::Sleep(200); |
52 ASSERT_EQ(ToString(AdblockPlus::WebRequest::NS_OK), jsEngine.Evaluate("foo.sta
tus")); | 52 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine.Evaluate("foo.status")->AsI
nt()); |
53 ASSERT_EQ("123", jsEngine.Evaluate("foo.responseStatus")); | 53 ASSERT_EQ(123, jsEngine.Evaluate("foo.responseStatus")->AsInt()); |
54 ASSERT_EQ("http://example.com/\nX\nY", jsEngine.Evaluate("foo.responseText")); | 54 ASSERT_EQ("http://example.com/\nX\nY", jsEngine.Evaluate("foo.responseText")->
AsString()); |
55 ASSERT_EQ("{\"Foo\":\"Bar\"}", jsEngine.Evaluate("JSON.stringify(foo.responseH
eaders)")); | 55 ASSERT_EQ("{\"Foo\":\"Bar\"}", jsEngine.Evaluate("JSON.stringify(foo.responseH
eaders)")->AsString()); |
56 } | 56 } |
57 | 57 |
58 #if defined(HAVE_CURL) | 58 #if defined(HAVE_CURL) |
59 TEST(WebRequestTest, RealWebRequest) | 59 TEST(WebRequestTest, RealWebRequest) |
60 { | 60 { |
61 AdblockPlus::DefaultWebRequest webRequest; | 61 AdblockPlus::DefaultWebRequest webRequest; |
62 AdblockPlus::JsEngine jsEngine(0, &webRequest, 0); | 62 AdblockPlus::JsEngine jsEngine(0, &webRequest, 0); |
63 | 63 |
64 // This URL should redirect to easylist-downloads.adblockplus.org and we | 64 // This URL should redirect to easylist-downloads.adblockplus.org and we |
65 // should get the actual filter list back. | 65 // should get the actual filter list back. |
66 jsEngine.Evaluate("_webRequest.GET('https://easylist.adblockplus.org/easylist.
txt', {}, function(result) {foo = result;} )"); | 66 jsEngine.Evaluate("_webRequest.GET('https://easylist.adblockplus.org/easylist.
txt', {}, function(result) {foo = result;} )"); |
67 do | 67 do |
68 { | 68 { |
69 AdblockPlus::Sleep(200); | 69 AdblockPlus::Sleep(200); |
70 } while (jsEngine.Evaluate("typeof foo") == "undefined"); | 70 } while (jsEngine.Evaluate("this.foo")->IsUndefined()); |
71 ASSERT_EQ(ToString(AdblockPlus::WebRequest::NS_OK), jsEngine.Evaluate("foo.sta
tus")); | 71 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine.Evaluate("foo.status")->AsI
nt()); |
72 ASSERT_EQ("200", jsEngine.Evaluate("foo.responseStatus")); | 72 ASSERT_EQ(200, jsEngine.Evaluate("foo.responseStatus")->AsInt()); |
73 ASSERT_EQ("[Adblock Plus ", jsEngine.Evaluate("foo.responseText.substr(0, 14)"
)); | 73 ASSERT_EQ("[Adblock Plus ", jsEngine.Evaluate("foo.responseText.substr(0, 14)"
)->AsString()); |
74 ASSERT_EQ("text/plain", jsEngine.Evaluate("foo.responseHeaders['content-type']
.substr(0, 10)")); | 74 ASSERT_EQ("text/plain", jsEngine.Evaluate("foo.responseHeaders['content-type']
.substr(0, 10)")->AsString()); |
75 ASSERT_EQ("undefined", jsEngine.Evaluate("typeof foo.responseHeaders['location
']")); | 75 ASSERT_TRUE(jsEngine.Evaluate("foo.responseHeaders['location']")->IsUndefined(
)); |
76 } | 76 } |
77 #elif defined(HAVE_WININET) | 77 #elif defined(HAVE_WININET) |
78 TEST(WebRequestTest, RealWebRequest) | 78 TEST(WebRequestTest, RealWebRequest) |
79 { | 79 { |
80 AdblockPlus::WebRequestWinInet webRequest; | 80 AdblockPlus::WebRequestWinInet webRequest; |
81 AdblockPlus::JsEngine jsEngine(0, &webRequest, 0); | 81 AdblockPlus::JsEngine jsEngine(0, &webRequest, 0); |
82 jsEngine.Evaluate("_webRequest.GET('https://easylist.adblockplus.org/easylist.
txt', {}, function(result) {foo = result;} )"); | 82 jsEngine.Evaluate("_webRequest.GET('https://easylist.adblockplus.org/easylist.
txt', {}, function(result) {foo = result;} )"); |
83 do | 83 do |
84 { | 84 { |
85 AdblockPlus::Sleep(200); | 85 AdblockPlus::Sleep(200); |
86 } while (jsEngine.Evaluate("typeof foo") == "undefined"); | 86 } while (jsEngine.Evaluate("typeof foo") == "undefined"); |
87 ASSERT_EQ(ToString(AdblockPlus::WebRequest::NS_OK), jsEngine.Evaluate("foo.sta
tus")); | 87 ASSERT_EQ(ToString(AdblockPlus::WebRequest::NS_OK), jsEngine.Evaluate("foo.sta
tus")); |
88 ASSERT_EQ("200", jsEngine.Evaluate("foo.responseStatus")); | 88 ASSERT_EQ("200", jsEngine.Evaluate("foo.responseStatus")); |
89 ASSERT_EQ("[Adblock Plus ", jsEngine.Evaluate("foo.responseText.substr(0, 14)"
)); | 89 ASSERT_EQ("[Adblock Plus ", jsEngine.Evaluate("foo.responseText.substr(0, 14)"
)); |
90 //TODO: Shall we be strict with letter casing here? Content-Type vs content-ty
pe | 90 //TODO: Shall we be strict with letter casing here? Content-Type vs content-ty
pe |
91 ASSERT_EQ("text/plain", jsEngine.Evaluate("foo.responseHeaders['Content-Type']
.substr(0, 10)")); | 91 ASSERT_EQ("text/plain", jsEngine.Evaluate("foo.responseHeaders['Content-Type']
.substr(0, 10)")); |
92 ASSERT_EQ("undefined", jsEngine.Evaluate("typeof foo.responseHeaders['location
']")); | 92 ASSERT_EQ("undefined", jsEngine.Evaluate("typeof foo.responseHeaders['location
']")); |
93 } | 93 } |
94 #else | 94 #else |
95 TEST(WebRequestTest, DummyWebRequest) | 95 TEST(WebRequestTest, DummyWebRequest) |
96 { | 96 { |
97 AdblockPlus::DefaultWebRequest webRequest; | 97 AdblockPlus::DefaultWebRequest webRequest; |
98 AdblockPlus::JsEngine jsEngine(0, &webRequest, 0); | 98 AdblockPlus::JsEngine jsEngine(0, &webRequest, 0); |
99 jsEngine.Evaluate("_webRequest.GET('https://easylist.adblockplus.org/easylist.
txt', {}, function(result) {foo = result;} )"); | 99 jsEngine.Evaluate("_webRequest.GET('https://easylist.adblockplus.org/easylist.
txt', {}, function(result) {foo = result;} )"); |
100 do | 100 do |
101 { | 101 { |
102 AdblockPlus::Sleep(200); | 102 AdblockPlus::Sleep(200); |
103 } while (jsEngine.Evaluate("typeof foo") == "undefined"); | 103 } while (jsEngine.Evaluate("this.foo")->IsUndefined()); |
104 ASSERT_EQ(ToString(AdblockPlus::WebRequest::NS_ERROR_FAILURE), jsEngine.Evalua
te("foo.status")); | 104 ASSERT_EQ(AdblockPlus::WebRequest::NS_ERROR_FAILURE, jsEngine.Evaluate("foo.st
atus")->AsInt()); |
105 ASSERT_EQ("0", jsEngine.Evaluate("foo.responseStatus")); | 105 ASSERT_EQ(0, jsEngine.Evaluate("foo.responseStatus")->AsInt()); |
106 ASSERT_EQ("", jsEngine.Evaluate("foo.responseText")); | 106 ASSERT_EQ("", jsEngine.Evaluate("foo.responseText")->AsString()); |
107 ASSERT_EQ("{}", jsEngine.Evaluate("JSON.stringify(foo.responseHeaders)")); | 107 ASSERT_EQ("{}", jsEngine.Evaluate("JSON.stringify(foo.responseHeaders)")->AsSt
ring()); |
108 } | 108 } |
109 #endif | 109 #endif |
OLD | NEW |