Fail gracefully if multiple request data files are supplied (#1042)

This commit is contained in:
Jan Verbeek 2021-04-15 09:35:50 +02:00 committed by GitHub
parent fc45bf0fe3
commit 611bcdaab1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 6 deletions

View File

@ -358,13 +358,17 @@ class HTTPieArgumentParser(argparse.ArgumentParser):
if self.args.files and not self.args.form: if self.args.files and not self.args.form:
# `http url @/path/to/file` # `http url @/path/to/file`
file_fields = list(self.args.files.keys()) request_file = None
if file_fields != ['']: for key, file in self.args.files.items():
self.error( if key != '':
'Invalid file fields (perhaps you meant --form?): %s' self.error(
% ','.join(file_fields)) 'Invalid file fields (perhaps you meant --form?): %s'
% ','.join(self.args.files.keys()))
if request_file is not None:
self.error("Can't read request from multiple files")
request_file = file
fn, fd, ct = self.args.files[''] fn, fd, ct = request_file
self.args.files = {} self.args.files = {}
self._body_from_file(fd) self._body_from_file(fd)

View File

@ -270,3 +270,16 @@ class TestRequestBodyFromFilePath:
) )
assert r.exit_status == ExitStatus.ERROR assert r.exit_status == ExitStatus.ERROR
assert 'cannot be mixed' in r.stderr assert 'cannot be mixed' in r.stderr
def test_multiple_request_bodies_from_file_by_path(self, httpbin):
env = MockEnvironment(stdin_isatty=True)
r = http(
'--verbose',
'POST', httpbin.url + '/post',
'@' + FILE_PATH_ARG,
'@' + FILE_PATH_ARG,
env=env,
tolerate_error_exit_status=True,
)
assert r.exit_status == ExitStatus.ERROR
assert 'from multiple files' in r.stderr