[py3] Ported django.core.servers.

This commit is contained in:
Aymeric Augustin 2012-08-16 13:01:16 +02:00
parent 8c356acf2e
commit fcc8de0598
2 changed files with 19 additions and 13 deletions

View File

@ -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! been reviewed for security issues. DON'T USE IT FOR PRODUCTION USE!
""" """
from __future__ import unicode_literals
import os import os
import socket import socket
import sys import sys
@ -71,12 +73,12 @@ class WSGIServerException(Exception):
class ServerHandler(simple_server.ServerHandler, object): class ServerHandler(simple_server.ServerHandler, object):
error_status = "500 INTERNAL SERVER ERROR" error_status = str("500 INTERNAL SERVER ERROR")
def write(self, data): 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: if not self.status:
raise AssertionError("write() before start_response()") raise AssertionError("write() before start_response()")

View File

@ -1,5 +1,8 @@
from __future__ import unicode_literals
from io import BytesIO
from django.core.servers.basehttp import ServerHandler from django.core.servers.basehttp import ServerHandler
from django.utils.six import StringIO
from django.utils.unittest import TestCase from django.utils.unittest import TestCase
# #
@ -23,12 +26,12 @@ class FileWrapperHandler(ServerHandler):
return True return True
def wsgi_app(environ, start_response): def wsgi_app(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/plain')]) start_response(str('200 OK'), [(str('Content-Type'), str('text/plain'))])
return ['Hello World!'] return [b'Hello World!']
def wsgi_app_file_wrapper(environ, start_response): def wsgi_app_file_wrapper(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/plain')]) start_response(str('200 OK'), [(str('Content-Type'), str('text/plain'))])
return environ['wsgi.file_wrapper'](StringIO('foo')) return environ['wsgi.file_wrapper'](BytesIO(b'foo'))
class WSGIFileWrapperTests(TestCase): class WSGIFileWrapperTests(TestCase):
""" """
@ -37,15 +40,16 @@ class WSGIFileWrapperTests(TestCase):
def test_file_wrapper_uses_sendfile(self): def test_file_wrapper_uses_sendfile(self):
env = {'SERVER_PROTOCOL': 'HTTP/1.0'} env = {'SERVER_PROTOCOL': 'HTTP/1.0'}
err = StringIO() handler = FileWrapperHandler(None, BytesIO(), BytesIO(), env)
handler = FileWrapperHandler(None, StringIO(), err, env)
handler.run(wsgi_app_file_wrapper) handler.run(wsgi_app_file_wrapper)
self.assertTrue(handler._used_sendfile) self.assertTrue(handler._used_sendfile)
self.assertEqual(handler.stdout.getvalue(), b'')
self.assertEqual(handler.stderr.getvalue(), b'')
def test_file_wrapper_no_sendfile(self): def test_file_wrapper_no_sendfile(self):
env = {'SERVER_PROTOCOL': 'HTTP/1.0'} env = {'SERVER_PROTOCOL': 'HTTP/1.0'}
err = StringIO() handler = FileWrapperHandler(None, BytesIO(), BytesIO(), env)
handler = FileWrapperHandler(None, StringIO(), err, env)
handler.run(wsgi_app) handler.run(wsgi_app)
self.assertFalse(handler._used_sendfile) 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'')