~bzr-pqm/bzr/bzr.dev

2052.3.2 by John Arbash Meinel
Change Copyright .. by Canonical to Copyright ... Canonical
1
# Copyright (C) 2005, 2006 Canonical Ltd
1786.1.8 by John Arbash Meinel
[merge] Johan Rydberg test updates
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
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
1786.1.27 by John Arbash Meinel
Fix up the http transports so that tests pass with the new configuration.
20
import mimetools
1786.1.8 by John Arbash Meinel
[merge] Johan Rydberg test updates
21
1786.1.13 by John Arbash Meinel
Found a few bugs in error handling code, updated tests
22
from bzrlib import errors
1786.1.25 by John Arbash Meinel
Test that we can extract headers properly.
23
from bzrlib.transport import http
1786.1.13 by John Arbash Meinel
Found a few bugs in error handling code, updated tests
24
from bzrlib.transport.http import response
1786.1.8 by John Arbash Meinel
[merge] Johan Rydberg test updates
25
from bzrlib.tests import TestCase
26
27
1786.1.12 by John Arbash Meinel
Add tests for ResponseRange and streamline class
28
class TestResponseRange(TestCase):
29
    """Test the ResponseRange class."""
30
31
    def test_cmp(self):
1786.1.13 by John Arbash Meinel
Found a few bugs in error handling code, updated tests
32
        RR = response.ResponseRange
33
        r1 = RR(0, 10, 0)
34
        r2 = RR(15, 20, 10)
1786.1.12 by John Arbash Meinel
Add tests for ResponseRange and streamline class
35
        self.assertTrue(r1 < r2)
36
        self.assertFalse(r1 > r2)
37
        self.assertTrue(r1 < 5)
38
        self.assertFalse(r2 < 5)
39
1786.1.13 by John Arbash Meinel
Found a few bugs in error handling code, updated tests
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))
1786.1.12 by John Arbash Meinel
Add tests for ResponseRange and streamline class
43
44
    def test_sort_list(self):
1786.1.13 by John Arbash Meinel
Found a few bugs in error handling code, updated tests
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]
1786.1.12 by John Arbash Meinel
Add tests for ResponseRange and streamline class
48
        lst.sort()
1786.1.13 by John Arbash Meinel
Found a few bugs in error handling code, updated tests
49
        self.assertEqual([RR(3,7,0), RR(3,8,0), 5, 6], lst)
1786.1.12 by John Arbash Meinel
Add tests for ResponseRange and streamline class
50
51
1786.1.8 by John Arbash Meinel
[merge] Johan Rydberg test updates
52
class TestRangeFile(TestCase):
53
    """Test RangeFile."""
54
55
    def setUp(self):
56
        content = "abcdefghijklmnopqrstuvwxyz"
1786.1.13 by John Arbash Meinel
Found a few bugs in error handling code, updated tests
57
        self.fp = response.RangeFile('foo', StringIO(content))
1786.1.8 by John Arbash Meinel
[merge] Johan Rydberg test updates
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)
1786.1.13 by John Arbash Meinel
Found a few bugs in error handling code, updated tests
81
        self.assertRaises(errors.InvalidRange, self.fp.read, 2)
1786.1.8 by John Arbash Meinel
[merge] Johan Rydberg test updates
82
        self.fp.seek(39)
1786.1.13 by John Arbash Meinel
Found a few bugs in error handling code, updated tests
83
        self.assertRaises(errors.InvalidRange, self.fp.read, 2)
1786.1.8 by John Arbash Meinel
[merge] Johan Rydberg test updates
84
        self.fp.seek(19)
1786.1.13 by John Arbash Meinel
Found a few bugs in error handling code, updated tests
85
        self.assertRaises(errors.InvalidRange, self.fp.read, 2)
1786.1.12 by John Arbash Meinel
Add tests for ResponseRange and streamline class
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)
1786.1.13 by John Arbash Meinel
Found a few bugs in error handling code, updated tests
124
125
1786.1.16 by John Arbash Meinel
Refactor tests
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())
1786.1.13 by John Arbash Meinel
Found a few bugs in error handling code, updated tests
134
135
    def test_range_re(self):
136
        """Test that we match valid ranges."""
1786.1.24 by John Arbash Meinel
Move the functions/regexes to be static members
137
        self.regex = response.HttpRangeResponse._CONTENT_RANGE_RE
1786.1.16 by John Arbash Meinel
Refactor tests
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):
1786.1.24 by John Arbash Meinel
Move the functions/regexes to be static members
148
        self.regex = response.HttpMultipartRangeResponse._CONTENT_TYPE_RE
1979.1.2 by John Arbash Meinel
Use the regex, rather than stripping off the boundary later.
149
        self.assertRegexMatches(('', 'xxyyzz'),
1786.1.16 by John Arbash Meinel
Refactor tests
150
                                'multipart/byteranges; boundary = xxyyzz')
1979.1.2 by John Arbash Meinel
Use the regex, rather than stripping off the boundary later.
151
        self.assertRegexMatches(('', 'xxyyzz'),
1786.1.16 by John Arbash Meinel
Refactor tests
152
                                'multipart/byteranges;boundary=xxyyzz')
1979.1.2 by John Arbash Meinel
Use the regex, rather than stripping off the boundary later.
153
        self.assertRegexMatches(('', 'xx yy zz'),
1786.1.16 by John Arbash Meinel
Refactor tests
154
                                ' multipart/byteranges ; boundary= xx yy zz ')
1979.1.2 by John Arbash Meinel
Use the regex, rather than stripping off the boundary later.
155
        self.assertRegexMatches(('"', 'xx yy zz'),
156
                                ' multipart/byteranges ; boundary= "xx yy zz" ')
157
        self.assertEqual(None,
158
                         self.regex.match(
159
                             ' multipart/byteranges ; boundary= "xx yy zz '))
160
        self.assertEqual(None,
161
                         self.regex.match(
162
                             ' multipart/byteranges ; boundary= xx yy zz" '))
1786.1.21 by John Arbash Meinel
(broken) Work on factoring out handle_response so we can test with fake headers.
163
        self.assertEqual(None,
1786.1.16 by John Arbash Meinel
Refactor tests
164
                self.regex.match('multipart byteranges;boundary=xx'))
165
166
1786.1.17 by John Arbash Meinel
Adding tests for _parse_boundary.
167
simple_data = """
168
--xxyyzz\r
169
foo\r
170
Content-range: bytes 1-10/20\r
171
\r
172
1234567890
173
--xxyyzz\r
174
Content-Range: bytes 21-30/20\r
175
bar\r
176
\r
177
abcdefghij
178
--xxyyzz\r
179
content-range: bytes 41-50/20\r
180
\r
181
zyxwvutsrq
182
--xxyyzz\r
1786.1.18 by John Arbash Meinel
Add tests for HttpMultiRangeResponse
183
content-range: bytes 51-60/20\r
1786.1.17 by John Arbash Meinel
Adding tests for _parse_boundary.
184
\r
185
xxyyzz fbd
186
"""
187
188
189
class TestHelpers(TestCase):
190
    """Test the helper functions"""
1786.1.13 by John Arbash Meinel
Found a few bugs in error handling code, updated tests
191
192
    def test__parse_range(self):
193
        """Test that _parse_range acts reasonably."""
194
        content = StringIO('')
1786.1.24 by John Arbash Meinel
Move the functions/regexes to be static members
195
        parse_range = response.HttpRangeResponse._parse_range
1786.1.15 by John Arbash Meinel
Factor out _parse_range into a separate function for easier testing/better hierarchy.
196
        self.assertEqual((1,2), parse_range('bytes 1-2/3'))
197
        self.assertEqual((10,20), parse_range('bytes 10-20/2'))
198
199
        self.assertRaises(errors.InvalidHttpRange, parse_range, 'char 1-3/2')
200
        self.assertRaises(errors.InvalidHttpRange, parse_range, 'bytes a-3/2')
201
202
        try:
203
            parse_range('bytes x-10/3', path='http://foo/bar')
204
        except errors.InvalidHttpRange, e:
205
            self.assertContainsRe(str(e), 'http://foo/bar')
1786.1.17 by John Arbash Meinel
Adding tests for _parse_boundary.
206
            self.assertContainsRe(str(e), 'bytes x-10/3')
1786.1.15 by John Arbash Meinel
Factor out _parse_range into a separate function for easier testing/better hierarchy.
207
        else:
208
            self.fail('Did not raise InvalidHttpRange')
1786.1.13 by John Arbash Meinel
Found a few bugs in error handling code, updated tests
209
1786.1.17 by John Arbash Meinel
Adding tests for _parse_boundary.
210
    def test__parse_boundary_simple(self):
211
        """Test that _parse_boundary handles Content-type properly"""
1786.1.24 by John Arbash Meinel
Move the functions/regexes to be static members
212
        parse_boundary = response.HttpMultipartRangeResponse._parse_boundary
213
        m = parse_boundary(' multipart/byteranges; boundary=xxyyzz')
1786.1.17 by John Arbash Meinel
Adding tests for _parse_boundary.
214
        self.assertNotEqual(None, m)
