From 6eed0d92ebfbde42aa90f4295ffba26d536a1e58 Mon Sep 17 00:00:00 2001 From: Jakub Roztocil Date: Sun, 29 Jul 2012 07:14:54 +0200 Subject: [PATCH] Better error messages. --- httpie/input.py | 26 ++++++-------------------- tests/tests.py | 5 ++--- 2 files changed, 8 insertions(+), 23 deletions(-) diff --git a/httpie/input.py b/httpie/input.py index 86d0d713..c7ea7b2f 100644 --- a/httpie/input.py +++ b/httpie/input.py @@ -173,27 +173,15 @@ class Parser(argparse.ArgumentParser): if args.files and not args.form: # `http url @/path/to/file` - # It's not --form so the file contents will be used as the - # body of the requests. Also, we try to detect the appropriate - # Content-Type. - if len(args.files) > 1: + file_fields = list(args.files.keys()) + if file_fields != ['']: self.error( - 'Only one file can be specified unless' - ' --form is used. File fields: %s' - % ','.join(args.files.keys())) + 'Invalid file fields (perhaps you meant --form?): %s' + % ','.join(file_fields)) - field_name = list(args.files.keys())[0] - fn, data = args.files[field_name] - if field_name: - # `http url name@/path' doesn't make sense here. - self.error( - 'file fields (name@/path) require --form / -f') - - self._body_from_file(args, data) - - # Reset files + fn, data = args.files[''] args.files = {} - + self._body_from_file(args, data) if 'Content-Type' not in args.headers: mime, encoding = mimetypes.guess_type(fn, strict=False) if mime: @@ -415,8 +403,6 @@ def parse_items(items, data=None, headers=None, files=None, params=None): except IOError as e: raise ParseError( 'Invalid argument "%s": %s' % (item.orig, e)) - if not key: - key = os.path.basename(item.value) target = files elif item.sep in [SEP_DATA, SEP_DATA_RAW_JSON]: diff --git a/tests/tests.py b/tests/tests.py index e3b69429..2d372da8 100755 --- a/tests/tests.py +++ b/tests/tests.py @@ -619,12 +619,11 @@ class RequestBodyFromFilePathTest(BaseTestCase): self.assertIn(TEST_FILE_CONTENT, r) self.assertIn('"Content-Type": "x-foo/bar"', r) - def test_request_body_from_file_by_path_only_one_file_allowed(self): + def test_request_body_from_file_by_path_no_field_name_allowed(self): self.assertRaises(SystemExit, lambda: http( 'POST', httpbin('/post'), - '@' + TEST_FILE_PATH, - '@' + TEST_FILE2_PATH) + 'field-name@' + TEST_FILE_PATH) ) def test_request_body_from_file_by_path_no_data_items_allowed(self):