PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/jinglebox/v1/badappgdata/gdata-2.0.11/tests/atom_tests/mock_http_core_test.py

https://github.com/kaosbeat/MK10-koerssoftware
Python | 198 lines | 145 code | 24 blank | 29 comment | 4 complexity | 301f0c90519b61cc3b1e5c322a983b86 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. # This test may make an actual HTTP request.
  18. __author__ = 'j.s@google.com (Jeff Scudder)'
  19. import unittest
  20. import StringIO
  21. import os.path
  22. import atom.mock_http_core
  23. import atom.http_core
  24. class EchoClientTest(unittest.TestCase):
  25. def test_echo_response(self):
  26. client = atom.mock_http_core.EchoHttpClient()
  27. # Send a bare-bones POST request.
  28. request = atom.http_core.HttpRequest(method='POST',
  29. uri=atom.http_core.Uri(host='www.jeffscudder.com', path='/'))
  30. request.add_body_part('hello world!', 'text/plain')
  31. response = client.request(request)
  32. self.assert_(response.getheader('Echo-Host') == 'www.jeffscudder.com:None')
  33. self.assert_(response.getheader('Echo-Uri') == '/')
  34. self.assert_(response.getheader('Echo-Scheme') is None)
  35. self.assert_(response.getheader('Echo-Method') == 'POST')
  36. self.assert_(response.getheader('Content-Length') == str(len(
  37. 'hello world!')))
  38. self.assert_(response.getheader('Content-Type') == 'text/plain')
  39. self.assert_(response.read() == 'hello world!')
  40. # Test a path of None should default to /
  41. request = atom.http_core.HttpRequest(method='POST',
  42. uri=atom.http_core.Uri(host='www.jeffscudder.com', path=None))
  43. response = client.request(request)
  44. self.assert_(response.getheader('Echo-Host') == 'www.jeffscudder.com:None')
  45. self.assert_(response.getheader('Echo-Method') == 'POST')
  46. self.assert_(response.getheader('Echo-Uri') == '/')
  47. # Send a multipart request.
  48. request = atom.http_core.HttpRequest(method='POST',
  49. uri=atom.http_core.Uri(scheme='https', host='www.jeffscudder.com',
  50. port=8080, path='/multipart',
  51. query={'test': 'true', 'happy': 'yes'}),
  52. headers={'Authorization':'Test xyzzy', 'Testing':'True'})
  53. request.add_body_part('start', 'text/plain')
  54. request.add_body_part(StringIO.StringIO('<html><body>hi</body></html>'),
  55. 'text/html', len('<html><body>hi</body></html>'))
  56. request.add_body_part('alert("Greetings!")', 'text/javascript')
  57. response = client.request(request)
  58. self.assert_(response.getheader('Echo-Host') == 'www.jeffscudder.com:8080')
  59. self.assert_(
  60. response.getheader('Echo-Uri') == '/multipart?test=true&happy=yes')
  61. self.assert_(response.getheader('Echo-Scheme') == 'https')
  62. self.assert_(response.getheader('Echo-Method') == 'POST')
  63. self.assert_(response.getheader('Content-Type') == (
  64. 'multipart/related; boundary="%s"' % (atom.http_core.MIME_BOUNDARY,)))
  65. expected_body = ('Media multipart posting'
  66. '\r\n--%s\r\n'
  67. 'Content-Type: text/plain\r\n\r\n'
  68. 'start'
  69. '\r\n--%s\r\n'
  70. 'Content-Type: text/html\r\n\r\n'
  71. '<html><body>hi</body></html>'
  72. '\r\n--%s\r\n'
  73. 'Content-Type: text/javascript\r\n\r\n'
  74. 'alert("Greetings!")'
  75. '\r\n--%s--') % (atom.http_core.MIME_BOUNDARY,
  76. atom.http_core.MIME_BOUNDARY, atom.http_core.MIME_BOUNDARY,
  77. atom.http_core.MIME_BOUNDARY,)
  78. self.assert_(response.read() == expected_body)
  79. self.assert_(response.getheader('Content-Length') == str(
  80. len(expected_body)))
  81. class MockHttpClientTest(unittest.TestCase):
  82. def setUp(self):
  83. self.client = atom.mock_http_core.MockHttpClient()
  84. def test_respond_with_recording(self):
  85. request = atom.http_core.HttpRequest(method='GET')
  86. atom.http_core.parse_uri('http://www.google.com/').modify_request(request)
  87. self.client.add_response(request, 200, 'OK', body='Testing')
  88. response = self.client.request(request)
  89. self.assert_(response.status == 200)
  90. self.assert_(response.reason == 'OK')
  91. self.assert_(response.read() == 'Testing')
  92. def test_save_and_load_recordings(self):
  93. request = atom.http_core.HttpRequest(method='GET')
  94. atom.http_core.parse_uri('http://www.google.com/').modify_request(request)
  95. self.client.add_response(request, 200, 'OK', body='Testing')
  96. response = self.client.request(request)
  97. self.client._save_recordings('test_save_and_load_recordings')
  98. self.client._recordings = []
  99. try:
  100. response = self.client.request(request)
  101. self.fail('There should be no recording for this request.')
  102. except atom.mock_http_core.NoRecordingFound:
  103. pass
  104. self.client._load_recordings('test_save_and_load_recordings')
  105. response = self.client.request(request)
  106. self.assert_(response.status == 200)
  107. self.assert_(response.reason == 'OK')
  108. self.assert_(response.read() == 'Testing')
  109. def test_use_recordings(self):
  110. request = atom.http_core.HttpRequest(method='GET')
  111. atom.http_core.parse_uri('http://www.google.com/').modify_request(request)
  112. self.client._load_or_use_client('test_use_recordings',
  113. atom.http_core.HttpClient())
  114. response = self.client.request(request)
  115. if self.client.real_client:
  116. self.client._save_recordings('test_use_recordings')
  117. self.assert_(response.status == 200)
  118. self.assert_(response.reason == 'OK')
  119. self.assert_(response.getheader('server') == 'gws')
  120. body = response.read()
  121. self.assert_(body.startswith('<!doctype html>'))
  122. def test_match_request(self):
  123. x = atom.http_core.HttpRequest('http://example.com/', 'GET')
  124. y = atom.http_core.HttpRequest('http://example.com/', 'GET')
  125. self.assert_(atom.mock_http_core._match_request(x, y))
  126. y = atom.http_core.HttpRequest('http://example.com/', 'POST')
  127. self.assert_(not atom.mock_http_core._match_request(x, y))
  128. y = atom.http_core.HttpRequest('http://example.com/1', 'GET')
  129. self.assert_(not atom.mock_http_core._match_request(x, y))
  130. y = atom.http_core.HttpRequest('http://example.com/?gsessionid=1', 'GET')
  131. self.assert_(not atom.mock_http_core._match_request(x, y))
  132. y = atom.http_core.HttpRequest('http://example.com/?start_index=1', 'GET')
  133. self.assert_(atom.mock_http_core._match_request(x, y))
  134. x = atom.http_core.HttpRequest('http://example.com/?gsessionid=1', 'GET')
  135. y = atom.http_core.HttpRequest('http://example.com/?gsessionid=1', 'GET')
  136. self.assert_(atom.mock_http_core._match_request(x, y))
  137. y = atom.http_core.HttpRequest('http://example.com/?gsessionid=2', 'GET')
  138. self.assert_(not atom.mock_http_core._match_request(x, y))
  139. y = atom.http_core.HttpRequest('http://example.com/', 'GET')
  140. self.assert_(not atom.mock_http_core._match_request(x, y))
  141. def test_use_named_sessions(self):
  142. self.client._delete_recordings('mock_http_test.test_use_named_sessions')
  143. self.client.use_cached_session('mock_http_test.test_use_named_sessions',
  144. atom.mock_http_core.EchoHttpClient())
  145. request = atom.http_core.HttpRequest('http://example.com', 'GET')
  146. response = self.client.request(request)
  147. self.assertEqual(response.getheader('Echo-Method'), 'GET')
  148. self.assertEqual(response.getheader('Echo-Host'), 'example.com:None')
  149. # We will insert a Cache-Marker header to indicate that this is a
  150. # recorded response, but initially it should not be present.
  151. self.assertEqual(response.getheader('Cache-Marker'), None)
  152. # Modify the recorded response to allow us to identify a cached result
  153. # from an echoed result. We need to be able to check to see if this
  154. # came from a recording.
  155. self.assert_('Cache-Marker' not in self.client._recordings[0][1]._headers)
  156. self.client._recordings[0][1]._headers['Cache-Marker'] = '1'
  157. self.assert_('Cache-Marker' in self.client._recordings[0][1]._headers)
  158. # Save the recorded responses.
  159. self.client.close_session()
  160. # Create a new client, and have it use the recorded session.
  161. client = atom.mock_http_core.MockHttpClient()
  162. client.use_cached_session('mock_http_test.test_use_named_sessions',
  163. atom.mock_http_core.EchoHttpClient())
  164. # Make the same request, which should use the recorded result.
  165. response = client.request(request)
  166. self.assertEqual(response.getheader('Echo-Method'), 'GET')
  167. self.assertEqual(response.getheader('Echo-Host'), 'example.com:None')
  168. # We should now see the cache marker since the response is replayed.
  169. self.assertEqual(response.getheader('Cache-Marker'), '1')
  170. def suite():
  171. return unittest.TestSuite((unittest.makeSuite(MockHttpClientTest, 'test'),
  172. unittest.makeSuite(EchoClientTest, 'test')))
  173. if __name__ == '__main__':
  174. unittest.main()