-
Notifications
You must be signed in to change notification settings - Fork 2
/
HTTPTest.javascript
274 lines (222 loc) · 8.55 KB
/
HTTPTest.javascript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
* HTTP.cpp
* MacClient
*
* Created by Rick Kimball on 12/7/10.
* Copyright 2010 Roblox Inc. All rights reserved.
*
*/
#include "FastLog.h"
#include <boost/test/unit_test.hpp>
#include <boost/scoped_ptr.hpp>
#include "Util/Http.h"
#include "Util/Guid.h"
#include <sstream>
#include "rbx/test/ScopedFastFlagSetting.h"
#include "rbx/test/test_tools.h"
#include "rbx/test/DataModelFixture.h"
#include "Server.h"
#include "V8Xml/WebParser.h"
#include "v8datamodel/HttpRbxApiService.h"
BOOST_AUTO_TEST_SUITE(HTTPSuite)
static void testGet(RBX::Http& http, bool externalRequest = false)
{
std::string response1a;
http.get(response1a, externalRequest);
if (externalRequest)
{
BOOST_CHECK_EQUAL(std::string::npos != response1a.find("\"url\": \"http://httpbin.org/get\""), true);
}
else
{
BOOST_CHECK_EQUAL(response1a.substr(0, 6),"<html>");
}
}
static void testGetFailure(RBX::Http& http, bool externalRequest)
{
std::string response1a;
BOOST_CHECK_THROW(http.get(response1a, externalRequest), std::exception);
}
static void testPost(RBX::Http& http)
{
std::stringstream formdata;
formdata << "test=fame";
std::string response;
http.post(formdata, RBX::Http::kContentTypeUrlEncoded, false, response, true);
BOOST_CHECK_EQUAL(std::string::npos != response.find("\"test\": \"fame\""), true);
}
std::string guidify(std::string url)
{
if (url.find('?') == std::string::npos)
return url + "?" + RBX::Guid().readableString(12);
else
return url + "&" + RBX::Guid().readableString(12);
}
static void PostAsyncSuccess(std::string response)
{
BOOST_CHECK_EQUAL(response, "\"RGS OK\"");
}
static void PostAsyncError(std::string error)
{
BOOST_CHECK_EQUAL(true, false);
}
static void GetAsyncSuccess(std::string response)
{
shared_ptr<const RBX::Reflection::ValueTable> table(rbx::make_shared<const RBX::Reflection::ValueTable>());
RBX::WebParser::parseJSONTable(response, table);
RBX::Reflection::Variant assetVariant = table->at("AssetId");
if (assetVariant.isType<int>())
{
int asset = assetVariant.cast<int>();
BOOST_CHECK_EQUAL(asset, 28277486);
}
RBX::Reflection::Variant productVariant = table->at("ProductId");
if (productVariant.isType<int>())
{
int product = productVariant.cast<int>();
BOOST_CHECK_EQUAL(product, 4678951);
}
}
static void GetAsyncError(std::string error)
{
BOOST_CHECK_EQUAL(true, false);
}
BOOST_AUTO_TEST_CASE(HTTP_SYNC_GET_TESTS)
{
{
// Make a synchronous get request expecting data
{
RBX::Http a(guidify("http://www.roblox.com/Info/EULA.htm"));
RBX_TEST_WITH_TIMEOUT(boost::bind(&testGet, boost::ref(a), false), RBX::Time::Interval(10));
RBX::Http b(guidify("http://www.roblox.com/Info/EULA.htm"), RBX::Http::WinHttp);
RBX_TEST_WITH_TIMEOUT(boost::bind(&testGet, boost::ref(b), false), RBX::Time::Interval(10));
RBX::Http c(guidify("http://www.roblox.com/Info/EULA.htm"), RBX::Http::WinInet);
RBX_TEST_WITH_TIMEOUT(boost::bind(&testGet, boost::ref(c), false), RBX::Time::Interval(10));
}
{
RBX::Http a(guidify("http://blahblah.roblox.com/this_should_fail.php"));
RBX_TEST_WITH_TIMEOUT(boost::bind(&testGetFailure, boost::ref(a), false), RBX::Time::Interval(10));
RBX::Http b(guidify("http://blahblah.roblox.com/this_should_fail.php"), RBX::Http::WinHttp);
RBX_TEST_WITH_TIMEOUT(boost::bind(&testGetFailure, boost::ref(b), false), RBX::Time::Interval(10));
RBX::Http c(guidify("http://blahblah.roblox.com/this_should_fail.php"), RBX::Http::WinInet);
RBX_TEST_WITH_TIMEOUT(boost::bind(&testGetFailure, boost::ref(c), false), RBX::Time::Interval(10));
}
// Need test for iostream read
}
}
BOOST_AUTO_TEST_CASE(HTTP_SYNC_GET_EXTERNAL_TESTS)
{
{
RBX::Http a("http://httpbin.org/get");
RBX_TEST_WITH_TIMEOUT(boost::bind(&testGet, boost::ref(a), true), RBX::Time::Interval(10));
RBX::Http b("http://httpbin.org/get", RBX::Http::WinHttp);
RBX_TEST_WITH_TIMEOUT(boost::bind(&testGet, boost::ref(b), true), RBX::Time::Interval(10));
RBX::Http c("http://httpbin.org/get", RBX::Http::WinInet);
RBX_TEST_WITH_TIMEOUT(boost::bind(&testGet, boost::ref(c), true), RBX::Time::Interval(10));
}
{
RBX::Http a("http://httpbin.org/status/404");
RBX_TEST_WITH_TIMEOUT(boost::bind(&testGetFailure, boost::ref(a), true), RBX::Time::Interval(10));
RBX::Http b("http://httpbin.org/status/404", RBX::Http::WinHttp);
RBX_TEST_WITH_TIMEOUT(boost::bind(&testGetFailure, boost::ref(b), true), RBX::Time::Interval(10));
RBX::Http c("http://httpbin.org/status/404", RBX::Http::WinInet);
RBX_TEST_WITH_TIMEOUT(boost::bind(&testGetFailure, boost::ref(c), true), RBX::Time::Interval(10));
}
}
BOOST_AUTO_TEST_CASE(HTTP_SYNC_POST_EXTERNAL_TESTS)
{
{
std::string response;
RBX::Http a("http://httpbin.org/post");
RBX_TEST_WITH_TIMEOUT(boost::bind(&testPost, boost::ref(a)), RBX::Time::Interval(10));
RBX::Http b("http://httpbin.org/post", RBX::Http::WinHttp);
RBX_TEST_WITH_TIMEOUT(boost::bind(&testPost, boost::ref(b)), RBX::Time::Interval(10));
RBX::Http c("http://httpbin.org/post", RBX::Http::WinInet);
RBX_TEST_WITH_TIMEOUT(boost::bind(&testPost, boost::ref(c)), RBX::Time::Interval(10));
}
}
static void doHTTP_SYNC_POST_TESTS()
{
std::stringstream formdata;
formdata << "";
std::string url = "https://games.api.sitetest1.robloxlabs.com/";
RBX::Http postrequest1a(url);
std::string response1a;
postrequest1a.post(formdata, RBX::Http::kContentTypeApplicationJson, false, response1a);
BOOST_CHECK_EQUAL(response1a, "\"RGS OK\"");
// Need test for iostream write/read
}
BOOST_AUTO_TEST_CASE(HTTP_SYNC_POST_TESTS)
{
RBX_TEST_WITH_TIMEOUT(doHTTP_SYNC_POST_TESTS, RBX::Time::Interval(10));
}
BOOST_AUTO_TEST_CASE(HTTP_URL_ENCODE_TEST)
{
BOOST_CHECK_EQUAL(RBX::Http::urlEncode("this is a&#test"), "this%20is%20a%26%23test");
}
BOOST_AUTO_TEST_CASE(HTTP_URL_DECODE_TEST)
{
std::string allChars;
for (int i = 1; i < 256; ++i)
{
allChars += (char)i;
}
BOOST_REQUIRE_EQUAL(allChars.size(), 255);
BOOST_CHECK_EQUAL(allChars, RBX::Http::urlDecode(RBX::Http::urlEncode(allChars)));
}
BOOST_AUTO_TEST_CASE(API_PROXY_HTTP_SYNC_GET_TESTS)
{
DataModelFixture dm;
RBX::DataModel::LegacyLock l(&dm, RBX::DataModelJob::Write);
RBX::Reflection::Tuple args;
dm.execute("game:GetService('ContentProvider'):SetBaseUrl('http://www.roblox.com/')", args);
NetworkFixture::startServer(dm);
RBX::HttpRbxApiService* apiService = RBX::ServiceProvider::create<RBX::HttpRbxApiService>(&dm);
std::string responseString;
apiService->get("marketplace/productinfo?assetId=28277486", true, RBX::PRIORITY_EXTREME, responseString);
shared_ptr<const RBX::Reflection::ValueTable> table(rbx::make_shared<const RBX::Reflection::ValueTable>());
RBX::WebParser::parseJSONTable(responseString, table);
RBX::Reflection::Variant assetVariant = table->at("AssetId");
if (assetVariant.isType<int>())
{
int asset = assetVariant.cast<int>();
BOOST_CHECK_EQUAL(asset, 28277486);
}
RBX::Reflection::Variant productVariant = table->at("ProductId");
if (productVariant.isType<int>())
{
int product = productVariant.cast<int>();
BOOST_CHECK_EQUAL(product, 4678951);
}
}
BOOST_AUTO_TEST_CASE(API_PROXY_HTTP_ASYNC_GET_TESTS)
{
DataModelFixture dm;
RBX::DataModel::LegacyLock l(&dm, RBX::DataModelJob::Write);
RBX::Reflection::Tuple args;
dm.execute("game:GetService('ContentProvider'):SetBaseUrl('http://www.roblox.com/')", args);
NetworkFixture::startServer(dm);
RBX::HttpRbxApiService* apiService = RBX::ServiceProvider::create<RBX::HttpRbxApiService>(&dm);
apiService->getAsync("marketplace/productinfo?assetId=28277486", true, RBX::PRIORITY_EXTREME,
boost::bind(&GetAsyncSuccess, _1),
boost::bind(&GetAsyncError, _1) );
G3D::System::sleep(3.0);
}
BOOST_AUTO_TEST_CASE(API_PROXY_HTTP_ASYNC_POST_TESTS)
{
DataModelFixture dm;
RBX::DataModel::LegacyLock l(&dm, RBX::DataModelJob::Write);
RBX::Reflection::Tuple args;
dm.execute("game:GetService('ContentProvider'):SetBaseUrl('http://games.www.sitetest1.robloxlabs.com/')", args);
RBX::HttpRbxApiService* apiService = RBX::ServiceProvider::create<RBX::HttpRbxApiService>(&dm);
RBX::ContentProvider* cp = RBX::ServiceProvider::create<RBX::ContentProvider>(&dm);
NetworkFixture::startServer(dm);
std::string apiBaseUrl = cp->getApiBaseUrl();
RBX::Http http(apiBaseUrl);
std::string postString(" ");
apiService->postAsync(http, postString, RBX::PRIORITY_EXTREME, RBX::HttpService::APPLICATION_JSON,
boost::bind(&PostAsyncSuccess, _1),
boost::bind(&PostAsyncError, _1));
G3D::System::sleep(3.0);
}
BOOST_AUTO_TEST_SUITE_END()