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

Side by Side Diff: test/WebRequest.cpp

Issue 10214006: Added JS-based XMLHttpRequest implementation (Closed)
Patch Set: Unbitrotted patch Created April 16, 2013, 3:31 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/compat.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
95 TEST(WebRequestTest, XMLHttpRequest)
96 {
97 AdblockPlus::DefaultWebRequest webRequest;
98 AdblockPlus::JsEngine jsEngine(0, &webRequest, 0);
99 AdblockPlus::FilterEngine filterEngine(jsEngine);
100
101 jsEngine.Evaluate("\
102 var result;\
103 var request = new XMLHttpRequest();\
104 request.open('GET', 'https://easylist.adblockplus.org/easylist.txt');\
105 request.setRequestHeader('X', 'Y');\
106 request.overrideMimeType('text/plain');\
107 request.addEventListener('load', function() {result = request.responseText;} , false);\
108 request.addEventListener('error', function() {result = 'error';}, false);\
109 request.send(null);");
110 do
111 {
112 AdblockPlus::Sleep(200);
113 } while (jsEngine.Evaluate("result")->IsUndefined());
114 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine.Evaluate("request.channel.s tatus")->AsInt());
115 ASSERT_EQ(200, jsEngine.Evaluate("request.status")->AsInt());
116 ASSERT_EQ("[Adblock Plus ", jsEngine.Evaluate("result.substr(0, 14)")->AsStrin g());
117 ASSERT_EQ("text/plain", jsEngine.Evaluate("request.getResponseHeader('Content- Type').substr(0, 10)")->AsString());
118 ASSERT_TRUE(jsEngine.Evaluate("request.getResponseHeader('Location')")->IsNull ());
119 }
94 #else 120 #else
95 TEST(WebRequestTest, DummyWebRequest) 121 TEST(WebRequestTest, DummyWebRequest)
96 { 122 {
97 AdblockPlus::DefaultWebRequest webRequest; 123 AdblockPlus::DefaultWebRequest webRequest;
98 AdblockPlus::JsEngine jsEngine(0, &webRequest, 0); 124 AdblockPlus::JsEngine jsEngine(0, &webRequest, 0);
99 jsEngine.Evaluate("_webRequest.GET('https://easylist.adblockplus.org/easylist. txt', {}, function(result) {foo = result;} )"); 125 jsEngine.Evaluate("_webRequest.GET('https://easylist.adblockplus.org/easylist. txt', {}, function(result) {foo = result;} )");
100 do 126 do
101 { 127 {
102 AdblockPlus::Sleep(200); 128 AdblockPlus::Sleep(200);
103 } while (jsEngine.Evaluate("this.foo")->IsUndefined()); 129 } while (jsEngine.Evaluate("this.foo")->IsUndefined());
104 ASSERT_EQ(AdblockPlus::WebRequest::NS_ERROR_FAILURE, jsEngine.Evaluate("foo.st atus")->AsInt()); 130 ASSERT_EQ(AdblockPlus::WebRequest::NS_ERROR_FAILURE, jsEngine.Evaluate("foo.st atus")->AsInt());
105 ASSERT_EQ(0, jsEngine.Evaluate("foo.responseStatus")->AsInt()); 131 ASSERT_EQ(0, jsEngine.Evaluate("foo.responseStatus")->AsInt());
106 ASSERT_EQ("", jsEngine.Evaluate("foo.responseText")->AsString()); 132 ASSERT_EQ("", jsEngine.Evaluate("foo.responseText")->AsString());
107 ASSERT_EQ("{}", jsEngine.Evaluate("JSON.stringify(foo.responseHeaders)")->AsSt ring()); 133 ASSERT_EQ("{}", jsEngine.Evaluate("JSON.stringify(foo.responseHeaders)")->AsSt ring());
108 } 134 }
135
136 TEST(WebRequestTest, XMLHttpRequest)
137 {
138 AdblockPlus::DefaultWebRequest webRequest;
139 AdblockPlus::JsEngine jsEngine(0, &webRequest, 0);
140 AdblockPlus::FilterEngine filterEngine(jsEngine);
141
142 jsEngine.Evaluate("\
143 var result;\
144 var request = new XMLHttpRequest();\
145 request.open('GET', 'https://easylist.adblockplus.org/easylist.txt');\
146 request.setRequestHeader('X', 'Y');\
147 request.overrideMimeType('text/plain');\
148 request.addEventListener('load', function() {result = request.responseText;} , false);\
149 request.addEventListener('error', function() {result = 'error';}, false);\
150 request.send(null);");
151 do
152 {
153 AdblockPlus::Sleep(200);
154 } while (jsEngine.Evaluate("result")->IsUndefined());
155 ASSERT_EQ(AdblockPlus::WebRequest::NS_ERROR_FAILURE, jsEngine.Evaluate("reques t.channel.status")->AsInt());
156 ASSERT_EQ(0, jsEngine.Evaluate("request.status")->AsInt());
157 ASSERT_EQ("error", jsEngine.Evaluate("result")->AsString());
158 ASSERT_TRUE(jsEngine.Evaluate("request.getResponseHeader('Content-Type')")->Is Null());
159 }
109 #endif 160 #endif
OLDNEW
« 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