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/lliohttpserver_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 '')
-rw-r--r-- | linden/indra/test/lliohttpserver_tut.cpp | 303 |
1 files changed, 303 insertions, 0 deletions
diff --git a/linden/indra/test/lliohttpserver_tut.cpp b/linden/indra/test/lliohttpserver_tut.cpp new file mode 100644 index 0000000..1cc94bb --- /dev/null +++ b/linden/indra/test/lliohttpserver_tut.cpp | |||
@@ -0,0 +1,303 @@ | |||
1 | /** | ||
2 | * @file lliohttpserver_tut.cpp | ||
3 | * @date May 2006 | ||
4 | * @brief HTTP server unit tests | ||
5 | * | ||
6 | * Copyright (c) 2006-2007, Linden Research, Inc. | ||
7 | * | ||
8 | * The source code in this file ("Source Code") is provided by Linden Lab | ||
9 | * to you under the terms of the GNU General Public License, version 2.0 | ||
10 | * ("GPL"), unless you have obtained a separate licensing agreement | ||
11 | * ("Other License"), formally executed by you and Linden Lab. Terms of | ||
12 | * the GPL can be found in doc/GPL-license.txt in this distribution, or | ||
13 | * online at http://secondlife.com/developers/opensource/gplv2 | ||
14 | * | ||
15 | * There are special exceptions to the terms and conditions of the GPL as | ||
16 | * it is applied to this Source Code. View the full text of the exception | ||
17 | * in the file doc/FLOSS-exception.txt in this software distribution, or | ||
18 | * online at http://secondlife.com/developers/opensource/flossexception | ||
19 | * | ||
20 | * By copying, modifying or distributing this software, you acknowledge | ||
21 | * that you have read and understood your obligations described above, | ||
22 | * and agree to abide by those obligations. | ||
23 | * | ||
24 | * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO | ||
25 | * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY, | ||
26 | * COMPLETENESS OR PERFORMANCE. | ||
27 | */ | ||
28 | |||
29 | #include <tut/tut.h> | ||
30 | #include "lltut.h" | ||
31 | |||
32 | #include "llbufferstream.h" | ||
33 | #include "lliohttpserver.h" | ||
34 | #include "llsdhttpserver.h" | ||
35 | #include "llsdserialize.h" | ||
36 | |||
37 | #include "llpipeutil.h" | ||
38 | |||
39 | |||
40 | namespace tut | ||
41 | { | ||
42 | class HTTPServiceTestData | ||
43 | { | ||
44 | public: | ||
45 | class DelayedEcho : public LLHTTPNode | ||
46 | { | ||
47 | HTTPServiceTestData* mTester; | ||
48 | |||
49 | public: | ||
50 | DelayedEcho(HTTPServiceTestData* tester) : mTester(tester) { } | ||
51 | |||
52 | void post(ResponsePtr response, const LLSD& context, const LLSD& input) const | ||
53 | { | ||
54 | ensure("response already set", mTester->mResponse == ResponsePtr(NULL)); | ||
55 | mTester->mResponse = response; | ||
56 | mTester->mResult = input; | ||
57 | } | ||
58 | }; | ||
59 | |||
60 | class WireHello : public LLIOPipe | ||
61 | { | ||
62 | protected: | ||
63 | virtual EStatus process_impl( | ||
64 | const LLChannelDescriptors& channels, | ||
65 | buffer_ptr_t& buffer, | ||
66 | bool& eos, | ||
67 | LLSD& context, | ||
68 | LLPumpIO* pump) | ||
69 | { | ||
70 | if(!eos) return STATUS_BREAK; | ||
71 | LLSD sd = "yo!"; | ||
72 | LLBufferStream ostr(channels, buffer.get()); | ||
73 | ostr << LLSDXMLStreamer(sd); | ||
74 | return STATUS_DONE; | ||
75 | } | ||
76 | }; | ||
77 | |||
78 | HTTPServiceTestData() | ||
79 | : mResponse(NULL) | ||
80 | { | ||
81 | LLHTTPStandardServices::useServices(); | ||
82 | LLHTTPRegistrar::buildAllServices(mRoot); | ||
83 | mRoot.addNode("/delayed/echo", new DelayedEcho(this)); | ||
84 | mRoot.addNode("/wire/hello", new LLHTTPNodeForPipe<WireHello>); | ||
85 | } | ||
86 | |||
87 | LLHTTPNode mRoot; | ||
88 | LLHTTPNode::ResponsePtr mResponse; | ||
89 | LLSD mResult; | ||
90 | |||
91 | void pumpPipe(LLPumpIO* pump, S32 iterations) | ||
92 | { | ||
93 | while(iterations > 0) | ||
94 | { | ||
95 | pump->pump(); | ||
96 | pump->callback(); | ||
97 | --iterations; | ||
98 | } | ||
99 | } | ||
100 | |||
101 | std::string makeRequest( | ||
102 | const std::string& name, | ||
103 | const std::string& httpRequest, | ||
104 | bool timeout = false) | ||
105 | { | ||
106 | LLPipeStringInjector* injector = new LLPipeStringInjector(httpRequest); | ||
107 | LLPipeStringExtractor* extractor = new LLPipeStringExtractor(); | ||
108 | |||
109 | apr_pool_t* pool; | ||
110 | apr_pool_create(&pool, NULL); | ||
111 | |||
112 | LLPumpIO* pump; | ||
113 | pump = new LLPumpIO(pool); | ||
114 | |||
115 | LLPumpIO::chain_t chain; | ||
116 | LLSD context; | ||
117 | |||
118 | chain.push_back(LLIOPipe::ptr_t(injector)); | ||
119 | LLCreateHTTPPipe(chain, mRoot); | ||
120 | chain.push_back(LLIOPipe::ptr_t(extractor)); | ||
121 | |||
122 | pump->addChain(chain, DEFAULT_CHAIN_EXPIRY_SECS); | ||
123 | |||
124 | pumpPipe(pump, 10); | ||
125 | if(mResponse && (! timeout)) | ||
126 | { | ||
127 | mResponse->result(mResult); | ||
128 | mResponse = NULL; | ||
129 | } | ||
130 | pumpPipe(pump, 10); | ||
131 | |||
132 | std::string httpResult = extractor->string(); | ||
133 | |||
134 | chain.clear(); | ||
135 | delete pump; | ||
136 | apr_pool_destroy(pool); | ||
137 | |||
138 | if(mResponse && timeout) | ||
139 | { | ||
140 | mResponse->result(mResult); | ||
141 | mResponse = NULL; | ||
142 | } | ||
143 | |||
144 | return httpResult; | ||
145 | } | ||
146 | |||
147 | std::string httpGET(const std::string& uri, | ||
148 | bool timeout = false) | ||
149 | { | ||
150 | std::string httpRequest = "GET " + uri + " HTTP/1.0\r\n\r\n"; | ||
151 | return makeRequest(uri, httpRequest, timeout); | ||
152 | } | ||
153 | |||
154 | std::string httpPOST(const std::string& uri, | ||
155 | const std::string& body, | ||
156 | bool timeout, | ||
157 | const std::string& evilExtra = "") | ||
158 | { | ||
159 | std::ostringstream httpRequest; | ||
160 | httpRequest << "POST " + uri + " HTTP/1.0\r\n"; | ||
161 | httpRequest << "Content-Length: " << body.size() << "\r\n"; | ||
162 | httpRequest << "\r\n"; | ||
163 | httpRequest << body; | ||
164 | httpRequest << evilExtra; | ||
165 | |||
166 | return makeRequest(uri, httpRequest.str(), timeout); | ||
167 | } | ||
168 | |||
169 | std::string httpPOST(const std::string& uri, | ||
170 | const std::string& body, | ||
171 | const std::string& evilExtra = "") | ||
172 | { | ||
173 | bool timeout = false; | ||
174 | return httpPOST(uri, body, timeout, evilExtra); | ||
175 | } | ||
176 | }; | ||
177 | |||
178 | typedef test_group<HTTPServiceTestData> HTTPServiceTestGroup; | ||
179 | typedef HTTPServiceTestGroup::object HTTPServiceTestObject; | ||
180 | HTTPServiceTestGroup httpServiceTestGroup("http service"); | ||
181 | |||
182 | template<> template<> | ||
183 | void HTTPServiceTestObject::test<1>() | ||
184 | { | ||
185 | std::string result = httpGET("web/hello"); | ||
186 | |||
187 | ensure_starts_with("web/hello status", result, | ||
188 | "HTTP/1.0 200 OK\r\n"); | ||
189 | |||
190 | ensure_contains("web/hello content type", result, | ||
191 | "Content-Type: application/xml\r\n"); | ||
192 | |||
193 | ensure_contains("web/hello content length", result, | ||
194 | "Content-Length: 36\r\n"); | ||
195 | |||
196 | ensure_contains("web/hello content", result, | ||
197 | "\r\n" | ||
198 | "<llsd><string>hello</string></llsd>" | ||
199 | ); | ||
200 | } | ||
201 | |||
202 | template<> template<> | ||
203 | void HTTPServiceTestObject::test<2>() | ||
204 | { | ||
205 | // test various HTTP errors | ||
206 | |||
207 | std::string actual; | ||
208 | |||
209 | actual = httpGET("web/missing"); | ||
210 | ensure_starts_with("web/missing 404", actual, | ||
211 | "HTTP/1.0 404 Not Found\r\n"); | ||
212 | |||
213 | actual = httpGET("web/echo"); | ||
214 | ensure_starts_with("web/echo 405", actual, | ||
215 | "HTTP/1.0 405 Method Not Allowed\r\n"); | ||
216 | } | ||
217 | |||
218 | template<> template<> | ||
219 | void HTTPServiceTestObject::test<3>() | ||
220 | { | ||
221 | // test POST & content-length handling | ||
222 | |||
223 | std::string result; | ||
224 | |||
225 | result = httpPOST("web/echo", | ||
226 | "<llsd><integer>42</integer></llsd>"); | ||
227 | |||
228 | ensure_starts_with("web/echo status", result, | ||
229 | "HTTP/1.0 200 OK\r\n"); | ||
230 | |||
231 | ensure_contains("web/echo content type", result, | ||
232 | "Content-Type: application/xml\r\n"); | ||
233 | |||
234 | ensure_contains("web/echo content length", result, | ||
235 | "Content-Length: 35\r\n"); | ||
236 | |||
237 | ensure_contains("web/hello content", result, | ||
238 | "\r\n" | ||
239 | "<llsd><integer>42</integer></llsd>" | ||
240 | ); | ||
241 | |||
242 | /* TO DO: this test doesn't pass!! | ||
243 | |||
244 | result = httpPOST("web/echo", | ||
245 | "<llsd><string>evil</string></llsd>", | ||
246 | "really! evil!!!"); | ||
247 | |||
248 | ensure_equals("web/echo evil result", result, | ||
249 | "HTTP/1.0 200 OK\r\n" | ||
250 | "Content-Length: 34\r\n" | ||
251 | "\r\n" | ||
252 | "<llsd><string>evil</string></llsd>" | ||
253 | ); | ||
254 | */ | ||
255 | } | ||
256 | |||
257 | template<> template<> | ||
258 | void HTTPServiceTestObject::test<4>() | ||
259 | { | ||
260 | // test calling things based on pipes | ||
261 | |||
262 | std::string result; | ||
263 | |||
264 | result = httpGET("wire/hello"); | ||
265 | |||
266 | ensure_contains("wire/hello", result, "yo!"); | ||
267 | } | ||
268 | |||
269 | template<> template<> | ||
270 | void HTTPServiceTestObject::test<5>() | ||
271 | { | ||
272 | // test timeout before async response | ||
273 | std::string result; | ||
274 | |||
275 | bool timeout = true; | ||
276 | result = httpPOST("delayed/echo", | ||
277 | "<llsd><string>agent99</string></llsd>", timeout); | ||
278 | |||
279 | ensure_equals("timeout delayed/echo status", result, std::string("")); | ||
280 | } | ||
281 | |||
282 | template<> template<> | ||
283 | void HTTPServiceTestObject::test<6>() | ||
284 | { | ||
285 | // test delayed service | ||
286 | std::string result; | ||
287 | |||
288 | result = httpPOST("delayed/echo", | ||
289 | "<llsd><string>agent99</string></llsd>"); | ||
290 | |||
291 | ensure_starts_with("delayed/echo status", result, | ||
292 | "HTTP/1.0 200 OK\r\n"); | ||
293 | |||
294 | ensure_contains("delayed/echo content", result, | ||
295 | "\r\n" | ||
296 | "<llsd><string>agent99</string></llsd>" | ||
297 | ); | ||
298 | } | ||
299 | |||
300 | /* TO DO: | ||
301 | test generation of not found and method not allowed errors | ||
302 | */ | ||
303 | } | ||