From 53e0659287692975afd570cb645b75d060fcffbc Mon Sep 17 00:00:00 2001
From: Claude Paroz <claude@2xlibre.net>
Date: Sat, 20 Oct 2012 12:34:50 +0200
Subject: [PATCH] Removed custom WSGIRequestHandler.get_environ
We probably historically customized it for good reasons, but
currently, the differences are not significant any longer.
Also adding a test for #19075.
---
django/core/servers/basehttp.py | 34 +-------------------------------
tests/regressiontests/servers/tests.py | 8 +++++++
tests/regressiontests/servers/urls.py | 3 +-
tests/regressiontests/servers/views.py | 6 ++++-
4 files changed, 16 insertions(+), 35 deletions(-)
diff --git a/django/core/servers/basehttp.py b/django/core/servers/basehttp.py
index 19b287a..a7004f2 100644
a
|
b
|
import socket
|
14 | 14 | import sys |
15 | 15 | import traceback |
16 | 16 | try: |
17 | | from urllib.parse import unquote, urljoin |
| 17 | from urllib.parse import urljoin |
18 | 18 | except ImportError: # Python 2 |
19 | | from urllib import unquote |
20 | 19 | from urlparse import urljoin |
21 | 20 | from django.utils.six.moves import socketserver |
22 | 21 | from wsgiref import simple_server |
… |
… |
class WSGIRequestHandler(simple_server.WSGIRequestHandler, object):
|
139 | 138 | self.style = color_style() |
140 | 139 | super(WSGIRequestHandler, self).__init__(*args, **kwargs) |
141 | 140 | |
142 | | def get_environ(self): |
143 | | env = self.server.base_environ.copy() |
144 | | env['SERVER_PROTOCOL'] = self.request_version |
145 | | env['REQUEST_METHOD'] = self.command |
146 | | if '?' in self.path: |
147 | | path,query = self.path.split('?',1) |
148 | | else: |
149 | | path,query = self.path,'' |
150 | | |
151 | | env['PATH_INFO'] = unquote(path) |
152 | | env['QUERY_STRING'] = query |
153 | | env['REMOTE_ADDR'] = self.client_address[0] |
154 | | env['CONTENT_TYPE'] = self.headers.get('content-type', 'text/plain') |
155 | | |
156 | | length = self.headers.get('content-length') |
157 | | if length: |
158 | | env['CONTENT_LENGTH'] = length |
159 | | |
160 | | for key, value in self.headers.items(): |
161 | | key = key.replace('-','_').upper() |
162 | | value = value.strip() |
163 | | if key in env: |
164 | | # Skip content length, type, etc. |
165 | | continue |
166 | | if 'HTTP_' + key in env: |
167 | | # Comma-separate multiple headers |
168 | | env['HTTP_' + key] += ',' + value |
169 | | else: |
170 | | env['HTTP_' + key] = value |
171 | | return env |
172 | | |
173 | 141 | def log_message(self, format, *args): |
174 | 142 | # Don't bother logging requests for admin images or the favicon. |
175 | 143 | if (self.path.startswith(self.admin_static_prefix) |
diff --git a/tests/regressiontests/servers/tests.py b/tests/regressiontests/servers/tests.py
index c90c785..f54e34c 100644
a
|
b
|
|
| 1 | # -*- encoding: utf-8 -*- |
1 | 2 | """ |
2 | 3 | Tests for django.core.servers. |
3 | 4 | """ |
| 5 | from __future__ import unicode_literals |
| 6 | |
4 | 7 | import os |
5 | 8 | try: |
6 | 9 | from urllib.request import urlopen, HTTPError |
… |
… |
from django.core.exceptions import ImproperlyConfigured
|
11 | 14 | from django.test import LiveServerTestCase |
12 | 15 | from django.core.servers.basehttp import WSGIServerException |
13 | 16 | from django.test.utils import override_settings |
| 17 | from django.utils.http import urlencode |
14 | 18 | |
15 | 19 | from .models import Person |
16 | 20 | |
… |
… |
class LiveServerViews(LiveServerBase):
|
134 | 138 | f = self.urlopen('/media/example_media_file.txt') |
135 | 139 | self.assertEqual(f.read().rstrip(b'\r\n'), b'example media file') |
136 | 140 | |
| 141 | def test_environ(self): |
| 142 | f = self.urlopen('/environ_view/?%s' % urlencode({'q': 'тест'})) |
| 143 | self.assertIn(b"QUERY_STRING: 'q=%D1%82%D0%B5%D1%81%D1%82'", f.read()) |
| 144 | |
137 | 145 | |
138 | 146 | class LiveServerDatabase(LiveServerBase): |
139 | 147 | |
diff --git a/tests/regressiontests/servers/urls.py b/tests/regressiontests/servers/urls.py
index c8ca1ac..a857c45 100644
a
|
b
|
urlpatterns = patterns('',
|
9 | 9 | url(r'^example_view/$', views.example_view), |
10 | 10 | url(r'^model_view/$', views.model_view), |
11 | 11 | url(r'^create_model_instance/$', views.create_model_instance), |
12 | | ) |
13 | | No newline at end of file |
| 12 | url(r'^environ_view/$', views.environ_view), |
| 13 | ) |
diff --git a/tests/regressiontests/servers/views.py b/tests/regressiontests/servers/views.py
index 94a4f2d..00baf4b 100644
a
|
b
|
def model_view(request):
|
14 | 14 | def create_model_instance(request): |
15 | 15 | person = Person(name='emily') |
16 | 16 | person.save() |
17 | | return HttpResponse('') |
18 | | No newline at end of file |
| 17 | return HttpResponse('') |
| 18 | |
| 19 | |
| 20 | def environ_view(request): |
| 21 | return HttpResponse("\n".join(["%s: %r" % (k, v) for k, v in request.environ.items()])) |