Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: test/WebRequest.cpp

Issue 10214006: Added JS-based XMLHttpRequest implementation (Closed)
Patch Set: Unbitrotted patch Created April 16, 2013, 3:31 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/compat.js ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
« no previous file with comments | « lib/compat.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld