Better error messages.

This commit is contained in:
Jakub Roztocil 2012-07-29 07:14:54 +02:00
parent edf87c3392
commit 6eed0d92eb
2 changed files with 8 additions and 23 deletions

View File

@ -173,27 +173,15 @@ class Parser(argparse.ArgumentParser):
if args.files and not args.form: if args.files and not args.form:
# `http url @/path/to/file` # `http url @/path/to/file`
# It's not --form so the file contents will be used as the file_fields = list(args.files.keys())
# body of the requests. Also, we try to detect the appropriate if file_fields != ['']:
# Content-Type.
if len(args.files) > 1:
self.error( self.error(
'Only one file can be specified unless' 'Invalid file fields (perhaps you meant --form?): %s'
' --form is used. File fields: %s' % ','.join(file_fields))
% ','.join(args.files.keys()))
field_name = list(args.files.keys())[0] fn, data = args.files['']
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
args.files = {} args.files = {}
self._body_from_file(args, data)
if 'Content-Type' not in args.headers: if 'Content-Type' not in args.headers:
mime, encoding = mimetypes.guess_type(fn, strict=False) mime, encoding = mimetypes.guess_type(fn, strict=False)
if mime: if mime:
@ -415,8 +403,6 @@ def parse_items(items, data=None, headers=None, files=None, params=None):
except IOError as e: except IOError as e:
raise ParseError( raise ParseError(
'Invalid argument "%s": %s' % (item.orig, e)) 'Invalid argument "%s": %s' % (item.orig, e))
if not key:
key = os.path.basename(item.value)
target = files target = files
elif item.sep in [SEP_DATA, SEP_DATA_RAW_JSON]: elif item.sep in [SEP_DATA, SEP_DATA_RAW_JSON]:

View File

@ -619,12 +619,11 @@ class RequestBodyFromFilePathTest(BaseTestCase):
self.assertIn(TEST_FILE_CONTENT, r) self.assertIn(TEST_FILE_CONTENT, r)
self.assertIn('"Content-Type": "x-foo/bar"', 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( self.assertRaises(SystemExit, lambda: http(
'POST', 'POST',
httpbin('/post'), httpbin('/post'),
'@' + TEST_FILE_PATH, 'field-name@' + TEST_FILE_PATH)
'@' + TEST_FILE2_PATH)
) )
def test_request_body_from_file_by_path_no_data_items_allowed(self): def test_request_body_from_file_by_path_no_data_items_allowed(self):