From 611bcdaab18fb4d5062da7470dd2d9c6af5d44be Mon Sep 17 00:00:00 2001 From: Jan Verbeek Date: Thu, 15 Apr 2021 09:35:50 +0200 Subject: [PATCH] Fail gracefully if multiple request data files are supplied (#1042) --- httpie/cli/argparser.py | 16 ++++++++++------ tests/test_uploads.py | 13 +++++++++++++ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/httpie/cli/argparser.py b/httpie/cli/argparser.py index 720e70b1..20abcfd4 100644 --- a/httpie/cli/argparser.py +++ b/httpie/cli/argparser.py @@ -358,13 +358,17 @@ class HTTPieArgumentParser(argparse.ArgumentParser): if self.args.files and not self.args.form: # `http url @/path/to/file` - file_fields = list(self.args.files.keys()) - if file_fields != ['']: - self.error( - 'Invalid file fields (perhaps you meant --form?): %s' - % ','.join(file_fields)) + request_file = None + for key, file in self.args.files.items(): + if key != '': + self.error( + '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._body_from_file(fd) diff --git a/tests/test_uploads.py b/tests/test_uploads.py index 05fb2001..7cdccfa3 100644 --- a/tests/test_uploads.py +++ b/tests/test_uploads.py @@ -270,3 +270,16 @@ class TestRequestBodyFromFilePath: ) assert r.exit_status == ExitStatus.ERROR 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