215
        # Check that the returned regex is capable of splitting simple_data
216
        matches = list(m.finditer(simple_data))
217
        self.assertEqual(4, len(matches))
218
219
        # match.group() should be the content-range entry
220
        # and match.end() should be the start of the content
221
        self.assertEqual(' bytes 1-10/20', matches[0].group(1))
222
        self.assertEqual(simple_data.find('1234567890'), matches[0].end())
223
        self.assertEqual(' bytes 21-30/20', matches[1].group(1))
224
        self.assertEqual(simple_data.find('abcdefghij'), matches[1].end())
225
        self.assertEqual(' bytes 41-50/20', matches[2].group(1))
226
        self.assertEqual(simple_data.find('zyxwvutsrq'), matches[2].end())
1786.1.18 by John Arbash Meinel
Add tests for HttpMultiRangeResponse
227
        self.assertEqual(' bytes 51-60/20', matches[3].group(1))
1786.1.17 by John Arbash Meinel
Adding tests for _parse_boundary.
228
        self.assertEqual(simple_data.find('xxyyzz fbd'), matches[3].end())
229
230
    def test__parse_boundary_invalid(self):
1786.1.24 by John Arbash Meinel
Move the functions/regexes to be static members
231
        parse_boundary = response.HttpMultipartRangeResponse._parse_boundary
1786.1.17 by John Arbash Meinel
Adding tests for _parse_boundary.
232
        try:
1786.1.24 by John Arbash Meinel
Move the functions/regexes to be static members
233
            parse_boundary(' multipart/bytes;boundary=xxyyzz',
234
                           path='http://foo/bar')
1786.1.17 by John Arbash Meinel
Adding tests for _parse_boundary.
235
        except errors.InvalidHttpContentType, e:
236
            self.assertContainsRe(str(e), 'http://foo/bar')
237
            self.assertContainsRe(str(e), 'multipart/bytes;boundary=xxyyzz')
238
        else:
239
            self.fail('Did not raise InvalidHttpContentType')
240
241
242
class TestHttpRangeResponse(TestCase):
243
1786.1.13 by John Arbash Meinel
Found a few bugs in error handling code, updated tests
244
    def test_smoketest(self):
245
        """A basic test that HttpRangeResponse is reasonable."""
246
        content = StringIO('0123456789')
247
        f = response.HttpRangeResponse('http://foo', 'bytes 1-10/9', content)
248
        self.assertEqual([response.ResponseRange(1,10,0)], f._ranges)
249
250
        f.seek(0)
251
        self.assertRaises(errors.InvalidRange, f.read, 2)
252
        f.seek(1)
253
        self.assertEqual('012345', f.read(6))
1786.1.14 by John Arbash Meinel
Testing basic functionality of HttpMultipartRangeResponse
254
1786.1.17 by John Arbash Meinel
Adding tests for _parse_boundary.
255
    def test_invalid(self):
256
        try:
257
            f = response.HttpRangeResponse('http://foo', 'bytes x-10/9',
258
                                           StringIO('0123456789'))
259
        except errors.InvalidHttpRange, e:
260
            self.assertContainsRe(str(e), 'http://foo')
261
            self.assertContainsRe(str(e), 'bytes x-10/9')
262
        else:
263
            self.fail('Failed to raise InvalidHttpRange')
264
1786.1.14 by John Arbash Meinel
Testing basic functionality of HttpMultipartRangeResponse
265
266
class TestHttpMultipartRangeResponse(TestCase):
1786.1.17 by John Arbash Meinel
Adding tests for _parse_boundary.
267
    """Test the handling of multipart range responses"""
1786.1.18 by John Arbash Meinel
Add tests for HttpMultiRangeResponse
268
269
    def test_simple(self):
270
        content = StringIO(simple_data)
1786.1.21 by John Arbash Meinel
(broken) Work on factoring out handle_response so we can test with fake headers.
271
        multi = response.HttpMultipartRangeResponse('http://foo',
1786.1.18 by John Arbash Meinel
Add tests for HttpMultiRangeResponse
272
                    'multipart/byteranges; boundary = xxyyzz', content)
273
274
        self.assertEqual(4, len(multi._ranges))
275
276
        multi.seek(1)
277
        self.assertEqual('1234567890', multi.read(10))
278
        multi.seek(21)
279
        self.assertEqual('abcdefghij', multi.read(10))
280
        multi.seek(41)
281
        self.assertEqual('zyxwvutsrq', multi.read(10))
282
        multi.seek(51)
283
        self.assertEqual('xxyyzz fbd', multi.read(10))
