Index: test/WebRequest.cpp |
=================================================================== |
--- a/test/WebRequest.cpp |
+++ b/test/WebRequest.cpp |
@@ -86,24 +86,75 @@ TEST(WebRequestTest, RealWebRequest) |
} while (jsEngine.Evaluate("typeof foo") == "undefined"); |
ASSERT_EQ(ToString(AdblockPlus::WebRequest::NS_OK), jsEngine.Evaluate("foo.status")); |
ASSERT_EQ("200", jsEngine.Evaluate("foo.responseStatus")); |
ASSERT_EQ("[Adblock Plus ", jsEngine.Evaluate("foo.responseText.substr(0, 14)")); |
//TODO: Shall we be strict with letter casing here? Content-Type vs content-type |
ASSERT_EQ("text/plain", jsEngine.Evaluate("foo.responseHeaders['Content-Type'].substr(0, 10)")); |
ASSERT_EQ("undefined", jsEngine.Evaluate("typeof foo.responseHeaders['location']")); |
} |
+ |
+TEST(WebRequestTest, XMLHttpRequest) |
+{ |
+ AdblockPlus::DefaultWebRequest webRequest; |
+ AdblockPlus::JsEngine jsEngine(0, &webRequest, 0); |
+ AdblockPlus::FilterEngine filterEngine(jsEngine); |
+ |
+ jsEngine.Evaluate("\ |
+ var result;\ |
+ var request = new XMLHttpRequest();\ |
+ request.open('GET', 'https://easylist.adblockplus.org/easylist.txt');\ |
+ request.setRequestHeader('X', 'Y');\ |
+ request.overrideMimeType('text/plain');\ |
+ request.addEventListener('load', function() {result = request.responseText;}, false);\ |
+ request.addEventListener('error', function() {result = 'error';}, false);\ |
+ request.send(null);"); |
+ do |
+ { |
+ AdblockPlus::Sleep(200); |
+ } while (jsEngine.Evaluate("result")->IsUndefined()); |
+ ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine.Evaluate("request.channel.status")->AsInt()); |
+ ASSERT_EQ(200, jsEngine.Evaluate("request.status")->AsInt()); |
+ ASSERT_EQ("[Adblock Plus ", jsEngine.Evaluate("result.substr(0, 14)")->AsString()); |
+ ASSERT_EQ("text/plain", jsEngine.Evaluate("request.getResponseHeader('Content-Type').substr(0, 10)")->AsString()); |
+ ASSERT_TRUE(jsEngine.Evaluate("request.getResponseHeader('Location')")->IsNull()); |
+} |
#else |
TEST(WebRequestTest, DummyWebRequest) |
{ |
AdblockPlus::DefaultWebRequest webRequest; |
AdblockPlus::JsEngine jsEngine(0, &webRequest, 0); |
jsEngine.Evaluate("_webRequest.GET('https://easylist.adblockplus.org/easylist.txt', {}, function(result) {foo = result;} )"); |
do |
{ |
AdblockPlus::Sleep(200); |
} while (jsEngine.Evaluate("this.foo")->IsUndefined()); |
ASSERT_EQ(AdblockPlus::WebRequest::NS_ERROR_FAILURE, jsEngine.Evaluate("foo.status")->AsInt()); |
ASSERT_EQ(0, jsEngine.Evaluate("foo.responseStatus")->AsInt()); |
ASSERT_EQ("", jsEngine.Evaluate("foo.responseText")->AsString()); |
ASSERT_EQ("{}", jsEngine.Evaluate("JSON.stringify(foo.responseHeaders)")->AsString()); |
} |
+ |
+TEST(WebRequestTest, XMLHttpRequest) |
+{ |
+ AdblockPlus::DefaultWebRequest webRequest; |
+ AdblockPlus::JsEngine jsEngine(0, &webRequest, 0); |
+ AdblockPlus::FilterEngine filterEngine(jsEngine); |
+ |
+ jsEngine.Evaluate("\ |
+ var result;\ |
+ var request = new XMLHttpRequest();\ |
+ request.open('GET', 'https://easylist.adblockplus.org/easylist.txt');\ |
+ request.setRequestHeader('X', 'Y');\ |
+ request.overrideMimeType('text/plain');\ |
+ request.addEventListener('load', function() {result = request.responseText;}, false);\ |
+ request.addEventListener('error', function() {result = 'error';}, false);\ |
+ request.send(null);"); |
+ do |
+ { |
+ AdblockPlus::Sleep(200); |
+ } while (jsEngine.Evaluate("result")->IsUndefined()); |
+ ASSERT_EQ(AdblockPlus::WebRequest::NS_ERROR_FAILURE, jsEngine.Evaluate("request.channel.status")->AsInt()); |
+ ASSERT_EQ(0, jsEngine.Evaluate("request.status")->AsInt()); |
+ ASSERT_EQ("error", jsEngine.Evaluate("result")->AsString()); |
+ ASSERT_TRUE(jsEngine.Evaluate("request.getResponseHeader('Content-Type')")->IsNull()); |
+} |
#endif |