2014-04-24 15:07:31 +03:00
|
|
|
import os
|
2020-08-15 17:50:00 +02:00
|
|
|
from unittest import mock
|
2014-04-24 15:07:31 +03:00
|
|
|
|
2014-04-24 18:08:40 +03:00
|
|
|
import pytest
|
|
|
|
|
2019-08-31 15:17:10 +02:00
|
|
|
from httpie.cli.exceptions import ParseError
|
2020-08-19 10:22:42 +02:00
|
|
|
from httpie.client import FORM_CONTENT_TYPE
|
2020-08-15 17:50:00 +02:00
|
|
|
from httpie.output.streams import LARGE_UPLOAD_SUPPRESSED_NOTICE
|
2020-01-23 16:54:43 +02:00
|
|
|
from httpie.status import ExitStatus
|
2017-12-28 19:17:48 +02:00
|
|
|
from utils import MockEnvironment, http, HTTP_OK
|
2014-04-28 12:29:41 +03:00
|
|
|
from fixtures import FILE_PATH_ARG, FILE_PATH, FILE_CONTENT
|
2014-04-24 15:07:31 +03:00
|
|
|
|
|
|
|
|
2014-04-25 12:39:59 +03:00
|
|
|
class TestMultipartFormDataFileUpload:
|
2014-06-28 17:35:57 +03:00
|
|
|
|
|
|
|
def test_non_existent_file_raises_parse_error(self, httpbin):
|
2014-04-24 18:08:40 +03:00
|
|
|
with pytest.raises(ParseError):
|
2014-06-28 17:35:57 +03:00
|
|
|
http('--form',
|
|
|
|
'POST', httpbin.url + '/post', 'foo@/__does_not_exist__')
|
2014-04-24 15:07:31 +03:00
|
|
|
|
2014-06-28 17:35:57 +03:00
|
|
|
def test_upload_ok(self, httpbin):
|
|
|
|
r = http('--form', '--verbose', 'POST', httpbin.url + '/post',
|
2020-06-08 17:59:41 +02:00
|
|
|
f'test-file@{FILE_PATH_ARG}', 'foo=bar')
|
2014-04-24 15:58:15 +03:00
|
|
|
assert HTTP_OK in r
|
|
|
|
assert 'Content-Disposition: form-data; name="foo"' in r
|
|
|
|
assert 'Content-Disposition: form-data; name="test-file";' \
|
2020-06-08 17:59:41 +02:00
|
|
|
f' filename="{os.path.basename(FILE_PATH)}"' in r
|
2014-06-28 17:35:57 +03:00
|
|
|
assert FILE_CONTENT in r
|
2014-04-24 15:58:15 +03:00
|
|
|
assert '"foo": "bar"' in r
|
2016-02-28 09:45:45 +02:00
|
|
|
assert 'Content-Type: text/plain' in r
|
2014-04-24 15:07:31 +03:00
|
|
|
|
2014-10-20 15:40:55 +03:00
|
|
|
def test_upload_multiple_fields_with_the_same_name(self, httpbin):
|
|
|
|
r = http('--form', '--verbose', 'POST', httpbin.url + '/post',
|
2020-06-08 17:59:41 +02:00
|
|
|
f'test-file@{FILE_PATH_ARG}',
|
|
|
|
f'test-file@{FILE_PATH_ARG}')
|
2014-10-20 15:40:55 +03:00
|
|
|
assert HTTP_OK in r
|
|
|
|
assert r.count('Content-Disposition: form-data; name="test-file";'
|
2020-06-08 17:59:41 +02:00
|
|
|
f' filename="{os.path.basename(FILE_PATH)}"') == 2
|
2014-10-20 15:40:55 +03:00
|
|
|
# Should be 4, but is 3 because httpbin
|
|
|
|
# doesn't seem to support filed field lists
|
|
|
|
assert r.count(FILE_CONTENT) in [3, 4]
|
2016-02-28 09:45:45 +02:00
|
|
|
assert r.count('Content-Type: text/plain') == 2
|
2014-10-20 15:40:55 +03:00
|
|
|
|
2020-06-08 17:59:41 +02:00
|
|
|
def test_upload_custom_content_type(self, httpbin):
|
2020-08-15 17:50:00 +02:00
|
|
|
r = http(
|
|
|
|
'--form',
|
|
|
|
'--verbose',
|
|
|
|
httpbin.url + '/post',
|
|
|
|
f'test-file@{FILE_PATH_ARG};type=image/vnd.microsoft.icon'
|
|
|
|
)
|
2020-06-08 17:59:41 +02:00
|
|
|
assert HTTP_OK in r
|
|
|
|
# Content type is stripped from the filename
|
|
|
|
assert 'Content-Disposition: form-data; name="test-file";' \
|
|
|
|
f' filename="{os.path.basename(FILE_PATH)}"' in r
|
2020-08-15 17:50:00 +02:00
|
|
|
assert r.count(FILE_CONTENT) == 2
|
2020-06-08 17:59:41 +02:00
|
|
|
assert 'Content-Type: image/vnd.microsoft.icon' in r
|
|
|
|
|
2020-09-25 14:44:22 +02:00
|
|
|
@mock.patch('httpie.uploads.MULTIPART_UPLOAD_BUFFER', 0)
|
2020-08-15 17:50:00 +02:00
|
|
|
def test_large_upload_display_suppressed(self, httpbin):
|
|
|
|
r = http(
|
|
|
|
'--form',
|
|
|
|
'--verbose',
|
|
|
|
httpbin.url + '/post',
|
|
|
|
f'test-file@{FILE_PATH_ARG}',
|
|
|
|
'foo=bar',
|
|
|
|
)
|
|
|
|
assert HTTP_OK in r
|
|
|
|
assert r.count(FILE_CONTENT) == 1
|
|
|
|
assert LARGE_UPLOAD_SUPPRESSED_NOTICE.decode() in r
|
|
|
|
|
2020-08-19 10:22:42 +02:00
|
|
|
def test_form_no_files_urlencoded(self, httpbin):
|
|
|
|
r = http(
|
|
|
|
'--form',
|
|
|
|
'--verbose',
|
|
|
|
httpbin.url + '/post',
|
|
|
|
'AAAA=AAA',
|
|
|
|
'BBB=BBB',
|
|
|
|
)
|
|
|
|
assert HTTP_OK in r
|
|
|
|
assert FORM_CONTENT_TYPE in r
|
|
|
|
|
2020-09-25 14:44:22 +02:00
|
|
|
def test_multipart(self, httpbin):
|
2020-08-19 10:22:42 +02:00
|
|
|
r = http(
|
|
|
|
'--verbose',
|
|
|
|
'--multipart',
|
|
|
|
httpbin.url + '/post',
|
|
|
|
'AAAA=AAA',
|
|
|
|
'BBB=BBB',
|
|
|
|
)
|
|
|
|
assert HTTP_OK in r
|
|
|
|
assert FORM_CONTENT_TYPE not in r
|
|
|
|
assert 'multipart/form-data' in r
|
|
|
|
|
2020-09-25 14:44:22 +02:00
|
|
|
def test_multipart_too_large_for_terminal(self, httpbin):
|
|
|
|
with mock.patch('httpie.uploads.MULTIPART_UPLOAD_BUFFER', 0):
|
|
|
|
r = http(
|
|
|
|
'--verbose',
|
|
|
|
'--multipart',
|
|
|
|
httpbin.url + '/post',
|
|
|
|
'AAAA=AAA',
|
|
|
|
'BBB=BBB',
|
|
|
|
)
|
|
|
|
assert HTTP_OK in r
|
|
|
|
assert FORM_CONTENT_TYPE not in r
|
|
|
|
assert 'multipart/form-data' in r
|
|
|
|
|
|
|
|
def test_multipart_too_large_for_terminal_non_pretty(self, httpbin):
|
|
|
|
with mock.patch('httpie.uploads.MULTIPART_UPLOAD_BUFFER', 0):
|
|
|
|
r = http(
|
|
|
|
'--verbose',
|
|
|
|
'--multipart',
|
|
|
|
'--pretty=none',
|
|
|
|
httpbin.url + '/post',
|
|
|
|
'AAAA=AAA',
|
|
|
|
'BBB=BBB',
|
|
|
|
)
|
|
|
|
assert HTTP_OK in r
|
|
|
|
assert FORM_CONTENT_TYPE not in r
|
|
|
|
assert 'multipart/form-data' in r
|
|
|
|
|
2020-08-19 10:22:42 +02:00
|
|
|
def test_form_multipart_custom_boundary(self, httpbin):
|
|
|
|
boundary = 'HTTPIE_FTW'
|
|
|
|
r = http(
|
|
|
|
'--print=HB',
|
|
|
|
'--check-status',
|
|
|
|
'--multipart',
|
|
|
|
f'--boundary={boundary}',
|
|
|
|
httpbin.url + '/post',
|
|
|
|
'AAAA=AAA',
|
|
|
|
'BBB=BBB',
|
|
|
|
)
|
|
|
|
assert f'multipart/form-data; boundary={boundary}' in r
|
|
|
|
assert r.count(boundary) == 4
|
|
|
|
|
|
|
|
def test_multipart_custom_content_type_boundary_added(self, httpbin):
|
|
|
|
boundary = 'HTTPIE_FTW'
|
|
|
|
r = http(
|
|
|
|
'--print=HB',
|
|
|
|
'--check-status',
|
|
|
|
'--multipart',
|
|
|
|
f'--boundary={boundary}',
|
|
|
|
httpbin.url + '/post',
|
|
|
|
'Content-Type: multipart/magic',
|
|
|
|
'AAAA=AAA',
|
|
|
|
'BBB=BBB',
|
|
|
|
)
|
|
|
|
assert f'multipart/magic; boundary={boundary}' in r
|
|
|
|
assert r.count(boundary) == 4
|
|
|
|
|
|
|
|
def test_multipart_custom_content_type_boundary_preserved(self, httpbin):
|
|
|
|
# Allow explicit nonsense requests.
|
|
|
|
boundary_in_header = 'HEADER_BOUNDARY'
|
|
|
|
boundary_in_body = 'BODY_BOUNDARY'
|
|
|
|
r = http(
|
|
|
|
'--print=HB',
|
|
|
|
'--check-status',
|
|
|
|
'--multipart',
|
|
|
|
f'--boundary={boundary_in_body}',
|
|
|
|
httpbin.url + '/post',
|
|
|
|
f'Content-Type: multipart/magic; boundary={boundary_in_header}',
|
|
|
|
'AAAA=AAA',
|
|
|
|
'BBB=BBB',
|
|
|
|
)
|
|
|
|
assert f'multipart/magic; boundary={boundary_in_header}' in r
|
|
|
|
assert r.count(boundary_in_body) == 3
|
|
|
|
|
2014-04-24 15:07:31 +03:00
|
|
|
|
2014-04-25 12:39:59 +03:00
|
|
|
class TestRequestBodyFromFilePath:
|
2014-04-24 15:07:31 +03:00
|
|
|
"""
|
|
|
|
`http URL @file'
|
|
|
|
|
|
|
|
"""
|
2014-04-24 16:48:01 +03:00
|
|
|
|
2014-06-28 17:35:57 +03:00
|
|
|
def test_request_body_from_file_by_path(self, httpbin):
|
|
|
|
r = http('--verbose',
|
|
|
|
'POST', httpbin.url + '/post', '@' + FILE_PATH_ARG)
|
2014-04-24 15:58:15 +03:00
|
|
|
assert HTTP_OK in r
|
2014-04-26 18:53:01 +03:00
|
|
|
assert FILE_CONTENT in r, r
|
2014-04-24 15:58:15 +03:00
|
|
|
assert '"Content-Type": "text/plain"' in r
|
2014-04-24 15:07:31 +03:00
|
|
|
|
2014-06-28 17:35:57 +03:00
|
|
|
def test_request_body_from_file_by_path_with_explicit_content_type(
|
|
|
|
self, httpbin):
|
|
|
|
r = http('--verbose',
|
|
|
|
'POST', httpbin.url + '/post', '@' + FILE_PATH_ARG,
|
|
|
|
'Content-Type:text/plain; charset=utf8')
|
2014-04-24 15:58:15 +03:00
|
|
|
assert HTTP_OK in r
|
|
|
|
assert FILE_CONTENT in r
|
2014-06-28 17:35:57 +03:00
|
|
|
assert 'Content-Type: text/plain; charset=utf8' in r
|
2014-04-24 15:07:31 +03:00
|
|
|
|
2014-06-28 17:35:57 +03:00
|
|
|
def test_request_body_from_file_by_path_no_field_name_allowed(
|
|
|
|
self, httpbin):
|
2017-12-28 19:17:48 +02:00
|
|
|
env = MockEnvironment(stdin_isatty=True)
|
2014-06-28 17:35:57 +03:00
|
|
|
r = http('POST', httpbin.url + '/post', 'field-name@' + FILE_PATH_ARG,
|
2019-09-03 17:14:39 +02:00
|
|
|
env=env, tolerate_error_exit_status=True)
|
2014-04-24 15:58:15 +03:00
|
|
|
assert 'perhaps you meant --form?' in r.stderr
|
2014-04-24 15:07:31 +03:00
|
|
|
|
2014-06-28 17:35:57 +03:00
|
|
|
def test_request_body_from_file_by_path_no_data_items_allowed(
|
|
|
|
self, httpbin):
|
2017-12-28 19:17:48 +02:00
|
|
|
env = MockEnvironment(stdin_isatty=False)
|
2019-09-03 17:14:39 +02:00
|
|
|
r = http(
|
|
|
|
'POST',
|
|
|
|
httpbin.url + '/post',
|
|
|
|
'@' + FILE_PATH_ARG, 'foo=bar',
|
|
|
|
env=env,
|
|
|
|
tolerate_error_exit_status=True,
|
|
|
|
)
|
2020-01-23 16:54:43 +02:00
|
|
|
assert r.exit_status == ExitStatus.ERROR
|
2014-04-24 15:58:15 +03:00
|
|
|
assert 'cannot be mixed' in r.stderr
|