aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/test/llhttpnode_tut.cpp
blob: 95c29ac4670043e6395e84e25192a81314e305c2 (plain)
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/** 
 * @file lliohttpnode_tut.cpp
 * @date   May 2006
 * @brief HTTP server unit tests
 *
 * $LicenseInfo:firstyear=2006&license=viewergpl$
 * 
 * Copyright (c) 2006-2008, Linden Research, Inc.
 * 
 * Second Life Viewer Source Code
 * The source code in this file ("Source Code") is provided by Linden Lab
 * to you under the terms of the GNU General Public License, version 2.0
 * ("GPL"), unless you have obtained a separate licensing agreement
 * ("Other License"), formally executed by you and Linden Lab.  Terms of
 * the GPL can be found in doc/GPL-license.txt in this distribution, or
 * online at http://secondlife.com/developers/opensource/gplv2
 * 
 * There are special exceptions to the terms and conditions of the GPL as
 * it is applied to this Source Code. View the full text of the exception
 * in the file doc/FLOSS-exception.txt in this software distribution, or
 * online at http://secondlife.com/developers/opensource/flossexception
 * 
 * By copying, modifying or distributing this software, you acknowledge
 * that you have read and understood your obligations described above,
 * and agree to abide by those obligations.
 * 
 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
 * COMPLETENESS OR PERFORMANCE.
 * $/LicenseInfo$
 */

#include "linden_common.h"
#include "lltut.h"
#include "llhttpnode.h"
#include "llsdhttpserver.h"

namespace tut
{
	struct HTTPNodeTestData
	{
		LLHTTPNode mRoot;
		LLSD mContext;
	
		const LLSD& context() { return mContext; }
		
		std::string remainderPath()
		{
			std::ostringstream pathOutput;
			bool addSlash = false;
			
			LLSD& remainder = mContext["request"]["remainder"];
			for (LLSD::array_const_iterator i = remainder.beginArray();
				i != remainder.endArray();
				++i)
			{
				if (addSlash) { pathOutput << '/'; }
				pathOutput << i->asString();
				addSlash = true;
			}
			
			return pathOutput.str();
		}
		
		void ensureRootTraversal(const std::string& path,
			const LLHTTPNode* expectedNode,
			const char* expectedRemainder)
		{
			mContext.clear();
			
			const LLHTTPNode* actualNode = mRoot.traverse(path, mContext);
			
			ensure_equals("traverse " + path + " node",
				actualNode, expectedNode);
			ensure_equals("traverse " + path + " remainder",
				remainderPath(), expectedRemainder);
		}

		class Response : public LLHTTPNode::Response
		{
		public:
			static LLPointer<Response> create() {return new Response();}

			LLSD mResult;

			void result(const LLSD& result) { mResult = result; }
			void status(S32 code, const std::string& message) { }

		private:
			Response() {;} // Must be accessed through LLPointer.
		};

		typedef LLPointer<Response> ResponsePtr;

		LLSD get(const std::string& path)
		{
			mContext.clear();
			const LLHTTPNode* node = mRoot.traverse(path, mContext);
			ensure(path + " found", node != NULL);

			ResponsePtr response = Response::create();
			node->get(LLHTTPNode::ResponsePtr(response), mContext);
			return response->mResult;
		}
		
		LLSD post(const std::string& path, const LLSD& input)
		{
			mContext.clear();
			const LLHTTPNode* node = mRoot.traverse(path, mContext);
			ensure(path + " found", node != NULL);

			ResponsePtr response = Response::create();
			node->post(LLHTTPNode::ResponsePtr(response), mContext, input);
			return response->mResult;
		}
		
		void ensureMemberString(const std::string& name,
			const LLSD& actualMap, const std::string& member,
			const std::string& expectedValue)
		{
			ensure_equals(name + " " + member,
				actualMap[member].asString(), expectedValue);
		}
		
		
		void ensureInArray(const LLSD& actualArray,
			const std::string& expectedValue)
		{
			LLSD::array_const_iterator i = actualArray.beginArray();
			LLSD::array_const_iterator end = actualArray.endArray();
		
			for (; i != end; ++i)
			{
				std::string path = i->asString();

				if (path == expectedValue)
				{
					return;
				}
			}
			
			fail("didn't find " + expectedValue);
		}
			
	};
	
	typedef test_group<HTTPNodeTestData>	HTTPNodeTestGroup;
	typedef HTTPNodeTestGroup::object		HTTPNodeTestObject;
	HTTPNodeTestGroup httpNodeTestGroup("http node");

	template<> template<>
	void HTTPNodeTestObject::test<1>()
	{
		// traversal of the lone node
		
		ensureRootTraversal("", &mRoot, "");
		ensureRootTraversal("/", &mRoot, "");
		ensureRootTraversal("foo", NULL, "foo");
		ensureRootTraversal("foo/bar", NULL, "foo/bar");
		
		ensure_equals("root of root", mRoot.rootNode(), &mRoot);
	}
	
