Updated HttpRequest.build_absolute_uri() tests to use subTest().

This commit is contained in:
Tim Graham 2018-01-10 10:56:02 -05:00
parent 66d74676e2
commit 366451880a
1 changed files with 26 additions and 66 deletions

View File

@ -179,20 +179,6 @@ class RequestsTests(SimpleTestCase):
# left percent-encoded in the path. # left percent-encoded in the path.
self.assertEqual(request.path, "/caf%E9/") self.assertEqual(request.path, "/caf%E9/")
def test_httprequest_location(self):
request = HttpRequest()
self.assertEqual(
request.build_absolute_uri(location="https://www.example.com/asdf"),
'https://www.example.com/asdf'
)
request.get_host = lambda: 'www.example.com'
request.path = ''
self.assertEqual(
request.build_absolute_uri(location="/path/with:colons"),
'http://www.example.com/path/with:colons'
)
def test_limited_stream(self): def test_limited_stream(self):
# Read all of a limited stream # Read all of a limited stream
stream = LimitedStream(BytesIO(b'test'), 2) stream = LimitedStream(BytesIO(b'test'), 2)
@ -784,62 +770,36 @@ class HostValidationTests(SimpleTestCase):
self.assertEqual(port, '8080') self.assertEqual(port, '8080')
class BuildAbsoluteURITestCase(SimpleTestCase): class BuildAbsoluteURITests(SimpleTestCase):
""" factory = RequestFactory()
Regression tests for ticket #18314.
"""
def setUp(self): def test_absolute_url(self):
self.factory = RequestFactory() request = HttpRequest()
url = 'https://www.example.com/asdf'
self.assertEqual(request.build_absolute_uri(location=url), url)
def test_build_absolute_uri_no_location(self): def test_host_retrieval(self):
""" request = HttpRequest()
``request.build_absolute_uri()`` returns the proper value when request.get_host = lambda: 'www.example.com'
the ``location`` argument is not provided, and ``request.path`` request.path = ''
begins with //.
"""
# //// is needed to create a request with a path beginning with //
request = self.factory.get('////absolute-uri')
self.assertEqual( self.assertEqual(
request.build_absolute_uri(), request.build_absolute_uri(location='/path/with:colons'),
'http://testserver//absolute-uri' 'http://www.example.com/path/with:colons'
) )
def test_build_absolute_uri_absolute_location(self): def test_request_path_begins_with_two_slashes(self):
""" # //// creates a request with a path beginning with //
``request.build_absolute_uri()`` returns the proper value when
an absolute URL ``location`` argument is provided, and ``request.path``
begins with //.
"""
# //// is needed to create a request with a path beginning with //
request = self.factory.get('////absolute-uri') request = self.factory.get('////absolute-uri')
self.assertEqual( tests = (
request.build_absolute_uri(location='http://example.com/?foo=bar'), # location isn't provided
'http://example.com/?foo=bar' (None, 'http://testserver//absolute-uri'),
) # An absolute URL
('http://example.com/?foo=bar', 'http://example.com/?foo=bar'),
def test_build_absolute_uri_schema_relative_location(self): # A schema-relative URL
""" ('//example.com/?foo=bar', 'http://example.com/?foo=bar'),
``request.build_absolute_uri()`` returns the proper value when # A relative URL
a schema-relative URL ``location`` argument is provided, and ('/foo/bar/', 'http://testserver/foo/bar/'),
``request.path`` begins with //.
"""
# //// is needed to create a request with a path beginning with //
request = self.factory.get('////absolute-uri')
self.assertEqual(
request.build_absolute_uri(location='//example.com/?foo=bar'),
'http://example.com/?foo=bar'
)
def test_build_absolute_uri_relative_location(self):
"""
``request.build_absolute_uri()`` returns the proper value when
a relative URL ``location`` argument is provided, and ``request.path``
begins with //.
"""
# //// is needed to create a request with a path beginning with //
request = self.factory.get('////absolute-uri')
self.assertEqual(
request.build_absolute_uri(location='/foo/bar/'),
'http://testserver/foo/bar/'
) )
for location, expected_url in tests:
with self.subTest(location=location):
self.assertEqual(request.build_absolute_uri(location=location), expected_url)