~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_http_response.py

  • Committer: Robert Collins
  • Date: 2006-07-24 03:28:53 UTC
  • mto: (1880.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 1881.
  • Revision ID: robertc@robertcollins.net-20060724032853-39d9a8a238c105e3
Fix test suite deprecation warnings from use of EmptyTtree.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
 
1
# Copyright (C) 2005, 2006 by Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
 
 
17
 
"""Tests from HTTP response parsing.
18
 
 
19
 
The handle_response method read the response body of a GET request an returns
20
 
the corresponding RangeFile.
21
 
 
22
 
There are four different kinds of RangeFile:
23
 
- a whole file whose size is unknown, seen as a simple byte stream,
24
 
- a whole file whose size is known, we can't read past its end,
25
 
- a single range file, a part of a file with a start and a size,
26
 
- a multiple range file, several consecutive parts with known start offset
27
 
  and size.
28
 
 
29
 
Some properties are common to all kinds:
30
 
- seek can only be forward (its really a socket underneath),
31
 
- read can't cross ranges,
32
 
- successive ranges are taken into account transparently,
33
 
 
34
 
- the expected pattern of use is either seek(offset)+read(size) or a single
35
 
  read with no size specified. For multiple range files, multiple read() will
36
 
  return the corresponding ranges, trying to read further will raise
37
 
  InvalidHttpResponse.
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""Tests from HTTP response parsing."""
 
18
 
 
19
from cStringIO import StringIO
 
20
import mimetools
 
21
 
 
22
from bzrlib import errors
 
23
from bzrlib.transport import http
 
24
from bzrlib.transport.http import response
 
25
from bzrlib.tests import TestCase
 
26
 
 
27
 
 
28
class TestResponseRange(TestCase):
 
29
    """Test the ResponseRange class."""
 
30
 
 
31
    def test_cmp(self):
 
32
        RR = response.ResponseRange
 
33
        r1 = RR(0, 10, 0)
 
34
        r2 = RR(15, 20, 10)
 
35
        self.assertTrue(r1 < r2)
 
36
        self.assertFalse(r1 > r2)
 
37
        self.assertTrue(r1 < 5)
 
38
        self.assertFalse(r2 < 5)
 
39
 
 
40
        self.assertEqual(RR(0, 10, 5), RR(0, 10, 5))
 
41
        self.assertNotEqual(RR(0, 10, 5), RR(0, 8, 5))
 
42
        self.assertNotEqual(RR(0, 10, 5), RR(0, 10, 6))
 
43
 
 
44
    def test_sort_list(self):
 
45
        """Ensure longer ranges are sorted after shorter ones"""
 
46
        RR = response.ResponseRange
 
47
        lst = [RR(3, 8, 0), 5, RR(3, 7, 0), 6]
 
48
        lst.sort()
 
49
        self.assertEqual([RR(3,7,0), RR(3,8,0), 5, 6], lst)
 
50
 
 
51
 
 
52
class TestRangeFile(TestCase):
 
53
    """Test RangeFile."""
 
54
 
 
55
    def setUp(self):
 
56
        content = "abcdefghijklmnopqrstuvwxyz"
 
57
        self.fp = response.RangeFile('foo', StringIO(content))
 
58
        self.fp._add_range(0,  9,   0)
 
59
        self.fp._add_range(20, 29, 10)
 
60
        self.fp._add_range(30, 39, 15)
 
61
 
 
62
    def test_valid_accesses(self):
 
63
        """Test so that valid accesses work to the file."""
 
64
        self.fp.seek(0, 0)
 
65
        self.assertEquals(self.fp.read(3), 'abc')
 
66
        self.assertEquals(self.fp.read(3), 'def')
 
67
        self.assertEquals(self.fp.tell(), 6)
 
68
        self.fp.seek(20, 0)
 
69
        self.assertEquals(self.fp.read(3), 'klm')
 
70
        self.assertEquals(self.fp.read(2), 'no')
 
71
        self.assertEquals(self.fp.tell(), 25)
 
72
        # should wrap over to 30-39 entity
 
73
        self.assertEquals(self.fp.read(3), 'pqr')
 
74
        self.fp.seek(3)
 
75
        self.assertEquals(self.fp.read(3), 'def')
 
76
        self.assertEquals(self.fp.tell(), 6)
 
77
 
 
78
    def test_invalid_accesses(self):
 
79
        """Test so that invalid accesses trigger errors."""
 
80
        self.fp.seek(9)
 
81
        self.assertRaises(errors.InvalidRange, self.fp.read, 2)
 
82
        self.fp.seek(39)
 
83
        self.assertRaises(errors.InvalidRange, self.fp.read, 2)
 
84
        self.fp.seek(19)
 
85
        self.assertRaises(errors.InvalidRange, self.fp.read, 2)
 
86
 
 
87
    def test__finish_ranges(self):
 
88
        """Test that after RangeFile._finish_ranges the list is sorted."""
 
89
        self.fp._add_range(1, 2, 3)
 
90
        self.fp._add_range(8, 9, 10)
 
91
        self.fp._add_range(3, 4, 5)
 
92
 
 
93
        # TODO: jam 20060706 If we switch to inserting
 
94
        #       in sorted order, remove this test
 
95
        self.assertNotEqual(self.fp._ranges, sorted(self.fp._ranges))
 
96
 
 
97
        self.fp._finish_ranges()
 
98
        self.assertEqual(self.fp._ranges, sorted(self.fp._ranges))
 
99
 
 
100
    def test_seek_and_tell(self):
 
101
        # Check for seeking before start
 
102
        self.fp.seek(-2, 0)
 
103
        self.assertEqual(0, self.fp.tell())
 
104
 
 
105
        self.fp.seek(5, 0)
 
106
        self.assertEqual(5, self.fp.tell())
 
107
 
 
108
        self.fp.seek(-2, 1)
 
109
        self.assertEqual(3, self.fp.tell())
 
110
 
 
111
        # TODO: jam 20060706 following tests will fail if this 
 
112
        #       is not true, and would be difficult to debug
 
113
        #       but it is a layering violation
 
114
        self.assertEqual(39, self.fp._len)
 
115
 
 
116
        self.fp.seek(0, 2)
 
117
        self.assertEqual(39, self.fp.tell())
 
118
 
 
119
        self.fp.seek(-10, 2)
 
120
        self.assertEqual(29, self.fp.tell())
 
121
 
 
122
        self.assertRaises(ValueError, self.fp.seek, 0, 4)
 
123
        self.assertRaises(ValueError, self.fp.seek, 0, -1)
 
124
 
 
125
 
 
126
class TestRegexes(TestCase):
 
127
 
 
128
    def assertRegexMatches(self, groups, text):
 
129
        """Check that the regex matches and returns the right values"""
 
130
        m = self.regex.match(text)
 
131
        self.assertNotEqual(None, m, "text %s did not match regex" % (text,))
 
132
 
 
133
        self.assertEqual(groups, m.groups())
 
134
 
 
135
    def test_range_re(self):
 
136
        """Test that we match valid ranges."""
 
137
        self.regex = response.HttpRangeResponse._CONTENT_RANGE_RE
 
138
        self.assertRegexMatches(('bytes', '1', '10', '11'),
 
139
                           'bytes 1-10/11')
 
140
        self.assertRegexMatches(('bytes', '1', '10', '11'),
 
141
                           '\tbytes  1-10/11   ')
 
142
        self.assertRegexMatches(('bytes', '2123', '4242', '1231'),
 
143
                           '\tbytes  2123-4242/1231   ')
 
144
        self.assertRegexMatches(('chars', '1', '2', '3'),
 
145
                           ' chars 1-2/3')
 
146
 
 
147
    def test_content_type_re(self):
 
148
        self.regex = response.HttpMultipartRangeResponse._CONTENT_TYPE_RE
 
149
        self.assertRegexMatches(('xxyyzz',),
 
150
                                'multipart/byteranges; boundary = xxyyzz')
 
151
        self.assertRegexMatches(('xxyyzz',),
 
152
                                'multipart/byteranges;boundary=xxyyzz')
 
153
        self.assertRegexMatches(('xx yy zz',),
 
154
                                ' multipart/byteranges ; boundary= xx yy zz ')
 
155
        self.assertEqual(None,
 
156
                self.regex.match('multipart byteranges;boundary=xx'))
 
157
 
 
158
 
 
159
simple_data = """
 
160
--xxyyzz\r
 
161
foo\r
 
162
Content-range: bytes 1-10/20\r
 
163
\r
 
164
1234567890
 
165
--xxyyzz\r
 
166
Content-Range: bytes 21-30/20\r
 
167
bar\r
 
168
\r
 
169
abcdefghij
 
170
--xxyyzz\r
 
171
content-range: bytes 41-50/20\r
 
172
\r
 
173
zyxwvutsrq
 
174
--xxyyzz\r
 
175
content-range: bytes 51-60/20\r
 
176
\r
 
177
xxyyzz fbd
38
178
"""
39
179
 
40
 
from cStringIO import StringIO
41
 
import httplib
42
 
 
43
 
from bzrlib import (
44
 
    errors,
45
 
    tests,
46
 
    )
47
 
from bzrlib.transport.http import (
48
 
    response,
49
 
    _urllib2_wrappers,
50
 
    )
51
 
from bzrlib.tests.file_utils import (
52
 
    FakeReadFile,
53
 
    )
54
 
 
55
 
 
56
 
class ReadSocket(object):
57
 
    """A socket-like object that can be given a predefined content."""
58
 
 
59
 
    def __init__(self, data):
60
 
        self.readfile = StringIO(data)
61
 
 
62
 
    def makefile(self, mode='r', bufsize=None):
63
 
        return self.readfile
64
 
 
65
 
 
66
 
class FakeHTTPConnection(_urllib2_wrappers.HTTPConnection):
67
 
 
68
 
    def __init__(self, sock):
69
 
        _urllib2_wrappers.HTTPConnection.__init__(self, 'localhost')
70
 
        # Set the socket to bypass the connection
71
 
        self.sock = sock
72
 
 
73
 
    def send(self, str):
74
 
        """Ignores the writes on the socket."""
75
 
        pass
76
 
 
77
 
 
78
 
class TestHTTPConnection(tests.TestCase):
79
 
 
80
 
    def test_cleanup_pipe(self):
81
 
        sock = ReadSocket("""HTTP/1.1 200 OK\r
82
 
Content-Type: text/plain; charset=UTF-8\r
83
 
Content-Length: 18
84
 
\r
85
 
0123456789
86
 
garbage""")
87
 
        conn = FakeHTTPConnection(sock)
88
 
        # Simulate the request sending so that the connection will be able to
89
 
        # read the response.
90
 
        conn.putrequest('GET', 'http://localhost/fictious')
91
 
        conn.endheaders()
92
 
        # Now, get the response
93
 
        resp = conn.getresponse()
94
 
        # Read part of the response
95
 
        self.assertEquals('0123456789\n', resp.read(11))
96
 
        # Override the thresold to force the warning emission
97
 
        conn._range_warning_thresold = 6 # There are 7 bytes pending
98
 
        conn.cleanup_pipe()
99
 
        self.assertContainsRe(self._get_log(keep_log_file=True),
100
 
                              'Got a 200 response when asking')
101
 
 
102
 
 
103
 
class TestRangeFileMixin(object):
104
 
    """Tests for accessing the first range in a RangeFile."""
105
 
 
106
 
    # A simple string used to represent a file part (also called a range), in
107
 
    # which offsets are easy to calculate for test writers. It's used as a
108
 
    # building block with slight variations but basically 'a' is the first char
109
 
    # of the range and 'z' is the last.
110
 
    alpha = 'abcdefghijklmnopqrstuvwxyz'
111
 
 
112
 
    def test_can_read_at_first_access(self):
113
 
        """Test that the just created file can be read."""
114
 
        self.assertEquals(self.alpha, self._file.read())
115
 
 
116
 
    def test_seek_read(self):
117
 
        """Test seek/read inside the range."""
118
 
        f = self._file
119
 
        start = self.first_range_start
120
 
        # Before any use, tell() should be at the range start
121
 
        self.assertEquals(start, f.tell())
122
 
        cur = start # For an overall offset assertion
123
 
        f.seek(start + 3)
124
 
        cur += 3
125
 
        self.assertEquals('def', f.read(3))
126
 
        cur += len('def')
127
 
        f.seek(4, 1)
128
 
        cur += 4
129
 
        self.assertEquals('klmn', f.read(4))
130
 
        cur += len('klmn')
131
 
        # read(0) in the middle of a range
132
 
        self.assertEquals('', f.read(0))
133
 
        # seek in place
134
 
        here = f.tell()
135
 
        f.seek(0, 1)
136
 
        self.assertEquals(here, f.tell())
137
 
        self.assertEquals(cur, f.tell())
138
 
 
139
 
    def test_read_zero(self):
140
 
        f = self._file
141
 
        start = self.first_range_start
142
 
        self.assertEquals('', f.read(0))
143
 
        f.seek(10, 1)
144
 
        self.assertEquals('', f.read(0))
145
 
 
146
 
    def test_seek_at_range_end(self):
147
 
        f = self._file
148
 
        f.seek(26, 1)
149
 
 
150
 
    def test_read_at_range_end(self):
151
 
        """Test read behaviour at range end."""
152
 
        f = self._file
153
 
        self.assertEquals(self.alpha, f.read())
154
 
        self.assertEquals('', f.read(0))
155
 
        self.assertRaises(errors.InvalidRange, f.read, 1)
156
 
 
157
 
    def test_unbounded_read_after_seek(self):
158
 
        f = self._file
159
 
        f.seek(24, 1)
160
 
        # Should not cross ranges
161
 
        self.assertEquals('yz', f.read())
162
 
 
163
 
    def test_seek_backwards(self):
164
 
        f = self._file
165
 
        start = self.first_range_start
166
 
        f.seek(start)
167
 
        f.read(12)
168
 
        self.assertRaises(errors.InvalidRange, f.seek, start + 5)
169
 
 
170
 
    def test_seek_outside_single_range(self):
171
 
        f = self._file
172
 
        if f._size == -1 or f._boundary is not None:
173
 
            raise tests.TestNotApplicable('Needs a fully defined range')
174
 
        # Will seek past the range and then errors out
175
 
        self.assertRaises(errors.InvalidRange,
176
 
                          f.seek, self.first_range_start + 27)
177
 
 
178
 
    def test_read_past_end_of_range(self):
179
 
        f = self._file
180
 
        if f._size == -1:
181
 
            raise tests.TestNotApplicable("Can't check an unknown size")
182
 
        start = self.first_range_start
183
 
        f.seek(start + 20)
184
 
        self.assertRaises(errors.InvalidRange, f.read, 10)
185
 
 
186
 
    def test_seek_from_end(self):
187
 
       """Test seeking from the end of the file.
188
 
 
189
 
       The semantic is unclear in case of multiple ranges. Seeking from end
190
 
       exists only for the http transports, cannot be used if the file size is
191
 
       unknown and is not used in bzrlib itself. This test must be (and is)
192
 
       overridden by daughter classes.
193
 
 
194
 
       Reading from end makes sense only when a range has been requested from
195
 
       the end of the file (see HttpTransportBase._get() when using the
196
 
       'tail_amount' parameter). The HTTP response can only be a whole file or
197
 
       a single range.
198
 
       """
199
 
       f = self._file
200
 
       f.seek(-2, 2)
201
 
       self.assertEquals('yz', f.read())
202
 
 
203
 
 
204
 
class TestRangeFileSizeUnknown(tests.TestCase, TestRangeFileMixin):
205
 
    """Test a RangeFile for a whole file whose size is not known."""
206
 
 
207
 
    def setUp(self):
208
 
        super(TestRangeFileSizeUnknown, self).setUp()
209
 
        self._file = response.RangeFile('Whole_file_size_known',
210
 
                                        StringIO(self.alpha))
211
 
        # We define no range, relying on RangeFile to provide default values
212
 
        self.first_range_start = 0 # It's the whole file
213
 
 
214
 
    def test_seek_from_end(self):
215
 
        """See TestRangeFileMixin.test_seek_from_end.
216
 
 
217
 
        The end of the file can't be determined since the size is unknown.
218
 
        """
219
 
        self.assertRaises(errors.InvalidRange, self._file.seek, -1, 2)
220
 
 
221
 
    def test_read_at_range_end(self):
222
 
        """Test read behaviour at range end."""
223
 
        f = self._file
224
 
        self.assertEquals(self.alpha, f.read())
225
 
        self.assertEquals('', f.read(0))
226
 
        self.assertEquals('', f.read(1))
227
 
 
228
 
 
229
 
class TestRangeFileSizeKnown(tests.TestCase, TestRangeFileMixin):
230
 
    """Test a RangeFile for a whole file whose size is known."""
231
 
 
232
 
    def setUp(self):
233
 
        super(TestRangeFileSizeKnown, self).setUp()
234
 
        self._file = response.RangeFile('Whole_file_size_known',
235
 
                                        StringIO(self.alpha))
236
 
        self._file.set_range(0, len(self.alpha))
237
 
        self.first_range_start = 0 # It's the whole file
238
 
 
239
 
 
240
 
class TestRangeFileSingleRange(tests.TestCase, TestRangeFileMixin):
241
 
    """Test a RangeFile for a single range."""
242
 
 
243
 
    def setUp(self):
244
 
        super(TestRangeFileSingleRange, self).setUp()
245
 
        self._file = response.RangeFile('Single_range_file',
246
 
                                        StringIO(self.alpha))
247
 
        self.first_range_start = 15
248
 
        self._file.set_range(self.first_range_start, len(self.alpha))
249
 
 
250
 
 
251
 
    def test_read_before_range(self):
252
 
        # This can't occur under normal circumstances, we have to force it
253
 
        f = self._file
254
 
        f._pos = 0 # Force an invalid pos
 
180
 
 
181
class TestHelpers(TestCase):
 
182
    """Test the helper functions"""
 
183
 
 
184
    def test__parse_range(self):
 
185
        """Test that _parse_range acts reasonably."""
 
186
        content = StringIO('')
 
187
        parse_range = response.HttpRangeResponse._parse_range
 
188
        self.assertEqual((1,2), parse_range('bytes 1-2/3'))
 
189
        self.assertEqual((10,20), parse_range('bytes 10-20/2'))
 
190
 
 
191
        self.assertRaises(errors.InvalidHttpRange, parse_range, 'char 1-3/2')
 
192
        self.assertRaises(errors.InvalidHttpRange, parse_range, 'bytes a-3/2')
 
193
 
 
194
        try:
 
195
            parse_range('bytes x-10/3', path='http://foo/bar')
 
196
        except errors.InvalidHttpRange, e:
 
197
            self.assertContainsRe(str(e), 'http://foo/bar')
 
198
            self.assertContainsRe(str(e), 'bytes x-10/3')
 
199
        else:
 
200
            self.fail('Did not raise InvalidHttpRange')
 
201
 
 
202
    def test__parse_boundary_simple(self):
 
203
        """Test that _parse_boundary handles Content-type properly"""
 
204
        parse_boundary = response.HttpMultipartRangeResponse._parse_boundary
 
205
        m = parse_boundary(' multipart/byteranges; boundary=xxyyzz')
 
206
        self.assertNotEqual(None, m)
 
207
        # Check that the returned regex is capable of splitting simple_data
 
208
        matches = list(m.finditer(simple_data))
 
209
        self.assertEqual(4, len(matches))
 
210
 
 
211
        # match.group() should be the content-range entry
 
212
        # and match.end() should be the start of the content
 
213
        self.assertEqual(' bytes 1-10/20', matches[0].group(1))
 
214
        self.assertEqual(simple_data.find('1234567890'), matches[0].end())
 
215
        self.assertEqual(' bytes 21-30/20', matches[1].group(1))
 
216
        self.assertEqual(simple_data.find('abcdefghij'), matches[1].end())
 
217
        self.assertEqual(' bytes 41-50/20', matches[2].group(1))
 
218
        self.assertEqual(simple_data.find('zyxwvutsrq'), matches[2].end())
 
219
        self.assertEqual(' bytes 51-60/20', matches[3].group(1))
 
220
        self.assertEqual(simple_data.find('xxyyzz fbd'), matches[3].end())
 
221
 
 
222
    def test__parse_boundary_invalid(self):
 
223
        parse_boundary = response.HttpMultipartRangeResponse._parse_boundary
 
224
        try:
 
225
            parse_boundary(' multipart/bytes;boundary=xxyyzz',
 
226
                           path='http://foo/bar')
 
227
        except errors.InvalidHttpContentType, e:
 
228
            self.assertContainsRe(str(e), 'http://foo/bar')
 
229
            self.assertContainsRe(str(e), 'multipart/bytes;boundary=xxyyzz')
 
230
        else:
 
231
            self.fail('Did not raise InvalidHttpContentType')
 
232
 
 
233
 
 
234
class TestHttpRangeResponse(TestCase):
 
235
 
 
236
    def test_smoketest(self):
 
237
        """A basic test that HttpRangeResponse is reasonable."""
 
238
        content = StringIO('0123456789')
 
239
        f = response.HttpRangeResponse('http://foo', 'bytes 1-10/9', content)
 
240
        self.assertEqual([response.ResponseRange(1,10,0)], f._ranges)
 
241
 
 
242
        f.seek(0)
255
243
        self.assertRaises(errors.InvalidRange, f.read, 2)
256
 
 
257
 
 
258
 
class TestRangeFileMultipleRanges(tests.TestCase, TestRangeFileMixin):
259
 
    """Test a RangeFile for multiple ranges.
260
 
 
261
 
    The RangeFile used for the tests contains three ranges:
262
 
 
263
 
    - at offset 25: alpha
264
 
    - at offset 100: alpha
265
 
    - at offset 126: alpha.upper()
266
 
 
267
 
    The two last ranges are contiguous. This only rarely occurs (should not in
268
 
    fact) in real uses but may lead to hard to track bugs.
269
 
    """
270
 
 
271
 
    # The following is used to represent the boundary paramter defined
272
 
    # in HTTP response headers and the boundary lines that separate
273
 
    # multipart content.
274
 
 
275
 
    boundary = "separation"
276
 
 
277
 
    def setUp(self):
278
 
        super(TestRangeFileMultipleRanges, self).setUp()
279
 
 
280
 
        boundary = self.boundary
281
 
 
282
 
        content = ''
283
 
        self.first_range_start = 25
284
 
        file_size = 200 # big enough to encompass all ranges
285
 
        for (start, part) in [(self.first_range_start, self.alpha),
286
 
                              # Two contiguous ranges
287
 
                              (100, self.alpha),
288
 
                              (126, self.alpha.upper())]:
289
 
            content += self._multipart_byterange(part, start, boundary,
290
 
                                                 file_size)
291
 
        # Final boundary
292
 
        content += self._boundary_line()
293
 
 
294
 
        self._file = response.RangeFile('Multiple_ranges_file',
295
 
                                        StringIO(content))
296
 
        self.set_file_boundary()
297
 
 
298
 
    def _boundary_line(self):
299
 
        """Helper to build the formatted boundary line."""
300
 
        return '--' + self.boundary + '\r\n'
301
 
 
302
 
    def set_file_boundary(self):
303
 
        # Ranges are set by decoding the range headers, the RangeFile user is
304
 
        # supposed to call the following before using seek or read since it
305
 
        # requires knowing the *response* headers (in that case the boundary
306
 
        # which is part of the Content-Type header).
307
 
        self._file.set_boundary(self.boundary)
308
 
 
309
 
    def _multipart_byterange(self, data, offset, boundary, file_size='*'):
310
 
        """Encode a part of a file as a multipart/byterange MIME type.
311
 
 
312
 
        When a range request is issued, the HTTP response body can be
313
 
        decomposed in parts, each one representing a range (start, size) in a
314
 
        file.
315
 
 
316
 
        :param data: The payload.
317
 
        :param offset: where data starts in the file
318
 
        :param boundary: used to separate the parts
319
 
        :param file_size: the size of the file containing the range (default to
320
 
            '*' meaning unknown)
321
 
 
322
 
        :return: a string containing the data encoded as it will appear in the
323
 
            HTTP response body.
324
 
        """
325
 
        bline = self._boundary_line()
326
 
        # Each range begins with a boundary line
327
 
        range = bline
328
 
        # A range is described by a set of headers, but only 'Content-Range' is
329
 
        # required for our implementation (TestHandleResponse below will
330
 
        # exercise ranges with multiple or missing headers')
331
 
        range += 'Content-Range: bytes %d-%d/%d\r\n' % (offset,
332
 
                                                        offset+len(data)-1,
333
 
                                                        file_size)
334
 
        range += '\r\n'
335
 
        # Finally the raw bytes
336
 
        range += data
337
 
        return range
338
 
 
339
 
    def test_read_all_ranges(self):
340
 
        f = self._file
341
 
        self.assertEquals(self.alpha, f.read()) # Read first range
342
 
        f.seek(100) # Trigger the second range recognition
343
 
        self.assertEquals(self.alpha, f.read()) # Read second range
344
 
        self.assertEquals(126, f.tell())
345
 
        f.seek(126) # Start of third range which is also the current pos !
346
 
        self.assertEquals('A', f.read(1))
347
 
        f.seek(10, 1)
348
 
        self.assertEquals('LMN', f.read(3))
349
 
 
350
 
    def test_seek_from_end(self):
351
 
        """See TestRangeFileMixin.test_seek_from_end."""
352
 
        # The actual implementation will seek from end for the first range only
353
 
        # and then fail. Since seeking from end is intended to be used for a
354
 
        # single range only anyway, this test just document the actual
355
 
        # behaviour.
356
 
        f = self._file
357
 
        f.seek(-2, 2)
358
 
        self.assertEquals('yz', f.read())
359
 
        self.assertRaises(errors.InvalidRange, f.seek, -2, 2)
360
 
 
361
 
    def test_seek_into_void(self):
362
 
        f = self._file
363
 
        start = self.first_range_start
364
 
        f.seek(start)
365
 
        # Seeking to a point between two ranges is possible (only once) but
366
 
        # reading there is forbidden
367
 
        f.seek(start + 40)
368
 
        # We crossed a range boundary, so now the file is positioned at the
369
 
        # start of the new range (i.e. trying to seek below 100 will error out)
370
 
        f.seek(100)
371
 
        f.seek(125)
372
 
 
373
 
    def test_seek_across_ranges(self):
374
 
        f = self._file
375
 
        start = self.first_range_start
376
 
        f.seek(126) # skip the two first ranges
377
 
        self.assertEquals('AB', f.read(2))
378
 
 
379
 
    def test_checked_read_dont_overflow_buffers(self):
380
 
        f = self._file
381
 
        start = self.first_range_start
382
 
        # We force a very low value to exercise all code paths in _checked_read
383
 
        f._discarded_buf_size = 8
384
 
        f.seek(126) # skip the two first ranges
385
 
        self.assertEquals('AB', f.read(2))
386
 
 
387
 
    def test_seek_twice_between_ranges(self):
388
 
        f = self._file
389
 
        start = self.first_range_start
390
 
        f.seek(start + 40) # Past the first range but before the second
391
 
        # Now the file is positioned at the second range start (100)
392
 
        self.assertRaises(errors.InvalidRange, f.seek, start + 41)
393
 
 
394
 
    def test_seek_at_range_end(self):
395
 
        """Test seek behavior at range end."""
396
 
        f = self._file
397
 
        f.seek(25 + 25)
398
 
        f.seek(100 + 25)
399
 
        f.seek(126 + 25)
400
 
 
401
 
    def test_read_at_range_end(self):
402
 
        f = self._file
403
 
        self.assertEquals(self.alpha, f.read())
404
 
        self.assertEquals(self.alpha, f.read())
405
 
        self.assertEquals(self.alpha.upper(), f.read())
406
 
        self.assertRaises(errors.InvalidHttpResponse, f.read, 1)
407
 
 
408
 
 
409
 
class TestRangeFileMultipleRangesQuotedBoundaries(TestRangeFileMultipleRanges):
410
 
    """Perform the same tests as TestRangeFileMultipleRanges, but uses
411
 
    an angle-bracket quoted boundary string like IIS 6.0 and 7.0
412
 
    (but not IIS 5, which breaks the RFC in a different way
413
 
    by using square brackets, not angle brackets)
414
 
 
415
 
    This reveals a bug caused by
416
 
 
417
 
    - The bad implementation of RFC 822 unquoting in Python (angles are not
418
 
      quotes), coupled with
419
 
 
420
 
    - The bad implementation of RFC 2046 in IIS (angles are not permitted chars
421
 
      in boundary lines).
422
 
 
423
 
    """
424
 
    # The boundary as it appears in boundary lines
425
 
    # IIS 6 and 7 use this value
426
 
    _boundary_trimmed = "q1w2e3r4t5y6u7i8o9p0zaxscdvfbgnhmjklkl"
427
 
    boundary = '<' + _boundary_trimmed + '>'
428
 
 
429
 
    def set_file_boundary(self):
430
 
        # Emulate broken rfc822.unquote() here by removing angles
431
 
        self._file.set_boundary(self._boundary_trimmed)
432
 
 
433
 
 
434
 
class TestRangeFileVarious(tests.TestCase):
435
 
    """Tests RangeFile aspects not covered elsewhere."""
436
 
 
437
 
    def test_seek_whence(self):
438
 
        """Test the seek whence parameter values."""
439
 
        f = response.RangeFile('foo', StringIO('abc'))
440
 
        f.set_range(0, 3)
441
 
        f.seek(0)
442
 
        f.seek(1, 1)
443
 
        f.seek(-1, 2)
444
 
        self.assertRaises(ValueError, f.seek, 0, 14)
445
 
 
446
 
    def test_range_syntax(self):
447
 
        """Test the Content-Range scanning."""
448
 
 
449
 
        f = response.RangeFile('foo', StringIO())
450
 
 
451
 
        def ok(expected, header_value):
452
 
            f.set_range_from_header(header_value)
453
 
            # Slightly peek under the covers to get the size
454
 
            self.assertEquals(expected, (f.tell(), f._size))
455
 
 
456
 
        ok((1, 10), 'bytes 1-10/11')
457
 
        ok((1, 10), 'bytes 1-10/*')
458
 
        ok((12, 2), '\tbytes 12-13/*')
459
 
        ok((28, 1), '  bytes 28-28/*')
460
 
        ok((2123, 2120), 'bytes  2123-4242/12310')
461
 
        ok((1, 10), 'bytes 1-10/ttt') # We don't check total (ttt)
462
 
 
463
 
        def nok(header_value):
464
 
            self.assertRaises(errors.InvalidHttpRange,
465
 
                              f.set_range_from_header, header_value)
466
 
 
467
 
        nok('bytes 10-2/3')
468
 
        nok('chars 1-2/3')
469
 
        nok('bytes xx-yyy/zzz')
470
 
        nok('bytes xx-12/zzz')
471
 
        nok('bytes 11-yy/zzz')
472
 
        nok('bytes10-2/3')
 
244
        f.seek(1)
 
245
        self.assertEqual('012345', f.read(6))
 
246
 
 
247
    def test_invalid(self):
 
248
        try:
 
249
            f = response.HttpRangeResponse('http://foo', 'bytes x-10/9',
 
250
                                           StringIO('0123456789'))
 
251
        except errors.InvalidHttpRange, e:
 
252
            self.assertContainsRe(str(e), 'http://foo')
 
253
            self.assertContainsRe(str(e), 'bytes x-10/9')
 
254
        else:
 
255
            self.fail('Failed to raise InvalidHttpRange')
 
256
 
 
257
 
 
258
class TestHttpMultipartRangeResponse(TestCase):
 
259
    """Test the handling of multipart range responses"""
 
260
 
 
261
    def test_simple(self):
 
262
        content = StringIO(simple_data)
 
263
        multi = response.HttpMultipartRangeResponse('http://foo',
 
264
                    'multipart/byteranges; boundary = xxyyzz', content)
 
265
 
 
266
        self.assertEqual(4, len(multi._ranges))
 
267
 
 
268
        multi.seek(1)
 
269
        self.assertEqual('1234567890', multi.read(10))
 
270
        multi.seek(21)
 
271
        self.assertEqual('abcdefghij', multi.read(10))
 
272
        multi.seek(41)
 
273
        self.assertEqual('zyxwvutsrq', multi.read(10))
 
274
        multi.seek(51)
 
275
        self.assertEqual('xxyyzz fbd', multi.read(10))
 
276
        # TODO: jam 20060706 Currently RangeFile does not support
 
277
        #       reading across ranges. Consider adding it.
 
278
        multi.seek(41)
 
279
        # self.assertEqual('zyxwvutsrqxxyyzz fbd', multi.read(20))
 
280
        self.assertRaises(errors.InvalidRange, multi.read, 20)
 
281
 
 
282
        multi.seek(21)
 
283
        self.assertRaises(errors.InvalidRange, multi.read, 11)
 
284
        multi.seek(31)
 
285
        self.assertRaises(errors.InvalidRange, multi.read, 10)
 
286
 
 
287
    def test_invalid(self):
 
288
        content = StringIO('')
 
289
        try:
 
290
            response.HttpMultipartRangeResponse('http://foo',
 
291
                        'multipart/byte;boundary=invalid', content)
 
292
        except errors.InvalidHttpContentType, e:
 
293
            self.assertContainsRe(str(e), 'http://foo')
 
294
            self.assertContainsRe(str(e), 'multipart/byte;')
473
295
 
474
296
 
475
297
# Taken from real request responses
487
309
""")
488
310
 
489
311
 
 
312
_missing_response = (404, """HTTP/1.1 404 Not Found\r
 
313
Date: Tue, 11 Jul 2006 04:32:56 GMT\r
 
314
Server: Apache/2.0.54 (Fedora)\r
 
315
Content-Length: 336\r
 
316
Connection: close\r
 
317
Content-Type: text/html; charset=iso-8859-1\r
 
318
\r
 
319
""", """<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
 
320
<html><head>
 
321
<title>404 Not Found</title>
 
322
</head><body>
 
323
<h1>Not Found</h1>
 
324
<p>The requested URL /branches/bzr/jam-integration/.bzr/repository/format was not found on this server.</p>
 
325
<hr>
 
326
<address>Apache/2.0.54 (Fedora) Server at bzr.arbash-meinel.com Port 80</address>
 
327
</body></html>
 
328
""")
 
329
 
 
330
 
490
331
_single_range_response = (206, """HTTP/1.1 206 Partial Content\r
491
332
Date: Tue, 11 Jul 2006 04:45:22 GMT\r
492
333
Server: Apache/2.0.54 (Fedora)\r
502
343
mbp@sourcefrog.net-20050309040929-eee0eb3e6d1e762""")
503
344
 
504
345
 
505
 
_single_range_no_content_type = (206, """HTTP/1.1 206 Partial Content\r
506
 
Date: Tue, 11 Jul 2006 04:45:22 GMT\r
507
 
Server: Apache/2.0.54 (Fedora)\r
508
 
Last-Modified: Thu, 06 Jul 2006 20:22:05 GMT\r
509
 
ETag: "238a3c-16ec2-805c5540"\r
510
 
Accept-Ranges: bytes\r
511
 
Content-Length: 100\r
512
 
Content-Range: bytes 100-199/93890\r
513
 
Connection: close\r
514
 
\r
515
 
""", """mbp@sourcefrog.net-20050309040815-13242001617e4a06
516
 
mbp@sourcefrog.net-20050309040929-eee0eb3e6d1e762""")
517
 
 
518
 
 
519
346
_multipart_range_response = (206, """HTTP/1.1 206 Partial Content\r
520
347
Date: Tue, 11 Jul 2006 04:49:48 GMT\r
521
348
Server: Apache/2.0.54 (Fedora)\r
562
389
mbp@sourcefrog.net-20050314025737-55eb441f430ab4ba
563
390
mbp@sourcefrog.net-20050314025901-d74aa93bb7ee8f62
564
391
mbp@source\r
565
 
--418470f848b63279b--\r
 
392
--418470f848b63279b--\r\n'
566
393
""")
567
394
 
568
395
 
569
 
_multipart_squid_range_response = (206, """HTTP/1.0 206 Partial Content\r
570
 
Date: Thu, 31 Aug 2006 21:16:22 GMT\r
571
 
Server: Apache/2.2.2 (Unix) DAV/2\r
572
 
Last-Modified: Thu, 31 Aug 2006 17:57:06 GMT\r
 
396
_redirect_response = (206, """HTTP/1.1 301 Moved Permanently\r
 
397
Date: Tue, 18 Jul 2006 20:29:22 GMT\r
 
398
Server: Apache/2.0.54 (Ubuntu) PHP/4.4.0-3ubuntu1 mod_ssl/2.0.54 OpenSSL/0.9.7g\r
 
399
Location: http://bazaar-vcs.org/bzr/bzr.dev/.bzr/repository/inventory.knit\r
 
400
Content-Length: 272\r
 
401
Keep-Alive: timeout=15, max=100\r
 
402
Connection: Keep-Alive\r
 
403
Content-Type: text/html; charset=iso-8859-1\r
 
404
\r
 
405
HTTP/1.1 206 Partial Content\r
 
406
Date: Tue, 18 Jul 2006 20:29:23 GMT\r
 
407
Server: Apache/2.0.54 (Ubuntu) PHP/4.4.0-3ubuntu1 mod_ssl/2.0.54 OpenSSL/0.9.7g\r
 
408
Last-Modified: Tue, 18 Jul 2006 20:24:59 GMT\r
 
409
ETag: "be8213-83958c-f0d3dcc0"\r
573
410
Accept-Ranges: bytes\r
574
 
Content-Type: multipart/byteranges; boundary="squid/2.5.STABLE12:C99323425AD4FE26F726261FA6C24196"\r
575
 
Content-Length: 598\r
576
 
X-Cache: MISS from localhost.localdomain\r
577
 
X-Cache-Lookup: HIT from localhost.localdomain:3128\r
578
 
Proxy-Connection: keep-alive\r
579
 
\r
580
 
""",
581
 
"""\r
582
 
--squid/2.5.STABLE12:C99323425AD4FE26F726261FA6C24196\r
583
 
Content-Type: text/plain\r
584
 
Content-Range: bytes 0-99/18672\r
585
 
\r
586
 
# bzr knit index 8
587
 
 
588
 
scott@netsplit.com-20050708230047-47c7868f276b939f fulltext 0 863  :
589
 
scott@netsp\r
590
 
--squid/2.5.STABLE12:C99323425AD4FE26F726261FA6C24196\r
591
 
Content-Type: text/plain\r
592
 
Content-Range: bytes 300-499/18672\r
593
 
\r
594
 
com-20050708231537-2b124b835395399a :
595
 
scott@netsplit.com-20050820234126-551311dbb7435b51 line-delta 1803 479 .scott@netsplit.com-20050820232911-dc4322a084eadf7e :
596
 
scott@netsplit.com-20050821213706-c86\r
597
 
--squid/2.5.STABLE12:C99323425AD4FE26F726261FA6C24196--\r
 
411
Content-Length: 425\r
 
412
Content-Range: bytes 8623075-8623499/8623500\r
 
413
Keep-Alive: timeout=15, max=100\r
 
414
Connection: Keep-Alive\r
 
415
Content-Type: text/plain; charset=UTF-8\r
 
416
\r
 
417
""", """this data intentionally removed, 
 
418
this is not meant to be tested by
 
419
handle_response, just _extract_headers
598
420
""")
599
421
 
600
422
 
601
423
# This is made up
602
 
_full_text_response_no_content_type = (200, """HTTP/1.1 200 OK\r
603
 
Date: Tue, 11 Jul 2006 04:32:56 GMT\r
604
 
Server: Apache/2.0.54 (Fedora)\r
605
 
Last-Modified: Sun, 23 Apr 2006 19:35:20 GMT\r
606
 
ETag: "56691-23-38e9ae00"\r
607
 
Accept-Ranges: bytes\r
608
 
Content-Length: 35\r
609
 
Connection: close\r
610
 
\r
611
 
""", """Bazaar-NG meta directory, format 1
612
 
""")
613
 
 
614
 
 
615
 
_full_text_response_no_content_length = (200, """HTTP/1.1 200 OK\r
616
 
Date: Tue, 11 Jul 2006 04:32:56 GMT\r
617
 
Server: Apache/2.0.54 (Fedora)\r
618
 
Last-Modified: Sun, 23 Apr 2006 19:35:20 GMT\r
619
 
ETag: "56691-23-38e9ae00"\r
620
 
Accept-Ranges: bytes\r
621
 
Connection: close\r
622
 
Content-Type: text/plain; charset=UTF-8\r
623
 
\r
624
 
""", """Bazaar-NG meta directory, format 1
625
 
""")
626
 
 
627
 
 
628
 
_single_range_no_content_range = (206, """HTTP/1.1 206 Partial Content\r
629
 
Date: Tue, 11 Jul 2006 04:45:22 GMT\r
630
 
Server: Apache/2.0.54 (Fedora)\r
631
 
Last-Modified: Thu, 06 Jul 2006 20:22:05 GMT\r
632
 
ETag: "238a3c-16ec2-805c5540"\r
633
 
Accept-Ranges: bytes\r
634
 
Content-Length: 100\r
635
 
Connection: close\r
636
 
\r
637
 
""", """mbp@sourcefrog.net-20050309040815-13242001617e4a06
638
 
mbp@sourcefrog.net-20050309040929-eee0eb3e6d1e762""")
639
 
 
640
 
 
641
 
_single_range_response_truncated = (206, """HTTP/1.1 206 Partial Content\r
642
 
Date: Tue, 11 Jul 2006 04:45:22 GMT\r
643
 
Server: Apache/2.0.54 (Fedora)\r
644
 
Last-Modified: Thu, 06 Jul 2006 20:22:05 GMT\r
645
 
ETag: "238a3c-16ec2-805c5540"\r
646
 
Accept-Ranges: bytes\r
647
 
Content-Length: 100\r
648
 
Content-Range: bytes 100-199/93890\r
649
 
Connection: close\r
650
 
Content-Type: text/plain; charset=UTF-8\r
651
 
\r
652
 
""", """mbp@sourcefrog.net-20050309040815-13242001617e4a06""")
653
 
 
654
 
 
655
424
_invalid_response = (444, """HTTP/1.1 444 Bad Response\r
656
425
Date: Tue, 11 Jul 2006 04:32:56 GMT\r
657
426
Connection: close\r
668
437
""")
669
438
 
670
439
 
671
 
_multipart_no_content_range = (206, """HTTP/1.0 206 Partial Content\r
672
 
Content-Type: multipart/byteranges; boundary=THIS_SEPARATES\r
673
 
Content-Length: 598\r
674
 
\r
675
 
""",
676
 
"""\r
677
 
--THIS_SEPARATES\r
678
 
Content-Type: text/plain\r
679
 
\r
680
 
# bzr knit index 8
681
 
--THIS_SEPARATES\r
682
 
""")
683
 
 
684
 
 
685
 
_multipart_no_boundary = (206, """HTTP/1.0 206 Partial Content\r
686
 
Content-Type: multipart/byteranges; boundary=THIS_SEPARATES\r
687
 
Content-Length: 598\r
688
 
\r
689
 
""",
690
 
"""\r
691
 
--THIS_SEPARATES\r
692
 
Content-Type: text/plain\r
693
 
Content-Range: bytes 0-18/18672\r
694
 
\r
695
 
# bzr knit index 8
696
 
 
697
 
The range ended at the line above, this text is garbage instead of a boundary
698
 
line
699
 
""")
700
 
 
701
 
 
702
 
class TestHandleResponse(tests.TestCase):
703
 
 
704
 
    def _build_HTTPMessage(self, raw_headers):
705
 
        status_and_headers = StringIO(raw_headers)
706
 
        # Get rid of the status line
707
 
        status_and_headers.readline()
708
 
        msg = httplib.HTTPMessage(status_and_headers)
709
 
        return msg
710
 
 
 
440
# This should be in test_http.py, but the headers we
 
441
# want to parse are here
 
442
class TestExtractHeader(TestCase):
 
443
    
 
444
    def use_response(self, response):
 
445
        self.headers = http._extract_headers(response[1], 'http://foo')
 
446
 
 
447
    def check_header(self, header, value):
 
448
        self.assertEqual(value, self.headers[header])
 
449
        
 
450
    def test_full_text(self):
 
451
        self.use_response(_full_text_response)
 
452
 
 
453
        self.check_header('Date', 'Tue, 11 Jul 2006 04:32:56 GMT')
 
454
        self.check_header('date', 'Tue, 11 Jul 2006 04:32:56 GMT')
 
455
        self.check_header('Content-Length', '35')
 
456
        self.check_header('Content-Type', 'text/plain; charset=UTF-8')
 
457
        self.check_header('content-type', 'text/plain; charset=UTF-8')
 
458
 
 
459
    def test_missing_response(self):
 
460
        self.use_response(_missing_response)
 
461
 
 
462
        self.check_header('Content-Length', '336')
 
463
        self.check_header('Content-Type', 'text/html; charset=iso-8859-1')
 
464
 
 
465
    def test_single_range(self):
 
466
        self.use_response(_single_range_response)
 
467
 
 
468
        self.check_header('Content-Length', '100')
 
469
        self.check_header('Content-Range', 'bytes 100-199/93890')
 
470
        self.check_header('Content-Type', 'text/plain; charset=UTF-8')
 
471
 
 
472
    def test_multi_range(self):
 
473
        self.use_response(_multipart_range_response)
 
474
 
 
475
        self.check_header('Content-Length', '1534')
 
476
        self.check_header('Content-Type',
 
477
                          'multipart/byteranges; boundary=418470f848b63279b')
 
478
 
 
479
    def test_redirect(self):
 
480
        """We default to returning the last group of headers in the file."""
 
481
        self.use_response(_redirect_response)
 
482
        self.check_header('Content-Range', 'bytes 8623075-8623499/8623500')
 
483
        self.check_header('Content-Type', 'text/plain; charset=UTF-8')
 
484
 
 
485
    def test_empty(self):
 
486
        self.assertRaises(errors.InvalidHttpResponse,
 
487
            http._extract_headers, '', 'bad url')
 
488
 
 
489
    def test_no_opening_http(self):
 
490
        # Remove the HTTP line from the header
 
491
        first, txt = _full_text_response[1].split('\r\n', 1)
 
492
        self.assertRaises(errors.InvalidHttpResponse,
 
493
            http._extract_headers, txt, 'missing HTTTP')
 
494
 
 
495
    def test_trailing_whitespace(self):
 
496
        # Test that we ignore bogus whitespace on the end
 
497
        code, txt, body = _full_text_response
 
498
        txt += '\r\n\n\n\n\n'
 
499
        self.use_response((code, txt, body))
 
500
 
 
501
        self.check_header('Date', 'Tue, 11 Jul 2006 04:32:56 GMT')
 
502
        self.check_header('Content-Length', '35')
 
503
        self.check_header('Content-Type', 'text/plain; charset=UTF-8')
 
504
 
 
505
    def test_trailing_non_http(self):
 
506
        # Test that we ignore bogus stuff on the end
 
507
        code, txt, body = _full_text_response
 
508
        txt = txt + 'Foo: Bar\r\nBaz: Bling\r\n\r\n'
 
509
        self.use_response((code, txt, body))
 
510
 
 
511
        self.check_header('Date', 'Tue, 11 Jul 2006 04:32:56 GMT')
 
512
        self.check_header('Content-Length', '35')
 
513
        self.check_header('Content-Type', 'text/plain; charset=UTF-8')
 
514
        self.assertRaises(KeyError, self.headers.__getitem__, 'Foo')
 
515
 
 
516
    def test_extra_whitespace(self):
 
517
        # Test that we read an HTTP response, even with extra whitespace
 
518
        code, txt, body = _redirect_response
 
519
        # Find the second HTTP location
 
520
        loc = txt.find('HTTP', 5)
 
521
        txt = txt[:loc] + '\r\n\n' + txt[loc:]
 
522
        self.use_response((code, txt, body))
 
523
        self.check_header('Content-Range', 'bytes 8623075-8623499/8623500')
 
524
        self.check_header('Content-Type', 'text/plain; charset=UTF-8')
 
525
 
 
526
 
 
527
class TestHandleResponse(TestCase):
 
528
    
711
529
    def get_response(self, a_response):
712
530
        """Process a supplied response, and return the result."""
713
 
        code, raw_headers, body = a_response
714
 
        msg = self._build_HTTPMessage(raw_headers)
715
 
        return response.handle_response('http://foo', code, msg,
 
531
        headers = http._extract_headers(a_response[1], 'http://foo')
 
532
        return response.handle_response('http://foo', a_response[0], headers,
716
533
                                        StringIO(a_response[2]))
717
534
 
718
535
    def test_full_text(self):
720
537
        # It is a StringIO from the original data
721
538
        self.assertEqual(_full_text_response[2], out.read())
722
539
 
 
540
    def test_missing_response(self):
 
541
        self.assertRaises(errors.NoSuchFile,
 
542
            self.get_response, _missing_response)
 
543
 
723
544
    def test_single_range(self):
724
545
        out = self.get_response(_single_range_response)
 
546
        self.assertIsInstance(out, response.HttpRangeResponse)
 
547
 
 
548
        self.assertRaises(errors.InvalidRange, out.read, 20)
725
549
 
726
550
        out.seek(100)
727
551
        self.assertEqual(_single_range_response[2], out.read(100))
728
552
 
729
 
    def test_single_range_no_content(self):
730
 
        out = self.get_response(_single_range_no_content_type)
731
 
 
732
 
        out.seek(100)
733
 
        self.assertEqual(_single_range_no_content_type[2], out.read(100))
734
 
 
735
 
    def test_single_range_truncated(self):
736
 
        out = self.get_response(_single_range_response_truncated)
737
 
        # Content-Range declares 100 but only 51 present
738
 
        self.assertRaises(errors.ShortReadvError, out.seek, out.tell() + 51)
739
 
 
740
553
    def test_multi_range(self):
741
554
        out = self.get_response(_multipart_range_response)
 
555
        self.assertIsInstance(out, response.HttpMultipartRangeResponse)
742
556
 
743
557
        # Just make sure we can read the right contents
744
558
        out.seek(0)
747
561
        out.seek(1000)
748
562
        out.read(1050)
749
563
 
750
 
    def test_multi_squid_range(self):
751
 
        out = self.get_response(_multipart_squid_range_response)
752
 
 
753
 
        # Just make sure we can read the right contents
754
 
        out.seek(0)
755
 
        out.read(100)
756
 
 
757
 
        out.seek(300)
758
 
        out.read(200)
759
 
 
760
564
    def test_invalid_response(self):
761
565
        self.assertRaises(errors.InvalidHttpResponse,
762
 
                          self.get_response, _invalid_response)
 
566
            self.get_response, _invalid_response)
763
567
 
764
568
    def test_full_text_no_content_type(self):
765
569
        # We should not require Content-Type for a full response
766
 
        code, raw_headers, body = _full_text_response_no_content_type
767
 
        msg = self._build_HTTPMessage(raw_headers)
768
 
        out = response.handle_response('http://foo', code, msg, StringIO(body))
769
 
        self.assertEqual(body, out.read())
770
 
 
771
 
    def test_full_text_no_content_length(self):
772
 
        code, raw_headers, body = _full_text_response_no_content_length
773
 
        msg = self._build_HTTPMessage(raw_headers)
774
 
        out = response.handle_response('http://foo', code, msg, StringIO(body))
775
 
        self.assertEqual(body, out.read())
 
570
        a_response = _full_text_response
 
571
        headers = http._extract_headers(a_response[1], 'http://foo')
 
572
        del headers['Content-Type']
 
573
        out = response.handle_response('http://foo', a_response[0], headers,
 
574
                                        StringIO(a_response[2]))
 
575
        self.assertEqual(_full_text_response[2], out.read())
 
576
 
 
577
    def test_missing_no_content_type(self):
 
578
        # Without Content-Type we should still raise NoSuchFile on a 404
 
579
        a_response = _missing_response
 
580
        headers = http._extract_headers(a_response[1], 'http://missing')
 
581
        del headers['Content-Type']
 
582
        self.assertRaises(errors.NoSuchFile,
 
583
            response.handle_response, 'http://missing', a_response[0], headers,
 
584
                                      StringIO(a_response[2]))
 
585
 
 
586
    def test_missing_content_type(self):
 
587
        a_response = _single_range_response
 
588
        headers = http._extract_headers(a_response[1], 'http://nocontent')
 
589
        del headers['Content-Type']
 
590
        self.assertRaises(errors.InvalidHttpContentType,
 
591
            response.handle_response, 'http://nocontent', a_response[0],
 
592
                                      headers, StringIO(a_response[2]))
776
593
 
777
594
    def test_missing_content_range(self):
778
 
        code, raw_headers, body = _single_range_no_content_range
779
 
        msg = self._build_HTTPMessage(raw_headers)
780
 
        self.assertRaises(errors.InvalidHttpResponse,
781
 
                          response.handle_response,
782
 
                          'http://bogus', code, msg, StringIO(body))
783
 
 
784
 
    def test_multipart_no_content_range(self):
785
 
        code, raw_headers, body = _multipart_no_content_range
786
 
        msg = self._build_HTTPMessage(raw_headers)
787
 
        self.assertRaises(errors.InvalidHttpResponse,
788
 
                          response.handle_response,
789
 
                          'http://bogus', code, msg, StringIO(body))
790
 
 
791
 
    def test_multipart_no_boundary(self):
792
 
        out = self.get_response(_multipart_no_boundary)
793
 
        out.read()  # Read the whole range
794
 
        # Fail to find the boundary line
795
 
        self.assertRaises(errors.InvalidHttpResponse, out.seek, 1, 1)
796
 
 
797
 
 
798
 
class TestRangeFileSizeReadLimited(tests.TestCase):
799
 
    """Test RangeFile _max_read_size functionality which limits the size of
800
 
    read blocks to prevent MemoryError messages in socket.recv.
801
 
    """
802
 
 
803
 
    def setUp(self):
804
 
        tests.TestCase.setUp(self)
805
 
        # create a test datablock larger than _max_read_size.
806
 
        chunk_size = response.RangeFile._max_read_size
807
 
        test_pattern = '0123456789ABCDEF'
808
 
        self.test_data =  test_pattern * (3 * chunk_size / len(test_pattern))
809
 
        self.test_data_len = len(self.test_data)
810
 
 
811
 
    def test_max_read_size(self):
812
 
        """Read data in blocks and verify that the reads are not larger than
813
 
           the maximum read size.
814
 
        """
815
 
        # retrieve data in large blocks from response.RangeFile object
816
 
        mock_read_file = FakeReadFile(self.test_data)
817
 
        range_file = response.RangeFile('test_max_read_size', mock_read_file)
818
 
        response_data = range_file.read(self.test_data_len)
819
 
 
820
 
        # verify read size was equal to the maximum read size
821
 
        self.assertTrue(mock_read_file.get_max_read_size() > 0)
822
 
        self.assertEqual(mock_read_file.get_max_read_size(),
823
 
                         response.RangeFile._max_read_size)
824
 
        self.assertEqual(mock_read_file.get_read_count(), 3)
825
 
 
826
 
        # report error if the data wasn't equal (we only report the size due
827
 
        # to the length of the data)
828
 
        if response_data != self.test_data:
829
 
            message = "Data not equal.  Expected %d bytes, received %d."
830
 
            self.fail(message % (len(response_data), self.test_data_len))
831
 
 
 
595
        a_response = _single_range_response
 
596
        headers = http._extract_headers(a_response[1], 'http://nocontent')
 
597
        del headers['Content-Range']
 
598
        self.assertRaises(errors.InvalidHttpResponse,
 
599
            response.handle_response, 'http://nocontent', a_response[0],
 
600
                                      headers, StringIO(a_response[2]))