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
This commit is contained in:
parent
86e33d1eff
commit
c3ed2aa479
@ -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::
|
||||
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user