284
        # TODO: jam 20060706 Currently RangeFile does not support
285
        #       reading across ranges. Consider adding it.
286
        multi.seek(41)
287
        # self.assertEqual('zyxwvutsrqxxyyzz fbd', multi.read(20))
288
        self.assertRaises(errors.InvalidRange, multi.read, 20)
289
290
        multi.seek(21)
291
        self.assertRaises(errors.InvalidRange, multi.read, 11)
292
        multi.seek(31)
293
        self.assertRaises(errors.InvalidRange, multi.read, 10)
1786.1.20 by John Arbash Meinel
Added an exception check for HttpMulti
294
295
    def test_invalid(self):
296
        content = StringIO('')
297
        try:
298
            response.HttpMultipartRangeResponse('http://foo',
299
                        'multipart/byte;boundary=invalid', content)
300
        except errors.InvalidHttpContentType, e:
301
            self.assertContainsRe(str(e), 'http://foo')
302
            self.assertContainsRe(str(e), 'multipart/byte;')
1786.1.21 by John Arbash Meinel
(broken) Work on factoring out handle_response so we can test with fake headers.
303
304
305
# Taken from real request responses
1786.1.26 by John Arbash Meinel
Update and test handle_response.
306
_full_text_response = (200, """HTTP/1.1 200 OK\r
1786.1.21 by John Arbash Meinel
(broken) Work on factoring out handle_response so we can test with fake headers.
307
Date: Tue, 11 Jul 2006 04:32:56 GMT\r
308
Server: Apache/2.0.54 (Fedora)\r
309
Last-Modified: Sun, 23 Apr 2006 19:35:20 GMT\r
310
ETag: "56691-23-38e9ae00"\r
311
Accept-Ranges: bytes\r
312
Content-Length: 35\r
313
Connection: close\r
314
Content-Type: text/plain; charset=UTF-8\r
315
\r
1786.1.25 by John Arbash Meinel
Test that we can extract headers properly.
316
""", """Bazaar-NG meta directory, format 1
317
""")
318
319
1786.1.26 by John Arbash Meinel
Update and test handle_response.
320
_missing_response = (404, """HTTP/1.1 404 Not Found\r
1786.1.21 by John Arbash Meinel
(broken) Work on factoring out handle_response so we can test with fake headers.
321
Date: Tue, 11 Jul 2006 04:32:56 GMT\r
322
Server: Apache/2.0.54 (Fedora)\r
323
Content-Length: 336\r
324
Connection: close\r
325
Content-Type: text/html; charset=iso-8859-1\r
326
\r
1786.1.25 by John Arbash Meinel
Test that we can extract headers properly.
327
""", """<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
1786.1.21 by John Arbash Meinel
(broken) Work on factoring out handle_response so we can test with fake headers.
328
<html><head>
329
<title>404 Not Found</title>
330
</head><body>
331
<h1>Not Found</h1>
332
<p>The requested URL /branches/bzr/jam-integration/.bzr/repository/format was not found on this server.</p>
333
<hr>
334
<address>Apache/2.0.54 (Fedora) Server at bzr.arbash-meinel.com Port 80</address>
335
</body></html>
1786.1.25 by John Arbash Meinel
Test that we can extract headers properly.
336
""")
337
338
1786.1.26 by John Arbash Meinel
Update and test handle_response.
339
_single_range_response = (206, """HTTP/1.1 206 Partial Content\r
1786.1.21 by John Arbash Meinel
(broken) Work on factoring out handle_response so we can test with fake headers.
340
Date: Tue, 11 Jul 2006 04:45:22 GMT\r
341
Server: Apache/2.0.54 (Fedora)\r
342
Last-Modified: Thu, 06 Jul 2006 20:22:05 GMT\r
343
ETag: "238a3c-16ec2-805c5540"\r
344
Accept-Ranges: bytes\r
345
Content-Length: 100\r
1786.1.26 by John Arbash Meinel
Update and test handle_response.
346
Content-Range: bytes 100-199/93890\r
1786.1.21 by John Arbash Meinel
(broken) Work on factoring out handle_response so we can test with fake headers.
347
Connection: close\r
348
Content-Type: text/plain; charset=UTF-8\r
349
\r
1786.1.25 by John Arbash Meinel
Test that we can extract headers properly.
350
""", """mbp@sourcefrog.net-20050309040815-13242001617e4a06
1786.1.26 by John Arbash Meinel
Update and test handle_response.
351
mbp@sourcefrog.net-20050309040929-eee0eb3e6d1e762""")
352
353
2070.1.1 by John Arbash Meinel
Fix bug #62473 by not requiring content-type in range responses
354
_single_range_no_content_type = (206, """HTTP/1.1 206 Partial Content\r
355
Date: Tue, 11 Jul 2006 04:45:22 GMT\r
356
Server: Apache/2.0.54 (Fedora)\r
357
Last-Modified: Thu, 06 Jul 2006 20:22:05 GMT\r
358
ETag: "238a3c-16ec2-805c5540"\r
359
Accept-Ranges: bytes\r
360
Content-Length: 100\r
361
Content-Range: bytes 100-199/93890\r
362
Connection: close\r
363
\r
364
""", """mbp@sourcefrog.net-20050309040815-13242001617e4a06
365
mbp@sourcefrog.net-20050309040929-eee0eb3e6d1e762""")
366
367
1786.1.26 by John Arbash Meinel
Update and test handle_response.
368
_multipart_range_response = (206, """HTTP/1.1 206 Partial Content\r
1786.1.21 by John Arbash Meinel
(broken) Work on factoring out handle_response so we can test with fake headers.
369
Date: Tue, 11 Jul 2006 04:49:48 GMT\r
370
Server: Apache/2.0.54 (Fedora)\r
371
Last-Modified: Thu, 06 Jul 2006 20:22:05 GMT\r
372
ETag: "238a3c-16ec2-805c5540"\r
373
Accept-Ranges: bytes\r
374
Content-Length: 1534\r
375
Connection: close\r
376
Content-Type: multipart/byteranges; boundary=418470f848b63279b\r
377
\r
1786.1.25 by John Arbash Meinel
Test that we can extract headers properly.
378
\r""", """--418470f848b63279b\r
1786.1.21 by John Arbash Meinel
(broken) Work on factoring out handle_response so we can test with fake headers.
379
Content-type: text/plain; charset=UTF-8\r
380
Content-range: bytes 0-254/93890\r
381
\r
382
mbp@sourcefrog.net-20050309040815-13242001617e4a06
383
mbp@sourcefrog.net-20050309040929-eee0eb3e6d1e7627
384
mbp@sourcefrog.net-20050309040957-6cad07f466bb0bb8
385
mbp@sourcefrog.net-20050309041501-c840e09071de3b67
386
mbp@sourcefrog.net-20050309044615-c24a3250be83220a
387
\r
388
--418470f848b63279b\r
389
Content-type: text/plain; charset=UTF-8\r
390
Content-range: bytes 1000-2049/93890\r
391
\r
392
40-fd4ec249b6b139ab
393
mbp@sourcefrog.net-20050311063625-07858525021f270b
394
mbp@sourcefrog.net-20050311231934-aa3776aff5200bb9
395
mbp@sourcefrog.net-20050311231953-73aeb3a131c3699a
396
mbp@sourcefrog.net-20050311232353-f5e33da490872c6a
397
mbp@sourcefrog.net-20050312071639-0a8f59a34a024ff0
398
mbp@sourcefrog.net-20050312073432-b2c16a55e0d6e9fb
399
mbp@sourcefrog.net-20050312073831-a47c3335ece1920f
400
mbp@sourcefrog.net-20050312085412-13373aa129ccbad3
401
mbp@sourcefrog.net-20050313052251-2bf004cb96b39933
402
mbp@sourcefrog.net-20050313052856-3edd84094687cb11
403
mbp@sourcefrog.net-20050313053233-e30a4f28aef48f9d
404
mbp@sourcefrog.net-20050313053853-7c64085594ff3072
405
mbp@sourcefrog.net-20050313054757-a86c3f5871069e22
406
mbp@sourcefrog.net-20050313061422-418f1f73b94879b9
407
mbp@sourcefrog.net-20050313120651-497bd231b19df600
408
mbp@sourcefrog.net-20050314024931-eae0170ef25a5d1a
409
mbp@sourcefrog.net-20050314025438-d52099f915fe65fc
410
mbp@sourcefrog.net-20050314025539-637a636692c055cf
411
mbp@sourcefrog.net-20050314025737-55eb441f430ab4ba
412
mbp@sourcefrog.net-20050314025901-d74aa93bb7ee8f62
413
mbp@source\r
1979.1.1 by John Arbash Meinel
Fix bug #57723, parse boundary="" correctly, since Squid uses it
414
--418470f848b63279b--\r
415
""")
416
417
_multipart_squid_range_response = (206, """HTTP/1.0 206 Partial Content\r
418
Date: Thu, 31 Aug 2006 21:16:22 GMT\r
419
Server: Apache/2.2.2 (Unix) DAV/2\r
420
Last-Modified: Thu, 31 Aug 2006 17:57:06 GMT\r
421
Accept-Ranges: bytes\r
422
Content-Type: multipart/byteranges; boundary="squid/2.5.STABLE12:C99323425AD4FE26F726261FA6C24196"\r
423
Content-Length: 598\r
424
X-Cache: MISS from localhost.localdomain\r
425
X-Cache-Lookup: HIT from localhost.localdomain:3128\r
426
Proxy-Connection: keep-alive\r
427
\r
428
""",
429
"""\r
430
--squid/2.5.STABLE12:C99323425AD4FE26F726261FA6C24196\r
431
Content-Type: text/plain\r
432
Content-Range: bytes 0-99/18672\r
433
\r
434
# bzr knit index 8
435
436
scott@netsplit.com-20050708230047-47c7868f276b939f fulltext 0 863  :
437
scott@netsp\r
438
--squid/2.5.STABLE12:C99323425AD4FE26F726261FA6C24196\r
439
Content-Type: text/plain\r
440
Content-Range: bytes 300-499/18672\r
441
\r
442
com-20050708231537-2b124b835395399a :
443
scott@netsplit.com-20050820234126-551311dbb7435b51 line-delta 1803 479 .scott@netsplit.com-20050820232911-dc4322a084eadf7e :
444
scott@netsplit.com-20050821213706-c86\r
445
--squid/2.5.STABLE12:C99323425AD4FE26F726261FA6C24196--\r
1786.1.25 by John Arbash Meinel
Test that we can extract headers properly.
446
""")
447
448
1786.1.41 by John Arbash Meinel
parse redirect headers properly.
449
_redirect_response = (206, """HTTP/1.1 301 Moved Permanently\r
450
Date: Tue, 18 Jul 2006 20:29:22 GMT\r
451
Server: Apache/2.0.54 (Ubuntu) PHP/4.4.0-3ubuntu1 mod_ssl/2.0.54 OpenSSL/0.9.7g\r
452
Location: http://bazaar-vcs.org/bzr/bzr.dev/.bzr/repository/inventory.knit\r
453
Content-Length: 272\r
454
Keep-Alive: timeout=15, max=100\r
455
Connection: Keep-Alive\r
456
Content-Type: text/html; charset=iso-8859-1\r
457
\r
458
HTTP/1.1 206 Partial Content\r
459
Date: Tue, 18 Jul 2006 20:29:23 GMT\r
460
Server: Apache/2.0.54 (Ubuntu) PHP/4.4.0-3ubuntu1 mod_ssl/2.0.54 OpenSSL/0.9.7g\r
461
Last-Modified: Tue, 18 Jul 2006 20:24:59 GMT\r
462
ETag: "be8213-83958c-f0d3dcc0"\r
463
Accept-Ranges: bytes\r
464
Content-Length: 425\r
465
Content-Range: bytes 8623075-8623499/8623500\r
466
Keep-Alive: timeout=15, max=100\r
467
Connection: Keep-Alive\r
468
Content-Type: text/plain; charset=UTF-8\r
469
\r
1786.1.42 by John Arbash Meinel
Update _extract_headers, make it less generic, and non recursive.
470
""", """this data intentionally removed, 
471
this is not meant to be tested by
472
handle_response, just _extract_headers
1786.1.41 by John Arbash Meinel
parse redirect headers properly.
473
""")
474
475
1786.1.26 by John Arbash Meinel
Update and test handle_response.
476
# This is made up
477
_invalid_response = (444, """HTTP/1.1 444 Bad Response\r
478
Date: Tue, 11 Jul 2006 04:32:56 GMT\r
479
Connection: close\r
480
Content-Type: text/html; charset=iso-8859-1\r
481
\r
482
""", """<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
483
<html><head>
484
<title>404 Not Found</title>
485
</head><body>
486
<h1>Not Found</h1>
487
<p>I don't know what I'm doing</p>
488
<hr>
489
</body></html>
490
""")
491
492
1786.1.25 by John Arbash Meinel
Test that we can extract headers properly.
493
# This should be in test_http.py, but the headers we
494
# want to parse are here
495
class TestExtractHeader(TestCase):
496
    
