Skip to content

Commit

Permalink
Add test for _encode_multipart which is not public API
Browse files Browse the repository at this point in the history
  • Loading branch information
digitalresistor committed Jan 22, 2020
1 parent 4f9b2f0 commit 1e408d5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/webob/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -1570,7 +1570,7 @@ def environ_add_POST(env, data, content_type=None):
data = list(data.items())

for _, v in data:
if isinstance(v, (tuple, list)):
if isinstance(v, (tuple, list)) or hasattr(v, 'filename'):
has_files = True

break
Expand Down
28 changes: 28 additions & 0 deletions tests/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -3024,6 +3024,31 @@ def test_fileupload_mime_type_detection(self):
assert "application/x-foo" in request.body.decode("ascii", str(request))


def test_environ_add_POST_file_with_content_type():
# For benefit of _encode_multipart which did not have adequate coverage for
# type_options
class FakeFile:
def __init__(self, filename, content_type, type_options, value):
self.filename = filename
self.type = content_type
self.type_options = type_options or {}
self.value = value

from webob.request import environ_from_url, environ_add_POST

env = environ_from_url("http://example.com/")
environ_add_POST(
env,
{
"sample_file":
FakeFile("test.txt", "text/plain", {"charset": "utf-8"}, "this is data"),
},
)
wsgi_input = env["wsgi.input"].read()
assert b"this is data" in wsgi_input
assert b'Content-type: text/plain; charset="utf-8"' in wsgi_input


class TestRequestMultipart(object):
def test_multipart_with_charset(self):
from webob.request import Request
Expand All @@ -3045,6 +3070,7 @@ def simpleapp(environ, start_response):

return [
bytes_(x)

for x in [
"Hello world!\n",
"The get is %r" % request.GET,
Expand All @@ -3053,6 +3079,7 @@ def simpleapp(environ, start_response):
% (
[
o

for o, _ in sorted(
request.accept_language.parsed or (),
key=lambda x: x[1], # sort by quality
Expand All @@ -3064,6 +3091,7 @@ def simpleapp(environ, start_response):
% ",".join(
[
o

for o, _ in request.accept.acceptable_offers(
["application/xml", "text/html"]
)
Expand Down

0 comments on commit 1e408d5

Please sign in to comment.