2012-10-20 10:34:50 +00:00
|
|
|
# -*- encoding: utf-8 -*-
|
2009-07-29 02:40:14 +00:00
|
|
|
"""
|
|
|
|
Tests for django.core.servers.
|
|
|
|
"""
|
2012-10-20 10:34:50 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2015-07-01 21:37:10 +00:00
|
|
|
import contextlib
|
2015-12-30 12:23:50 +00:00
|
|
|
import errno
|
2009-07-29 02:40:14 +00:00
|
|
|
import os
|
2013-09-22 13:55:09 +00:00
|
|
|
import socket
|
2009-07-29 02:40:14 +00: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 08:33:58 +00:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2015-01-28 12:35:27 +00:00
|
|
|
from django.test import LiveServerTestCase, override_settings
|
|
|
|
from django.utils._os import upath
|
2012-10-20 10:34:50 +00:00
|
|
|
from django.utils.http import urlencode
|
2015-06-10 20:57:51 +00:00
|
|
|
from django.utils.six import text_type
|
2013-09-05 19:38:59 +00:00
|
|
|
from django.utils.six.moves.urllib.error import HTTPError
|
|
|
|
from django.utils.six.moves.urllib.request import urlopen
|
2009-07-29 02:40:14 +00: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 08:33:58 +00:00
|
|
|
from .models import Person
|
2009-07-29 02:40:14 +00:00
|
|
|
|
2012-12-08 10:13:52 +00:00
|
|
|
TEST_ROOT = os.path.dirname(upath(__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 08:33:58 +00:00
|
|
|
TEST_SETTINGS = {
|
|
|
|
'MEDIA_URL': '/media/',
|
|
|
|
'MEDIA_ROOT': os.path.join(TEST_ROOT, 'media'),
|
|
|
|
'STATIC_URL': '/static/',
|
|
|
|
'STATIC_ROOT': os.path.join(TEST_ROOT, 'static'),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-18 18:03:10 +00: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 08:33:58 +00:00
|
|
|
class LiveServerBase(LiveServerTestCase):
|
2013-06-04 06:09:29 +00: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 08:33:58 +00:00
|
|
|
fixtures = ['testdata.json']
|
|
|
|
|
|
|
|
def urlopen(self, url):
|
2012-07-20 13:36:52 +00: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 08:33:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LiveServerAddress(LiveServerBase):
|
|
|
|
"""
|
|
|
|
Ensure that the address set in the environment variable is valid.
|
|
|
|
Refs #2879.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
# Backup original environment variable
|
|
|
|
address_predefined = 'DJANGO_LIVE_TEST_SERVER_ADDRESS' in os.environ
|
|
|
|
old_address = os.environ.get('DJANGO_LIVE_TEST_SERVER_ADDRESS')
|
|
|
|
|
|
|
|
# Just the host is not accepted
|
2011-12-29 20:22:13 +00:00
|
|
|
cls.raises_exception('localhost', ImproperlyConfigured)
|
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 08:33:58 +00:00
|
|
|
|
|
|
|
# The host must be valid
|
2013-09-22 13:55:09 +00:00
|
|
|
cls.raises_exception('blahblahblah:8081', socket.error)
|
2011-12-29 20:22:13 +00:00
|
|
|
|
|
|
|
# The list of ports must be in a valid format
|
|
|
|
cls.raises_exception('localhost:8081,', ImproperlyConfigured)
|
|
|
|
cls.raises_exception('localhost:8081,blah', ImproperlyConfigured)
|
|
|
|
cls.raises_exception('localhost:8081-', ImproperlyConfigured)
|
|
|
|
cls.raises_exception('localhost:8081-blah', ImproperlyConfigured)
|
|
|
|
cls.raises_exception('localhost:8081-8082-8083', ImproperlyConfigured)
|
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 08:33:58 +00:00
|
|
|
|
|
|
|
# Restore original environment variable
|
|
|
|
if address_predefined:
|
|
|
|
os.environ['DJANGO_LIVE_TEST_SERVER_ADDRESS'] = old_address
|
|
|
|
else:
|
|
|
|
del os.environ['DJANGO_LIVE_TEST_SERVER_ADDRESS']
|
|
|
|
|
2015-06-10 20:57:51 +00:00
|
|
|
# put it in a list to prevent descriptor lookups in test
|
|
|
|
cls.live_server_url_test = [cls.live_server_url]
|
|
|
|
|
2013-04-19 09:44:47 +00:00
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
# skip it, as setUpClass doesn't call its parent either
|
|
|
|
pass
|
|
|
|
|
2011-12-29 20:22:13 +00:00
|
|
|
@classmethod
|
|
|
|
def raises_exception(cls, address, exception):
|
|
|
|
os.environ['DJANGO_LIVE_TEST_SERVER_ADDRESS'] = address
|
|
|
|
try:
|
|
|
|
super(LiveServerAddress, cls).setUpClass()
|
|
|
|
raise Exception("The line above should have raised an exception")
|
|
|
|
except exception:
|
|
|
|
pass
|
2013-02-25 07:56:30 +00:00
|
|
|
finally:
|
|
|
|
super(LiveServerAddress, cls).tearDownClass()
|
2011-12-29 20:22:13 +00:00
|
|
|
|
2015-06-10 20:57:51 +00:00
|
|
|
def test_live_server_url_is_class_property(self):
|
|
|
|
self.assertIsInstance(self.live_server_url_test[0], text_type)
|
|
|
|
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 08:33:58 +00:00
|
|
|
|
2013-11-02 21:34:05 +00: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 08:33:58 +00:00
|
|
|
class LiveServerViews(LiveServerBase):
|
|
|
|
def test_404(self):
|
|
|
|
"""
|
|
|
|
Ensure that the LiveServerTestCase serves 404s.
|
|
|
|
Refs #2879.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
self.urlopen('/')
|
2012-07-20 13:36:52 +00:00
|
|
|
except HTTPError as err:
|
2012-05-03 14:39:16 +00:00
|
|
|
self.assertEqual(err.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 08:33:58 +00:00
|
|
|
else:
|
|
|
|
self.fail('Expected 404 response')
|
|
|
|
|
|
|
|
def test_view(self):
|
|
|
|
"""
|
|
|
|
Ensure that the LiveServerTestCase serves views.
|
|
|
|
Refs #2879.
|
|
|
|
"""
|
2015-07-01 21:37:10 +00:00
|
|
|
with contextlib.closing(self.urlopen('/example_view/')) as f:
|
|
|
|
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 08:33:58 +00:00
|
|
|
|
|
|
|
def test_static_files(self):
|
|
|
|
"""
|
|
|
|
Ensure that the LiveServerTestCase serves static files.
|
|
|
|
Refs #2879.
|
|
|
|
"""
|
2015-07-01 21:37:10 +00:00
|
|
|
with contextlib.closing(self.urlopen('/static/example_static_file.txt')) as f:
|
|
|
|
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 08:33:58 +00:00
|
|
|
|
2013-06-01 17:24:46 +00:00
|
|
|
def test_no_collectstatic_emulation(self):
|
2013-08-10 19:32:07 +00:00
|
|
|
"""
|
2013-06-01 17:24:46 +00:00
|
|
|
Test that LiveServerTestCase reports a 404 status code when HTTP client
|
2014-03-02 14:25:53 +00:00
|
|
|
tries to access a static file that isn't explicitly put under
|
2013-06-01 17:24:46 +00:00
|
|
|
STATIC_ROOT.
|
2013-08-10 19:32:07 +00:00
|
|
|
"""
|
2013-06-01 17:24:46 +00:00
|
|
|
try:
|
|
|
|
self.urlopen('/static/another_app/another_app_static_file.txt')
|
|
|
|
except HTTPError as err:
|
|
|
|
self.assertEqual(err.code, 404, 'Expected 404 response')
|
|
|
|
else:
|
|
|
|
self.fail('Expected 404 response (got %d)' % err.code)
|
2013-08-10 19:32:07 +00: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 08:33:58 +00:00
|
|
|
def test_media_files(self):
|
|
|
|
"""
|
|
|
|
Ensure that the LiveServerTestCase serves media files.
|
|
|
|
Refs #2879.
|
|
|
|
"""
|
2015-07-01 21:37:10 +00:00
|
|
|
with contextlib.closing(self.urlopen('/media/example_media_file.txt')) as f:
|
|
|
|
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 08:33:58 +00:00
|
|
|
|
2012-10-20 10:34:50 +00:00
|
|
|
def test_environ(self):
|
2015-07-01 21:37:10 +00:00
|
|
|
with contextlib.closing(self.urlopen('/environ_view/?%s' % urlencode({'q': 'тест'}))) as f:
|
|
|
|
self.assertIn(b"QUERY_STRING: 'q=%D1%82%D0%B5%D1%81%D1%82'", f.read())
|
2012-10-20 10:34:50 +00: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 08:33:58 +00:00
|
|
|
|
|
|
|
class LiveServerDatabase(LiveServerBase):
|
|
|
|
|
|
|
|
def test_fixtures_loaded(self):
|
|
|
|
"""
|
|
|
|
Ensure that fixtures are properly loaded and visible to the
|
|
|
|
live server thread.
|
|
|
|
Refs #2879.
|
|
|
|
"""
|
2015-07-01 21:37:10 +00:00
|
|
|
with contextlib.closing(self.urlopen('/model_view/')) as f:
|
|
|
|
self.assertEqual(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 08:33:58 +00:00
|
|
|
|
|
|
|
def test_database_writes(self):
|
|
|
|
"""
|
|
|
|
Ensure that data written to the database by a view can be read.
|
|
|
|
Refs #2879.
|
|
|
|
"""
|
|
|
|
self.urlopen('/create_model_instance/')
|
2012-04-05 14:06:06 +00:00
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Person.objects.all().order_by('pk'),
|
|
|
|
['jane', 'robert', 'emily'],
|
|
|
|
lambda b: b.name
|
|
|
|
)
|
2015-12-30 12:23:50 +00: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).
|
|
|
|
"""
|
|
|
|
TestCase = type(str("TestCase"), (LiveServerBase,), {})
|
|
|
|
try:
|
|
|
|
TestCase.setUpClass()
|
|
|
|
except socket.error as e:
|
2016-01-02 15:25:53 +00:00
|
|
|
if e.errno == errno.EADDRINUSE:
|
2015-12-30 12:23:50 +00:00
|
|
|
# We're out of ports, LiveServerTestCase correctly fails with
|
|
|
|
# a socket error.
|
|
|
|
return
|
|
|
|
# Unexpected error.
|
|
|
|
raise
|
|
|
|
try:
|
|
|
|
# We've acquired a port, ensure our server threads acquired
|
|
|
|
# different addresses.
|
|
|
|
self.assertNotEqual(
|
|
|
|
self.live_server_url, TestCase.live_server_url,
|
|
|
|
"Acquired duplicate server addresses for server threads: %s" % self.live_server_url
|
|
|
|
)
|
|
|
|
finally:
|
|
|
|
TestCase.tearDownClass()
|