1786.1.42 by John Arbash Meinel
Update _extract_headers, make it less generic, and non recursive.
497
    def use_response(self, response):
498
        self.headers = http._extract_headers(response[1], 'http://foo')
1786.1.25 by John Arbash Meinel
Test that we can extract headers properly.
499
500
    def check_header(self, header, value):
501
        self.assertEqual(value, self.headers[header])
502
        
503
    def test_full_text(self):
504
        self.use_response(_full_text_response)
505
506
        self.check_header('Date', 'Tue, 11 Jul 2006 04:32:56 GMT')
507
        self.check_header('date', 'Tue, 11 Jul 2006 04:32:56 GMT')
508
        self.check_header('Content-Length', '35')
509
        self.check_header('Content-Type', 'text/plain; charset=UTF-8')
510
        self.check_header('content-type', 'text/plain; charset=UTF-8')
511
512
    def test_missing_response(self):
513
        self.use_response(_missing_response)
514
515
        self.check_header('Content-Length', '336')
516
        self.check_header('Content-Type', 'text/html; charset=iso-8859-1')
517
518
    def test_single_range(self):
519
        self.use_response(_single_range_response)
520
521
        self.check_header('Content-Length', '100')
1786.1.26 by John Arbash Meinel
Update and test handle_response.
522
        self.check_header('Content-Range', 'bytes 100-199/93890')
