Skip to content

Commit

Permalink
Merge branch 'master' into access_log_factory
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetlov authored Oct 13, 2017
2 parents fc75be3 + 4ffdb3a commit a9246ad
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 4 deletions.
8 changes: 8 additions & 0 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ line of the output:
Please go to the link and make sure that your code change is covered.


The project uses *codecov.io* for storing coverage results. Visit
https://codecov.io/gh/aio-libs/aiohttp for looking on coverage of
master branch, history, pull requests etc.

The browser extension https://docs.codecov.io/docs/browser-extension
is highly recommended for analyzing the coverage just in *Files
Changed* tab on *GitHub Pull Request* review page.

Documentation
-------------

Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Daniel García
Daniel Nelson
Danny Song
David Michael Brown
Denilson Amorim
Denis Matiychuk
Dima Veselov
Dimitar Dimitrov
Expand Down
14 changes: 10 additions & 4 deletions aiohttp/web_fileresponse.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,14 @@ def prepare(self, request):
self._length_check = False
return (yield from super().prepare(request))

ct, encoding = mimetypes.guess_type(str(filepath))
if not ct:
ct = 'application/octet-stream'
if hdrs.CONTENT_TYPE not in self.headers:
ct, encoding = mimetypes.guess_type(str(filepath))
if not ct:
ct = 'application/octet-stream'
should_set_ct = True
else:
encoding = 'gzip' if gzip else None
should_set_ct = False

status = HTTPOk.status_code
file_size = st.st_size
Expand Down Expand Up @@ -224,7 +229,8 @@ def prepare(self, request):
status = HTTPPartialContent.status_code

self.set_status(status)
self.content_type = ct
if should_set_ct:
self.content_type = ct
if encoding:
self.headers[hdrs.CONTENT_ENCODING] = encoding
if gzip:
Expand Down
1 change: 1 addition & 0 deletions changes/2317.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix web.FileResponse overriding user supplied Content-Type
48 changes: 48 additions & 0 deletions tests/test_web_sendfile_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,54 @@ def handler(request):
resp.close()


@asyncio.coroutine
def test_static_file_custom_content_type(loop, test_client, sender):
filepath = (pathlib.Path(__file__).parent / 'hello.txt.gz')

@asyncio.coroutine
def handler(request):
resp = sender(filepath, chunk_size=16)
resp.content_type = 'application/pdf'
return resp

app = web.Application()
app.router.add_get('/', handler)
client = yield from test_client(lambda loop: app)

resp = yield from client.get('/')
assert resp.status == 200
body = yield from resp.read()
with filepath.open('rb') as f:
content = f.read()
assert content == body
assert resp.headers['Content-Type'] == 'application/pdf'
assert resp.headers.get('Content-Encoding') is None
resp.close()


@asyncio.coroutine
def test_static_file_custom_content_type_compress(loop, test_client, sender):
filepath = (pathlib.Path(__file__).parent / 'hello.txt')

@asyncio.coroutine
def handler(request):
resp = sender(filepath, chunk_size=16)
resp.content_type = 'application/pdf'
return resp

app = web.Application()
app.router.add_get('/', handler)
client = yield from test_client(lambda loop: app)

resp = yield from client.get('/')
assert resp.status == 200
body = yield from resp.read()
assert b'hello aiohttp\n' == body
assert resp.headers['Content-Type'] == 'application/pdf'
assert resp.headers.get('Content-Encoding') == 'gzip'
resp.close()


@asyncio.coroutine
def test_static_file_with_content_encoding(loop, test_client, sender):
filepath = pathlib.Path(__file__).parent / 'hello.txt.gz'
Expand Down

0 comments on commit a9246ad

Please sign in to comment.