Detect Content Type of file uploaded in multipart/form-data request

Closes #271 #285 #398

This adds filename-based detection. It's still not possible to specify the
content type manually, though.
This commit is contained in:
Jakub Roztocil 2016-02-28 15:45:45 +08:00
parent 59e22b16b8
commit 56f498c153
4 changed files with 12 additions and 3 deletions

1
.gitignore vendored
View File

@ -9,3 +9,4 @@ README.html
htmlcov
.idea
.DS_Store
.cache/

View File

@ -8,7 +8,8 @@ This project adheres to `Semantic Versioning <http://semver.org/>`_.
`1.0.0-dev`_ (Unreleased)
-------------------------
* Changed the default color style back to ``solarized`` as it supports
* Added ``Content-Type`` of files uploaded in ``multipart/form-data`` requests.
* Changed the default color style back to``solarized`` as it supports
both the light and dark terminal background mode.

View File

@ -605,9 +605,13 @@ class DataDict(RequestItemsDict):
RequestItems = namedtuple('RequestItems',
['headers', 'data', 'files', 'params'])
def get_content_type(filename):
"""Get content type for a filename in format appropriate for Content-Type
headers.
"""
Return the content type for ``filename`` in format appropriate
for Content-Type headers, or ``None`` if the file type is unknown
to ``mimetypes``.
"""
mime, encoding = mimetypes.guess_type(filename, strict=False)
if mime:
@ -616,6 +620,7 @@ def get_content_type(filename):
content_type = '%s; charset=%s' % (mime, encoding)
return content_type
def parse_items(items,
headers_class=CaseInsensitiveDict,
data_class=OrderedDict,

View File

@ -23,6 +23,7 @@ class TestMultipartFormDataFileUpload:
' filename="%s"' % os.path.basename(FILE_PATH) in r
assert FILE_CONTENT in r
assert '"foo": "bar"' in r
assert 'Content-Type: text/plain' in r
def test_upload_multiple_fields_with_the_same_name(self, httpbin):
r = http('--form', '--verbose', 'POST', httpbin.url + '/post',
@ -34,6 +35,7 @@ class TestMultipartFormDataFileUpload:
# Should be 4, but is 3 because httpbin
# doesn't seem to support filed field lists
assert r.count(FILE_CONTENT) in [3, 4]
assert r.count('Content-Type: text/plain') == 2
class TestRequestBodyFromFilePath: