test_request.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 uuid
  13. import requests
  14. import requests_mock
  15. from . import base
  16. class RequestTests(base.TestCase):
  17. def setUp(self):
  18. super(RequestTests, self).setUp()
  19. self.mocker = requests_mock.Mocker()
  20. self.addCleanup(self.mocker.stop)
  21. self.mocker.start()
  22. def do_request(self, **kwargs):
  23. method = kwargs.pop('method', 'GET')
  24. url = kwargs.pop('url', 'http://test.example.com/path')
  25. status_code = kwargs.pop('status_code', 200)
  26. data = uuid.uuid4().hex
  27. m = self.mocker.register_uri(method,
  28. url,
  29. text=data,
  30. status_code=status_code)
  31. resp = requests.request(method, url, **kwargs)
  32. self.assertEqual(status_code, resp.status_code)
  33. self.assertEqual(data, resp.text)
  34. self.assertTrue(m.called_once)
  35. return m.last_request
  36. def test_base_params(self):
  37. req = self.do_request(method='GET', status_code=200)
  38. self.assertIs(None, req.allow_redirects)
  39. self.assertIs(None, req.timeout)
  40. self.assertIs(True, req.verify)
  41. self.assertIs(None, req.cert)
  42. self.assertIs(False, req.stream)
  43. # actually it's an OrderedDict, but equality works fine
  44. # Skipping this check - it's problematic based on people's environments
  45. # and in CI systems where there are proxies set up at the environment
  46. # level. gh #127
  47. # self.assertEqual({}, req.proxies)
  48. def test_allow_redirects(self):
  49. req = self.do_request(allow_redirects=False, status_code=300)
  50. self.assertFalse(req.allow_redirects)
  51. def test_timeout(self):
  52. timeout = 300
  53. req = self.do_request(timeout=timeout)
  54. self.assertEqual(timeout, req.timeout)
  55. def test_verify_false(self):
  56. verify = False
  57. req = self.do_request(verify=verify)
  58. self.assertIs(verify, req.verify)
  59. def test_verify_path(self):
  60. verify = '/path/to/cacerts.pem'
  61. req = self.do_request(verify=verify)
  62. self.assertEqual(verify, req.verify)
  63. def test_stream(self):
  64. req = self.do_request()
  65. self.assertIs(False, req.stream)
  66. req = self.do_request(stream=False)
  67. self.assertIs(False, req.stream)
  68. req = self.do_request(stream=True)
  69. self.assertIs(True, req.stream)
  70. def test_certs(self):
  71. cert = ('/path/to/cert.pem', 'path/to/key.pem')
  72. req = self.do_request(cert=cert)
  73. self.assertEqual(cert, req.cert)
  74. self.assertTrue(req.verify)
  75. def test_proxies(self):
  76. proxies = {'http': 'foo.bar:3128',
  77. 'http://host.name': 'foo.bar:4012'}
  78. req = self.do_request(proxies=proxies)
  79. self.assertEqual(proxies, req.proxies)
  80. self.assertIsNot(proxies, req.proxies)
  81. def test_hostname_port_http(self):
  82. req = self.do_request(url='http://host.example.com:81/path')
  83. self.assertEqual('host.example.com:81', req.netloc)
  84. self.assertEqual('host.example.com', req.hostname)
  85. self.assertEqual(81, req.port)
  86. def test_hostname_port_https(self):
  87. req = self.do_request(url='https://host.example.com:8080/path')
  88. self.assertEqual('host.example.com:8080', req.netloc)
  89. self.assertEqual('host.example.com', req.hostname)
  90. self.assertEqual(8080, req.port)
  91. def test_hostname_default_port_http(self):
  92. req = self.do_request(url='http://host.example.com/path')
  93. self.assertEqual('host.example.com', req.netloc)
  94. self.assertEqual('host.example.com', req.hostname)
  95. self.assertEqual(80, req.port)
  96. def test_hostname_default_port_https(self):
  97. req = self.do_request(url='https://host.example.com/path')
  98. self.assertEqual('host.example.com', req.netloc)
  99. self.assertEqual('host.example.com', req.hostname)
  100. self.assertEqual(443, req.port)
  101. def test_to_string(self):
  102. req = self.do_request(url='https://host.example.com/path')
  103. self.assertEqual('GET https://host.example.com/path', str(req))
  104. def test_empty_query_string(self):
  105. req = self.do_request(url='https://host.example.com/path?key')
  106. self.assertEqual([''], req.qs['key'])