2009-07-29 10:40:14 +08:00
|
|
|
"""
|
|
|
|
Tests for django.core.servers.
|
|
|
|
"""
|
2024-01-26 19:45:07 +08:00
|
|
|
|
2015-12-30 20:23:50 +08:00
|
|
|
import errno
|
2009-07-29 10:40:14 +08:00
|
|
|
import os
|
2013-09-22 21:55:09 +08:00
|
|
|
import socket
|
2021-02-15 14:10:59 +08:00
|
|
|
import threading
|
2022-11-19 01:26:59 +08:00
|
|
|
import unittest
|
2018-11-05 02:03:20 +08:00
|
|
|
from http.client import HTTPConnection
|
2017-01-07 19:11:46 +08:00
|
|
|
from urllib.error import HTTPError
|
2017-01-26 21:25:15 +08:00
|
|
|
from urllib.parse import urlencode
|
2017-01-07 19:11:46 +08:00
|
|
|
from urllib.request import urlopen
|
2009-07-29 10:40:14 +08:00
|
|
|
|
2021-02-11 21:43:36 +08:00
|
|
|
from django.conf import settings
|
2021-02-15 14:10:59 +08:00
|
|
|
from django.core.servers.basehttp import ThreadedWSGIServer, WSGIServer
|
2022-11-19 01:26:59 +08:00
|
|
|
from django.db import DEFAULT_DB_ALIAS, connection, connections
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.test import LiveServerTestCase, override_settings
|
2019-07-06 23:43:59 +08:00
|
|
|
from django.test.testcases import LiveServerThread, QuietWSGIRequestHandler
|
2009-07-29 10:40:14 +08:00
|
|
|
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
from .models import Person
|
2009-07-29 10:40:14 +08:00
|
|
|
|
2017-01-20 21:01:02 +08:00
|
|
|
TEST_ROOT = os.path.dirname(__file__)
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
TEST_SETTINGS = {
|
2021-02-06 01:41:49 +08:00
|
|
|
"MEDIA_URL": "media/",
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
"MEDIA_ROOT": os.path.join(TEST_ROOT, "media"),
|
2021-02-06 01:41:49 +08:00
|
|
|
"STATIC_URL": "static/",
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
"STATIC_ROOT": os.path.join(TEST_ROOT, "static"),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-19 02:03:10 +08:00
|
|
|
@override_settings(ROOT_URLCONF="servers.urls", **TEST_SETTINGS)
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
class LiveServerBase(LiveServerTestCase):
|
2013-06-04 14:09:29 +08:00
|
|
|
available_apps = [
|
|
|
|
"servers",
|
|
|
|
"django.contrib.auth",
|
|
|
|
"django.contrib.contenttypes",
|
|
|
|
"django.contrib.sessions",
|
|
|
|
]
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
fixtures = ["testdata.json"]
|
|
|
|
|
|
|
|
def urlopen(self, url):
|
2012-07-20 21:36:52 +08:00
|
|
|
return urlopen(self.live_server_url + url)
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
|
|
|
|
|
2021-02-15 14:10:59 +08:00
|
|
|
class CloseConnectionTestServer(ThreadedWSGIServer):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
# This event is set right after the first time a request closes its
|
|
|
|
# database connections.
|
|
|
|
self._connections_closed = threading.Event()
|
|
|
|
|
|
|
|
def _close_connections(self):
|
|
|
|
super()._close_connections()
|
|
|
|
self._connections_closed.set()
|
|
|
|
|
|
|
|
|
|
|
|
class CloseConnectionTestLiveServerThread(LiveServerThread):
|
|
|
|
server_class = CloseConnectionTestServer
|
|
|
|
|
|
|
|
def _create_server(self, connections_override=None):
|
|
|
|
return super()._create_server(connections_override=self.connections_override)
|
|
|
|
|
|
|
|
|
|
|
|
class LiveServerTestCloseConnectionTest(LiveServerBase):
|
|
|
|
server_thread_class = CloseConnectionTestLiveServerThread
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _make_connections_override(cls):
|
|
|
|
conn = connections[DEFAULT_DB_ALIAS]
|
|
|
|
cls.conn = conn
|
|
|
|
cls.old_conn_max_age = conn.settings_dict["CONN_MAX_AGE"]
|
|
|
|
# Set the connection's CONN_MAX_AGE to None to simulate the
|
|
|
|
# CONN_MAX_AGE setting being set to None on the server. This prevents
|
|
|
|
# Django from closing the connection and allows testing that
|
|
|
|
# ThreadedWSGIServer closes connections.
|
|
|
|
conn.settings_dict["CONN_MAX_AGE"] = None
|
|
|
|
# Pass a database connection through to the server to check it is being
|
|
|
|
# closed by ThreadedWSGIServer.
|
|
|
|
return {DEFAULT_DB_ALIAS: conn}
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownConnectionTest(cls):
|
|
|
|
cls.conn.settings_dict["CONN_MAX_AGE"] = cls.old_conn_max_age
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
cls.tearDownConnectionTest()
|
|
|
|
super().tearDownClass()
|
|
|
|
|
|
|
|
def test_closes_connections(self):
|
|
|
|
# The server's request thread sets this event after closing
|
|
|
|
# its database connections.
|
|
|
|
closed_event = self.server_thread.httpd._connections_closed
|
|
|
|
conn = self.conn
|
|
|
|
# Open a connection to the database.
|
|
|
|
conn.connect()
|
|
|
|
self.assertIsNotNone(conn.connection)
|
|
|
|
with self.urlopen("/model_view/") as f:
|
|
|
|
# The server can access the database.
|
2022-04-14 18:12:13 +08:00
|
|
|
self.assertCountEqual(f.read().splitlines(), [b"jane", b"robert"])
|
2021-02-15 14:10:59 +08:00
|
|
|
# Wait for the server's request thread to close the connection.
|
|
|
|
# A timeout of 0.1 seconds should be more than enough. If the wait
|
|
|
|
# times out, the assertion after should fail.
|
|
|
|
closed_event.wait(timeout=0.1)
|
|
|
|
self.assertIsNone(conn.connection)
|
|
|
|
|
|
|
|
|
2022-11-19 01:26:59 +08:00
|
|
|
@unittest.skipUnless(connection.vendor == "sqlite", "SQLite specific test.")
|
|
|
|
class LiveServerInMemoryDatabaseLockTest(LiveServerBase):
|
|
|
|
def test_in_memory_database_lock(self):
|
|
|
|
"""
|
|
|
|
With a threaded LiveServer and an in-memory database, an error can
|
|
|
|
occur when 2 requests reach the server and try to lock the database
|
|
|
|
at the same time, if the requests do not share the same database
|
|
|
|
connection.
|
|
|
|
"""
|
|
|
|
conn = self.server_thread.connections_override[DEFAULT_DB_ALIAS]
|
2023-08-23 15:09:23 +08:00
|
|
|
source_connection = conn.connection
|
2022-11-19 01:26:59 +08:00
|
|
|
# Open a connection to the database.
|
|
|
|
conn.connect()
|
|
|
|
# Create a transaction to lock the database.
|
|
|
|
cursor = conn.cursor()
|
|
|
|
cursor.execute("BEGIN IMMEDIATE TRANSACTION")
|
|
|
|
try:
|
|
|
|
with self.urlopen("/create_model_instance/") as f:
|
|
|
|
self.assertEqual(f.status, 200)
|
|
|
|
except HTTPError:
|
|
|
|
self.fail("Unexpected error due to a database lock.")
|
|
|
|
finally:
|
|
|
|
# Release the transaction.
|
|
|
|
cursor.execute("ROLLBACK")
|
2023-08-23 15:09:23 +08:00
|
|
|
source_connection.close()
|
2022-11-19 01:26:59 +08:00
|
|
|
|
|
|
|
|
2021-02-11 21:43:36 +08:00
|
|
|
class FailingLiveServerThread(LiveServerThread):
|
2022-11-19 01:26:59 +08:00
|
|
|
def _create_server(self, connections_override=None):
|
2021-02-11 21:43:36 +08:00
|
|
|
raise RuntimeError("Error creating server.")
|
|
|
|
|
|
|
|
|
|
|
|
class LiveServerTestCaseSetupTest(LiveServerBase):
|
|
|
|
server_thread_class = FailingLiveServerThread
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def check_allowed_hosts(cls, expected):
|
|
|
|
if settings.ALLOWED_HOSTS != expected:
|
|
|
|
raise RuntimeError(f"{settings.ALLOWED_HOSTS} != {expected}")
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
cls.check_allowed_hosts(["testserver"])
|
|
|
|
try:
|
|
|
|
super().setUpClass()
|
|
|
|
except RuntimeError:
|
|
|
|
# LiveServerTestCase's change to ALLOWED_HOSTS should be reverted.
|
2021-10-17 05:15:50 +08:00
|
|
|
cls.doClassCleanups()
|
2021-02-11 21:43:36 +08:00
|
|
|
cls.check_allowed_hosts(["testserver"])
|
|
|
|
else:
|
|
|
|
raise RuntimeError("Server did not fail.")
|
|
|
|
cls.set_up_called = True
|
|
|
|
|
|
|
|
def test_set_up_class(self):
|
|
|
|
self.assertIs(self.set_up_called, True)
|
|
|
|
|
|
|
|
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
class LiveServerAddress(LiveServerBase):
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().setUpClass()
|
2015-06-11 04:57:51 +08:00
|
|
|
# put it in a list to prevent descriptor lookups in test
|
|
|
|
cls.live_server_url_test = [cls.live_server_url]
|
|
|
|
|
|
|
|
def test_live_server_url_is_class_property(self):
|
2016-12-29 23:27:49 +08:00
|
|
|
self.assertIsInstance(self.live_server_url_test[0], str)
|
2015-06-11 04:57:51 +08:00
|
|
|
self.assertEqual(self.live_server_url_test[0], self.live_server_url)
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2019-07-06 23:43:59 +08:00
|
|
|
class LiveServerSingleThread(LiveServerThread):
|
2022-11-19 01:26:59 +08:00
|
|
|
def _create_server(self, connections_override=None):
|
2019-07-06 23:43:59 +08:00
|
|
|
return WSGIServer(
|
|
|
|
(self.host, self.port), QuietWSGIRequestHandler, allow_reuse_address=False
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class SingleThreadLiveServerTestCase(LiveServerTestCase):
|
|
|
|
server_thread_class = LiveServerSingleThread
|
|
|
|
|
|
|
|
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
class LiveServerViews(LiveServerBase):
|
2015-10-31 07:38:34 +08:00
|
|
|
def test_protocol(self):
|
|
|
|
"""Launched server serves with HTTP 1.1."""
|
2017-07-28 22:10:13 +08:00
|
|
|
with self.urlopen("/example_view/") as f:
|
|
|
|
self.assertEqual(f.version, 11)
|
|
|
|
|
|
|
|
def test_closes_connection_without_content_length(self):
|
|
|
|
"""
|
2021-10-19 00:06:00 +08:00
|
|
|
An HTTP 1.1 server is supposed to support keep-alive. Since our
|
2018-11-05 02:03:20 +08:00
|
|
|
development server is rather simple we support it only in cases where
|
|
|
|
we can detect a content length from the response. This should be doable
|
|
|
|
for all simple views and streaming responses where an iterable with
|
|
|
|
length of one is passed. The latter follows as result of `set_content_length`
|
2021-11-22 18:47:38 +08:00
|
|
|
from https://github.com/python/cpython/blob/main/Lib/wsgiref/handlers.py.
|
2018-11-05 02:03:20 +08:00
|
|
|
|
|
|
|
If we cannot detect a content length we explicitly set the `Connection`
|
|
|
|
header to `close` to notify the client that we do not actually support
|
|
|
|
it.
|
2017-07-28 22:10:13 +08:00
|
|
|
"""
|
|
|
|
conn = HTTPConnection(
|
|
|
|
LiveServerViews.server_thread.host,
|
|
|
|
LiveServerViews.server_thread.port,
|
|
|
|
timeout=1,
|
|
|
|
)
|
|
|
|
try:
|
2018-11-05 02:03:20 +08:00
|
|
|
conn.request(
|
|
|
|
"GET", "/streaming_example_view/", headers={"Connection": "keep-alive"}
|
|
|
|
)
|
|
|
|
response = conn.getresponse()
|
|
|
|
self.assertTrue(response.will_close)
|
|
|
|
self.assertEqual(response.read(), b"Iamastream")
|
|
|
|
self.assertEqual(response.status, 200)
|
|
|
|
self.assertEqual(response.getheader("Connection"), "close")
|
|
|
|
|
|
|
|
conn.request(
|
|
|
|
"GET", "/streaming_example_view/", headers={"Connection": "close"}
|
|
|
|
)
|
|
|
|
response = conn.getresponse()
|
|
|
|
self.assertTrue(response.will_close)
|
|
|
|
self.assertEqual(response.read(), b"Iamastream")
|
|
|
|
self.assertEqual(response.status, 200)
|
|
|
|
self.assertEqual(response.getheader("Connection"), "close")
|
|
|
|
finally:
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
def test_keep_alive_on_connection_with_content_length(self):
|
|
|
|
"""
|
|
|
|
See `test_closes_connection_without_content_length` for details. This
|
|
|
|
is a follow up test, which ensure that we do not close the connection
|
|
|
|
if not needed, hence allowing us to take advantage of keep-alive.
|
|
|
|
"""
|
|
|
|
conn = HTTPConnection(
|
|
|
|
LiveServerViews.server_thread.host, LiveServerViews.server_thread.port
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
conn.request("GET", "/example_view/", headers={"Connection": "keep-alive"})
|
|
|
|
response = conn.getresponse()
|
|
|
|
self.assertFalse(response.will_close)
|
|
|
|
self.assertEqual(response.read(), b"example view")
|
|
|
|
self.assertEqual(response.status, 200)
|
|
|
|
self.assertIsNone(response.getheader("Connection"))
|
|
|
|
|
|
|
|
conn.request("GET", "/example_view/", headers={"Connection": "close"})
|
|
|
|
response = conn.getresponse()
|
|
|
|
self.assertFalse(response.will_close)
|
|
|
|
self.assertEqual(response.read(), b"example view")
|
|
|
|
self.assertEqual(response.status, 200)
|
|
|
|
self.assertIsNone(response.getheader("Connection"))
|
2017-07-28 22:10:13 +08:00
|
|
|
finally:
|
|
|
|
conn.close()
|
2015-10-31 07:38:34 +08:00
|
|
|
|
2018-12-06 23:08:01 +08:00
|
|
|
def test_keep_alive_connection_clears_previous_request_data(self):
|
|
|
|
conn = HTTPConnection(
|
|
|
|
LiveServerViews.server_thread.host, LiveServerViews.server_thread.port
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
conn.request(
|
|
|
|
"POST", "/method_view/", b"{}", headers={"Connection": "keep-alive"}
|
|
|
|
)
|
|
|
|
response = conn.getresponse()
|
|
|
|
self.assertFalse(response.will_close)
|
|
|
|
self.assertEqual(response.status, 200)
|
|
|
|
self.assertEqual(response.read(), b"POST")
|
|
|
|
|
|
|
|
conn.request(
|
|
|
|
"POST", "/method_view/", b"{}", headers={"Connection": "close"}
|
|
|
|
)
|
|
|
|
response = conn.getresponse()
|
|
|
|
self.assertFalse(response.will_close)
|
|
|
|
self.assertEqual(response.status, 200)
|
|
|
|
self.assertEqual(response.read(), b"POST")
|
|
|
|
finally:
|
|
|
|
conn.close()
|
|
|
|
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
def test_404(self):
|
2016-06-16 21:07:51 +08:00
|
|
|
with self.assertRaises(HTTPError) as err:
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
self.urlopen("/")
|
2017-06-05 21:56:51 +08:00
|
|
|
err.exception.close()
|
2016-06-16 21:07:51 +08:00
|
|
|
self.assertEqual(err.exception.code, 404, "Expected 404 response")
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
|
|
|
|
def test_view(self):
|
2017-01-19 21:50:28 +08:00
|
|
|
with self.urlopen("/example_view/") as f:
|
2015-07-02 05:37:10 +08:00
|
|
|
self.assertEqual(f.read(), b"example view")
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
|
|
|
|
def test_static_files(self):
|
2017-01-19 21:50:28 +08:00
|
|
|
with self.urlopen("/static/example_static_file.txt") as f:
|
2015-07-02 05:37:10 +08:00
|
|
|
self.assertEqual(f.read().rstrip(b"\r\n"), b"example static file")
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
|
2013-06-02 01:24:46 +08:00
|
|
|
def test_no_collectstatic_emulation(self):
|
2013-08-11 03:32:07 +08:00
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
LiveServerTestCase reports a 404 status code when HTTP client
|
2014-03-02 22:25:53 +08:00
|
|
|
tries to access a static file that isn't explicitly put under
|
2013-06-02 01:24:46 +08:00
|
|
|
STATIC_ROOT.
|
2013-08-11 03:32:07 +08:00
|
|
|
"""
|
2016-06-16 21:07:51 +08:00
|
|
|
with self.assertRaises(HTTPError) as err:
|
2013-06-02 01:24:46 +08:00
|
|
|
self.urlopen("/static/another_app/another_app_static_file.txt")
|
2017-06-05 21:56:51 +08:00
|
|
|
err.exception.close()
|
2016-06-16 21:07:51 +08:00
|
|
|
self.assertEqual(err.exception.code, 404, "Expected 404 response")
|
2013-08-11 03:32:07 +08:00
|
|
|
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
def test_media_files(self):
|
2017-01-19 21:50:28 +08:00
|
|
|
with self.urlopen("/media/example_media_file.txt") as f:
|
2015-07-02 05:37:10 +08:00
|
|
|
self.assertEqual(f.read().rstrip(b"\r\n"), b"example media file")
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
|
2012-10-20 18:34:50 +08:00
|
|
|
def test_environ(self):
|
2017-01-19 21:50:28 +08:00
|
|
|
with self.urlopen("/environ_view/?%s" % urlencode({"q": "тест"})) as f:
|
2015-07-02 05:37:10 +08:00
|
|
|
self.assertIn(b"QUERY_STRING: 'q=%D1%82%D0%B5%D1%81%D1%82'", f.read())
|
2012-10-20 18:34:50 +08:00
|
|
|
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
|
2019-07-06 23:43:59 +08:00
|
|
|
@override_settings(ROOT_URLCONF="servers.urls")
|
2021-10-25 13:42:06 +08:00
|
|
|
class SingleThreadLiveServerViews(SingleThreadLiveServerTestCase):
|
2019-07-06 23:43:59 +08:00
|
|
|
available_apps = ["servers"]
|
|
|
|
|
|
|
|
def test_closes_connection_with_content_length(self):
|
|
|
|
"""
|
|
|
|
Contrast to
|
|
|
|
LiveServerViews.test_keep_alive_on_connection_with_content_length().
|
|
|
|
Persistent connections require threading server.
|
|
|
|
"""
|
|
|
|
conn = HTTPConnection(
|
2021-10-25 13:42:06 +08:00
|
|
|
SingleThreadLiveServerViews.server_thread.host,
|
|
|
|
SingleThreadLiveServerViews.server_thread.port,
|
2019-07-06 23:43:59 +08:00
|
|
|
timeout=1,
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
conn.request("GET", "/example_view/", headers={"Connection": "keep-alive"})
|
|
|
|
response = conn.getresponse()
|
|
|
|
self.assertTrue(response.will_close)
|
|
|
|
self.assertEqual(response.read(), b"example view")
|
|
|
|
self.assertEqual(response.status, 200)
|
|
|
|
self.assertEqual(response.getheader("Connection"), "close")
|
|
|
|
finally:
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
class LiveServerDatabase(LiveServerBase):
|
|
|
|
def test_fixtures_loaded(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
Fixtures are properly loaded and visible to the live server thread.
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
"""
|
2017-01-19 21:50:28 +08:00
|
|
|
with self.urlopen("/model_view/") as f:
|
2022-04-14 18:12:13 +08:00
|
|
|
self.assertCountEqual(f.read().splitlines(), [b"jane", b"robert"])
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
|
|
|
|
def test_database_writes(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
Data written to the database by a view can be read.
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
"""
|
2017-06-05 21:56:51 +08:00
|
|
|
with self.urlopen("/create_model_instance/"):
|
|
|
|
pass
|
2022-09-24 18:29:58 +08:00
|
|
|
self.assertQuerySetEqual(
|
2022-02-22 17:29:38 +08:00
|
|
|
Person.objects.order_by("pk"),
|
2012-04-05 22:06:06 +08:00
|
|
|
["jane", "robert", "emily"],
|
|
|
|
lambda b: b.name,
|
|
|
|
)
|
2015-12-30 20:23:50 +08:00
|
|
|
|
|
|
|
|
|
|
|
class LiveServerPort(LiveServerBase):
|
|
|
|
def test_port_bind(self):
|
|
|
|
"""
|
|
|
|
Each LiveServerTestCase binds to a unique port or fails to start a
|
|
|
|
server thread when run concurrently (#26011).
|
|
|
|
"""
|
2017-01-19 22:48:01 +08:00
|
|
|
TestCase = type("TestCase", (LiveServerBase,), {})
|
2015-12-30 20:23:50 +08:00
|
|
|
try:
|
2021-10-17 05:15:50 +08:00
|
|
|
TestCase._start_server_thread()
|
2019-01-28 23:01:35 +08:00
|
|
|
except OSError as e:
|
2016-01-02 23:25:53 +08:00
|
|
|
if e.errno == errno.EADDRINUSE:
|
2015-12-30 20:23:50 +08:00
|
|
|
# We're out of ports, LiveServerTestCase correctly fails with
|
2019-01-28 23:01:35 +08:00
|
|
|
# an OSError.
|
2015-12-30 20:23:50 +08:00
|
|
|
return
|
|
|
|
# Unexpected error.
|
|
|
|
raise
|
2023-01-12 13:04:10 +08:00
|
|
|
try:
|
|
|
|
self.assertNotEqual(
|
|
|
|
self.live_server_url,
|
|
|
|
TestCase.live_server_url,
|
|
|
|
f"Acquired duplicate server addresses for server threads: "
|
|
|
|
f"{self.live_server_url}",
|
|
|
|
)
|
|
|
|
finally:
|
|
|
|
TestCase.doClassCleanups()
|
2016-12-07 03:38:43 +08:00
|
|
|
|
2017-05-23 01:16:56 +08:00
|
|
|
def test_specified_port_bind(self):
|
|
|
|
"""LiveServerTestCase.port customizes the server's port."""
|
2019-11-18 22:30:10 +08:00
|
|
|
TestCase = type("TestCase", (LiveServerBase,), {})
|
2017-05-23 01:16:56 +08:00
|
|
|
# Find an open port and tell TestCase to use it.
|
|
|
|
s = socket.socket()
|
|
|
|
s.bind(("", 0))
|
|
|
|
TestCase.port = s.getsockname()[1]
|
|
|
|
s.close()
|
2021-10-17 05:15:50 +08:00
|
|
|
TestCase._start_server_thread()
|
2023-01-12 13:04:10 +08:00
|
|
|
try:
|
|
|
|
self.assertEqual(
|
|
|
|
TestCase.port,
|
|
|
|
TestCase.server_thread.port,
|
|
|
|
f"Did not use specified port for LiveServerTestCase thread: "
|
|
|
|
f"{TestCase.port}",
|
|
|
|
)
|
|
|
|
finally:
|
|
|
|
TestCase.doClassCleanups()
|
2017-05-23 01:16:56 +08:00
|
|
|
|
2016-12-07 03:38:43 +08:00
|
|
|
|
2020-12-15 15:51:58 +08:00
|
|
|
class LiveServerThreadedTests(LiveServerBase):
|
|
|
|
"""If LiveServerTestCase isn't threaded, these tests will hang."""
|
2016-12-07 03:38:43 +08:00
|
|
|
|
|
|
|
def test_view_calls_subview(self):
|
|
|
|
url = "/subview_calling_view/?%s" % urlencode({"url": self.live_server_url})
|
|
|
|
with self.urlopen(url) as f:
|
|
|
|
self.assertEqual(f.read(), b"subview calling view: subview")
|
|
|
|
|
|
|
|
def test_check_model_instance_from_subview(self):
|
|
|
|
url = "/check_model_instance_from_subview/?%s" % urlencode(
|
|
|
|
{
|
|
|
|
"url": self.live_server_url,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
with self.urlopen(url) as f:
|
|
|
|
self.assertIn(b"emily", f.read())
|