2014-05-15 17:41:55 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2013-09-07 15:43:44 +00:00
|
|
|
|
2013-09-07 15:25:43 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2015-03-08 14:06:23 +00:00
|
|
|
from django.core.handlers.wsgi import WSGIHandler, WSGIRequest, get_script_name
|
2015-01-28 12:35:27 +00:00
|
|
|
from django.core.signals import request_finished, request_started
|
2013-03-06 10:12:24 +00:00
|
|
|
from django.db import close_old_connections, connection
|
2015-01-28 12:35:27 +00:00
|
|
|
from django.test import (
|
2015-04-17 21:38:20 +00:00
|
|
|
RequestFactory, SimpleTestCase, TransactionTestCase, override_settings,
|
2015-01-28 12:35:27 +00:00
|
|
|
)
|
2013-09-07 15:25:43 +00:00
|
|
|
from django.utils import six
|
2015-01-28 12:35:27 +00:00
|
|
|
from django.utils.encoding import force_str
|
2011-04-22 12:15:52 +00:00
|
|
|
|
2012-12-30 14:19:22 +00:00
|
|
|
|
2015-04-17 21:38:20 +00:00
|
|
|
class HandlerTests(SimpleTestCase):
|
2011-01-14 23:18:21 +00:00
|
|
|
|
2013-02-18 10:37:26 +00:00
|
|
|
def setUp(self):
|
|
|
|
request_started.disconnect(close_old_connections)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
request_started.connect(close_old_connections)
|
|
|
|
|
2012-10-20 21:22:46 +00:00
|
|
|
# Mangle settings so the handler will fail
|
|
|
|
@override_settings(MIDDLEWARE_CLASSES=42)
|
2011-01-14 23:18:21 +00:00
|
|
|
def test_lock_safety(self):
|
|
|
|
"""
|
|
|
|
Tests for bug #11193 (errors inside middleware shouldn't leave
|
|
|
|
the initLock locked).
|
|
|
|
"""
|
|
|
|
# Try running the handler, it will fail in load_middleware
|
|
|
|
handler = WSGIHandler()
|
|
|
|
self.assertEqual(handler.initLock.locked(), False)
|
2012-09-07 19:06:23 +00:00
|
|
|
with self.assertRaises(Exception):
|
2011-01-14 23:18:21 +00:00
|
|
|
handler(None, None)
|
|
|
|
self.assertEqual(handler.initLock.locked(), False)
|
|
|
|
|
2011-04-22 12:15:52 +00:00
|
|
|
def test_bad_path_info(self):
|
|
|
|
"""Tests for bug #15672 ('request' referenced before assignment)"""
|
|
|
|
environ = RequestFactory().get('/').environ
|
2013-09-07 15:25:43 +00:00
|
|
|
environ['PATH_INFO'] = b'\xed' if six.PY2 else '\xed'
|
2011-04-22 12:15:52 +00:00
|
|
|
handler = WSGIHandler()
|
|
|
|
response = handler(environ, lambda *a, **k: None)
|
|
|
|
self.assertEqual(response.status_code, 400)
|
2012-12-30 14:19:22 +00:00
|
|
|
|
2013-09-07 16:41:34 +00:00
|
|
|
def test_non_ascii_query_string(self):
|
2014-07-12 17:37:59 +00:00
|
|
|
"""
|
|
|
|
Test that non-ASCII query strings are properly decoded (#20530, #22996).
|
|
|
|
"""
|
2013-09-07 16:41:34 +00:00
|
|
|
environ = RequestFactory().get('/').environ
|
2014-07-12 17:37:59 +00:00
|
|
|
raw_query_strings = [
|
2014-08-20 18:37:33 +00:00
|
|
|
b'want=caf%C3%A9', # This is the proper way to encode 'café'
|
|
|
|
b'want=caf\xc3\xa9', # UA forgot to quote bytes
|
|
|
|
b'want=caf%E9', # UA quoted, but not in UTF-8
|
|
|
|
b'want=caf\xe9', # UA forgot to convert Latin-1 to UTF-8 and to quote (typical of MSIE)
|
2014-07-12 17:37:59 +00:00
|
|
|
]
|
|
|
|
got = []
|
|
|
|
for raw_query_string in raw_query_strings:
|
|
|
|
if six.PY3:
|
|
|
|
# Simulate http.server.BaseHTTPRequestHandler.parse_request handling of raw request
|
|
|
|
environ['QUERY_STRING'] = str(raw_query_string, 'iso-8859-1')
|
|
|
|
else:
|
|
|
|
environ['QUERY_STRING'] = raw_query_string
|
|
|
|
request = WSGIRequest(environ)
|
|
|
|
got.append(request.GET['want'])
|
|
|
|
if six.PY2:
|
|
|
|
self.assertListEqual(got, ['café', 'café', 'café', 'café'])
|
|
|
|
else:
|
|
|
|
# On Python 3, %E9 is converted to the unicode replacement character by parse_qsl
|
|
|
|
self.assertListEqual(got, ['café', 'café', 'caf\ufffd', 'café'])
|
2013-09-07 16:41:34 +00:00
|
|
|
|
2013-09-07 15:25:43 +00:00
|
|
|
def test_non_ascii_cookie(self):
|
|
|
|
"""Test that non-ASCII cookies set in JavaScript are properly decoded (#20557)."""
|
|
|
|
environ = RequestFactory().get('/').environ
|
2013-09-07 15:43:44 +00:00
|
|
|
raw_cookie = 'want="café"'
|
|
|
|
if six.PY3:
|
|
|
|
raw_cookie = raw_cookie.encode('utf-8').decode('iso-8859-1')
|
|
|
|
environ['HTTP_COOKIE'] = raw_cookie
|
2013-09-07 15:25:43 +00:00
|
|
|
request = WSGIRequest(environ)
|
2013-09-07 16:41:34 +00:00
|
|
|
# If would be nicer if request.COOKIES returned unicode values.
|
|
|
|
# However the current cookie parser doesn't do this and fixing it is
|
|
|
|
# much more work than fixing #20557. Feel free to remove force_str()!
|
2013-09-07 15:43:44 +00:00
|
|
|
self.assertEqual(request.COOKIES['want'], force_str("café"))
|
2013-09-07 15:25:43 +00:00
|
|
|
|
2014-10-12 18:53:19 +00:00
|
|
|
def test_invalid_unicode_cookie(self):
|
|
|
|
"""
|
|
|
|
Invalid cookie content should result in an absent cookie, but not in a
|
|
|
|
crash while trying to decode it (#23638).
|
|
|
|
"""
|
|
|
|
environ = RequestFactory().get('/').environ
|
|
|
|
environ['HTTP_COOKIE'] = 'x=W\x03c(h]\x8e'
|
|
|
|
request = WSGIRequest(environ)
|
2014-10-14 07:26:13 +00:00
|
|
|
# We don't test COOKIES content, as the result might differ between
|
|
|
|
# Python version because parsing invalid content became stricter in
|
|
|
|
# latest versions.
|
|
|
|
self.assertIsInstance(request.COOKIES, dict)
|
2014-10-12 18:53:19 +00:00
|
|
|
|
2014-11-21 20:47:46 +00:00
|
|
|
@override_settings(ROOT_URLCONF='handlers.urls')
|
|
|
|
def test_invalid_multipart_boundary(self):
|
|
|
|
"""
|
|
|
|
Invalid boundary string should produce a "Bad Request" response, not a
|
|
|
|
server error (#23887).
|
|
|
|
"""
|
|
|
|
environ = RequestFactory().post('/malformed_post/').environ
|
|
|
|
environ['CONTENT_TYPE'] = 'multipart/form-data; boundary=WRONG\x07'
|
|
|
|
handler = WSGIHandler()
|
|
|
|
response = handler(environ, lambda *a, **k: None)
|
|
|
|
# Expect "bad request" response
|
|
|
|
self.assertEqual(response.status_code, 400)
|
|
|
|
|
2012-12-30 14:19:22 +00:00
|
|
|
|
2014-04-05 06:04:46 +00:00
|
|
|
@override_settings(ROOT_URLCONF='handlers.urls')
|
2013-03-06 10:12:24 +00:00
|
|
|
class TransactionsPerRequestTests(TransactionTestCase):
|
2013-06-04 06:09:29 +00:00
|
|
|
|
|
|
|
available_apps = []
|
2013-03-06 10:12:24 +00:00
|
|
|
|
|
|
|
def test_no_transaction(self):
|
|
|
|
response = self.client.get('/in_transaction/')
|
|
|
|
self.assertContains(response, 'False')
|
|
|
|
|
|
|
|
def test_auto_transaction(self):
|
|
|
|
old_atomic_requests = connection.settings_dict['ATOMIC_REQUESTS']
|
|
|
|
try:
|
|
|
|
connection.settings_dict['ATOMIC_REQUESTS'] = True
|
|
|
|
response = self.client.get('/in_transaction/')
|
|
|
|
finally:
|
|
|
|
connection.settings_dict['ATOMIC_REQUESTS'] = old_atomic_requests
|
|
|
|
self.assertContains(response, 'True')
|
|
|
|
|
|
|
|
def test_no_auto_transaction(self):
|
|
|
|
old_atomic_requests = connection.settings_dict['ATOMIC_REQUESTS']
|
|
|
|
try:
|
|
|
|
connection.settings_dict['ATOMIC_REQUESTS'] = True
|
|
|
|
response = self.client.get('/not_in_transaction/')
|
|
|
|
finally:
|
|
|
|
connection.settings_dict['ATOMIC_REQUESTS'] = old_atomic_requests
|
|
|
|
self.assertContains(response, 'False')
|
|
|
|
|
2013-05-15 23:14:28 +00:00
|
|
|
|
2014-04-05 06:04:46 +00:00
|
|
|
@override_settings(ROOT_URLCONF='handlers.urls')
|
2015-04-17 21:38:20 +00:00
|
|
|
class SignalsTests(SimpleTestCase):
|
2012-12-30 14:19:22 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.signals = []
|
2013-11-21 01:03:02 +00:00
|
|
|
self.signaled_environ = None
|
2013-02-18 10:37:26 +00:00
|
|
|
request_started.connect(self.register_started)
|
|
|
|
request_finished.connect(self.register_finished)
|
2012-12-30 14:19:22 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
2013-02-18 10:37:26 +00:00
|
|
|
request_started.disconnect(self.register_started)
|
|
|
|
request_finished.disconnect(self.register_finished)
|
2012-12-30 14:19:22 +00:00
|
|
|
|
|
|
|
def register_started(self, **kwargs):
|
|
|
|
self.signals.append('started')
|
2013-11-21 01:03:02 +00:00
|
|
|
self.signaled_environ = kwargs.get('environ')
|
2012-12-30 14:19:22 +00:00
|
|
|
|
|
|
|
def register_finished(self, **kwargs):
|
|
|
|
self.signals.append('finished')
|
|
|
|
|
|
|
|
def test_request_signals(self):
|
|
|
|
response = self.client.get('/regular/')
|
|
|
|
self.assertEqual(self.signals, ['started', 'finished'])
|
|
|
|
self.assertEqual(response.content, b"regular content")
|
2013-11-21 01:03:02 +00:00
|
|
|
self.assertEqual(self.signaled_environ, response.wsgi_request.environ)
|
2012-12-30 14:19:22 +00:00
|
|
|
|
|
|
|
def test_request_signals_streaming_response(self):
|
|
|
|
response = self.client.get('/streaming/')
|
|
|
|
self.assertEqual(self.signals, ['started'])
|
|
|
|
self.assertEqual(b''.join(response.streaming_content), b"streaming content")
|
|
|
|
self.assertEqual(self.signals, ['started', 'finished'])
|
2013-05-15 23:14:28 +00:00
|
|
|
|
|
|
|
|
2014-04-05 06:04:46 +00:00
|
|
|
@override_settings(ROOT_URLCONF='handlers.urls')
|
2015-04-17 21:38:20 +00:00
|
|
|
class HandlerSuspiciousOpsTest(SimpleTestCase):
|
2013-05-15 23:14:28 +00:00
|
|
|
|
|
|
|
def test_suspiciousop_in_view_returns_400(self):
|
|
|
|
response = self.client.get('/suspicious/')
|
|
|
|
self.assertEqual(response.status_code, 400)
|
2014-07-22 12:25:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
@override_settings(ROOT_URLCONF='handlers.urls')
|
2015-04-17 21:38:20 +00:00
|
|
|
class HandlerNotFoundTest(SimpleTestCase):
|
2014-07-22 12:25:22 +00:00
|
|
|
|
|
|
|
def test_invalid_urls(self):
|
|
|
|
response = self.client.get('~%A9helloworld')
|
|
|
|
self.assertContains(response, '~%A9helloworld', status_code=404)
|
|
|
|
|
|
|
|
response = self.client.get('d%aao%aaw%aan%aal%aao%aaa%aad%aa/')
|
|
|
|
self.assertContains(response, 'd%AAo%AAw%AAn%AAl%AAo%AAa%AAd%AA', status_code=404)
|
|
|
|
|
|
|
|
response = self.client.get('/%E2%99%E2%99%A5/')
|
|
|
|
self.assertContains(response, '%E2%99\u2665', status_code=404)
|
|
|
|
|
|
|
|
response = self.client.get('/%E2%98%8E%E2%A9%E2%99%A5/')
|
|
|
|
self.assertContains(response, '\u260e%E2%A9\u2665', status_code=404)
|
|
|
|
|
|
|
|
def test_environ_path_info_type(self):
|
|
|
|
environ = RequestFactory().get('/%E2%A8%87%87%A5%E2%A8%A0').environ
|
|
|
|
self.assertIsInstance(environ['PATH_INFO'], six.text_type)
|
2015-03-08 14:06:23 +00:00
|
|
|
|
|
|
|
|
2015-04-17 21:38:20 +00:00
|
|
|
class ScriptNameTests(SimpleTestCase):
|
2015-03-08 14:06:23 +00:00
|
|
|
def test_get_script_name(self):
|
|
|
|
# Regression test for #23173
|
|
|
|
# Test first without PATH_INFO
|
|
|
|
script_name = get_script_name({'SCRIPT_URL': '/foobar/'})
|
|
|
|
self.assertEqual(script_name, '/foobar/')
|
|
|
|
|
|
|
|
script_name = get_script_name({'SCRIPT_URL': '/foobar/', 'PATH_INFO': '/'})
|
|
|
|
self.assertEqual(script_name, '/foobar')
|
2015-10-23 12:53:32 +00:00
|
|
|
|
|
|
|
def test_get_script_name_double_slashes(self):
|
|
|
|
"""
|
|
|
|
WSGI squashes multiple successive slashes in PATH_INFO, get_script_name
|
|
|
|
should take that into account when forming SCRIPT_NAME (#17133).
|
|
|
|
"""
|
|
|
|
script_name = get_script_name({
|
|
|
|
'SCRIPT_URL': '/mst/milestones//accounts/login//help',
|
|
|
|
'PATH_INFO': '/milestones/accounts/login/help',
|
|
|
|
})
|
|
|
|
self.assertEqual(script_name, '/mst')
|