summaryrefslogtreecommitdiff
path: root/indra/test/lliohttpserver_tut.cpp
blob: e0001438c7b4d5500a918a44dbd8b8c7a6f3a1a9 (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
/** 
 * @file lliohttpserver_tut.cpp
 * @date   May 2006
 * @brief HTTP server unit tests
 *
 * Copyright (c) 2006-$CurrentYear$, Linden Research, Inc.
 * $License$
 */

#include <tut/tut.h>
#include "lltut.h"

#include "llbufferstream.h"
#include "lliohttpserver.h"
#include "llsdhttpserver.h"
#include "llsdserialize.h"

#include "llpipeutil.h"


namespace tut
{
	class HTTPServiceTestData
	{
	public:
		class DelayedEcho : public LLHTTPNode
		{
			HTTPServiceTestData* mTester;

		public:
			DelayedEcho(HTTPServiceTestData* tester) : mTester(tester) { }

			void post(ResponsePtr response, const LLSD& context, const LLSD& input) const
			{
				ensure("response already set", mTester->mResponse == ResponsePtr(NULL));
				mTester->mResponse = response;
				mTester->mResult = input;
			}
		};

		class WireHello : public LLIOPipe
		{
		protected:
			virtual EStatus process_impl(
				const LLChannelDescriptors& channels,
				buffer_ptr_t& buffer,
				bool& eos,
				LLSD& context,
				LLPumpIO* pump)
			{
				if(!eos) return STATUS_BREAK;
				LLSD sd = "yo!";
				LLBufferStream ostr(channels, buffer.get());
				ostr << LLSDXMLStreamer(sd);
				return STATUS_DONE;
			}
		};

		HTTPServiceTestData()
			: mResponse(NULL)
		{
			LLHTTPStandardServices::useServices();
			LLHTTPRegistrar::buildAllServices(mRoot);
			mRoot.addNode("/delayed/echo", new DelayedEcho(this));
			mRoot.addNode("/wire/hello", new LLHTTPNodeForPipe<WireHello>);
		}
		
		LLHTTPNode mRoot;
		LLHTTPNode::ResponsePtr mResponse;
		LLSD mResult;

		void pumpPipe(LLPumpIO* pump, S32 iterations)
		{
			while(iterations > 0)
			{
				pump->pump();
				pump->callback();
				--iterations;
			}
		}

		std::string makeRequest(
			const std::string& name,
			const std::string& httpRequest,
			bool timeout = false)
		{
			LLPipeStringInjector* injector = new LLPipeStringInjector(httpRequest);
			LLPipeStringExtractor* extractor = new LLPipeStringExtractor();
			
			apr_pool_t* pool;
			apr_pool_create(&pool, NULL);

			LLPumpIO* pump;
			pump = new LLPumpIO(pool);

			LLPumpIO::chain_t chain;
			LLSD context;

			chain.push_back(LLIOPipe::ptr_t(injector));
			LLIOHTTPServer::createPipe(chain, mRoot, LLSD());
			chain.push_back(LLIOPipe::ptr_t(extractor));

			pump->addChain(chain, DEFAULT_CHAIN_EXPIRY_SECS);

			pumpPipe(pump, 10);
			if(mResponse && (! timeout)) 
			{
				mResponse->result(mResult);
				mResponse = NULL;
			}
			pumpPipe(pump, 10);
			
			std::string httpResult = extractor->string();

			chain.clear();
			delete pump;
			apr_pool_destroy(pool);

			if(mResponse && timeout)
			{
				mResponse->result(mResult);
				mResponse = NULL;
			}
			
			return httpResult;
		}
		
		std::string httpGET(const std::string& uri, 
							bool timeout = false)
		{
			std::string httpRequest = "GET " + uri + " HTTP/1.0\r\n\r\n";
			return makeRequest(uri, httpRequest, timeout);
		}
		
		std::string httpPOST(const std::string& uri,
			const std::string& body,
			bool timeout,
			const std::string& evilExtra = "")
		{
			std::ostringstream httpRequest;
			httpRequest << "POST " + uri + " HTTP/1.0\r\n";
			httpRequest << "Content-Length: " << body.size() << "\r\n";
			httpRequest << "\r\n";
			httpRequest << body;
			httpRequest << evilExtra;
				
			return makeRequest(uri, httpRequest.str(), timeout);
		}

		std::string httpPOST(const std::string& uri,
			const std::string& body,
			const std::string& evilExtra = "")
		{
			bool timeout = false;
			return httpPOST(uri, body, timeout, evilExtra);
		}
	};

	typedef test_group<HTTPServiceTestData>		HTTPServiceTestGroup;
	typedef HTTPServiceTestGroup::object		HTTPServiceTestObject;
	HTTPServiceTestGroup httpServiceTestGroup("http service");

	template<> template<>
	void HTTPServiceTestObject::test<1>()
	{
		std::string result = httpGET("web/hello");
		
		ensure_starts_with("web/hello status", result,
			"HTTP/1.0 200 OK\r\n");
						
		ensure_contains("web/hello content type", result,
			"Content-Type: application/xml\r\n");

		ensure_contains("web/hello content length", result,
			"Content-Length: 36\r\n");

		ensure_contains("web/hello content", result,
			"\r\n"
			"<llsd><string>hello</string></llsd>"
			);
	}
	
	template<> template<>
	void HTTPServiceTestObject::test<2>()
	{
		// test various HTTP errors
		
		std::string actual;

		actual = httpGET("web/missing");
		ensure_starts_with("web/missing 404", actual,
			"HTTP/1.0 404 Not Found\r\n");

		actual = httpGET("web/echo");			
		ensure_starts_with("web/echo 405", actual,
			"HTTP/1.0 405 Method Not Allowed\r\n");
	}
	
	template<> template<>
	void HTTPServiceTestObject::test<3>()
	{
		// test POST & content-length handling
		
		std::string result;
		
		result = httpPOST("web/echo",
			"<llsd><integer>42</integer></llsd>");
			
		ensure_starts_with("web/echo status", result,
			"HTTP/1.0 200 OK\r\n");
						
		ensure_contains("web/echo content type", result,
			"Content-Type: application/xml\r\n");

		ensure_contains("web/echo content length", result,
			"Content-Length: 35\r\n");

		ensure_contains("web/hello content", result,
			"\r\n"
			"<llsd><integer>42</integer></llsd>"
			);

/* TO DO: this test doesn't pass!!
		
		result = httpPOST("web/echo",
			"<llsd><string>evil</string></llsd>",
			"really!  evil!!!");
			
		ensure_equals("web/echo evil result", result,
			"HTTP/1.0 200 OK\r\n"
			"Content-Length: 34\r\n"
			"\r\n"
			"<llsd><string>evil</string></llsd>"
			);
*/
	}

	template<> template<>
	void HTTPServiceTestObject::test<4>()
	{
		// test calling things based on pipes

		std::string result;

		result = httpGET("wire/hello");

		ensure_contains("wire/hello", result, "yo!");
	}

    template<> template<>
	void HTTPServiceTestObject::test<5>()
    {
		// test timeout before async response
		std::string result;

		bool timeout = true;
		result = httpPOST("delayed/echo",
			"<llsd><string>agent99</string></llsd>", timeout);

		ensure_equals("timeout delayed/echo status", result, std::string(""));
	}

	template<> template<>
	void HTTPServiceTestObject::test<6>()
	{
		// test delayed service		
		std::string result;
		
		result = httpPOST("delayed/echo",
			"<llsd><string>agent99</string></llsd>");
			
		ensure_starts_with("delayed/echo status", result,
			"HTTP/1.0 200 OK\r\n");
						
		ensure_contains("delayed/echo content", result,
			"\r\n"
			"<llsd><string>agent99</string></llsd>"
			);
	}

	template<> template<>
	void HTTPServiceTestObject::test<7>()
	{
		// test large request
		std::stringstream stream;

		//U32 size = 36 * 1024 * 1024;
		//U32 size = 36 * 1024;
		//std::vector<char> data(size);
		//memset(&(data[0]), '1', size);
		//data[size - 1] = '\0';

		
		//std::string result = httpPOST("web/echo", &(data[0]));

		stream << "<llsd><array>";
		for(U32 i = 0; i < 1000000; ++i)
		{
			stream << "<integer>42</integer>";
		}
		stream << "</array></llsd>";
		llinfos << "HTTPServiceTestObject::test<7>"
				<< stream.str().length() << llendl;
		std::string result = httpPOST("web/echo", stream.str());
		ensure_starts_with("large echo status", result, "HTTP/1.0 200 OK\r\n");
	}

	/* TO DO:
		test generation of not found and method not allowed errors
	*/
}