PageRenderTime 29ms CodeModel.GetById 12ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/test/mock_http_client.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 191 lines | 139 code | 27 blank | 25 comment | 5 complexity | cf725dbb4e0794491b7725c73f19fa49 MD5 | raw file
  1/** 
  2 * @file mock_http_client.cpp
  3 * @brief Framework for testing HTTP requests
  4 *
  5 * $LicenseInfo:firstyear=2007&license=viewerlgpl$
  6 * Second Life Viewer Source Code
  7 * Copyright (C) 2010, Linden Research, Inc.
  8 * 
  9 * This library is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU Lesser General Public
 11 * License as published by the Free Software Foundation;
 12 * version 2.1 of the License only.
 13 * 
 14 * This library is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 17 * Lesser General Public License for more details.
 18 * 
 19 * You should have received a copy of the GNU Lesser General Public
 20 * License along with this library; if not, write to the Free Software
 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 22 * 
 23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 24 * $/LicenseInfo$
 25 */
 26
 27#include "llsdhttpserver.h"
 28#include "lliohttpserver.h"
 29#include "llhttpclient.h"
 30#include "llformat.h"
 31#include "llpipeutil.h"
 32#include "llpumpio.h"
 33
 34namespace tut
 35{
 36	struct MockHttpClient
 37	{
 38	public:
 39		MockHttpClient()
 40		{
 41			apr_pool_create(&mPool, NULL);
 42			mServerPump = new LLPumpIO(mPool);
 43			mClientPump = new LLPumpIO(mPool);
 44			
 45			LLHTTPClient::setPump(*mClientPump);
 46		}
 47		
 48		~MockHttpClient()
 49		{
 50			delete mServerPump;
 51			delete mClientPump;
 52			apr_pool_destroy(mPool);
 53		}
 54
 55		void setupTheServer()
 56		{
 57			LLHTTPNode& root = LLIOHTTPServer::create(mPool, *mServerPump, 8888);
 58
 59			LLHTTPStandardServices::useServices();
 60			LLHTTPRegistrar::buildAllServices(root);
 61		}
 62		
 63		void runThePump(float timeout = 100.0f)
 64		{
 65			LLTimer timer;
 66			timer.setTimerExpirySec(timeout);
 67
 68			while(!mSawCompleted && !timer.hasExpired())
 69			{
 70				if (mServerPump)
 71				{
 72					mServerPump->pump();
 73					mServerPump->callback();
 74				}
 75				if (mClientPump)
 76				{
 77					mClientPump->pump();
 78					mClientPump->callback();
 79				}
 80			}
 81		}
 82
 83		void killServer()
 84		{
 85			delete mServerPump;
 86			mServerPump = NULL;
 87		}
 88	
 89	private:
 90		apr_pool_t* mPool;
 91		LLPumpIO* mServerPump;
 92		LLPumpIO* mClientPump;
 93
 94		
 95	protected:
 96		void ensureStatusOK()
 97		{
 98			if (mSawError)
 99			{
100				std::string msg =
101					llformat("error() called when not expected, status %d",
102						mStatus); 
103				fail(msg);
104			}
105		}
106	
107		void ensureStatusError()
108		{
109			if (!mSawError)
110			{
111				fail("error() wasn't called");
112			}
113		}
114		
115		LLSD getResult()
116		{
117			return mResult;
118		}
119	
120	protected:
121		bool mSawError;
122		U32 mStatus;
123		std::string mReason;
124		bool mSawCompleted;
125		LLSD mResult;
126		bool mResultDeleted;
127
128		class Result : public LLHTTPClient::Responder
129		{
130		protected:
131			Result(MockHttpClient& client)
132				: mClient(client)
133			{
134			}
135		
136		public:
137			static boost::intrusive_ptr<Result> build(MockHttpClient& client)
138			{
139				return boost::intrusive_ptr<Result>(new Result(client));
140			}
141			
142			~Result()
143			{
144				mClient.mResultDeleted = true;
145			}
146			
147			virtual void error(U32 status, const std::string& reason)
148			{
149				mClient.mSawError = true;
150				mClient.mStatus = status;
151				mClient.mReason = reason;
152			}
153
154			virtual void result(const LLSD& content)
155			{
156				mClient.mResult = content;
157			}
158
159			virtual void completed(
160							U32 status, const std::string& reason,
161							const LLSD& content)
162			{
163				LLHTTPClient::Responder::completed(status, reason, content);
164				
165				mClient.mSawCompleted = true;
166			}
167
168		private:
169			MockHttpClient& mClient;
170		};
171
172		friend class Result;
173
174	protected:
175
176		void reset()
177		{
178			mSawError = false;
179			mStatus = 0;
180			mSawCompleted = false;
181			mResult.clear();
182			mResultDeleted = false;
183		}
184
185		LLHTTPClient::ResponderPtr newResult()
186		{
187			reset();
188			return Result::build(*this);
189		}
190	};
191}