aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/test/mock_http_client.h
diff options
context:
space:
mode:
Diffstat (limited to 'linden/indra/test/mock_http_client.h')
-rw-r--r--linden/indra/test/mock_http_client.h196
1 files changed, 196 insertions, 0 deletions
diff --git a/linden/indra/test/mock_http_client.h b/linden/indra/test/mock_http_client.h
new file mode 100644
index 0000000..c3f1ade
--- /dev/null
+++ b/linden/indra/test/mock_http_client.h
@@ -0,0 +1,196 @@
1/**
2 * @file mock_http_client.cpp
3 * @brief Framework for testing HTTP requests
4 *
5 * $LicenseInfo:firstyear=2007&license=viewergpl$
6 *
7 * Copyright (c) 2007-2008, Linden Research, Inc.
8 *
9 * Second Life Viewer Source Code
10 * The source code in this file ("Source Code") is provided by Linden Lab
11 * to you under the terms of the GNU General Public License, version 2.0
12 * ("GPL"), unless you have obtained a separate licensing agreement
13 * ("Other License"), formally executed by you and Linden Lab. Terms of
14 * the GPL can be found in doc/GPL-license.txt in this distribution, or
15 * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
16 *
17 * There are special exceptions to the terms and conditions of the GPL as
18 * it is applied to this Source Code. View the full text of the exception
19 * in the file doc/FLOSS-exception.txt in this software distribution, or
20 * online at http://secondlifegrid.net/programs/open_source/licensing/flossexception
21 *
22 * By copying, modifying or distributing this software, you acknowledge
23 * that you have read and understood your obligations described above,
24 * and agree to abide by those obligations.
25 *
26 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
27 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
28 * COMPLETENESS OR PERFORMANCE.
29 * $/LicenseInfo$
30 */
31
32#include "llsdhttpserver.h"
33#include "lliohttpserver.h"
34#include "llhttpclient.h"
35#include "llformat.h"
36#include "llpipeutil.h"
37#include "llpumpio.h"
38
39namespace tut
40{
41 struct MockHttpClient
42 {
43 public:
44 MockHttpClient()
45 {
46 apr_pool_create(&mPool, NULL);
47 mServerPump = new LLPumpIO(mPool);
48 mClientPump = new LLPumpIO(mPool);
49
50 LLHTTPClient::setPump(*mClientPump);
51 }
52
53 ~MockHttpClient()
54 {
55 delete mServerPump;
56 delete mClientPump;
57 apr_pool_destroy(mPool);
58 }
59
60 void setupTheServer()
61 {
62 LLHTTPNode& root = LLIOHTTPServer::create(mPool, *mServerPump, 8888);
63
64 LLHTTPStandardServices::useServices();
65 LLHTTPRegistrar::buildAllServices(root);
66 }
67
68 void runThePump(float timeout = 100.0f)
69 {
70 LLTimer timer;
71 timer.setTimerExpirySec(timeout);
72
73 while(!mSawCompleted && !timer.hasExpired())
74 {
75 if (mServerPump)
76 {
77 mServerPump->pump();
78 mServerPump->callback();
79 }
80 if (mClientPump)
81 {
82 mClientPump->pump();
83 mClientPump->callback();
84 }
85 }
86 }
87
88 void killServer()
89 {
90 delete mServerPump;
91 mServerPump = NULL;
92 }
93
94 private:
95 apr_pool_t* mPool;
96 LLPumpIO* mServerPump;
97 LLPumpIO* mClientPump;
98
99
100 protected:
101 void ensureStatusOK()
102 {
103 if (mSawError)
104 {
105 std::string msg =
106 llformat("error() called when not expected, status %d",
107 mStatus);
108 fail(msg);
109 }
110 }
111
112 void ensureStatusError()
113 {
114 if (!mSawError)
115 {
116 fail("error() wasn't called");
117 }
118 }
119
120 LLSD getResult()
121 {
122 return mResult;
123 }
124
125 protected:
126 bool mSawError;
127 U32 mStatus;
128 std::string mReason;
129 bool mSawCompleted;
130 LLSD mResult;
131 bool mResultDeleted;
132
133 class Result : public LLHTTPClient::Responder
134 {
135 protected:
136 Result(MockHttpClient& client)
137 : mClient(client)
138 {
139 }
140
141 public:
142 static boost::intrusive_ptr<Result> build(MockHttpClient& client)
143 {
144 return boost::intrusive_ptr<Result>(new Result(client));
145 }
146
147 ~Result()
148 {
149 mClient.mResultDeleted = true;
150 }
151
152 virtual void error(U32 status, const std::string& reason)
153 {
154 mClient.mSawError = true;
155 mClient.mStatus = status;
156 mClient.mReason = reason;
157 }
158
159 virtual void result(const LLSD& content)
160 {
161 mClient.mResult = content;
162 }
163
164 virtual void completed(
165 U32 status, const std::string& reason,
166 const LLSD& content)
167 {
168 LLHTTPClient::Responder::completed(status, reason, content);
169
170 mClient.mSawCompleted = true;
171 }
172
173 private:
174 MockHttpClient& mClient;
175 };
176
177 friend class Result;
178
179 protected:
180
181 void reset()
182 {
183 mSawError = false;
184 mStatus = 0;
185 mSawCompleted = false;
186 mResult.clear();
187 mResultDeleted = false;
188 }
189
190 LLHTTPClient::ResponderPtr newResult()
191 {
192 reset();
193 return Result::build(*this);
194 }
195 };
196}