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

Side by Side Diff: test/WebRequest.cpp

Issue 10260028: Refactor tests, use fixtures and avoid duplication (Closed)
Patch Set: Created April 19, 2013, 4:11 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 | « test/JsValue.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 // TODO: Use a fixture here
8
9 namespace
8 { 10 {
9 public: 11 class TestWebRequest : public AdblockPlus::WebRequest
10 AdblockPlus::ServerResponse GET(const std::string& url, const AdblockPlus::Hea derList& requestHeaders) const
11 { 12 {
12 AdblockPlus::Sleep(50); 13 public:
14 AdblockPlus::ServerResponse GET(const std::string& url, const AdblockPlus::H eaderList& requestHeaders) const
15 {
16 AdblockPlus::Sleep(50);
13 17
14 AdblockPlus::ServerResponse result; 18 AdblockPlus::ServerResponse result;
15 result.status = NS_OK; 19 result.status = NS_OK;
16 result.responseStatus = 123; 20 result.responseStatus = 123;
17 result.responseHeaders.push_back(std::pair<std::string, std::string>("Foo", "Bar")); 21 result.responseHeaders.push_back(std::pair<std::string, std::string>("Foo" , "Bar"));
18 result.responseText = url + "\n" + requestHeaders[0].first + "\n" + requestH eaders[0].second; 22 result.responseText = url + "\n" + requestHeaders[0].first + "\n" + reques tHeaders[0].second;
19 return result; 23 return result;
24 }
25 };
26
27 std::string ToString(unsigned int i)
28 {
29 std::stringstream stream;
30 stream << i;
31 return stream.str();
20 } 32 }
21 };
22
23 std::string ToString(unsigned int i)
24 {
25 std::stringstream stream;
26 stream << i;
27 return stream.str();
28 } 33 }
29 34
30 TEST(WebRequestTest, BadCall) 35 TEST(WebRequestTest, BadCall)
31 { 36 {
32 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New()); 37 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
33 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET()")); 38 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET()"));
34 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('', {}, function(){})")); 39 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('', {}, function(){})"));
35 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET({toString: false}, {}, fu nction(){})")); 40 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET({toString: false}, {}, fu nction(){})"));
36 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', nu ll, function(){})")); 41 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', nu ll, function(){})"));
37 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', {} , null)")); 42 ASSERT_ANY_THROW(jsEngine->Evaluate("_webRequest.GET('http://example.com/', {} , null)"));
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 do 107 do
103 { 108 {
104 AdblockPlus::Sleep(200); 109 AdblockPlus::Sleep(200);
105 } while (jsEngine->Evaluate("this.foo")->IsUndefined()); 110 } while (jsEngine->Evaluate("this.foo")->IsUndefined());
106 ASSERT_EQ(AdblockPlus::WebRequest::NS_ERROR_FAILURE, jsEngine->Evaluate("foo.s tatus")->AsInt()); 111 ASSERT_EQ(AdblockPlus::WebRequest::NS_ERROR_FAILURE, jsEngine->Evaluate("foo.s tatus")->AsInt());
107 ASSERT_EQ(0, jsEngine->Evaluate("foo.responseStatus")->AsInt()); 112 ASSERT_EQ(0, jsEngine->Evaluate("foo.responseStatus")->AsInt());
108 ASSERT_EQ("", jsEngine->Evaluate("foo.responseText")->AsString()); 113 ASSERT_EQ("", jsEngine->Evaluate("foo.responseText")->AsString());
109 ASSERT_EQ("{}", jsEngine->Evaluate("JSON.stringify(foo.responseHeaders)")->AsS tring()); 114 ASSERT_EQ("{}", jsEngine->Evaluate("JSON.stringify(foo.responseHeaders)")->AsS tring());
110 } 115 }
111 116
112 #if !FILTER_ENGINE_STUBS
113 TEST(WebRequestTest, XMLHttpRequest) 117 TEST(WebRequestTest, XMLHttpRequest)
114 { 118 {
115 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New()); 119 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
116 AdblockPlus::FilterEngine filterEngine(jsEngine); 120 AdblockPlus::FilterEngine filterEngine(jsEngine);
117 121
118 jsEngine->Evaluate("\ 122 jsEngine->Evaluate("\
119 var result;\ 123 var result;\
120 var request = new XMLHttpRequest();\ 124 var request = new XMLHttpRequest();\
121 request.open('GET', 'https://easylist.adblockplus.org/easylist.txt');\ 125 request.open('GET', 'https://easylist.adblockplus.org/easylist.txt');\
122 request.setRequestHeader('X', 'Y');\ 126 request.setRequestHeader('X', 'Y');\
123 request.overrideMimeType('text/plain');\ 127 request.overrideMimeType('text/plain');\
124 request.addEventListener('load', function() {result = request.responseText;} , false);\ 128 request.addEventListener('load', function() {result = request.responseText;} , false);\
125 request.addEventListener('error', function() {result = 'error';}, false);\ 129 request.addEventListener('error', function() {result = 'error';}, false);\
126 request.send(null);"); 130 request.send(null);");
127 do 131 do
128 { 132 {
129 AdblockPlus::Sleep(200); 133 AdblockPlus::Sleep(200);
130 } while (jsEngine->Evaluate("result")->IsUndefined()); 134 } while (jsEngine->Evaluate("result")->IsUndefined());
131 ASSERT_EQ(AdblockPlus::WebRequest::NS_ERROR_FAILURE, jsEngine->Evaluate("reque st.channel.status")->AsInt()); 135 ASSERT_EQ(AdblockPlus::WebRequest::NS_ERROR_FAILURE, jsEngine->Evaluate("reque st.channel.status")->AsInt());
132 ASSERT_EQ(0, jsEngine->Evaluate("request.status")->AsInt()); 136 ASSERT_EQ(0, jsEngine->Evaluate("request.status")->AsInt());
133 ASSERT_EQ("error", jsEngine->Evaluate("result")->AsString()); 137 ASSERT_EQ("error", jsEngine->Evaluate("result")->AsString());
134 ASSERT_TRUE(jsEngine->Evaluate("request.getResponseHeader('Content-Type')")->I sNull()); 138 ASSERT_TRUE(jsEngine->Evaluate("request.getResponseHeader('Content-Type')")->I sNull());
135 } 139 }
136 #endif
137 140
138 #endif 141 #endif
OLDNEW
« no previous file with comments | « test/JsValue.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld