diff options
author | Jacek Antonelli | 2008-08-15 23:44:46 -0500 |
---|---|---|
committer | Jacek Antonelli | 2008-08-15 23:44:46 -0500 |
commit | 38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4 (patch) | |
tree | adca584755d22ca041a2dbfc35d4eca01f70b32c /linden/indra/test/llhttpclient_tut.cpp | |
parent | README.txt (diff) | |
download | meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.zip meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.gz meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.bz2 meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.xz |
Second Life viewer sources 1.13.2.12
Diffstat (limited to 'linden/indra/test/llhttpclient_tut.cpp')
-rw-r--r-- | linden/indra/test/llhttpclient_tut.cpp | 315 |
1 files changed, 315 insertions, 0 deletions
diff --git a/linden/indra/test/llhttpclient_tut.cpp b/linden/indra/test/llhttpclient_tut.cpp new file mode 100644 index 0000000..c84d34b --- /dev/null +++ b/linden/indra/test/llhttpclient_tut.cpp | |||
@@ -0,0 +1,315 @@ | |||
1 | /** | ||
2 | * @file llhttpclient_tut.cpp | ||
3 | * @brief Testing the HTTP client classes. | ||
4 | * | ||
5 | * Copyright (c) 2006-2007, Linden Research, Inc. | ||
6 | * | ||
7 | * The source code in this file ("Source Code") is provided by Linden Lab | ||
8 | * to you under the terms of the GNU General Public License, version 2.0 | ||
9 | * ("GPL"), unless you have obtained a separate licensing agreement | ||
10 | * ("Other License"), formally executed by you and Linden Lab. Terms of | ||
11 | * the GPL can be found in doc/GPL-license.txt in this distribution, or | ||
12 | * online at http://secondlife.com/developers/opensource/gplv2 | ||
13 | * | ||
14 | * There are special exceptions to the terms and conditions of the GPL as | ||
15 | * it is applied to this Source Code. View the full text of the exception | ||
16 | * in the file doc/FLOSS-exception.txt in this software distribution, or | ||
17 | * online at http://secondlife.com/developers/opensource/flossexception | ||
18 | * | ||
19 | * By copying, modifying or distributing this software, you acknowledge | ||
20 | * that you have read and understood your obligations described above, | ||
21 | * and agree to abide by those obligations. | ||
22 | * | ||
23 | * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO | ||
24 | * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY, | ||
25 | * COMPLETENESS OR PERFORMANCE. | ||
26 | */ | ||
27 | |||
28 | /** | ||
29 | * | ||
30 | * These classes test the HTTP client framework. | ||
31 | * | ||
32 | */ | ||
33 | |||
34 | #include <tut/tut.h> | ||
35 | #include "lltut.h" | ||
36 | |||
37 | #include "llhttpclient.h" | ||
38 | #include "llpipeutil.h" | ||
39 | #include "llpumpio.h" | ||
40 | |||
41 | #include "llsdhttpserver.h" | ||
42 | #include "lliohttpserver.h" | ||
43 | #include "lliosocket.h" | ||
44 | |||
45 | namespace tut | ||
46 | { | ||
47 | LLSD storage; | ||
48 | |||
49 | class LLSDStorageNode : public LLHTTPNode | ||
50 | { | ||
51 | public: | ||
52 | LLSD get() const { return storage; } | ||
53 | LLSD put(const LLSD& value) const { storage = value; return LLSD(); } | ||
54 | }; | ||
55 | |||
56 | class ErrorNode : public LLHTTPNode | ||
57 | { | ||
58 | public: | ||
59 | void get(ResponsePtr r, const LLSD& context) const | ||
60 | { r->status(599, "Intentional error"); } | ||
61 | void post(ResponsePtr r, const LLSD& context, const LLSD& input) const | ||
62 | { r->status(input["status"], input["reason"]); } | ||
63 | }; | ||
64 | |||
65 | class TimeOutNode : public LLHTTPNode | ||
66 | { | ||
67 | public: | ||
68 | void get(ResponsePtr r, const LLSD& context) const | ||
69 | { | ||
70 | /* do nothing, the request will eventually time out */ | ||
71 | } | ||
72 | }; | ||
73 | |||
74 | LLHTTPRegistration<LLSDStorageNode> gStorageNode("/test/storage"); | ||
75 | LLHTTPRegistration<ErrorNode> gErrorNode("/test/error"); | ||
76 | LLHTTPRegistration<TimeOutNode> gTimeOutNode("/test/timeout"); | ||
77 | |||
78 | struct HTTPClientTestData | ||
79 | { | ||
80 | public: | ||
81 | HTTPClientTestData() | ||
82 | { | ||
83 | apr_pool_create(&mPool, NULL); | ||
84 | mServerPump = new LLPumpIO(mPool); | ||
85 | mClientPump = new LLPumpIO(mPool); | ||
86 | |||
87 | LLHTTPClient::setPump(*mClientPump); | ||
88 | } | ||
89 | |||
90 | ~HTTPClientTestData() | ||
91 | { | ||
92 | delete mServerPump; | ||
93 | delete mClientPump; | ||
94 | apr_pool_destroy(mPool); | ||
95 | } | ||
96 | |||
97 | void setupTheServer() | ||
98 | { | ||
99 | LLHTTPNode& root = LLCreateHTTPServer(mPool, *mServerPump, 8888); | ||
100 | |||
101 | LLHTTPStandardServices::useServices(); | ||
102 | LLHTTPRegistrar::buildAllServices(root); | ||
103 | } | ||
104 | |||
105 | void runThePump(float timeout = 100.0f) | ||
106 | { | ||
107 | LLTimer timer; | ||
108 | timer.setTimerExpirySec(timeout); | ||
109 | |||
110 | while(!mSawCompleted && !timer.hasExpired()) | ||
111 | { | ||
112 | if (mServerPump) | ||
113 | { | ||
114 | mServerPump->pump(); | ||
115 | mServerPump->callback(); | ||
116 | } | ||
117 | if (mClientPump) | ||
118 | { | ||
119 | mClientPump->pump(); | ||
120 | mClientPump->callback(); | ||
121 | } | ||
122 | } | ||
123 | } | ||
124 | |||
125 | void killServer() | ||
126 | { | ||
127 | delete mServerPump; | ||
128 | mServerPump = NULL; | ||
129 | } | ||
130 | |||
131 | private: | ||
132 | apr_pool_t* mPool; | ||
133 | LLPumpIO* mServerPump; | ||
134 | LLPumpIO* mClientPump; | ||
135 | |||
136 | |||
137 | protected: | ||
138 | void ensureStatusOK() | ||
139 | { | ||
140 | if (mSawError) | ||
141 | { | ||
142 | std::string msg = | ||
143 | llformat("error() called when not expected, status %d", | ||
144 | mStatus); | ||
145 | fail(msg); | ||
146 | } | ||
147 | } | ||
148 | |||
149 | void ensureStatusError() | ||
150 | { | ||
151 | if (!mSawError) | ||
152 | { | ||
153 | fail("error() wasn't called"); | ||
154 | } | ||
155 | } | ||
156 | |||
157 | LLSD getResult() | ||
158 | { | ||
159 | return mResult; | ||
160 | } | ||
161 | |||
162 | protected: | ||
163 | bool mSawError; | ||
164 | U32 mStatus; | ||
165 | std::string mReason; | ||
166 | bool mSawCompleted; | ||
167 | LLSD mResult; | ||
168 | bool mResultDeleted; | ||
169 | |||
170 | class Result : public LLHTTPClient::Responder | ||
171 | { | ||
172 | protected: | ||
173 | Result(HTTPClientTestData& client) | ||
174 | : mClient(client) | ||
175 | { | ||
176 | } | ||
177 | |||
178 | public: | ||
179 | static boost::intrusive_ptr<Result> build(HTTPClientTestData& client) | ||
180 | { | ||
181 | return boost::intrusive_ptr<Result>(new Result(client)); | ||
182 | } | ||
183 | |||
184 | ~Result() | ||
185 | { | ||
186 | mClient.mResultDeleted = true; | ||
187 | } | ||
188 | |||
189 | virtual void error(U32 status, const std::string& reason) | ||
190 | { | ||
191 | mClient.mSawError = true; | ||
192 | mClient.mStatus = status; | ||
193 | mClient.mReason = reason; | ||
194 | } | ||
195 | |||
196 | virtual void result(const LLSD& content) | ||
197 | { | ||
198 | mClient.mResult = content; | ||
199 | } | ||
200 | |||
201 | virtual void completed( | ||
202 | U32 status, const std::string& reason, | ||
203 | const LLSD& content) | ||
204 | { | ||
205 | LLHTTPClient::Responder::completed(status, reason, content); | ||
206 | |||
207 | mClient.mSawCompleted = true; | ||
208 | } | ||
209 | |||
210 | private: | ||
211 | HTTPClientTestData& mClient; | ||
212 | }; | ||
213 | |||
214 | friend class Result; | ||
215 | |||
216 | protected: | ||
217 | LLHTTPClient::ResponderPtr newResult() | ||
218 | { | ||
219 | mSawError = false; | ||
220 | mStatus = 0; | ||
221 | mSawCompleted = false; | ||
222 | mResult.clear(); | ||
223 | mResultDeleted = false; | ||
224 | |||
225 | return Result::build(*this); | ||
226 | } | ||
227 | }; | ||
228 | |||
229 | |||
230 | typedef test_group<HTTPClientTestData> HTTPClientTestGroup; | ||
231 | typedef HTTPClientTestGroup::object HTTPClientTestObject; | ||
232 | HTTPClientTestGroup httpClientTestGroup("http_client"); | ||
233 | |||
234 | template<> template<> | ||
235 | void HTTPClientTestObject::test<1>() | ||
236 | { | ||
237 | LLHTTPClient::get("http://www.google.com/", newResult()); | ||
238 | runThePump(); | ||
239 | ensureStatusOK(); | ||
240 | ensure("result object wasn't destroyed", mResultDeleted); | ||
241 | } | ||
242 | |||
243 | template<> template<> | ||
244 | void HTTPClientTestObject::test<2>() | ||
245 | { | ||
246 | LLHTTPClient::get("http://www.invalid", newResult()); | ||
247 | runThePump(); | ||
248 | ensureStatusError(); | ||
249 | } | ||
250 | |||
251 | template<> template<> | ||
252 | void HTTPClientTestObject::test<3>() | ||
253 | { | ||
254 | LLSD sd; | ||
255 | |||
256 | sd["list"][0]["one"] = 1; | ||
257 | sd["list"][0]["two"] = 2; | ||
258 | sd["list"][1]["three"] = 3; | ||
259 | sd["list"][1]["four"] = 4; | ||
260 | |||
261 | setupTheServer(); | ||
262 | |||
263 | LLHTTPClient::post("http://localhost:8888/web/echo", sd, newResult()); | ||
264 | runThePump(); | ||
265 | ensureStatusOK(); | ||
266 | ensure_equals("echoed result matches", getResult(), sd); | ||
267 | } | ||
268 | |||
269 | template<> template<> | ||
270 | void HTTPClientTestObject::test<4>() | ||
271 | { | ||
272 | LLSD sd; | ||
273 | |||
274 | sd["message"] = "This is my test message."; | ||
275 | |||
276 | setupTheServer(); | ||
277 | LLHTTPClient::put("http://localhost:8888/test/storage", sd, newResult()); | ||
278 | runThePump(); | ||
279 | ensureStatusOK(); | ||
280 | |||
281 | LLHTTPClient::get("http://localhost:8888/test/storage", newResult()); | ||
282 | runThePump(); | ||
283 | ensureStatusOK(); | ||
284 | ensure_equals("echoed result matches", getResult(), sd); | ||
285 | |||
286 | } | ||
287 | |||
288 | template<> template<> | ||
289 | void HTTPClientTestObject::test<5>() | ||
290 | { | ||
291 | LLSD sd; | ||
292 | sd["status"] = 543; | ||
293 | sd["reason"] = "error for testing"; | ||
294 | |||
295 | setupTheServer(); | ||
296 | |||
297 | LLHTTPClient::post("http://localhost:8888/test/error", sd, newResult()); | ||
298 | runThePump(); | ||
299 | ensureStatusError(); | ||
300 | ensure_contains("reason", mReason, sd["reason"]); | ||
301 | } | ||
302 | |||
303 | template<> template<> | ||
304 | void HTTPClientTestObject::test<6>() | ||
305 | { | ||
306 | setupTheServer(); | ||
307 | |||
308 | LLHTTPClient::get("http://localhost:8888/test/timeout", newResult()); | ||
309 | runThePump(1.0f); | ||
310 | killServer(); | ||
311 | runThePump(); | ||
312 | ensureStatusError(); | ||
313 | ensure_equals("reason", mReason, "STATUS_ERROR"); | ||
314 | } | ||
315 | } | ||