Commit 8fff7924 authored by Jeremy Hylton's avatar Jeremy Hylton

Fix tests to use bytes() where the actual sockets return bytes().

Use io.BytesIO() instead of StringIO.StringIO().  FakeSocket still
accepts regular strings and coverts them to bytes internally.
parent 9648d62f
import httplib import httplib
import StringIO import io
import sys import sys
import socket import socket
...@@ -8,7 +8,9 @@ from unittest import TestCase ...@@ -8,7 +8,9 @@ from unittest import TestCase
from test import test_support from test import test_support
class FakeSocket: class FakeSocket:
def __init__(self, text, fileclass=StringIO.StringIO): def __init__(self, text, fileclass=io.BytesIO):
if isinstance(text, str):
text = bytes(text)
self.text = text self.text = text
self.fileclass = fileclass self.fileclass = fileclass
self.data = b'' self.data = b''
...@@ -21,20 +23,20 @@ class FakeSocket: ...@@ -21,20 +23,20 @@ class FakeSocket:
raise httplib.UnimplementedFileMode() raise httplib.UnimplementedFileMode()
return self.fileclass(self.text) return self.fileclass(self.text)
class NoEOFStringIO(StringIO.StringIO): class NoEOFStringIO(io.BytesIO):
"""Like StringIO, but raises AssertionError on EOF. """Like StringIO, but raises AssertionError on EOF.
This is used below to test that httplib doesn't try to read This is used below to test that httplib doesn't try to read
more from the underlying file than it should. more from the underlying file than it should.
""" """
def read(self, n=-1): def read(self, n=-1):
data = StringIO.StringIO.read(self, n) data = io.BytesIO.read(self, n)
if data == '': if data == '':
raise AssertionError('caller tried to read past EOF') raise AssertionError('caller tried to read past EOF')
return data return data
def readline(self, length=None): def readline(self, length=None):
data = StringIO.StringIO.readline(self, length) data = io.BytesIO.readline(self, length)
if data == '': if data == '':
raise AssertionError('caller tried to read past EOF') raise AssertionError('caller tried to read past EOF')
return data return data
...@@ -80,7 +82,7 @@ class BasicTest(TestCase): ...@@ -80,7 +82,7 @@ class BasicTest(TestCase):
sock = FakeSocket(body) sock = FakeSocket(body)
resp = httplib.HTTPResponse(sock) resp = httplib.HTTPResponse(sock)
resp.begin() resp.begin()
self.assertEqual(resp.read(), 'Text') self.assertEqual(resp.read(), b"Text")
resp.close() resp.close()
body = "HTTP/1.1 400.100 Not Ok\r\n\r\nText" body = "HTTP/1.1 400.100 Not Ok\r\n\r\nText"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment