2012-10-20 12:34:50 +02:00
|
|
|
# -*- encoding: utf-8 -*-
|
2009-07-29 04:40:14 +02:00
|
|
|
"""
|
|
|
|
Tests for django.core.servers.
|
|
|
|
"""
|
2012-10-20 12:34:50 +02:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2015-07-01 23:37:10 +02:00
|
|
|
import contextlib
|
2015-12-30 13:23:50 +01:00
|
|
|
import errno
|
2009-07-29 04:40:14 +02:00
|
|
|
import os
|
2013-09-22 15:55:09 +02:00
|
|
|
import socket
|
2009-07-29 04:40:14 +02:00
|
|
|
|
2015-01-28 13:35:27 +01:00
|
|
|
from django.test import LiveServerTestCase, override_settings
|
|
|
|
from django.utils._os import upath
|
2012-10-20 12:34:50 +02:00
|
|
|
from django.utils.http import urlencode
|
2015-06-10 22:57:51 +02:00
|
|
|
from django.utils.six import text_type
|
2013-09-05 21:38:59 +02:00
|
|
|
from django.utils.six.moves.urllib.error import HTTPError
|
|
|
|
from django.utils.six.moves.urllib.request import urlopen
|
2009-07-29 04:40:14 +02: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 09:33:58 +01:00
|
|
|
from .models import Person
|
2009-07-29 04:40:14 +02:00
|
|
|
|
2012-12-08 11:13:52 +01: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 09:33:58 +01: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 20:03:10 +02: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 09:33:58 +01:00
|
|
|
class LiveServerBase(LiveServerTestCase):
|
2013-06-04 08:09:29 +02: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 09:33:58 +01:00
|
|
|
fixtures = ['testdata.json']
|
|
|
|
|
|
|
|
def urlopen(self, url):
|
2012-07-20 15:36:52 +02: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 09:33:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
class LiveServerAddress(LiveServerBase):
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
2016-06-23 18:04:05 +02:00
|
|
|
super(LiveServerAddress, cls).setUpClass()
|
2015-06-10 22:57:51 +02: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):
|
|
|
|
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 09:33:58 +01:00
|
|
|
|
2013-11-02 22:34:05 +01: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 09:33:58 +01:00
|
|
|
class LiveServerViews(LiveServerBase):
|
|
|
|
def test_404(self):
|
|
|
|
"""
|
|
|
|
Ensure that the LiveServerTestCase serves 404s.
|
|
|
|
Refs #2879.
|
|
|
|
"""
|
2016-06-16 15:07:51 +02: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 09:33:58 +01:00
|
|
|
self.urlopen('/')
|
2016-06-16 15:07:51 +02: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 09:33:58 +01:00
|
|
|
|
|
|
|
def test_view(self):
|
|
|
|
"""
|
|
|
|
Ensure that the LiveServerTestCase serves views.
|
|
|
|
Refs #2879.
|
|
|
|
"""
|
2015-07-01 23:37:10 +02: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 09:33:58 +01:00
|
|
|
|
|
|
|
def test_static_files(self):
|
|
|
|
"""
|
|
|
|
Ensure that the LiveServerTestCase serves static files.
|
|
|
|
Refs #2879.
|
|
|
|
"""
|
2015-07-01 23:37:10 +02: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 09:33:58 +01:00
|
|
|
|
2013-06-01 19:24:46 +02:00
|
|
|
def test_no_collectstatic_emulation(self):
|
2013-08-10 21:32:07 +02:00
|
|
|
"""
|
2013-06-01 19:24:46 +02:00
|
|
|
Test that LiveServerTestCase reports a 404 status code when HTTP client
|
2014-03-02 15:25:53 +01:00
|
|
|
tries to access a static file that isn't explicitly put under
|
2013-06-01 19:24:46 +02:00
|
|
|
STATIC_ROOT.
|
2013-08-10 21:32:07 +02:00
|
|
|
"""
|
2016-06-16 15:07:51 +02:00
|
|
|
with self.assertRaises(HTTPError) as err:
|
2013-06-01 19:24:46 +02:00
|
|
|
self.urlopen('/static/another_app/another_app_static_file.txt')
|
2016-06-16 15:07:51 +02:00
|
|
|
self.assertEqual(err.exception.code, 404, 'Expected 404 response')
|
2013-08-10 21:32:07 +02: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 09:33:58 +01:00
|
|
|
def test_media_files(self):
|
|
|
|
"""
|
|
|
|
Ensure that the LiveServerTestCase serves media files.
|
|
|
|
Refs #2879.
|
|
|
|
"""
|
2015-07-01 23:37:10 +02: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 09:33:58 +01:00
|
|
|
|
2012-10-20 12:34:50 +02:00
|
|
|
def test_environ(self):
|
2015-07-01 23:37:10 +02: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 12:34:50 +02: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 09:33:58 +01: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 23:37:10 +02: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 09:33:58 +01: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 16:06:06 +02:00
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Person.objects.all().order_by('pk'),
|
|
|
|
['jane', 'robert', 'emily'],
|
|
|
|
lambda b: b.name
|
|
|
|
)
|
2015-12-30 13:23:50 +01: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 16:25:53 +01:00
|
|
|
if e.errno == errno.EADDRINUSE:
|
2015-12-30 13:23:50 +01: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()
|