/atom/mock_http_core.py

http://radioappz.googlecode.com/ · Python · 323 lines · 209 code · 35 blank · 79 comment · 29 complexity · 209522f565186e6c24e3bfc1239aa752 MD5 · raw file

  1. #!/usr/bin/env python
  2. #
  3. # Copyright (C) 2009 Google Inc.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. # This module is used for version 2 of the Google Data APIs.
  17. __author__ = 'j.s@google.com (Jeff Scudder)'
  18. import StringIO
  19. import pickle
  20. import os.path
  21. import tempfile
  22. import atom.http_core
  23. class Error(Exception):
  24. pass
  25. class NoRecordingFound(Error):
  26. pass
  27. class MockHttpClient(object):
  28. debug = None
  29. real_client = None
  30. last_request_was_live = False
  31. # The following members are used to construct the session cache temp file
  32. # name.
  33. # These are combined to form the file name
  34. # /tmp/cache_prefix.cache_case_name.cache_test_name
  35. cache_name_prefix = 'gdata_live_test'
  36. cache_case_name = ''
  37. cache_test_name = ''
  38. def __init__(self, recordings=None, real_client=None):
  39. self._recordings = recordings or []
  40. if real_client is not None:
  41. self.real_client = real_client
  42. def add_response(self, http_request, status, reason, headers=None,
  43. body=None):
  44. response = MockHttpResponse(status, reason, headers, body)
  45. # TODO Scrub the request and the response.
  46. self._recordings.append((http_request._copy(), response))
  47. AddResponse = add_response
  48. def request(self, http_request):
  49. """Provide a recorded response, or record a response for replay.
  50. If the real_client is set, the request will be made using the
  51. real_client, and the response from the server will be recorded.
  52. If the real_client is None (the default), this method will examine
  53. the recordings and find the first which matches.
  54. """
  55. request = http_request._copy()
  56. _scrub_request(request)
  57. if self.real_client is None:
  58. self.last_request_was_live = False
  59. for recording in self._recordings:
  60. if _match_request(recording[0], request):
  61. return recording[1]
  62. else:
  63. # Pass along the debug settings to the real client.
  64. self.real_client.debug = self.debug
  65. # Make an actual request since we can use the real HTTP client.
  66. self.last_request_was_live = True
  67. response = self.real_client.request(http_request)
  68. scrubbed_response = _scrub_response(response)
  69. self.add_response(request, scrubbed_response.status,
  70. scrubbed_response.reason,
  71. dict(atom.http_core.get_headers(scrubbed_response)),
  72. scrubbed_response.read())
  73. # Return the recording which we just added.
  74. return self._recordings[-1][1]
  75. raise NoRecordingFound('No recoding was found for request: %s %s' % (
  76. request.method, str(request.uri)))
  77. Request = request
  78. def _save_recordings(self, filename):
  79. recording_file = open(os.path.join(tempfile.gettempdir(), filename),
  80. 'wb')
  81. pickle.dump(self._recordings, recording_file)
  82. recording_file.close()
  83. def _load_recordings(self, filename):
  84. recording_file = open(os.path.join(tempfile.gettempdir(), filename),
  85. 'rb')
  86. self._recordings = pickle.load(recording_file)
  87. recording_file.close()
  88. def _delete_recordings(self, filename):
  89. full_path = os.path.join(tempfile.gettempdir(), filename)
  90. if os.path.exists(full_path):
  91. os.remove(full_path)
  92. def _load_or_use_client(self, filename, http_client):
  93. if os.path.exists(os.path.join(tempfile.gettempdir(), filename)):
  94. self._load_recordings(filename)
  95. else:
  96. self.real_client = http_client
  97. def use_cached_session(self, name=None, real_http_client=None):
  98. """Attempts to load recordings from a previous live request.
  99. If a temp file with the recordings exists, then it is used to fulfill
  100. requests. If the file does not exist, then a real client is used to
  101. actually make the desired HTTP requests. Requests and responses are
  102. recorded and will be written to the desired temprary cache file when
  103. close_session is called.
  104. Args:
  105. name: str (optional) The file name of session file to be used. The file
  106. is loaded from the temporary directory of this machine. If no name
  107. is passed in, a default name will be constructed using the
  108. cache_name_prefix, cache_case_name, and cache_test_name of this
  109. object.
  110. real_http_client: atom.http_core.HttpClient the real client to be used
  111. if the cached recordings are not found. If the default
  112. value is used, this will be an
  113. atom.http_core.HttpClient.
  114. """
  115. if real_http_client is None:
  116. real_http_client = atom.http_core.HttpClient()
  117. if name is None:
  118. self._recordings_cache_name = self.get_cache_file_name()
  119. else:
  120. self._recordings_cache_name = name
  121. self._load_or_use_client(self._recordings_cache_name, real_http_client)
  122. def close_session(self):
  123. """Saves recordings in the temporary file named in use_cached_session."""
  124. if self.real_client is not None:
  125. self._save_recordings(self._recordings_cache_name)
  126. def delete_session(self, name=None):
  127. """Removes recordings from a previous live request."""
  128. if name is None:
  129. self._delete_recordings(self._recordings_cache_name)
  130. else:
  131. self._delete_recordings(name)
  132. def get_cache_file_name(self):
  133. return '%s.%s.%s' % (self.cache_name_prefix, self.cache_case_name,
  134. self.cache_test_name)
  135. def _dump(self):
  136. """Provides debug information in a string."""
  137. output = 'MockHttpClient\n real_client: %s\n cache file name: %s\n' % (
  138. self.real_client, self.get_cache_file_name())
  139. output += ' recordings:\n'
  140. i = 0
  141. for recording in self._recordings:
  142. output += ' recording %i is for: %s %s\n' % (
  143. i, recording[0].method, str(recording[0].uri))
  144. i += 1
  145. return output
  146. def _match_request(http_request, stored_request):
  147. """Determines whether a request is similar enough to a stored request
  148. to cause the stored response to be returned."""
  149. # Check to see if the host names match.
  150. if (http_request.uri.host is not None
  151. and http_request.uri.host != stored_request.uri.host):
  152. return False
  153. # Check the request path in the URL (/feeds/private/full/x)
  154. elif http_request.uri.path != stored_request.uri.path:
  155. return False
  156. # Check the method used in the request (GET, POST, etc.)
  157. elif http_request.method != stored_request.method:
  158. return False
  159. # If there is a gsession ID in either request, make sure that it is matched
  160. # exactly.
  161. elif ('gsessionid' in http_request.uri.query
  162. or 'gsessionid' in stored_request.uri.query):
  163. if 'gsessionid' not in stored_request.uri.query:
  164. return False
  165. elif 'gsessionid' not in http_request.uri.query:
  166. return False
  167. elif (http_request.uri.query['gsessionid']
  168. != stored_request.uri.query['gsessionid']):
  169. return False
  170. # Ignores differences in the query params (?start-index=5&max-results=20),
  171. # the body of the request, the port number, HTTP headers, just to name a
  172. # few.
  173. return True
  174. def _scrub_request(http_request):
  175. """ Removes email address and password from a client login request.
  176. Since the mock server saves the request and response in plantext, sensitive
  177. information like the password should be removed before saving the
  178. recordings. At the moment only requests sent to a ClientLogin url are
  179. scrubbed.
  180. """
  181. if (http_request and http_request.uri and http_request.uri.path and
  182. http_request.uri.path.endswith('ClientLogin')):
  183. # Remove the email and password from a ClientLogin request.
  184. http_request._body_parts = []
  185. http_request.add_form_inputs(
  186. {'form_data': 'client login request has been scrubbed'})
  187. else:
  188. # We can remove the body of the post from the recorded request, since
  189. # the request body is not used when finding a matching recording.
  190. http_request._body_parts = []
  191. return http_request
  192. def _scrub_response(http_response):
  193. return http_response
  194. class EchoHttpClient(object):
  195. """Sends the request data back in the response.
  196. Used to check the formatting of the request as it was sent. Always responds
  197. with a 200 OK, and some information from the HTTP request is returned in
  198. special Echo-X headers in the response. The following headers are added
  199. in the response:
  200. 'Echo-Host': The host name and port number to which the HTTP connection is
  201. made. If no port was passed in, the header will contain
  202. host:None.
  203. 'Echo-Uri': The path portion of the URL being requested. /example?x=1&y=2
  204. 'Echo-Scheme': The beginning of the URL, usually 'http' or 'https'
  205. 'Echo-Method': The HTTP method being used, 'GET', 'POST', 'PUT', etc.
  206. """
  207. def request(self, http_request):
  208. return self._http_request(http_request.uri, http_request.method,
  209. http_request.headers, http_request._body_parts)
  210. def _http_request(self, uri, method, headers=None, body_parts=None):
  211. body = StringIO.StringIO()
  212. response = atom.http_core.HttpResponse(status=200, reason='OK', body=body)
  213. if headers is None:
  214. response._headers = {}
  215. else:
  216. # Copy headers from the request to the response but convert values to
  217. # strings. Server response headers always come in as strings, so an int
  218. # should be converted to a corresponding string when echoing.
  219. for header, value in headers.iteritems():
  220. response._headers[header] = str(value)
  221. response._headers['Echo-Host'] = '%s:%s' % (uri.host, str(uri.port))
  222. response._headers['Echo-Uri'] = uri._get_relative_path()
  223. response._headers['Echo-Scheme'] = uri.scheme
  224. response._headers['Echo-Method'] = method
  225. for part in body_parts:
  226. if isinstance(part, str):
  227. body.write(part)
  228. elif hasattr(part, 'read'):
  229. body.write(part.read())
  230. body.seek(0)
  231. return response
  232. class SettableHttpClient(object):
  233. """An HTTP Client which responds with the data given in set_response."""
  234. def __init__(self, status, reason, body, headers):
  235. """Configures the response for the server.
  236. See set_response for details on the arguments to the constructor.
  237. """
  238. self.set_response(status, reason, body, headers)
  239. self.last_request = None
  240. def set_response(self, status, reason, body, headers):
  241. """Determines the response which will be sent for each request.
  242. Args:
  243. status: An int for the HTTP status code, example: 200, 404, etc.
  244. reason: String for the HTTP reason, example: OK, NOT FOUND, etc.
  245. body: The body of the HTTP response as a string or a file-like
  246. object (something with a read method).
  247. headers: dict of strings containing the HTTP headers in the response.
  248. """
  249. self.response = atom.http_core.HttpResponse(status=status, reason=reason,
  250. body=body)
  251. self.response._headers = headers.copy()
  252. def request(self, http_request):
  253. self.last_request = http_request
  254. return self.response
  255. class MockHttpResponse(atom.http_core.HttpResponse):
  256. def __init__(self, status=None, reason=None, headers=None, body=None):
  257. self._headers = headers or {}
  258. if status is not None:
  259. self.status = status
  260. if reason is not None:
  261. self.reason = reason
  262. if body is not None:
  263. # Instead of using a file-like object for the body, store as a string
  264. # so that reads can be repeated.
  265. if hasattr(body, 'read'):
  266. self._body = body.read()
  267. else:
  268. self._body = body
  269. def read(self):
  270. return self._body