Made HTTP_HOST of page dummy request include port. (#4954)

pull/5011/head
Sergey Fedoseev 2018-12-12 10:30:32 +05:00 zatwierdzone przez Matt Westcott
rodzic dc2f487391
commit 057690815b
4 zmienionych plików z 31 dodań i 1 usunięć

Wyświetl plik

@ -7,6 +7,7 @@ Changelog
* Added support for customising EditHandler-based forms on a per-request basis (Bertrand Bordage)
* Added more informative error message when `|richtext` filter is applied to a non-string value (mukesh5)
* Fix: Set `SERVER_PORT` to 443 in `Page.dummy_request()` for HTTPS sites (Sergey Fedoseev)
* Fix: Include port number in `Host` header of `Page.dummy_request()` (Sergey Fedoseev)
* Fix: Validation error messages in `InlinePanel` no longer count towards `max_num` when disabling the 'add' button (Todd Dembrey, Thibaud Colas)

Wyświetl plik

@ -22,6 +22,7 @@ Bug fixes
~~~~~~~~~
* Set ``SERVER_PORT`` to 443 in ``Page.dummy_request()`` for HTTPS sites (Sergey Fedoseev)
* Include port number in ``Host`` header of ``Page.dummy_request()`` (Sergey Fedoseev)
* Validation error messages in ``InlinePanel`` no longer count towards ``max_num`` when disabling the 'add' button (Todd Dembrey, Thibaud Colas)

Wyświetl plik

@ -1224,13 +1224,16 @@ class Page(AbstractPage, index.Indexed, ClusterableModel, metaclass=PageBase):
port = 80
scheme = 'http'
http_host = hostname
if port != (443 if scheme == 'https' else 80):
http_host = '%s:%s' % (http_host, port)
dummy_values = {
'REQUEST_METHOD': 'GET',
'PATH_INFO': path,
'SERVER_NAME': hostname,
'SERVER_PORT': port,
'SERVER_PROTOCOL': 'HTTP/1.1',
'HTTP_HOST': hostname,
'HTTP_HOST': http_host,
'wsgi.version': (1, 0),
'wsgi.input': StringIO(),
'wsgi.errors': StringIO(),

Wyświetl plik

@ -1382,6 +1382,31 @@ class TestDummyRequest(TestCase):
self.assertIn('wsgi.multiprocess', request.META)
self.assertIn('wsgi.run_once', request.META)
def test_dummy_request_for_accessible_page_non_standard_port(self):
Site.objects.update(port=8888)
event_index = Page.objects.get(url_path='/home/events/')
request = event_index.dummy_request()
# request should have the correct path and hostname for this page
self.assertEqual(request.path, '/events/')
self.assertEqual(request.META['HTTP_HOST'], 'localhost:8888')
# check other env vars required by the WSGI spec
self.assertEqual(request.META['REQUEST_METHOD'], 'GET')
self.assertEqual(request.META['SCRIPT_NAME'], '')
self.assertEqual(request.META['PATH_INFO'], '/events/')
self.assertEqual(request.META['SERVER_NAME'], 'localhost')
self.assertEqual(request.META['SERVER_PORT'], 8888)
self.assertEqual(request.META['SERVER_PROTOCOL'], 'HTTP/1.1')
self.assertEqual(request.META['wsgi.version'], (1, 0))
self.assertEqual(request.META['wsgi.url_scheme'], 'http')
self.assertIn('wsgi.input', request.META)
self.assertIn('wsgi.errors', request.META)
self.assertIn('wsgi.multithread', request.META)
self.assertIn('wsgi.multiprocess', request.META)
self.assertIn('wsgi.run_once', request.META)
def test_dummy_request_for_accessible_page_with_original_request(self):
event_index = Page.objects.get(url_path='/home/events/')
original_headers = {