	template<> template<>
	void HTTPNodeTestObject::test<2>()
	{
		// simple traversal of a single node
		
		LLHTTPNode* helloNode = new LLHTTPNode;
		mRoot.addNode("hello", helloNode);

		ensureRootTraversal("hello", helloNode, "");
		ensureRootTraversal("/hello", helloNode, "");
		ensureRootTraversal("hello/", helloNode, "");
		ensureRootTraversal("/hello/", helloNode, "");

		ensureRootTraversal("hello/there", NULL, "there");
		
		ensure_equals("root of hello", helloNode->rootNode(), &mRoot);
	}

	template<> template<>
	void HTTPNodeTestObject::test<3>()
	{
		// traversal of mutli-branched tree
		
		LLHTTPNode* greekNode = new LLHTTPNode;
		LLHTTPNode* alphaNode = new LLHTTPNode;
		LLHTTPNode* betaNode = new LLHTTPNode;
		LLHTTPNode* gammaNode = new LLHTTPNode;
		
		greekNode->addNode("alpha", alphaNode);
		greekNode->addNode("beta", betaNode);
		greekNode->addNode("gamma", gammaNode);
		mRoot.addNode("greek", greekNode);
		
		LLHTTPNode* hebrewNode = new LLHTTPNode;
		LLHTTPNode* alephNode = new LLHTTPNode;
		
		hebrewNode->addNode("aleph", alephNode);
		mRoot.addNode("hebrew", hebrewNode);

		ensureRootTraversal("greek/alpha", alphaNode, "");
		ensureRootTraversal("greek/beta", betaNode, "");
		ensureRootTraversal("greek/delta", NULL, "delta");
		ensureRootTraversal("greek/gamma", gammaNode, "");
		ensureRootTraversal("hebrew/aleph", alephNode, "");

		ensure_equals("root of greek", greekNode->rootNode(), &mRoot);
		ensure_equals("root of alpha", alphaNode->rootNode(), &mRoot);
		ensure_equals("root of beta", betaNode->rootNode(), &mRoot);
		ensure_equals("root of gamma", gammaNode->rootNode(), &mRoot);
		ensure_equals("root of hebrew", hebrewNode->rootNode(), &mRoot);
		ensure_equals("root of aleph", alephNode->rootNode(), &mRoot);
	}

	template<> template<>
	void HTTPNodeTestObject::test<4>()
	{
		// automatic creation of parent nodes and not overriding existing nodes
		
		LLHTTPNode* alphaNode = new LLHTTPNode;
		LLHTTPNode* betaNode = new LLHTTPNode;
		LLHTTPNode* gammaNode = new LLHTTPNode;
		LLHTTPNode* gamma2Node = new LLHTTPNode;
		
		mRoot.addNode("greek/alpha", alphaNode);
		mRoot.addNode("greek/beta", betaNode);
		
		mRoot.addNode("greek/gamma", gammaNode);
		mRoot.addNode("greek/gamma", gamma2Node);
		
		LLHTTPNode* alephNode = new LLHTTPNode;
		
		mRoot.addNode("hebrew/aleph", alephNode);

		ensureRootTraversal("greek/alpha", alphaNode, "");
		ensureRootTraversal("greek/beta", betaNode, "");
		ensureRootTraversal("greek/delta", NULL, "delta");
		ensureRootTraversal("greek/gamma", gammaNode, "");
		ensureRootTraversal("hebrew/aleph", alephNode, "");

		ensure_equals("root of alpha", alphaNode->rootNode(), &mRoot);
		ensure_equals("root of beta", betaNode->rootNode(), &mRoot);
		ensure_equals("root of gamma", gammaNode->rootNode(), &mRoot);
		ensure_equals("root of aleph", alephNode->rootNode(), &mRoot);
	}
	
	class IntegerNode : public LLHTTPNode
	{
	public:
		virtual void get(ResponsePtr response, const LLSD& context) const
		{
			int n = context["extra"]["value"];
			
			LLSD info;
			info["value"] = n;
			info["positive"] = n > 0;
			info["zero"] = n == 0;
			info["negative"] = n < 0;
		
			response->result(info);
		}
		
		virtual bool validate(const std::string& name, LLSD& context) const
		{
			int n;
			std::istringstream i_stream(name);
			i_stream >> n;

			if (i_stream.fail()  ||  i_stream.get() != EOF)
			{
				return false;
			}

			context["extra"]["value"] = n;
			return true;
		}
	};
	
	class SquareNode : public LLHTTPNode
	{
	public:
		virtual void get(ResponsePtr response, const LLSD& context) const
		{
			int n = context["extra"]["value"];		
			response->result(n*n);
		}
	};
	
