Skip to content

Commit

Permalink
Cope with 'Content-Length' value being a string (try-except)
Browse files Browse the repository at this point in the history
Side benefits of this PR:
* makes tests compatible with python 2.7.0-2.7.8 (pin httpretty no higher than 0.9.4)
* makes tests compatible with httpretty 0.9.3 (as ckan uses, so this is handy for a developer using the same venv) ('content-length': None)
  • Loading branch information
David Read committed Oct 26, 2018
1 parent 1538e49 commit 4dda1fa
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
11 changes: 7 additions & 4 deletions datapusher/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,10 +360,13 @@ def push_to_datastore(task_id, input, dry_run=False):
response.raise_for_status()

cl = response.headers.get('content-length')
if cl and int(cl) > MAX_CONTENT_LENGTH:
raise util.JobError(
'Resource too large to download: {cl} > max ({max_cl}).'
.format(cl=cl, max_cl=MAX_CONTENT_LENGTH))
try:
if cl and int(cl) > MAX_CONTENT_LENGTH:
raise util.JobError(
'Resource too large to download: {cl} > max ({max_cl}).'
.format(cl=cl, max_cl=MAX_CONTENT_LENGTH))
except ValueError:
pass

tmp = tempfile.TemporaryFile()
length = 0
Expand Down
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
-r requirements.txt
httpretty>=0.6
httpretty==0.9.4
nose==1.2.1
29 changes: 27 additions & 2 deletions tests/test_acceptance.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def register_urls(self, filename='simple.csv', format='CSV',
@httpretty.activate
@raises(util.JobError)
def test_too_large_content_length(self):
"""It should raise JobError if the returned Content-Length header
"""It should raise JobError if the returned Content-Length header
is too large.
If the returned header is larger than MAX_CONTENT_LENGTH then the async
Expand Down Expand Up @@ -169,7 +169,32 @@ def test_too_large_file(self):
body='a' * size,
content_type='application/json',
forcing_headers={
'content-length': ''
'content-length': None
})

jobs.push_to_datastore('fake_id', data, True)

@httpretty.activate
def test_content_length_string(self):
"""If the Content-Length header value is a string, just ignore it.
"""
self.register_urls()
data = {
'api_key': self.api_key,
'job_type': 'push_to_datastore',
'metadata': {
'ckan_url': 'http://%s/' % self.host,
'resource_id': self.resource_id
}
}

source_url = 'http://www.source.org/static/file'
httpretty.register_uri(
httpretty.GET, source_url,
body='aaaaa',
content_type='application/json',
forcing_headers={
'Content-Length': 'some string'
})

jobs.push_to_datastore('fake_id', data, True)
Expand Down

0 comments on commit 4dda1fa

Please sign in to comment.