1786.1.25 by John Arbash Meinel
Test that we can extract headers properly.
523
        self.check_header('Content-Type', 'text/plain; charset=UTF-8')
524
2070.1.1 by John Arbash Meinel
Fix bug #62473 by not requiring content-type in range responses
525
    def test_single_range_no_content(self):
526
        self.use_response(_single_range_no_content_type)
527
528
        self.check_header('Content-Length', '100')
529
        self.check_header('Content-Range', 'bytes 100-199/93890')
530
1786.1.25 by John Arbash Meinel
Test that we can extract headers properly.
531
    def test_multi_range(self):
532
        self.use_response(_multipart_range_response)
533
534
        self.check_header('Content-Length', '1534')
535
        self.check_header('Content-Type',
536
                          'multipart/byteranges; boundary=418470f848b63279b')
1786.1.26 by John Arbash Meinel
Update and test handle_response.
537
1979.1.1 by John Arbash Meinel
Fix bug #57723, parse boundary="" correctly, since Squid uses it
538
    def test_multi_squid_range(self):
539
        self.use_response(_multipart_squid_range_response)
540
541
        self.check_header('Content-Length', '598')
542
        self.check_header('Content-Type',
543
                          'multipart/byteranges; '\
544
                          'boundary="squid/2.5.STABLE12:C99323425AD4FE26F726261FA6C24196"')
