From fcc8de0598f5a2024ad8a49e9bc38912b9511ca7 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Thu, 16 Aug 2012 13:01:16 +0200 Subject: [PATCH] [py3] Ported django.core.servers. --- django/core/servers/basehttp.py | 8 ++++--- tests/regressiontests/builtin_server/tests.py | 24 +++++++++++-------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/django/core/servers/basehttp.py b/django/core/servers/basehttp.py index 2db4e5ef6a..ce1558a924 100644 --- a/django/core/servers/basehttp.py +++ b/django/core/servers/basehttp.py @@ -7,6 +7,8 @@ This is a simple server for use in testing or debugging Django apps. It hasn't been reviewed for security issues. DON'T USE IT FOR PRODUCTION USE! """ +from __future__ import unicode_literals + import os import socket import sys @@ -71,12 +73,12 @@ class WSGIServerException(Exception): class ServerHandler(simple_server.ServerHandler, object): - error_status = "500 INTERNAL SERVER ERROR" + error_status = str("500 INTERNAL SERVER ERROR") def write(self, data): - """'write()' callable as specified by PEP 333""" + """'write()' callable as specified by PEP 3333""" - assert isinstance(data, str), "write() argument must be string" + assert isinstance(data, bytes), "write() argument must be bytestring" if not self.status: raise AssertionError("write() before start_response()") diff --git a/tests/regressiontests/builtin_server/tests.py b/tests/regressiontests/builtin_server/tests.py index 4a3b44176b..c8dc77e42e 100644 --- a/tests/regressiontests/builtin_server/tests.py +++ b/tests/regressiontests/builtin_server/tests.py @@ -1,5 +1,8 @@ +from __future__ import unicode_literals + +from io import BytesIO + from django.core.servers.basehttp import ServerHandler -from django.utils.six import StringIO from django.utils.unittest import TestCase # @@ -23,12 +26,12 @@ class FileWrapperHandler(ServerHandler): return True def wsgi_app(environ, start_response): - start_response('200 OK', [('Content-Type', 'text/plain')]) - return ['Hello World!'] + start_response(str('200 OK'), [(str('Content-Type'), str('text/plain'))]) + return [b'Hello World!'] def wsgi_app_file_wrapper(environ, start_response): - start_response('200 OK', [('Content-Type', 'text/plain')]) - return environ['wsgi.file_wrapper'](StringIO('foo')) + start_response(str('200 OK'), [(str('Content-Type'), str('text/plain'))]) + return environ['wsgi.file_wrapper'](BytesIO(b'foo')) class WSGIFileWrapperTests(TestCase): """ @@ -37,15 +40,16 @@ class WSGIFileWrapperTests(TestCase): def test_file_wrapper_uses_sendfile(self): env = {'SERVER_PROTOCOL': 'HTTP/1.0'} - err = StringIO() - handler = FileWrapperHandler(None, StringIO(), err, env) + handler = FileWrapperHandler(None, BytesIO(), BytesIO(), env) handler.run(wsgi_app_file_wrapper) self.assertTrue(handler._used_sendfile) + self.assertEqual(handler.stdout.getvalue(), b'') + self.assertEqual(handler.stderr.getvalue(), b'') def test_file_wrapper_no_sendfile(self): env = {'SERVER_PROTOCOL': 'HTTP/1.0'} - err = StringIO() - handler = FileWrapperHandler(None, StringIO(), err, env) + handler = FileWrapperHandler(None, BytesIO(), BytesIO(), env) handler.run(wsgi_app) self.assertFalse(handler._used_sendfile) - self.assertEqual(handler.stdout.getvalue().splitlines()[-1],'Hello World!') + self.assertEqual(handler.stdout.getvalue().splitlines()[-1], b'Hello World!') + self.assertEqual(handler.stderr.getvalue(), b'')