mirror of
https://github.com/django/django.git
synced 2025-09-18 08:20:10 +00:00
Refs #26666 -- Added ALLOWED_HOSTS validation when running tests.
Also used ALLOWED_HOSTS to check for external hosts in assertRedirects().
This commit is contained in:
parent
00551c3eff
commit
17e661641d
11 changed files with 105 additions and 12 deletions
|
@ -90,14 +90,18 @@ If the ``Host`` header (or ``X-Forwarded-Host`` if
|
|||
list, the :meth:`django.http.HttpRequest.get_host()` method will raise
|
||||
:exc:`~django.core.exceptions.SuspiciousOperation`.
|
||||
|
||||
When :setting:`DEBUG` is ``True`` or when running tests, host validation is
|
||||
disabled; any host will be accepted. Thus it's usually only necessary to set it
|
||||
in production.
|
||||
When :setting:`DEBUG` is ``True``, host validation is disabled; any host will
|
||||
be accepted. ``ALLOWED_HOSTS`` is :ref:`checked when running tests
|
||||
<topics-testing-advanced-multiple-hosts>`.
|
||||
|
||||
This validation only applies via :meth:`~django.http.HttpRequest.get_host()`;
|
||||
if your code accesses the ``Host`` header directly from ``request.META`` you
|
||||
are bypassing this security protection.
|
||||
|
||||
.. versionchanged:: 1.11
|
||||
|
||||
In older versions, ``ALLOWED_HOSTS`` wasn't checked when running tests.
|
||||
|
||||
.. setting:: APPEND_SLASH
|
||||
|
||||
``APPEND_SLASH``
|
||||
|
|
|
@ -262,6 +262,11 @@ Miscellaneous
|
|||
* CSRF failures are logged to the ``django.security.csrf ``` logger instead of
|
||||
``django.request``.
|
||||
|
||||
* :setting:`ALLOWED_HOSTS` validation is no longer disabled when running tests.
|
||||
If your application includes tests with custom host names, you must include
|
||||
those host names in :setting:`ALLOWED_HOSTS`. See
|
||||
:ref:`topics-testing-advanced-multiple-hosts`.
|
||||
|
||||
* Using a foreign key's id (e.g. ``'field_id'``) in ``ModelAdmin.list_display``
|
||||
displays the related object's ID. Remove the ``_id`` suffix if you want the
|
||||
old behavior of the string representation of the object.
|
||||
|
|
|
@ -498,6 +498,7 @@ multiline
|
|||
multilinestring
|
||||
multipart
|
||||
multipolygon
|
||||
multitenancy
|
||||
multithreaded
|
||||
multithreading
|
||||
Mumbai
|
||||
|
|
|
@ -67,6 +67,61 @@ The following is a simple unit test using the request factory::
|
|||
response = MyView.as_view()(request)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
.. _topics-testing-advanced-multiple-hosts:
|
||||
|
||||
Tests and multiple host names
|
||||
=============================
|
||||
|
||||
The :setting:`ALLOWED_HOSTS` setting is validated when running tests. This
|
||||
allows the test client to differentiate between internal and external URLs.
|
||||
|
||||
Projects that support multitenancy or otherwise alter business logic based on
|
||||
the request's host and use custom host names in tests must include those hosts
|
||||
in :setting:`ALLOWED_HOSTS`.
|
||||
|
||||
The first and simplest option to do so is to add the hosts to your settings
|
||||
file. For example, the test suite for docs.djangoproject.com includes the
|
||||
following::
|
||||
|
||||
from django.test import TestCase
|
||||
|
||||
class SearchFormTestCase(TestCase):
|
||||
def test_empty_get(self):
|
||||
response = self.client.get('/en/dev/search/', HTTP_HOST='docs.djangoproject.dev:8000')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
and the settings file includes a list of the domains supported by the project::
|
||||
|
||||
ALLOWED_HOSTS = [
|
||||
'www.djangoproject.dev',
|
||||
'docs.djangoproject.dev',
|
||||
...
|
||||
]
|
||||
|
||||
Another option is to add the required hosts to :setting:`ALLOWED_HOSTS` using
|
||||
:meth:`~django.test.override_settings()` or
|
||||
:attr:`~django.test.SimpleTestCase.modify_settings()`. This option may be
|
||||
preferable in standalone apps that can't package their own settings file or
|
||||
for projects where the list of domains is not static (e.g., subdomains for
|
||||
multitenancy). For example, you could write a test for the domain
|
||||
``http://otherserver/`` as follows::
|
||||
|
||||
from django.test import TestCase, override_settings
|
||||
|
||||
class MultiDomainTestCase(TestCase):
|
||||
@override_settings(ALLOWED_HOSTS=['otherserver'])
|
||||
def test_other_domain(self):
|
||||
response = self.client.get('http://otherserver/foo/bar/')
|
||||
|
||||
Disabling :setting:`ALLOWED_HOSTS` checking (``ALLOWED_HOSTS = ['*']``) when
|
||||
running tests prevents the test client from raising a helpful error message if
|
||||
you follow a redirect to an external URL.
|
||||
|
||||
.. versionchanged:: 1.11
|
||||
|
||||
Older versions didn't validate ``ALLOWED_HOSTS`` while testing so these
|
||||
techniques weren't necessary.
|
||||
|
||||
.. _topics-testing-advanced-multidb:
|
||||
|
||||
Tests and multiple databases
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue