| 1 |
""" |
| 2 |
Tests for django.core.servers. |
| 3 |
""" |
| 4 |
|
| 5 |
import errno |
| 6 |
import os |
| 7 |
import socket |
| 8 |
import threading |
| 9 |
import unittest |
| 10 |
from http.client import HTTPConnection |
| 11 |
from urllib.error import HTTPError |
| 12 |
from urllib.parse import urlencode |
| 13 |
from urllib.request import urlopen |
| 14 |
|
| 15 |
from django.conf import settings |
| 16 |
from django.core.servers.basehttp import ThreadedWSGIServer, WSGIServer |
| 17 |
from django.db import DEFAULT_DB_ALIAS, connection, connections |
| 18 |
from django.test import LiveServerTestCase, override_settings |
| 19 |
from django.test.testcases import LiveServerThread, QuietWSGIRequestHandler |
| 20 |
|
| 21 |
from .models import Person |
| 22 |
|
| 23 |
TEST_ROOT = os.path.dirname(__file__) |
| 24 |
TEST_SETTINGS = { |
| 25 |
"MEDIA_URL": "media/", |
| 26 |
"MEDIA_ROOT": os.path.join(TEST_ROOT, "media"), |
| 27 |
"STATIC_URL": "static/", |
| 28 |
"STATIC_ROOT": os.path.join(TEST_ROOT, "static"), |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
@override_settings(ROOT_URLCONF="servers.urls", **TEST_SETTINGS) |
| 33 |
class LiveServerBase(LiveServerTestCase): |
| 34 |
available_apps = [ |
| 35 |
"servers", |
| 36 |
"django.contrib.auth", |
| 37 |
"django.contrib.contenttypes", |
| 38 |
"django.contrib.sessions", |
| 39 |
] |
| 40 |
fixtures = ["testdata.json"] |
| 41 |
|
| 42 |
def urlopen(self, url): |
| 43 |
return urlopen(self.live_server_url + url) |
| 44 |
|
| 45 |
|
| 46 |
class CloseConnectionTestServer(ThreadedWSGIServer): |
| 47 |
def __init__(self, *args, **kwargs): |
| 48 |
super().__init__(*args, **kwargs) |
| 49 |
# This event is set right after the first time a request closes its |
| 50 |
# database connections. |
| 51 |
self._connections_closed = threading.Event() |
| 52 |
|
| 53 |
def _close_connections(self): |
| 54 |
super()._close_connections() |
| 55 |
self._connections_closed.set() |
| 56 |
|
| 57 |
|
| 58 |
class CloseConnectionTestLiveServerThread(LiveServerThread): |
| 59 |
server_class = CloseConnectionTestServer |
| 60 |
|
| 61 |
def _create_server(self, connections_override=None): |
| 62 |
return super()._create_server(connections_override=self.connections_override) |
| 63 |
|
| 64 |
|
| 65 |
class LiveServerTestCloseConnectionTest(LiveServerBase): |
| 66 |
server_thread_class = CloseConnectionTestLiveServerThread |
| 67 |
|
| 68 |
@classmethod |
| 69 |
def _make_connections_override(cls): |
| 70 |
conn = connections[DEFAULT_DB_ALIAS] |
| 71 |
cls.conn = conn |
| 72 |
cls.old_conn_max_age = conn.settings_dict["CONN_MAX_AGE"] |
| 73 |
# Set the connection's CONN_MAX_AGE to None to simulate the |
| 74 |
# CONN_MAX_AGE setting being set to None on the server. This prevents |
| 75 |
# Django from closing the connection and allows testing that |
| 76 |
# ThreadedWSGIServer closes connections. |
| 77 |
conn.settings_dict["CONN_MAX_AGE"] = None |
| 78 |
# Pass a database connection through to the server to check it is being |
| 79 |
# closed by ThreadedWSGIServer. |
| 80 |
return {DEFAULT_DB_ALIAS: conn} |
| 81 |
|
| 82 |
@classmethod |
| 83 |
def tearDownConnectionTest(cls): |
| 84 |
cls.conn.settings_dict["CONN_MAX_AGE"] = cls.old_conn_max_age |
| 85 |
|
| 86 |
@classmethod |
| 87 |
def tearDownClass(cls): |
| 88 |
cls.tearDownConnectionTest() |
| 89 |
super().tearDownClass() |
| 90 |
|
| 91 |
def test_closes_connections(self): |
| 92 |
# The server's request thread sets this event after closing |
| 93 |
# its database connections. |
| 94 |
closed_event = self.server_thread.httpd._connections_closed |
| 95 |
conn = self.conn |
| 96 |
# Open a connection to the database. |
| 97 |
conn.connect() |
| 98 |
self.assertIsNotNone(conn.connection) |
| 99 |
with self.urlopen("/model_view/") as f: |
| 100 |
# The server can access the database. |
| 101 |
self.assertCountEqual(f.read().splitlines(), [b"jane", b"robert"]) |
| 102 |
# Wait for the server's request thread to close the connection. |
| 103 |
# A timeout of 0.1 seconds should be more than enough. If the wait |
| 104 |
# times out, the assertion after should fail. |
| 105 |
closed_event.wait(timeout=0.1) |
| 106 |
self.assertIsNone(conn.connection) |
| 107 |
|
| 108 |
|
| 109 |
@unittest.skipUnless(connection.vendor == "sqlite", "SQLite specific test.") |
| 110 |
class LiveServerInMemoryDatabaseLockTest(LiveServerBase): |
| 111 |
def test_in_memory_database_lock(self): |
| 112 |
""" |
| 113 |
With a threaded LiveServer and an in-memory database, an error can |
| 114 |
occur when 2 requests reach the server and try to lock the database |
| 115 |
at the same time, if the requests do not share the same database |
| 116 |
connection. |
| 117 |
""" |
| 118 |
conn = self.server_thread.connections_override[DEFAULT_DB_ALIAS] |
| 119 |
source_connection = conn.connection |
| 120 |
# Open a connection to the database. |
| 121 |
conn.connect() |
| 122 |
# Create a transaction to lock the database. |
| 123 |
cursor = conn.cursor() |
| 124 |
cursor.execute("BEGIN IMMEDIATE TRANSACTION") |
| 125 |
try: |
| 126 |
with self.urlopen("/create_model_instance/") as f: |
| 127 |
self.assertEqual(f.status, 200) |
| 128 |
except HTTPError: |
| 129 |
self.fail("Unexpected error due to a database lock.") |
| 130 |
finally: |
| 131 |
# Release the transaction. |
| 132 |
cursor.execute("ROLLBACK") |
| 133 |
source_connection.close() |
| 134 |
|
| 135 |
|
| 136 |
class FailingLiveServerThread(LiveServerThread): |
| 137 |
def _create_server(self, connections_override=None): |
| 138 |
raise RuntimeError("Error creating server.") |
| 139 |
|
| 140 |
|
| 141 |
class LiveServerTestCaseSetupTest(LiveServerBase): |
| 142 |
server_thread_class = FailingLiveServerThread |
| 143 |
|
| 144 |
@classmethod |
| 145 |
def check_allowed_hosts(cls, expected): |
| 146 |
if settings.ALLOWED_HOSTS != expected: |
| 147 |
raise RuntimeError(f"{settings.ALLOWED_HOSTS} != {expected}") |
| 148 |
|
| 149 |
@classmethod |
| 150 |
def setUpClass(cls): |
| 151 |
cls.check_allowed_hosts(["testserver"]) |
| 152 |
try: |
| 153 |
super().setUpClass() |
| 154 |
except RuntimeError: |
| 155 |
# LiveServerTestCase's change to ALLOWED_HOSTS should be reverted. |
| 156 |
cls.doClassCleanups() |
| 157 |
cls.check_allowed_hosts(["testserver"]) |
| 158 |
else: |
| 159 |
raise RuntimeError("Server did not fail.") |
| 160 |
cls.set_up_called = True |
| 161 |
|
| 162 |
def test_set_up_class(self): |
| 163 |
self.assertIs(self.set_up_called, True) |
| 164 |
|
| 165 |
|
| 166 |
class LiveServerAddress(LiveServerBase): |
| 167 |
@classmethod |
| 168 |
def setUpClass(cls): |
| 169 |
super().setUpClass() |
| 170 |
# put it in a list to prevent descriptor lookups in test |
| 171 |
cls.live_server_url_test = [cls.live_server_url] |
| 172 |
|
| 173 |
def test_live_server_url_is_class_property(self): |
| 174 |
self.assertIsInstance(self.live_server_url_test[0], str) |
| 175 |
self.assertEqual(self.live_server_url_test[0], self.live_server_url) |
| 176 |
|
| 177 |
|
| 178 |
class LiveServerSingleThread(LiveServerThread): |
| 179 |
def _create_server(self, connections_override=None): |
| 180 |
return WSGIServer( |
| 181 |
(self.host, self.port), QuietWSGIRequestHandler, allow_reuse_address=False |
| 182 |
) |
| 183 |
|
| 184 |
|
| 185 |
class SingleThreadLiveServerTestCase(LiveServerTestCase): |
| 186 |
server_thread_class = LiveServerSingleThread |
| 187 |
|
| 188 |
|
| 189 |
class LiveServerViews(LiveServerBase): |
| 190 |
def test_protocol(self): |
| 191 |
"""Launched server serves with HTTP 1.1.""" |
| 192 |
with self.urlopen("/example_view/") as f: |
| 193 |
self.assertEqual(f.version, 11) |
| 194 |
|
| 195 |
def test_closes_connection_without_content_length(self): |
| 196 |
""" |
| 197 |
An HTTP 1.1 server is supposed to support keep-alive. Since our |
| 198 |
development server is rather simple we support it only in cases where |
| 199 |
we can detect a content length from the response. This should be doable |
| 200 |
for all simple views and streaming responses where an iterable with |
| 201 |
length of one is passed. The latter follows as result of `set_content_length` |
| 202 |
from https://github.com/python/cpython/blob/main/Lib/wsgiref/handlers.py. |
| 203 |
|
| 204 |
If we cannot detect a content length we explicitly set the `Connection` |
| 205 |
header to `close` to notify the client that we do not actually support |
| 206 |
it. |
| 207 |
""" |
| 208 |
conn = HTTPConnection( |
| 209 |
LiveServerViews.server_thread.host, |
| 210 |
LiveServerViews.server_thread.port, |
| 211 |
timeout=1, |
| 212 |
) |
| 213 |
try: |
| 214 |
conn.request( |
| 215 |
"GET", "/streaming_example_view/", headers={"Connection": "keep-alive"} |
| 216 |
) |
| 217 |
response = conn.getresponse() |
| 218 |
self.assertTrue(response.will_close) |
| 219 |
self.assertEqual(response.read(), b"Iamastream") |
| 220 |
self.assertEqual(response.status, 200) |
| 221 |
self.assertEqual(response.getheader("Connection"), "close") |
| 222 |
|
| 223 |
conn.request( |
| 224 |
"GET", "/streaming_example_view/", headers={"Connection": "close"} |
| 225 |
) |
| 226 |
response = conn.getresponse() |
| 227 |
self.assertTrue(response.will_close) |
| 228 |
self.assertEqual(response.read(), b"Iamastream") |
| 229 |
self.assertEqual(response.status, 200) |
| 230 |
self.assertEqual(response.getheader("Connection"), "close") |
| 231 |
finally: |
| 232 |
conn.close() |
| 233 |
|
| 234 |
def test_keep_alive_on_connection_with_content_length(self): |
| 235 |
""" |
| 236 |
See `test_closes_connection_without_content_length` for details. This |
| 237 |
is a follow up test, which ensure that we do not close the connection |
| 238 |
if not needed, hence allowing us to take advantage of keep-alive. |
| 239 |
""" |
| 240 |
conn = HTTPConnection( |
| 241 |
LiveServerViews.server_thread.host, LiveServerViews.server_thread.port |
| 242 |
) |
| 243 |
try: |
| 244 |
conn.request("GET", "/example_view/", headers={"Connection": "keep-alive"}) |
| 245 |
response = conn.getresponse() |
| 246 |
self.assertFalse(response.will_close) |
| 247 |
self.assertEqual(response.read(), b"example view") |
| 248 |
self.assertEqual(response.status, 200) |
| 249 |
self.assertIsNone(response.getheader("Connection")) |
| 250 |
|
| 251 |
conn.request("GET", "/example_view/", headers={"Connection": "close"}) |
| 252 |
response = conn.getresponse() |
| 253 |
self.assertFalse(response.will_close) |
| 254 |
self.assertEqual(response.read(), b"example view") |
| 255 |
self.assertEqual(response.status, 200) |
| 256 |
self.assertIsNone(response.getheader("Connection")) |
| 257 |
finally: |
| 258 |
conn.close() |
| 259 |
|
| 260 |
def test_keep_alive_connection_clears_previous_request_data(self): |
| 261 |
conn = HTTPConnection( |
| 262 |
LiveServerViews.server_thread.host, LiveServerViews.server_thread.port |
| 263 |
) |
| 264 |
try: |
| 265 |
conn.request( |
| 266 |
"POST", "/method_view/", b"{}", headers={"Connection": "keep-alive"} |
| 267 |
) |
| 268 |
response = conn.getresponse() |
| 269 |
self.assertFalse(response.will_close) |
| 270 |
self.assertEqual(response.status, 200) |
| 271 |
self.assertEqual(response.read(), b"POST") |
| 272 |
|
| 273 |
conn.request( |
| 274 |
"POST", "/method_view/", b"{}", headers={"Connection": "close"} |
| 275 |
) |
| 276 |
response = conn.getresponse() |
| 277 |
self.assertFalse(response.will_close) |
| 278 |
self.assertEqual(response.status, 200) |
| 279 |
self.assertEqual(response.read(), b"POST") |
| 280 |
finally: |
| 281 |
conn.close() |
| 282 |
|
| 283 |
def test_404(self): |
| 284 |
with self.assertRaises(HTTPError) as err: |
| 285 |
self.urlopen("/") |
| 286 |
err.exception.close() |
| 287 |
self.assertEqual(err.exception.code, 404, "Expected 404 response") |
| 288 |
|
| 289 |
def test_view(self): |
| 290 |
with self.urlopen("/example_view/") as f: |
| 291 |
self.assertEqual(f.read(), b"example view") |
| 292 |
|
| 293 |
def test_static_files(self): |
| 294 |
with self.urlopen("/static/example_static_file.txt") as f: |
| 295 |
self.assertEqual(f.read().rstrip(b"\r\n"), b"example static file") |
| 296 |
|
| 297 |
def test_no_collectstatic_emulation(self): |
| 298 |
""" |
| 299 |
LiveServerTestCase reports a 404 status code when HTTP client |
| 300 |
tries to access a static file that isn't explicitly put under |
| 301 |
STATIC_ROOT. |
| 302 |
""" |
| 303 |
with self.assertRaises(HTTPError) as err: |
| 304 |
self.urlopen("/static/another_app/another_app_static_file.txt") |
| 305 |
err.exception.close() |
| 306 |
self.assertEqual(err.exception.code, 404, "Expected 404 response") |
| 307 |
|
| 308 |
def test_media_files(self): |
| 309 |
with self.urlopen("/media/example_media_file.txt") as f: |
| 310 |
self.assertEqual(f.read().rstrip(b"\r\n"), b"example media file") |
| 311 |
|
| 312 |
def test_environ(self): |
| 313 |
with self.urlopen("/environ_view/?%s" % urlencode({"q": "ัะตัั"})) as f: |
| 314 |
self.assertIn(b"QUERY_STRING: 'q=%D1%82%D0%B5%D1%81%D1%82'", f.read()) |
| 315 |
|
| 316 |
|
| 317 |
@override_settings(ROOT_URLCONF="servers.urls") |
| 318 |
class SingleThreadLiveServerViews(SingleThreadLiveServerTestCase): |
| 319 |
available_apps = ["servers"] |
| 320 |
|
| 321 |
def test_closes_connection_with_content_length(self): |
| 322 |
""" |
| 323 |
Contrast to |
| 324 |
LiveServerViews.test_keep_alive_on_connection_with_content_length(). |
| 325 |
Persistent connections require threading server. |
| 326 |
""" |
| 327 |
conn = HTTPConnection( |
| 328 |
SingleThreadLiveServerViews.server_thread.host, |
| 329 |
SingleThreadLiveServerViews.server_thread.port, |
| 330 |
timeout=1, |
| 331 |
) |
| 332 |
try: |
| 333 |
conn.request("GET", "/example_view/", headers={"Connection": "keep-alive"}) |
| 334 |
response = conn.getresponse() |
| 335 |
self.assertTrue(response.will_close) |
| 336 |
self.assertEqual(response.read(), b"example view") |
| 337 |
self.assertEqual(response.status, 200) |
| 338 |
self.assertEqual(response.getheader("Connection"), "close") |
| 339 |
finally: |
| 340 |
conn.close() |
| 341 |
|
| 342 |
|
| 343 |
class LiveServerDatabase(LiveServerBase): |
| 344 |
def test_fixtures_loaded(self): |
| 345 |
""" |
| 346 |
Fixtures are properly loaded and visible to the live server thread. |
| 347 |
""" |
| 348 |
with self.urlopen("/model_view/") as f: |
| 349 |
self.assertCountEqual(f.read().splitlines(), [b"jane", b"robert"]) |
| 350 |
|
| 351 |
def test_database_writes(self): |
| 352 |
""" |
| 353 |
Data written to the database by a view can be read. |
| 354 |
""" |
| 355 |
with self.urlopen("/create_model_instance/"): |
| 356 |
pass |
| 357 |
self.assertQuerySetEqual( |
| 358 |
Person.objects.order_by("pk"), |
| 359 |
["jane", "robert", "emily"], |
| 360 |
lambda b: b.name, |
| 361 |
) |
| 362 |
|
| 363 |
|
| 364 |
class LiveServerPort(LiveServerBase): |
| 365 |
def test_port_bind(self): |
| 366 |
""" |
| 367 |
Each LiveServerTestCase binds to a unique port or fails to start a |
| 368 |
server thread when run concurrently (#26011). |
| 369 |
""" |
| 370 |
TestCase = type("TestCase", (LiveServerBase,), {}) |
| 371 |
try: |
| 372 |
TestCase._start_server_thread() |
| 373 |
except OSError as e: |
| 374 |
if e.errno == errno.EADDRINUSE: |
| 375 |
# We're out of ports, LiveServerTestCase correctly fails with |
| 376 |
# an OSError. |
| 377 |
return |
| 378 |
# Unexpected error. |
| 379 |
raise |
| 380 |
try: |
| 381 |
self.assertNotEqual( |
| 382 |
self.live_server_url, |
| 383 |
TestCase.live_server_url, |
| 384 |
f"Acquired duplicate server addresses for server threads: " |
| 385 |
f"{self.live_server_url}", |
| 386 |
) |
| 387 |
finally: |
| 388 |
TestCase.doClassCleanups() |
| 389 |
|
| 390 |
def test_specified_port_bind(self): |
| 391 |
"""LiveServerTestCase.port customizes the server's port.""" |
| 392 |
TestCase = type("TestCase", (LiveServerBase,), {}) |
| 393 |
# Find an open port and tell TestCase to use it. |
| 394 |
s = socket.socket() |
| 395 |
s.bind(("", 0)) |
| 396 |
TestCase.port = s.getsockname()[1] |
| 397 |
s.close() |
| 398 |
TestCase._start_server_thread() |
| 399 |
try: |
| 400 |
self.assertEqual( |
| 401 |
TestCase.port, |
| 402 |
TestCase.server_thread.port, |
| 403 |
f"Did not use specified port for LiveServerTestCase thread: " |
| 404 |
f"{TestCase.port}", |
| 405 |
) |
| 406 |
finally: |
| 407 |
TestCase.doClassCleanups() |
| 408 |
|
| 409 |
|
| 410 |
class LiveServerThreadedTests(LiveServerBase): |
| 411 |
"""If LiveServerTestCase isn't threaded, these tests will hang.""" |
| 412 |
|
| 413 |
def test_view_calls_subview(self): |
| 414 |
url = "/subview_calling_view/?%s" % urlencode({"url": self.live_server_url}) |
| 415 |
with self.urlopen(url) as f: |
| 416 |
self.assertEqual(f.read(), b"subview calling view: subview") |
| 417 |
|
| 418 |
def test_check_model_instance_from_subview(self): |
| 419 |
url = "/check_model_instance_from_subview/?%s" % urlencode( |
| 420 |
{ |
| 421 |
"url": self.live_server_url, |
| 422 |
} |
| 423 |
) |
| 424 |
with self.urlopen(url) as f: |
| 425 |
self.assertIn(b"emily", f.read()) |