Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #32: pip crashes when server does not send content-type header #872

Merged
merged 2 commits into from Mar 30, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion pip/download.py
Expand Up @@ -533,7 +533,7 @@ def unpack_http_url(link, location, download_cache, download_dir=None):
logger.notify('File was already downloaded %s' % already_downloaded)
else:
resp = _get_response_from_url(target_url, link)
content_type = resp.info()['content-type']
content_type = resp.info().get('content-type', '')
filename = link.filename # fallback
# Have a look at the Content-Disposition header for a better guess
content_disposition = resp.info().get('content-disposition')
Expand Down
28 changes: 27 additions & 1 deletion tests/test_download.py
@@ -1,5 +1,12 @@
import os
import textwrap
from tests.test_pip import reset_env, run_pip, write_file
from pip.download import _get_response_from_url as _get_response_from_url_original
from mock import patch
from shutil import rmtree
from tempfile import mkdtemp
from pip.download import path_to_url, unpack_http_url
from pip.index import Link
from tests.test_pip import reset_env, run_pip, write_file, here
from tests.path import Path


Expand Down Expand Up @@ -68,3 +75,22 @@ def test_download_should_skip_existing_files():
assert Path('scratch')/ 'INITools-0.1.tar.gz' not in result.files_created
assert env.site_packages/ 'initools' not in result.files_created
assert env.site_packages/ 'openid' not in result.files_created

def test_unpack_http_url_with_urllib_response_without_content_type():
"""
It should download and unpack files even if no Content-Type header exists
"""
def _get_response_from_url_mock(*args, **kw):
resp = _get_response_from_url_original(*args, **kw)
del resp.info()['content-type']
return resp

with patch('pip.download._get_response_from_url', _get_response_from_url_mock) as mocked:
uri = path_to_url(os.path.join(here, 'packages', 'simple-1.0.tar.gz'))
link = Link(uri)
temp_dir = mkdtemp()
try:
unpack_http_url(link, temp_dir, download_cache=None, download_dir=None)
assert set(os.listdir(temp_dir)) == set(['PKG-INFO', 'setup.cfg', 'setup.py', 'simple', 'simple.egg-info'])
finally:
rmtree(temp_dir)