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 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
derList& requestHeaders) const | 10 AdblockPlus::ServerResponse GET(const std::string& url, const AdblockPlus::Hea
derList& requestHeaders) const |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 do | 63 do |
64 { | 64 { |
65 AdblockPlus::Sleep(200); | 65 AdblockPlus::Sleep(200); |
66 } while (jsEngine.Evaluate("this.foo")->IsUndefined()); | 66 } while (jsEngine.Evaluate("this.foo")->IsUndefined()); |
67 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine.Evaluate("foo.status")->AsI
nt()); | 67 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine.Evaluate("foo.status")->AsI
nt()); |
68 ASSERT_EQ(200, jsEngine.Evaluate("foo.responseStatus")->AsInt()); | 68 ASSERT_EQ(200, jsEngine.Evaluate("foo.responseStatus")->AsInt()); |
69 ASSERT_EQ("[Adblock Plus ", jsEngine.Evaluate("foo.responseText.substr(0, 14)"
)->AsString()); | 69 ASSERT_EQ("[Adblock Plus ", jsEngine.Evaluate("foo.responseText.substr(0, 14)"
)->AsString()); |
70 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()); |
71 ASSERT_TRUE(jsEngine.Evaluate("foo.responseHeaders['location']")->IsUndefined(
)); | 71 ASSERT_TRUE(jsEngine.Evaluate("foo.responseHeaders['location']")->IsUndefined(
)); |
72 } | 72 } |
| 73 |
| 74 TEST(WebRequestTest, XMLHttpRequest) |
| 75 { |
| 76 AdblockPlus::DefaultWebRequest webRequest; |
| 77 AdblockPlus::JsEngine jsEngine(0, &webRequest, 0); |
| 78 AdblockPlus::FilterEngine filterEngine(jsEngine); |
| 79 |
| 80 jsEngine.Evaluate("\ |
| 81 var result;\ |
| 82 var request = new XMLHttpRequest();\ |
| 83 request.open('GET', 'https://easylist.adblockplus.org/easylist.txt');\ |
| 84 request.setRequestHeader('X', 'Y');\ |
| 85 request.overrideMimeType('text/plain');\ |
| 86 request.addEventListener('load', function() {result = request.responseText;}
, false);\ |
| 87 request.addEventListener('error', function() {result = 'error';}, false);\ |
| 88 request.send(null);"); |
| 89 do |
| 90 { |
| 91 AdblockPlus::Sleep(200); |
| 92 } while (jsEngine.Evaluate("result")->IsUndefined()); |
| 93 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine.Evaluate("request.channel.s
tatus")->AsInt()); |
| 94 ASSERT_EQ(200, jsEngine.Evaluate("request.status")->AsInt()); |
| 95 ASSERT_EQ("[Adblock Plus ", jsEngine.Evaluate("result.substr(0, 14)")->AsStrin
g()); |
| 96 ASSERT_EQ("text/plain", jsEngine.Evaluate("request.getResponseHeader('Content-
Type').substr(0, 10)")->AsString()); |
| 97 ASSERT_TRUE(jsEngine.Evaluate("request.getResponseHeader('Location')")->IsNull
()); |
| 98 } |
73 #else | 99 #else |
74 TEST(WebRequestTest, DummyWebRequest) | 100 TEST(WebRequestTest, DummyWebRequest) |
75 { | 101 { |
76 AdblockPlus::DefaultWebRequest webRequest; | 102 AdblockPlus::DefaultWebRequest webRequest; |
77 AdblockPlus::JsEngine jsEngine(0, &webRequest, 0); | 103 AdblockPlus::JsEngine jsEngine(0, &webRequest, 0); |
78 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;} )"); |
79 do | 105 do |
80 { | 106 { |
81 AdblockPlus::Sleep(200); | 107 AdblockPlus::Sleep(200); |
82 } while (jsEngine.Evaluate("this.foo")->IsUndefined()); | 108 } while (jsEngine.Evaluate("this.foo")->IsUndefined()); |
83 ASSERT_EQ(AdblockPlus::WebRequest::NS_ERROR_FAILURE, jsEngine.Evaluate("foo.st
atus")->AsInt()); | 109 ASSERT_EQ(AdblockPlus::WebRequest::NS_ERROR_FAILURE, jsEngine.Evaluate("foo.st
atus")->AsInt()); |
84 ASSERT_EQ(0, jsEngine.Evaluate("foo.responseStatus")->AsInt()); | 110 ASSERT_EQ(0, jsEngine.Evaluate("foo.responseStatus")->AsInt()); |
85 ASSERT_EQ("", jsEngine.Evaluate("foo.responseText")->AsString()); | 111 ASSERT_EQ("", jsEngine.Evaluate("foo.responseText")->AsString()); |
86 ASSERT_EQ("{}", jsEngine.Evaluate("JSON.stringify(foo.responseHeaders)")->AsSt
ring()); | 112 ASSERT_EQ("{}", jsEngine.Evaluate("JSON.stringify(foo.responseHeaders)")->AsSt
ring()); |
87 } | 113 } |
| 114 |
| 115 TEST(WebRequestTest, XMLHttpRequest) |
| 116 { |
| 117 AdblockPlus::DefaultWebRequest webRequest; |
| 118 AdblockPlus::JsEngine jsEngine(0, &webRequest, 0); |
| 119 AdblockPlus::FilterEngine filterEngine(jsEngine); |
| 120 |
| 121 jsEngine.Evaluate("\ |
| 122 var result;\ |
| 123 var request = new XMLHttpRequest();\ |
| 124 request.open('GET', 'https://easylist.adblockplus.org/easylist.txt');\ |
| 125 request.setRequestHeader('X', 'Y');\ |
| 126 request.overrideMimeType('text/plain');\ |
| 127 request.addEventListener('load', function() {result = request.responseText;}
, false);\ |
| 128 request.addEventListener('error', function() {result = 'error';}, false);\ |
| 129 request.send(null);"); |
| 130 do |
| 131 { |
| 132 AdblockPlus::Sleep(200); |
| 133 } while (jsEngine.Evaluate("result")->IsUndefined()); |
| 134 ASSERT_EQ(AdblockPlus::WebRequest::NS_ERROR_FAILURE, jsEngine.Evaluate("reques
t.channel.status")->AsInt()); |
| 135 ASSERT_EQ(0, jsEngine.Evaluate("request.status")->AsInt()); |
| 136 ASSERT_EQ("error", jsEngine.Evaluate("result")->AsString()); |
| 137 ASSERT_TRUE(jsEngine.Evaluate("request.getResponseHeader('Content-Type')")->Is
Null()); |
| 138 } |
88 #endif | 139 #endif |
OLD | NEW |