/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test_http_response.py

  • Committer: Jelmer Vernooij
  • Date: 2017-05-22 00:56:52 UTC
  • mfrom: (6621.2.26 py3_pokes)
  • Revision ID: jelmer@jelmer.uk-20170522005652-yjahcr9hwmjkno7n
Merge Python3 porting work ('py3 pokes')

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
  InvalidHttpResponse.
38
38
"""
39
39
 
40
 
from cStringIO import StringIO
41
40
import httplib
42
41
 
43
 
from breezy import (
 
42
from .. import (
44
43
    errors,
45
44
    tests,
46
45
    )
47
 
from breezy.transport.http import (
 
46
from ..sixish import (
 
47
    BytesIO,
 
48
    )
 
49
from ..transport.http import (
48
50
    response,
49
51
    _urllib2_wrappers,
50
52
    )
51
 
from breezy.tests.file_utils import (
 
53
from .file_utils import (
52
54
    FakeReadFile,
53
55
    )
54
56
 
57
59
    """A socket-like object that can be given a predefined content."""
58
60
 
59
61
    def __init__(self, data):
60
 
        self.readfile = StringIO(data)
 
62
        self.readfile = BytesIO(data)
61
63
 
62
64
    def makefile(self, mode='r', bufsize=None):
63
65
        return self.readfile
78
80
class TestResponseFileIter(tests.TestCase):
79
81
 
80
82
    def test_iter_empty(self):
81
 
        f = response.ResponseFile('empty', StringIO())
 
83
        f = response.ResponseFile('empty', BytesIO())
82
84
        self.assertEqual([], list(f))
83
85
 
84
86
    def test_iter_many(self):
85
 
        f = response.ResponseFile('many', StringIO('0\n1\nboo!\n'))
 
87
        f = response.ResponseFile('many', BytesIO(b'0\n1\nboo!\n'))
86
88
        self.assertEqual(['0\n', '1\n', 'boo!\n'], list(f))
87
89
 
88
90
 
216
218
    def setUp(self):
217
219
        super(TestRangeFileSizeUnknown, self).setUp()
218
220
        self._file = response.RangeFile('Whole_file_size_known',
219
 
                                        StringIO(self.alpha))
 
221
                                        BytesIO(self.alpha))
220
222
        # We define no range, relying on RangeFile to provide default values
221
223
        self.first_range_start = 0 # It's the whole file
222
224
 
241
243
    def setUp(self):
242
244
        super(TestRangeFileSizeKnown, self).setUp()
243
245
        self._file = response.RangeFile('Whole_file_size_known',
244
 
                                        StringIO(self.alpha))
 
246
                                        BytesIO(self.alpha))
245
247
        self._file.set_range(0, len(self.alpha))
246
248
        self.first_range_start = 0 # It's the whole file
247
249
 
252
254
    def setUp(self):
253
255
        super(TestRangeFileSingleRange, self).setUp()
254
256
        self._file = response.RangeFile('Single_range_file',
255
 
                                        StringIO(self.alpha))
 
257
                                        BytesIO(self.alpha))
256
258
        self.first_range_start = 15
257
259
        self._file.set_range(self.first_range_start, len(self.alpha))
258
260
 
301
303
        content += self._boundary_line()
302
304
 
303
305
        self._file = response.RangeFile('Multiple_ranges_file',
304
 
                                        StringIO(content))
 
306
                                        BytesIO(content))
305
307
        self.set_file_boundary()
306
308
 
307
309
    def _boundary_line(self):
443
445
 
444
446
    def test_seek_whence(self):
445
447
        """Test the seek whence parameter values."""
446
 
        f = response.RangeFile('foo', StringIO('abc'))
 
448
        f = response.RangeFile('foo', BytesIO(b'abc'))
447
449
        f.set_range(0, 3)
448
450
        f.seek(0)
449
451
        f.seek(1, 1)
453
455
    def test_range_syntax(self):
454
456
        """Test the Content-Range scanning."""
455
457
 
456
 
        f = response.RangeFile('foo', StringIO())
 
458
        f = response.RangeFile('foo', BytesIO())
457
459
 
458
460
        def ok(expected, header_value):
459
461
            f.set_range_from_header(header_value)
709
711
class TestHandleResponse(tests.TestCase):
710
712
 
711
713
    def _build_HTTPMessage(self, raw_headers):
712
 
        status_and_headers = StringIO(raw_headers)
 
714
        status_and_headers = BytesIO(raw_headers)
713
715
        # Get rid of the status line
714
716
        status_and_headers.readline()
715
717
        msg = httplib.HTTPMessage(status_and_headers)
720
722
        code, raw_headers, body = a_response
721
723
        msg = self._build_HTTPMessage(raw_headers)
722
724
        return response.handle_response('http://foo', code, msg,
723
 
                                        StringIO(a_response[2]))
 
725
                                        BytesIO(a_response[2]))
724
726
 
725
727
    def test_full_text(self):
726
728
        out = self.get_response(_full_text_response)
727
 
        # It is a StringIO from the original data
 
729
        # It is a BytesIO from the original data
728
730
        self.assertEqual(_full_text_response[2], out.read())
729
731
 
730
732
    def test_single_range(self):
772
774
        # We should not require Content-Type for a full response
773
775
        code, raw_headers, body = _full_text_response_no_content_type
774
776
        msg = self._build_HTTPMessage(raw_headers)
775
 
        out = response.handle_response('http://foo', code, msg, StringIO(body))
 
777
        out = response.handle_response('http://foo', code, msg, BytesIO(body))
776
778
        self.assertEqual(body, out.read())
777
779
 
778
780
    def test_full_text_no_content_length(self):
779
781
        code, raw_headers, body = _full_text_response_no_content_length
780
782
        msg = self._build_HTTPMessage(raw_headers)
781
 
        out = response.handle_response('http://foo', code, msg, StringIO(body))
 
783
        out = response.handle_response('http://foo', code, msg, BytesIO(body))
782
784
        self.assertEqual(body, out.read())
783
785
 
784
786
    def test_missing_content_range(self):
786
788
        msg = self._build_HTTPMessage(raw_headers)
787
789
        self.assertRaises(errors.InvalidHttpResponse,
788
790
                          response.handle_response,
789
 
                          'http://bogus', code, msg, StringIO(body))
 
791
                          'http://bogus', code, msg, BytesIO(body))
790
792
 
791
793
    def test_multipart_no_content_range(self):
792
794
        code, raw_headers, body = _multipart_no_content_range
793
795
        msg = self._build_HTTPMessage(raw_headers)
794
796
        self.assertRaises(errors.InvalidHttpResponse,
795
797
                          response.handle_response,
796
 
                          'http://bogus', code, msg, StringIO(body))
 
798
                          'http://bogus', code, msg, BytesIO(body))
797
799
 
798
800
    def test_multipart_no_boundary(self):
799
801
        out = self.get_response(_multipart_no_boundary)