From c3ed2aa479064cbb1363126bc9bae87afbc738c7 Mon Sep 17 00:00:00 2001 From: Jamie Lennox Date: Thu, 13 Oct 2016 13:45:30 +1100 Subject: [PATCH] Add hostname and port properties to request Using request.netloc can be confusing because you have to check whether or not the port is included. We are going to want this seperation in later patches so expose it on the request. Change-Id: I2e4bad425fdbc2501727b295752900d4ce4086bc --- doc/source/history.rst | 2 ++ requests_mock/request.py | 26 ++++++++++++++++++++++++++ requests_mock/tests/test_request.py | 28 ++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/doc/source/history.rst b/doc/source/history.rst index 2d61786..a0f1f0b 100644 --- a/doc/source/history.rst +++ b/doc/source/history.rst @@ -47,6 +47,8 @@ These additions include: :text: The data of the request converted into a unicode string. :json: The data of the request loaded from json into python objects. :qs: The query string of the request. See :py:func:`urllib.parse.parse_qs` for information on the return format. +:hostname: The host name that the request was sent to. +:port: The port the request was sent to. .. doctest:: diff --git a/requests_mock/request.py b/requests_mock/request.py index b15d15a..507d4b4 100644 --- a/requests_mock/request.py +++ b/requests_mock/request.py @@ -66,6 +66,32 @@ class _RequestObjectProxy(object): def netloc(self): return self._url_parts.netloc + @property + def hostname(self): + try: + return self.netloc.split(':')[0] + except IndexError: + return '' + + @property + def port(self): + components = self.netloc.split(':') + + try: + return int(components[1]) + except (IndexError, ValueError): + pass + + if self.scheme == 'https': + return 443 + if self.scheme == 'http': + return 80 + + # The default return shouldn't matter too much because if you are + # wanting to test this value you really should be explicitly setting it + # somewhere. 0 at least is a boolean False and an int. + return 0 + @property def path(self): return self._url_parts.path diff --git a/requests_mock/tests/test_request.py b/requests_mock/tests/test_request.py index d0af499..e1e0b6b 100644 --- a/requests_mock/tests/test_request.py +++ b/requests_mock/tests/test_request.py @@ -89,3 +89,31 @@ class RequestTests(base.TestCase): self.assertEqual(proxies, req.proxies) self.assertIsNot(proxies, req.proxies) + + def test_hostname_port_http(self): + req = self.do_request(url='http://host.example.com:81/path') + + self.assertEqual('host.example.com:81', req.netloc) + self.assertEqual('host.example.com', req.hostname) + self.assertEqual(81, req.port) + + def test_hostname_port_https(self): + req = self.do_request(url='https://host.example.com:8080/path') + + self.assertEqual('host.example.com:8080', req.netloc) + self.assertEqual('host.example.com', req.hostname) + self.assertEqual(8080, req.port) + + def test_hostname_default_port_http(self): + req = self.do_request(url='http://host.example.com/path') + + self.assertEqual('host.example.com', req.netloc) + self.assertEqual('host.example.com', req.hostname) + self.assertEqual(80, req.port) + + def test_hostname_default_port_https(self): + req = self.do_request(url='https://host.example.com/path') + + self.assertEqual('host.example.com', req.netloc) + self.assertEqual('host.example.com', req.hostname) + self.assertEqual(443, req.port)