/indra/test/mock_http_client.h

https://bitbucket.org/lindenlab/viewer-beta/ · C++ Header · 191 lines · 139 code · 27 blank · 25 comment · 6 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. #include "llsdhttpserver.h"
  27. #include "lliohttpserver.h"
  28. #include "llhttpclient.h"
  29. #include "llformat.h"
  30. #include "llpipeutil.h"
  31. #include "llpumpio.h"
  32. namespace tut
  33. {
  34. struct MockHttpClient
  35. {
  36. public:
  37. MockHttpClient()
  38. {
  39. apr_pool_create(&mPool, NULL);
  40. mServerPump = new LLPumpIO(mPool);
  41. mClientPump = new LLPumpIO(mPool);
  42. LLHTTPClient::setPump(*mClientPump);
  43. }
  44. ~MockHttpClient()
  45. {
  46. delete mServerPump;
  47. delete mClientPump;
  48. apr_pool_destroy(mPool);
  49. }
  50. void setupTheServer()
  51. {
  52. LLHTTPNode& root = LLIOHTTPServer::create(mPool, *mServerPump, 8888);
  53. LLHTTPStandardServices::useServices();
  54. LLHTTPRegistrar::buildAllServices(root);
  55. }
  56. void runThePump(float timeout = 100.0f)
  57. {
  58. LLTimer timer;
  59. timer.setTimerExpirySec(timeout);
  60. while(!mSawCompleted && !timer.hasExpired())
  61. {
  62. if (mServerPump)
  63. {
  64. mServerPump->pump();
  65. mServerPump->callback();
  66. }
  67. if (mClientPump)
  68. {
  69. mClientPump->pump();
  70. mClientPump->callback();
  71. }
  72. }
  73. }
  74. void killServer()
  75. {
  76. delete mServerPump;
  77. mServerPump = NULL;
  78. }
  79. private:
  80. apr_pool_t* mPool;
  81. LLPumpIO* mServerPump;
  82. LLPumpIO* mClientPump;
  83. protected:
  84. void ensureStatusOK()
  85. {
  86. if (mSawError)
  87. {
  88. std::string msg =
  89. llformat("error() called when not expected, status %d",
  90. mStatus);
  91. fail(msg);
  92. }
  93. }
  94. void ensureStatusError()
  95. {
  96. if (!mSawError)
  97. {
  98. fail("error() wasn't called");
  99. }
  100. }
  101. LLSD getResult()
  102. {
  103. return mResult;
  104. }
  105. protected:
  106. bool mSawError;
  107. U32 mStatus;
  108. std::string mReason;
  109. bool mSawCompleted;
  110. LLSD mResult;
  111. bool mResultDeleted;
  112. class Result : public LLHTTPClient::Responder
  113. {
  114. protected:
  115. Result(MockHttpClient& client)
  116. : mClient(client)
  117. {
  118. }
  119. public:
  120. static boost::intrusive_ptr<Result> build(MockHttpClient& client)
  121. {
  122. return boost::intrusive_ptr<Result>(new Result(client));
  123. }
  124. ~Result()
  125. {
  126. mClient.mResultDeleted = true;
  127. }
  128. virtual void error(U32 status, const std::string& reason)
  129. {
  130. mClient.mSawError = true;
  131. mClient.mStatus = status;
  132. mClient.mReason = reason;
  133. }
  134. virtual void result(const LLSD& content)
  135. {
  136. mClient.mResult = content;
  137. }
  138. virtual void completed(
  139. U32 status, const std::string& reason,
  140. const LLSD& content)
  141. {
  142. LLHTTPClient::Responder::completed(status, reason, content);
  143. mClient.mSawCompleted = true;
  144. }
  145. private:
  146. MockHttpClient& mClient;
  147. };
  148. friend class Result;
  149. protected:
  150. void reset()
  151. {
  152. mSawError = false;
  153. mStatus = 0;
  154. mSawCompleted = false;
  155. mResult.clear();
  156. mResultDeleted = false;
  157. }
  158. LLHTTPClient::ResponderPtr newResult()
  159. {
  160. reset();
  161. return Result::build(*this);
  162. }
  163. };
  164. }