Commit fcc8de05 authored by Aymeric Augustin's avatar Aymeric Augustin
Browse files

[py3] Ported django.core.servers.

parent 8c356acf
Loading
Loading
Loading
Loading
+5 −3
Original line number Diff line number Diff line
@@ -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()")
+14 −10
Original line number Diff line number Diff line
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'')