Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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 class TestWebRequest : public AdblockPlus::WebRequest | 7 class TestWebRequest : public AdblockPlus::WebRequest |
8 { | 8 { |
9 public: | 9 public: |
10 AdblockPlus::ServerResponse GET(const std::string& url, const AdblockPlus::Hea dersList& requestHeaders) const | 10 AdblockPlus::ServerResponse GET(const std::string& url, const AdblockPlus::Hea derList& requestHeaders) const |
11 { | 11 { |
12 AdblockPlus::Sleep(50); | 12 AdblockPlus::Sleep(50); |
13 | 13 |
14 AdblockPlus::ServerResponse result; | 14 AdblockPlus::ServerResponse result; |
15 result.status = NS_OK; | 15 result.status = NS_OK; |
16 result.responseStatus = 123; | 16 result.responseStatus = 123; |
17 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")); |
18 result.responseText = url + "\n" + requestHeaders[0].first + "\n" + requestH eaders[0].second; | 18 result.responseText = url + "\n" + requestHeaders[0].first + "\n" + requestH eaders[0].second; |
19 return result; | 19 return result; |
20 } | 20 } |
21 }; | 21 }; |
22 | 22 |
23 std::string ToString(unsigned int i) | 23 std::string ToString(unsigned int i) |
24 { | 24 { |
25 std::stringstream stream; | 25 std::stringstream stream; |
26 stream << i; | 26 stream << i; |
27 return stream.str(); | 27 return stream.str(); |
28 } | 28 } |
29 | 29 |
30 TEST(WebRequestTest, BadCall) | 30 TEST(WebRequestTest, BadCall) |
31 { | 31 { |
32 AdblockPlus::JsEngine jsEngine(0, 0, 0); | 32 AdblockPlus::JsEngine jsEngine(0, 0, 0); |
33 ASSERT_ANY_THROW(jsEngine.Evaluate("_request.GET()")); | 33 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET()")); |
34 ASSERT_ANY_THROW(jsEngine.Evaluate("_request.GET('', {}, function(){})")); | 34 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET('', {}, function(){})")); |
35 ASSERT_ANY_THROW(jsEngine.Evaluate("_request.GET({toString: false}, {}, functi on(){})")); | 35 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET({toString: false}, {}, fun ction(){})")); |
36 ASSERT_ANY_THROW(jsEngine.Evaluate("_request.GET('http://example.com/', null, function(){})")); | 36 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET('http://example.com/', nul l, function(){})")); |
37 ASSERT_ANY_THROW(jsEngine.Evaluate("_request.GET('http://example.com/', {}, nu ll)")); | 37 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET('http://example.com/', {}, null)")); |
38 ASSERT_ANY_THROW(jsEngine.Evaluate("_request.GET('http://example.com/', {}, fu nction(){}, 0)")); | 38 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET('http://example.com/', {}, function(){}, 0)")); |
39 } | 39 } |
40 | 40 |
41 TEST(WebRequestTest, TestWebRequest) | 41 TEST(WebRequestTest, TestWebRequest) |
42 { | 42 { |
43 TestWebRequest webRequest; | 43 TestWebRequest webRequest; |
44 AdblockPlus::JsEngine jsEngine(0, &webRequest, 0); | 44 AdblockPlus::JsEngine jsEngine(0, &webRequest, 0); |
45 jsEngine.Evaluate("_request.GET('http://example.com/', {X: 'Y'}, function(resu lt) {foo = result;} )"); | 45 jsEngine.Evaluate("_webRequest.GET('http://example.com/', {X: 'Y'}, function(r esult) {foo = result;} )"); |
46 ASSERT_EQ("undefined", jsEngine.Evaluate("typeof foo")); | 46 ASSERT_EQ("undefined", jsEngine.Evaluate("typeof foo")); |
47 AdblockPlus::Sleep(200); | 47 AdblockPlus::Sleep(200); |
48 ASSERT_EQ(ToString(NS_OK), jsEngine.Evaluate("foo.status")); | 48 ASSERT_EQ(ToString(AdblockPlus::WebRequest::NS_OK), jsEngine.Evaluate("foo.sta tus")); |
49 ASSERT_EQ("123", jsEngine.Evaluate("foo.responseStatus")); | 49 ASSERT_EQ("123", jsEngine.Evaluate("foo.responseStatus")); |
50 ASSERT_EQ("http://example.com/\nX\nY", jsEngine.Evaluate("foo.responseText")); | 50 ASSERT_EQ("http://example.com/\nX\nY", jsEngine.Evaluate("foo.responseText")); |
51 ASSERT_EQ("{\"Foo\":\"Bar\"}", jsEngine.Evaluate("JSON.stringify(foo.responseH eaders)")); | 51 ASSERT_EQ("{\"Foo\":\"Bar\"}", jsEngine.Evaluate("JSON.stringify(foo.responseH eaders)")); |
52 } | 52 } |
53 | 53 |
54 #if defined(HAVE_CURL) | 54 #if defined(HAVE_CURL) |
55 TEST(WebRequestTest, RealWebRequest) | 55 TEST(WebRequestTest, RealWebRequest) |
56 { | 56 { |
57 AdblockPlus::DefaultWebRequest webRequest; | 57 AdblockPlus::DefaultWebRequest webRequest; |
58 AdblockPlus::JsEngine jsEngine(0, &webRequest, 0); | 58 AdblockPlus::JsEngine jsEngine(0, &webRequest, 0); |
59 | 59 |
60 // This URL should redirect to easylist-downloads.adblockplus.org and we | 60 // This URL should redirect to easylist-downloads.adblockplus.org and we |
61 // should get the actual filter list back. | 61 // should get the actual filter list back. |
62 jsEngine.Evaluate("_request.GET('https://easylist.adblockplus.org/easylist.txt ', {}, function(result) {foo = result;} )"); | 62 jsEngine.Evaluate("_webRequest.GET('https://easylist.adblockplus.org/easylist. txt', {}, function(result) {foo = result;} )"); |
63 do | 63 do |
64 { | 64 { |
65 AdblockPlus::Sleep(200); | 65 AdblockPlus::Sleep(200); |
66 } while (jsEngine.Evaluate("typeof foo") == "undefined"); | 66 } while (jsEngine.Evaluate("typeof foo") == "undefined"); |
67 ASSERT_EQ(ToString(NS_OK), jsEngine.Evaluate("foo.status")); | 67 ASSERT_EQ(ToString(AdblockPlus::WebRequest::NS_OK), jsEngine.Evaluate("foo.sta tus")); |
68 ASSERT_EQ("200", jsEngine.Evaluate("foo.responseStatus")); | 68 ASSERT_EQ("200", jsEngine.Evaluate("foo.responseStatus")); |
69 ASSERT_EQ("[Adblock Plus ", jsEngine.Evaluate("foo.responseText.substr(0, 14)" )); | 69 ASSERT_EQ("[Adblock Plus ", jsEngine.Evaluate("foo.responseText.substr(0, 14)" )); |
70 ASSERT_EQ("text/plain", jsEngine.Evaluate("foo.responseHeaders['content-type'] .substr(0, 10)")); | 70 ASSERT_EQ("text/plain", jsEngine.Evaluate("foo.responseHeaders['content-type'] .substr(0, 10)")); |
Oleksandr
2013/04/16 12:02:01
Do we care if the header is "content-type" or "Con
Felix Dahlke
2013/04/16 12:21:09
HTTP headers are case insensitive, so we can easil
Wladimir Palant
2013/04/16 13:30:32
I don't think that curl lowercases header names -
Felix Dahlke
2013/04/16 14:51:37
Hm, seems I missed that then.
Wladimir Palant
2013/04/17 06:56:20
I don't quite get this suggestion, WebRequestJsObj
| |
71 ASSERT_EQ("undefined", jsEngine.Evaluate("typeof foo.responseHeaders['location ']")); | 71 ASSERT_EQ("undefined", jsEngine.Evaluate("typeof foo.responseHeaders['location ']")); |
72 } | 72 } |
73 #else | 73 #else |
74 TEST(WebRequestTest, DummyWebRequest) | 74 TEST(WebRequestTest, DummyWebRequest) |
75 { | 75 { |
76 AdblockPlus::DefaultWebRequest webRequest; | 76 AdblockPlus::DefaultWebRequest webRequest; |
77 AdblockPlus::JsEngine jsEngine(0, &webRequest, 0); | 77 AdblockPlus::JsEngine jsEngine(0, &webRequest, 0); |
78 jsEngine.Evaluate("_request.GET('https://easylist.adblockplus.org/easylist.txt ', {}, function(result) {foo = result;} )"); | 78 jsEngine.Evaluate("_webRequest.GET('https://easylist.adblockplus.org/easylist. txt', {}, function(result) {foo = result;} )"); |
79 do | 79 do |
80 { | 80 { |
81 AdblockPlus::Sleep(200); | 81 AdblockPlus::Sleep(200); |
82 } while (jsEngine.Evaluate("typeof foo") == "undefined"); | 82 } while (jsEngine.Evaluate("typeof foo") == "undefined"); |
83 ASSERT_EQ(ToString(NS_ERROR_FAILURE), jsEngine.Evaluate("foo.status")); | 83 ASSERT_EQ(ToString(AdblockPlus::WebRequest::NS_ERROR_FAILURE), jsEngine.Evalua te("foo.status")); |
84 ASSERT_EQ("0", jsEngine.Evaluate("foo.responseStatus")); | 84 ASSERT_EQ("0", jsEngine.Evaluate("foo.responseStatus")); |
85 ASSERT_EQ("", jsEngine.Evaluate("foo.responseText")); | 85 ASSERT_EQ("", jsEngine.Evaluate("foo.responseText")); |
86 ASSERT_EQ("{}", jsEngine.Evaluate("JSON.stringify(foo.responseHeaders)")); | 86 ASSERT_EQ("{}", jsEngine.Evaluate("JSON.stringify(foo.responseHeaders)")); |
87 } | 87 } |
88 #endif | 88 #endif |
LEFT | RIGHT |