545
1786.1.41 by John Arbash Meinel
parse redirect headers properly.
546
    def test_redirect(self):
547
        """We default to returning the last group of headers in the file."""
548
        self.use_response(_redirect_response)
549
        self.check_header('Content-Range', 'bytes 8623075-8623499/8623500')
550
        self.check_header('Content-Type', 'text/plain; charset=UTF-8')
551
1786.1.42 by John Arbash Meinel
Update _extract_headers, make it less generic, and non recursive.
552
    def test_empty(self):
553
        self.assertRaises(errors.InvalidHttpResponse,
554
            http._extract_headers, '', 'bad url')
555
556
    def test_no_opening_http(self):
557
        # Remove the HTTP line from the header
558
        first, txt = _full_text_response[1].split('\r\n', 1)
559
        self.assertRaises(errors.InvalidHttpResponse,
560
            http._extract_headers, txt, 'missing HTTTP')
561
562
    def test_trailing_whitespace(self):
563
        # Test that we ignore bogus whitespace on the end
564
        code, txt, body = _full_text_response
565
        txt += '\r\n\n\n\n\n'
566
        self.use_response((code, txt, body))
567
568
        self.check_header('Date', 'Tue, 11 Jul 2006 04:32:56 GMT')
569
        self.check_header('Content-Length', '35')
570
        self.check_header('Content-Type', 'text/plain; charset=UTF-8')
571
572
    def test_trailing_non_http(self):
573
        # Test that we ignore bogus stuff on the end
574
        code, txt, body = _full_text_response
575
        txt = txt + 'Foo: Bar\r\nBaz: Bling\r\n\r\n'
576
        self.use_response((code, txt, body))
577
578
        self.check_header('Date', 'Tue, 11 Jul 2006 04:32:56 GMT')
579
        self.check_header('Content-Length', '35')
580
        self.check_header('Content-Type', 'text/plain; charset=UTF-8')
581
        self.assertRaises(KeyError, self.headers.__getitem__, 'Foo')
582
583
    def test_extra_whitespace(self):
584
        # Test that we read an HTTP response, even with extra whitespace
585
        code, txt, body = _redirect_response
586
        # Find the second HTTP location
587
        loc = txt.find('HTTP', 5)
588
        txt = txt[:loc] + '\r\n\n' + txt[loc:]
589
        self.use_response((code, txt, body))
590
        self.check_header('Content-Range', 'bytes 8623075-8623499/8623500')
