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

Side by Side Diff: test/WebRequest.cpp

Issue 10213003: Make JsEngine::Evaluate() return a wrapper for v8::Value to accessdifferent variable types easily (Closed)
Patch Set: Created April 12, 2013, 2:55 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
« include/AdblockPlus/JsValue.h ('K') | « test/JsEngine.cpp ('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 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 25 matching lines...) Expand all
36 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET('http://example.com/', nul l, function(){})")); 36 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET('http://example.com/', nul l, function(){})"));
37 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET('http://example.com/', {}, null)")); 37 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET('http://example.com/', {}, null)"));
38 ASSERT_ANY_THROW(jsEngine.Evaluate("_webRequest.GET('http://example.com/', {}, function(){}, 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("_webRequest.GET('http://example.com/', {X: 'Y'}, function(r esult) {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(true, jsEngine.Evaluate("this.foo")->IsUndefined());
47 AdblockPlus::Sleep(200); 47 AdblockPlus::Sleep(200);
48 ASSERT_EQ(ToString(AdblockPlus::WebRequest::NS_OK), jsEngine.Evaluate("foo.sta tus")); 48 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine.Evaluate("foo.status")->AsI nt());
49 ASSERT_EQ("123", jsEngine.Evaluate("foo.responseStatus")); 49 ASSERT_EQ(123, jsEngine.Evaluate("foo.responseStatus")->AsInt());
50 ASSERT_EQ("http://example.com/\nX\nY", jsEngine.Evaluate("foo.responseText")); 50 ASSERT_EQ("http://example.com/\nX\nY", jsEngine.Evaluate("foo.responseText")-> AsString());
51 ASSERT_EQ("{\"Foo\":\"Bar\"}", jsEngine.Evaluate("JSON.stringify(foo.responseH eaders)")); 51 ASSERT_EQ("{\"Foo\":\"Bar\"}", jsEngine.Evaluate("JSON.stringify(foo.responseH eaders)")->AsString());
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("_webRequest.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("this.foo")->IsUndefined());
67 ASSERT_EQ(ToString(AdblockPlus::WebRequest::NS_OK), jsEngine.Evaluate("foo.sta tus")); 67 ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine.Evaluate("foo.status")->AsI nt());
68 ASSERT_EQ("200", jsEngine.Evaluate("foo.responseStatus")); 68 ASSERT_EQ(200, jsEngine.Evaluate("foo.responseStatus")->AsInt());
69 ASSERT_EQ("[Adblock Plus ", jsEngine.Evaluate("foo.responseText.substr(0, 14)" )); 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)")); 70 ASSERT_EQ("text/plain", jsEngine.Evaluate("foo.responseHeaders['content-type'] .substr(0, 10)")->AsString());
71 ASSERT_EQ("undefined", jsEngine.Evaluate("typeof foo.responseHeaders['location ']")); 71 ASSERT_EQ(true, jsEngine.Evaluate("foo.responseHeaders['location']")->IsUndefi ned());
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("_webRequest.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("this.foo")->IsUndefined());
83 ASSERT_EQ(ToString(AdblockPlus::WebRequest::NS_ERROR_FAILURE), jsEngine.Evalua te("foo.status")); 83 ASSERT_EQ(AdblockPlus::WebRequest::NS_ERROR_FAILURE, jsEngine.Evaluate("foo.st atus")->AsInt());
84 ASSERT_EQ("0", jsEngine.Evaluate("foo.responseStatus")); 84 ASSERT_EQ(0, jsEngine.Evaluate("foo.responseStatus")->AsInt());
85 ASSERT_EQ("", jsEngine.Evaluate("foo.responseText")); 85 ASSERT_EQ("", jsEngine.Evaluate("foo.responseText")->AsString());
86 ASSERT_EQ("{}", jsEngine.Evaluate("JSON.stringify(foo.responseHeaders)")); 86 ASSERT_EQ("{}", jsEngine.Evaluate("JSON.stringify(foo.responseHeaders)")->AsSt ring());
87 } 87 }
88 #endif 88 #endif
OLDNEW
« include/AdblockPlus/JsValue.h ('K') | « test/JsEngine.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld