test_response.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  2. # not use this file except in compliance with the License. You may obtain
  3. # a copy of the License at
  4. #
  5. # https://www.apache.org/licenses/LICENSE-2.0
  6. #
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  9. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  10. # License for the specific language governing permissions and limitations
  11. # under the License.
  12. import io
  13. import pickle
  14. from requests_mock import exceptions
  15. from requests_mock import request
  16. from requests_mock import response
  17. from . import base
  18. class ResponseTests(base.TestCase):
  19. def setUp(self):
  20. super(ResponseTests, self).setUp()
  21. self.method = 'GET'
  22. self.url = 'http://test.url/path'
  23. self.request = request._RequestObjectProxy._create(self.method,
  24. self.url,
  25. {})
  26. def create_response(self, **kwargs):
  27. return response.create_response(self.request, **kwargs)
  28. def test_create_response_body_args(self):
  29. self.assertRaises(RuntimeError,
  30. self.create_response,
  31. raw='abc',
  32. body='abc')
  33. self.assertRaises(RuntimeError,
  34. self.create_response,
  35. text='abc',
  36. json={'a': 1})
  37. def test_content_type(self):
  38. self.assertRaises(TypeError, self.create_response, text=55)
  39. self.assertRaises(TypeError, self.create_response, text={'a': 1})
  40. self.assertRaises(TypeError, self.create_response, text=b'')
  41. def test_text_type(self):
  42. self.assertRaises(TypeError, self.create_response, content=u't')
  43. self.assertRaises(TypeError, self.create_response, content={'a': 1})
  44. self.assertRaises(TypeError, self.create_response, content=u'')
  45. def test_json_body(self):
  46. data = {'a': 1}
  47. resp = self.create_response(json=data)
  48. self.assertEqual('{"a": 1}', resp.text)
  49. self.assertIsInstance(resp.text, str)
  50. self.assertIsInstance(resp.content, bytes)
  51. self.assertEqual(data, resp.json())
  52. def test_body_body(self):
  53. value = b'data'
  54. body = io.BytesIO(value)
  55. resp = self.create_response(body=body)
  56. self.assertEqual(value.decode(), resp.text)
  57. self.assertIsInstance(resp.text, str)
  58. self.assertIsInstance(resp.content, bytes)
  59. def test_setting_connection(self):
  60. conn = object()
  61. resp = self.create_response(connection=conn)
  62. self.assertIs(conn, resp.connection)
  63. def test_send_from_no_connection(self):
  64. resp = self.create_response()
  65. self.assertRaises(exceptions.InvalidRequest,
  66. resp.connection.send, self.request)
  67. def test_cookies_from_header(self):
  68. # domain must be same as request url to pass policy check
  69. headers = {'Set-Cookie': 'fig=newton; Path=/test; domain=.test.url'}
  70. resp = self.create_response(headers=headers)
  71. self.assertEqual('newton', resp.cookies['fig'])
  72. self.assertEqual(['/test'], resp.cookies.list_paths())
  73. self.assertEqual(['.test.url'], resp.cookies.list_domains())
  74. def test_cookies_from_dict(self):
  75. # This is a syntax we get from requests. I'm not sure i like it.
  76. resp = self.create_response(cookies={'fig': 'newton',
  77. 'sugar': 'apple'})
  78. self.assertEqual('newton', resp.cookies['fig'])
  79. self.assertEqual('apple', resp.cookies['sugar'])
  80. def test_cookies_with_jar(self):
  81. jar = response.CookieJar()
  82. jar.set('fig', 'newton', path='/foo', domain='.test.url')
  83. jar.set('sugar', 'apple', path='/bar', domain='.test.url')
  84. resp = self.create_response(cookies=jar)
  85. self.assertEqual('newton', resp.cookies['fig'])
  86. self.assertEqual('apple', resp.cookies['sugar'])
  87. self.assertEqual({'/foo', '/bar'}, set(resp.cookies.list_paths()))
  88. self.assertEqual(['.test.url'], resp.cookies.list_domains())
  89. def test_response_pickle(self):
  90. text = 'hello world'
  91. jar = response.CookieJar()
  92. jar.set('fig', 'newton', path='/foo', domain='.test.url')
  93. orig_resp = self.create_response(cookies=jar, text=text)
  94. d = pickle.dumps(orig_resp)
  95. new_resp = pickle.loads(d)
  96. self.assertEqual(text, new_resp.text)
  97. self.assertEqual('newton', new_resp.cookies['fig'])
  98. self.assertIsNone(new_resp.request.matcher)
  99. def test_response_encoding(self):
  100. headers = {"content-type": "text/html; charset=ISO-8859-1"}
  101. resp = self.create_response(headers=headers,
  102. text="<html><body></body></html")
  103. self.assertEqual('ISO-8859-1', resp.encoding)
  104. def test_default_reason(self):
  105. resp = self.create_response()
  106. self.assertEqual('OK', resp.reason)
  107. def test_custom_reason(self):
  108. reason = 'Live long and prosper'
  109. resp = self.create_response(status_code=201, reason=reason)
  110. self.assertEqual(201, resp.status_code)
  111. self.assertEqual(reason, resp.reason)
  112. def test_some_other_response_reasons(self):
  113. reasons = {
  114. 301: 'Moved Permanently',
  115. 410: 'Gone',
  116. 503: 'Service Unavailable',
  117. }
  118. for code, reason in reasons.items():
  119. self.assertEqual(reason,
  120. self.create_response(status_code=code).reason)