2012-10-20 18:34:50 +08:00
|
|
|
# -*- encoding: utf-8 -*-
|
2009-07-29 10:40:14 +08:00
|
|
|
"""
|
|
|
|
Tests for django.core.servers.
|
|
|
|
"""
|
2012-10-20 18:34:50 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2009-07-29 10:40:14 +08:00
|
|
|
import os
|
2013-09-22 21:55:09 +08:00
|
|
|
import socket
|
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 django.core.exceptions import ImproperlyConfigured
|
2012-04-09 05:13:32 +08:00
|
|
|
from django.test import LiveServerTestCase
|
2013-12-23 23:01:13 +08:00
|
|
|
from django.test import override_settings
|
2012-10-20 18:34:50 +08:00
|
|
|
from django.utils.http import urlencode
|
2013-09-06 03:38:59 +08:00
|
|
|
from django.utils.six.moves.urllib.error import HTTPError
|
|
|
|
from django.utils.six.moves.urllib.request import urlopen
|
2012-12-08 18:13:52 +08:00
|
|
|
from django.utils._os import upath
|
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
|
|
|
|
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-12-08 18:13:52 +08: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 16:33:58 +08: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'),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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']
|
2013-06-04 14:09:29 +08:00
|
|
|
urls = 'servers.urls'
|
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
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
# Override settings
|
|
|
|
cls.settings_override = override_settings(**TEST_SETTINGS)
|
|
|
|
cls.settings_override.enable()
|
|
|
|
super(LiveServerBase, cls).setUpClass()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
# Restore original settings
|
|
|
|
cls.settings_override.disable()
|
|
|
|
super(LiveServerBase, cls).tearDownClass()
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
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-30 04:22:13 +08: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 16:33:58 +08:00
|
|
|
|
|
|
|
# The host must be valid
|
2013-09-22 21:55:09 +08:00
|
|
|
cls.raises_exception('blahblahblah:8081', socket.error)
|
2011-12-30 04:22:13 +08: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 16:33:58 +08: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']
|
|
|
|
|
2013-04-19 17:44:47 +08:00
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
# skip it, as setUpClass doesn't call its parent either
|
|
|
|
pass
|
|
|
|
|
2011-12-30 04:22:13 +08: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 15:56:30 +08:00
|
|
|
finally:
|
|
|
|
super(LiveServerAddress, cls).tearDownClass()
|
2011-12-30 04:22:13 +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_test_test(self):
|
|
|
|
# Intentionally empty method so that the test is picked up by the
|
2013-07-28 09:45:25 +08:00
|
|
|
# test runner and the overridden setUpClass() method is executed.
|
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
|
|
|
pass
|
|
|
|
|
2013-11-03 05:34:05 +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
|
|
|
class LiveServerViews(LiveServerBase):
|
|
|
|
def test_404(self):
|
|
|
|
"""
|
|
|
|
Ensure that the LiveServerTestCase serves 404s.
|
|
|
|
Refs #2879.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
self.urlopen('/')
|
2012-07-20 21:36:52 +08:00
|
|
|
except HTTPError as err:
|
2012-05-03 22:39:16 +08: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 16:33:58 +08:00
|
|
|
else:
|
|
|
|
self.fail('Expected 404 response')
|
|
|
|
|
|
|
|
def test_view(self):
|
|
|
|
"""
|
|
|
|
Ensure that the LiveServerTestCase serves views.
|
|
|
|
Refs #2879.
|
|
|
|
"""
|
|
|
|
f = self.urlopen('/example_view/')
|
2012-05-17 17:05:38 +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):
|
|
|
|
"""
|
|
|
|
Ensure that the LiveServerTestCase serves static files.
|
|
|
|
Refs #2879.
|
|
|
|
"""
|
|
|
|
f = self.urlopen('/static/example_static_file.txt')
|
2012-05-17 17:05:38 +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
|
|
|
"""
|
2013-06-02 01:24:46 +08:00
|
|
|
Test that 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
|
|
|
"""
|
2013-06-02 01:24:46 +08: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-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):
|
|
|
|
"""
|
|
|
|
Ensure that the LiveServerTestCase serves media files.
|
|
|
|
Refs #2879.
|
|
|
|
"""
|
|
|
|
f = self.urlopen('/media/example_media_file.txt')
|
2012-05-17 17:05:38 +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):
|
|
|
|
f = self.urlopen('/environ_view/?%s' % urlencode({'q': 'тест'}))
|
|
|
|
self.assertIn(b"QUERY_STRING: 'q=%D1%82%D0%B5%D1%81%D1%82'", f.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
|
|
|
|
|
|
|
class LiveServerDatabase(LiveServerBase):
|
|
|
|
|
|
|
|
def test_fixtures_loaded(self):
|
|
|
|
"""
|
|
|
|
Ensure that fixtures are properly loaded and visible to the
|
|
|
|
live server thread.
|
|
|
|
Refs #2879.
|
|
|
|
"""
|
|
|
|
f = self.urlopen('/model_view/')
|
2012-08-15 23:41:09 +08:00
|
|
|
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 16:33:58 +08: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 22:06:06 +08:00
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Person.objects.all().order_by('pk'),
|
|
|
|
['jane', 'robert', 'emily'],
|
|
|
|
lambda b: b.name
|
|
|
|
)
|