	template<> template<>
	void HTTPNodeTestObject::test<5>()
	{
		// wildcard nodes
		
		LLHTTPNode* miscNode = new LLHTTPNode;
		LLHTTPNode* iNode = new IntegerNode;
		LLHTTPNode* sqNode = new SquareNode;
		
		mRoot.addNode("test/misc", miscNode);
		mRoot.addNode("test/<int>", iNode);
		mRoot.addNode("test/<int>/square", sqNode);
		
		ensureRootTraversal("test/42", iNode, "");
		ensure_equals("stored integer",
			context()["extra"]["value"].asInteger(), 42);
		
		ensureRootTraversal("test/bob", NULL, "bob");
		ensure("nothing stored",
			context()["extra"]["value"].isUndefined());
			
		ensureRootTraversal("test/3/square", sqNode, "");
		ResponsePtr response = Response::create();
		sqNode->get(LLHTTPNode::ResponsePtr(response), context());
		ensure_equals("square result", response->mResult.asInteger(), 9);
	}
	
	class AlphaNode : public LLHTTPNode
	{
	public:
		virtual bool handles(const LLSD& remainder, LLSD& context) const
		{
			LLSD::array_const_iterator i = remainder.beginArray();
			LLSD::array_const_iterator end = remainder.endArray();
			
			for (; i != end; ++i)
			{
				std::string s = i->asString();
				if (s.empty() || s[0] != 'a')
				{
					return false;
				}
			}
			
			return true;
		}
	};
	
	template<> template<>
	void HTTPNodeTestObject::test<6>()
	{
		// nodes that handle remainders
		
		LLHTTPNode* miscNode = new LLHTTPNode;
		LLHTTPNode* aNode = new AlphaNode;
		LLHTTPNode* zNode = new LLHTTPNode;
		
		mRoot.addNode("test/misc", miscNode);
		mRoot.addNode("test/alpha", aNode);
		mRoot.addNode("test/alpha/zebra", zNode);
		
		ensureRootTraversal("test/alpha", aNode, "");
		ensureRootTraversal("test/alpha/abe", aNode, "abe");
		ensureRootTraversal("test/alpha/abe/amy", aNode, "abe/amy");
		ensureRootTraversal("test/alpha/abe/bea", NULL, "abe/bea");
		ensureRootTraversal("test/alpha/bob", NULL, "bob");
		ensureRootTraversal("test/alpha/zebra", zNode, "");
	}
	
	template<> template<>
	void HTTPNodeTestObject::test<7>()
	{
		// test auto registration
		
		LLHTTPStandardServices::useServices();
		LLHTTPRegistrar::buildAllServices(mRoot);
		
		{
			LLSD result = get("web/hello");
			ensure_equals("hello result", result.asString(), "hello");
		}
		{
			LLSD stuff = 3.14159;
			LLSD result = post("web/echo", stuff);
			ensure_equals("echo result", result, stuff);
		}
	}

	template<> template<>
	void HTTPNodeTestObject::test<8>()
	{
		// test introspection
		
		LLHTTPRegistrar::buildAllServices(mRoot);
		
		mRoot.addNode("test/misc", new LLHTTPNode);
		mRoot.addNode("test/<int>", new IntegerNode);
		mRoot.addNode("test/<int>/square", new SquareNode);

		const LLSD result = get("web/server/api");
		
		ensure("result is array", result.isArray());
		ensure("result size", result.size() >= 2);
		
		ensureInArray(result, "web/echo");
		ensureInArray(result, "web/hello");
		ensureInArray(result, "test/misc");
		ensureInArray(result, "test/<int>");
		ensureInArray(result, "test/<int>/square");
	}
	
	template<> template<>
	void HTTPNodeTestObject::test<9>()
	{
		// test introspection details

		LLHTTPRegistrar::buildAllServices(mRoot);

		const LLSD helloDetails = get("web/server/api/web/hello");
		
		ensure_contains("hello description",
			helloDetails["description"].asString(), "hello");
		ensure_equals("method name", helloDetails["api"][0].asString(), std::string("GET"));
		ensureMemberString("hello", helloDetails, "output", "\"hello\"");
		ensure_contains("hello __file__",
			helloDetails["__file__"].asString(), "llsdhttpserver.cpp");
		ensure("hello line", helloDetails["__line__"].isInteger());


		const LLSD echoDetails = get("web/server/api/web/echo");
		
		ensure_contains("echo description",
			echoDetails["description"].asString(), "echo");
		ensure_equals("method name", echoDetails["api"][0].asString(), std::string("POST"));
		ensureMemberString("echo", echoDetails, "input", "<any>");
		ensureMemberString("echo", echoDetails, "output", "<the input>");
		ensure_contains("echo __file__",
			echoDetails["__file__"].asString(), "llsdhttpserver.cpp");
		ensure("echo", echoDetails["__line__"].isInteger());
	}
}