Skip to content

Commit

Permalink
Fix #143: off-by-one range in iterator.KeyIterator.get_headers.
Browse files Browse the repository at this point in the history
  • Loading branch information
tseaver committed Sep 26, 2014
1 parent 1823b0f commit be6e9b7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion gcloud/storage/iterator.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def has_more_data(self):

def get_headers(self):
start = self._bytes_written
end = self._bytes_written + self.key.CHUNK_SIZE
end = self._bytes_written + self.key.CHUNK_SIZE - 1

if self._total_bytes and end > self._total_bytes:
end = ''
Expand Down
34 changes: 17 additions & 17 deletions gcloud/storage/test_iterator.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,29 +206,29 @@ def test_ctor(self):

def test__iter__(self):
response1 = _Response(status=200)
response1['content-range'] = '0-10/15'
response1['content-range'] = '0-9/15'
response2 = _Response(status=200)
response2['content-range'] = '11-14/15'
connection = _Connection((response1, '01234567890'),
(response2, '1234'),
response2['content-range'] = '10-14/15'
connection = _Connection((response1, '0123456789'),
(response2, '01234'),
)
key = _Key(connection)
iterator = self._makeOne(key)
chunks = list(iterator)
self.assertEqual(len(chunks), 2)
self.assertEqual(chunks[0], '01234567890')
self.assertEqual(chunks[1], '1234')
self.assertEqual(chunks[0], '0123456789')
self.assertEqual(chunks[1], '01234')
self.assertEqual(iterator._bytes_written, 15)
self.assertEqual(iterator._total_bytes, 15)
kws = connection._requested
self.assertEqual(kws[0]['method'], 'GET')
self.assertEqual(kws[0]['url'],
self.assertEqual(kws[0]['url'],
'http://example.com/b/name/o/key?alt=media')
self.assertEqual(kws[0]['headers'], {'Range': 'bytes=0-10'})
self.assertEqual(kws[0]['headers'], {'Range': 'bytes=0-9'})
self.assertEqual(kws[1]['method'], 'GET')
self.assertEqual(kws[1]['url'],
self.assertEqual(kws[1]['url'],
'http://example.com/b/name/o/key?alt=media')
self.assertEqual(kws[1]['headers'], {'Range': 'bytes=11-'})
self.assertEqual(kws[1]['headers'], {'Range': 'bytes=10-'})

def test_reset(self):
connection = _Connection()
Expand Down Expand Up @@ -275,7 +275,7 @@ def test_get_headers_new(self):
iterator = self._makeOne(key)
headers = iterator.get_headers()
self.assertEqual(len(headers), 1)
self.assertEqual(headers['Range'], 'bytes=0-10')
self.assertEqual(headers['Range'], 'bytes=0-9')

def test_get_headers_ok(self):
connection = _Connection()
Expand All @@ -285,7 +285,7 @@ def test_get_headers_ok(self):
iterator._total_bytes = 1000
headers = iterator.get_headers()
self.assertEqual(len(headers), 1)
self.assertEqual(headers['Range'], 'bytes=10-20')
self.assertEqual(headers['Range'], 'bytes=10-19')

def test_get_headers_off_end(self):
connection = _Connection()
Expand Down Expand Up @@ -313,7 +313,7 @@ def test_get_next_chunk_underflow(self):

def test_get_next_chunk_200(self):
response = _Response(status=200)
response['content-range'] = '0-10/100'
response['content-range'] = '0-9/100'
connection = _Connection((response, 'CHUNK'))
key = _Key(connection)
iterator = self._makeOne(key)
Expand All @@ -323,9 +323,9 @@ def test_get_next_chunk_200(self):
self.assertEqual(iterator._total_bytes, 100)
kw, = connection._requested
self.assertEqual(kw['method'], 'GET')
self.assertEqual(kw['url'],
self.assertEqual(kw['url'],
'http://example.com/b/name/o/key?alt=media')
self.assertEqual(kw['headers'], {'Range': 'bytes=0-10'})
self.assertEqual(kw['headers'], {'Range': 'bytes=0-9'})

def test_get_next_chunk_206(self):
response = _Response(status=206)
Expand All @@ -338,9 +338,9 @@ def test_get_next_chunk_206(self):
self.assertEqual(iterator._bytes_written, len(chunk))
kw, = connection._requested
self.assertEqual(kw['method'], 'GET')
self.assertEqual(kw['url'],
self.assertEqual(kw['url'],
'http://example.com/b/name/o/key?alt=media')
self.assertEqual(kw['headers'], {'Range': 'bytes=0-10'})
self.assertEqual(kw['headers'], {'Range': 'bytes=0-9'})

def test_get_next_chunk_416(self):
response = _Response(status=416)
Expand Down

0 comments on commit be6e9b7

Please sign in to comment.