591
        self.check_header('Content-Type', 'text/plain; charset=UTF-8')
592
1786.1.26 by John Arbash Meinel
Update and test handle_response.
593
594
class TestHandleResponse(TestCase):
595
    
596
    def get_response(self, a_response):
597
        """Process a supplied response, and return the result."""
1786.1.42 by John Arbash Meinel
Update _extract_headers, make it less generic, and non recursive.
598
        headers = http._extract_headers(a_response[1], 'http://foo')
1786.1.26 by John Arbash Meinel
Update and test handle_response.
599
        return response.handle_response('http://foo', a_response[0], headers,
600
                                        StringIO(a_response[2]))
601
602
    def test_full_text(self):
603
        out = self.get_response(_full_text_response)
604
        # It is a StringIO from the original data
605
        self.assertEqual(_full_text_response[2], out.read())
606
607
    def test_missing_response(self):
608
        self.assertRaises(errors.NoSuchFile,
609
            self.get_response, _missing_response)
610
611
    def test_single_range(self):
612
        out = self.get_response(_single_range_response)
613
        self.assertIsInstance(out, response.HttpRangeResponse)
614
615
        self.assertRaises(errors.InvalidRange, out.read, 20)
616
617
        out.seek(100)
618
        self.assertEqual(_single_range_response[2], out.read(100))
619
2070.1.1 by John Arbash Meinel
Fix bug #62473 by not requiring content-type in range responses
620
    def test_single_range_no_content(self):
621
        out = self.get_response(_single_range_no_content_type)
622
        self.assertIsInstance(out, response.HttpRangeResponse)
623
624
        self.assertRaises(errors.InvalidRange, out.read, 20)
625
626
        out.seek(100)
627
        self.assertEqual(_single_range_no_content_type[2], out.read(100))
628
1786.1.26 by John Arbash Meinel
Update and test handle_response.
629
    def test_multi_range(self):
630
        out = self.get_response(_multipart_range_response)
631
        self.assertIsInstance(out, response.HttpMultipartRangeResponse)
632
633
        # Just make sure we can read the right contents
634
        out.seek(0)
635
        out.read(255)
636
637
        out.seek(1000)
638
        out.read(1050)
639
1979.1.1 by John Arbash Meinel
Fix bug #57723, parse boundary="" correctly, since Squid uses it
640
    def test_multi_squid_range(self):
641
        out = self.get_response(_multipart_squid_range_response)
642
        self.assertIsInstance(out, response.HttpMultipartRangeResponse)
643
644
        # Just make sure we can read the right contents
645
        out.seek(0)
646
        out.read(100)
647
648
        out.seek(300)
649
        out.read(200)
650
1786.1.26 by John Arbash Meinel
Update and test handle_response.
651
    def test_invalid_response(self):
652
        self.assertRaises(errors.InvalidHttpResponse,
653
            self.get_response, _invalid_response)
654
655
    def test_full_text_no_content_type(self):
656
        # We should not require Content-Type for a full response
657
        a_response = _full_text_response
1786.1.42 by John Arbash Meinel
Update _extract_headers, make it less generic, and non recursive.
658
        headers = http._extract_headers(a_response[1], 'http://foo')
1786.1.26 by John Arbash Meinel
Update and test handle_response.
659
        del headers['Content-Type']
660
        out = response.handle_response('http://foo', a_response[0], headers,
661
                                        StringIO(a_response[2]))
662
        self.assertEqual(_full_text_response[2], out.read())
663
664
    def test_missing_no_content_type(self):
665
        # Without Content-Type we should still raise NoSuchFile on a 404
666
        a_response = _missing_response
1786.1.42 by John Arbash Meinel
Update _extract_headers, make it less generic, and non recursive.
667
        headers = http._extract_headers(a_response[1], 'http://missing')
1786.1.26 by John Arbash Meinel
Update and test handle_response.
668
        del headers['Content-Type']
669
        self.assertRaises(errors.NoSuchFile,
670
            response.handle_response, 'http://missing', a_response[0], headers,
671
                                      StringIO(a_response[2]))
672
673
    def test_missing_content_range(self):
674
        a_response = _single_range_response
1786.1.42 by John Arbash Meinel
Update _extract_headers, make it less generic, and non recursive.
675
        headers = http._extract_headers(a_response[1], 'http://nocontent')
1786.1.26 by John Arbash Meinel
Update and test handle_response.
676
        del headers['Content-Range']
677
        self.assertRaises(errors.InvalidHttpResponse,
678
            response.handle_response, 'http://nocontent', a_response[0],
679
                                      headers, StringIO(a_response[2]))