style cleanup
Before adoption: this commit came in with the history the project adopted, so no landing records it.
break out header parts in test test for no filename* parameter for ascii header
authored by David Lord · committed by David Lord · parent d50a5db · Browse files · 2 files·+28 added−16 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+28 added−16 removed
- tests:+16 added−9 removed
- other:+12 added−7 removed
Provenance
- Commit
c197301- Tree
5b8fd40- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
d50a5db
2 files changed+28 added−16 removed
modified:flask/helpers.py+12 added-7 removed
12 unmodified lines13 import sys14 import pkgutil15 import posixpath16 import mimetypes17-from unicodedata import normalize17 from time import time18 from zlib import adler3219 from threading import RLock20+import unicodedata21 from werkzeug.routing import BuildError22 from functools import update_wrapper23 24 try:452 unmodified lines477 478 .. versionchanged:: 0.12479 The `attachment_filename` is preferred over `filename` for MIME-type480 detection.481+ 482+ .. versionchanged:: 0.13483+ UTF-8 filenames, as specified in `RFC 2231`_, are supported.484+ 485+ .. _RFC 2231: https://tools.ietf.org/html/rfc2231#section-4486 487 :param filename_or_fp: the filename of the file to send in `latin-1`.488 This is relative to the :attr:`~Flask.root_path`489 if a relative path is specified.49 unmodified lines539 if as_attachment:540 if attachment_filename is None:541 raise TypeError('filename unavailable, required for '542 'sending as attachment')538- normalized = normalize('NFKD', text_type(attachment_filename))543 544+ normalized = unicodedata.normalize(545+ 'NFKD', text_type(attachment_filename)546+ )547+548 try:549 normalized.encode('ascii')550 except UnicodeEncodeError:551 filenames = {552 'filename': normalized.encode('ascii', 'ignore'),553 'filename*': "UTF-8''%s" % url_quote(attachment_filename),554 }555 else:548- filenames = {549- 'filename': attachment_filename,550- }556+ filenames = {'filename': attachment_filename}557 552- headers.add('Content-Disposition', 'attachment',553- **filenames)558+ headers.add('Content-Disposition', 'attachment', **filenames)559 560 if current_app.use_x_sendfile and filename:561 if file is not None:562 file.close()418 unmodified linesmodified:tests/test_helpers.py+16 added-9 removed
538 unmodified lines539 attachment_filename='index.html')540 value, options = \541 parse_options_header(rv.headers['Content-Disposition'])542 assert value == 'attachment'543+ assert options['filename'] == 'index.html'544+ assert 'filename*' not in options545 rv.close()546 547 with app.test_request_context():546- assert options['filename'] == 'index.html'548 rv = flask.send_file('static/index.html', as_attachment=True)549 value, options = parse_options_header(rv.headers['Content-Disposition'])550 assert value == 'attachment'551 assert options['filename'] == 'index.html'10 unmodified lines562 rv.close()563 564 def test_attachment_with_utf8_filename(self):565 app = flask.Flask(__name__)566+567 with app.test_request_context():566- with open(os.path.join(app.root_path, 'static/index.html')) as f:567- rv = flask.send_file(f, as_attachment=True,568- attachment_filename=u'Ñandú/pingüino.txt')569- content_disposition = set(rv.headers['Content-Disposition'].split(';'))570- assert content_disposition == set(['attachment',571- ' filename="Nandu/pinguino.txt"',572- " filename*=UTF-8''%C3%91and%C3%BA%EF%BC%8Fping%C3%BCino.txt"])573- rv.close()568+ rv = flask.send_file(569+ 'static/index.html', as_attachment=True,570+ attachment_filename=u'Ñandú/pingüino.txt'571+ )572+ value, options = parse_options_header(573+ rv.headers['Content-Disposition']574+ )575+ rv.close()576 577+ assert value == 'attachment'578+ assert sorted(options.keys()) == ('filename', 'filename*')579+ assert options['filename'] == 'Nandu/pinguino.txt'580+ assert options['filename*'] == 'UTF-8''%C3%91and%C3%BA%EF%BC%8Fping%C3%BCino.txt'581+582 def test_static_file(self):583 app = flask.Flask(__name__)584 # default cache timeout is 12 hours585 with app.test_request_context():323 unmodified lines