2014-04-24 14:07:31 +02:00
|
|
|
"""CLI argument parsing related tests."""
|
|
|
|
import json
|
|
|
|
# noinspection PyCompatibility
|
|
|
|
import argparse
|
|
|
|
|
2014-04-25 11:39:59 +02:00
|
|
|
import pytest
|
2015-02-05 14:35:05 +01:00
|
|
|
from requests.exceptions import InvalidSchema
|
2014-04-25 11:39:59 +02:00
|
|
|
|
2014-04-24 18:20:23 +02:00
|
|
|
from httpie import input
|
2014-10-20 14:40:55 +02:00
|
|
|
from httpie.input import KeyValue, KeyValueArgType, DataDict
|
2014-04-24 18:20:23 +02:00
|
|
|
from httpie import ExitStatus
|
|
|
|
from httpie.cli import parser
|
2014-06-28 16:35:57 +02:00
|
|
|
from utils import TestEnvironment, http, HTTP_OK
|
2014-04-28 11:29:41 +02:00
|
|
|
from fixtures import (
|
2014-04-24 14:07:31 +02:00
|
|
|
FILE_PATH_ARG, JSON_FILE_PATH_ARG,
|
2014-04-24 15:17:04 +02:00
|
|
|
JSON_FILE_CONTENT, FILE_CONTENT, FILE_PATH
|
2014-04-24 14:07:31 +02:00
|
|
|
)
|
2014-04-24 15:17:04 +02:00
|
|
|
|
2014-04-24 14:07:31 +02:00
|
|
|
|
2014-04-25 11:39:59 +02:00
|
|
|
class TestItemParsing:
|
|
|
|
|
2014-09-05 18:33:46 +02:00
|
|
|
key_value = KeyValueArgType(*input.SEP_GROUP_ALL_ITEMS)
|
2014-04-24 14:07:31 +02:00
|
|
|
|
|
|
|
def test_invalid_items(self):
|
|
|
|
items = ['no-separator']
|
|
|
|
for item in items:
|
2014-09-05 18:33:46 +02:00
|
|
|
pytest.raises(argparse.ArgumentTypeError, self.key_value, item)
|
2014-04-24 14:07:31 +02:00
|
|
|
|
2014-09-05 18:33:46 +02:00
|
|
|
def test_escape_separator(self):
|
2014-09-05 07:51:35 +02:00
|
|
|
items = input.parse_items([
|
2014-04-24 14:07:31 +02:00
|
|
|
# headers
|
2014-09-05 18:33:46 +02:00
|
|
|
self.key_value(r'foo\:bar:baz'),
|
|
|
|
self.key_value(r'jack\@jill:hill'),
|
|
|
|
|
2014-04-24 14:07:31 +02:00
|
|
|
# data
|
2014-09-05 18:33:46 +02:00
|
|
|
self.key_value(r'baz\=bar=foo'),
|
|
|
|
|
2014-04-24 14:07:31 +02:00
|
|
|
# files
|
2014-09-05 18:33:46 +02:00
|
|
|
self.key_value(r'bar\@baz@%s' % FILE_PATH_ARG),
|
2014-04-24 14:07:31 +02:00
|
|
|
])
|
|
|
|
# `requests.structures.CaseInsensitiveDict` => `dict`
|
2014-09-05 07:51:35 +02:00
|
|
|
headers = dict(items.headers._store.values())
|
2014-09-05 18:33:46 +02:00
|
|
|
|
2014-04-24 14:58:15 +02:00
|
|
|
assert headers == {
|
2014-04-24 14:07:31 +02:00
|
|
|
'foo:bar': 'baz',
|
|
|
|
'jack@jill': 'hill',
|
2014-04-24 14:58:15 +02:00
|
|
|
}
|
2014-09-05 07:51:35 +02:00
|
|
|
assert items.data == {'baz=bar': 'foo'}
|
|
|
|
assert 'bar@baz' in items.files
|
2014-04-24 14:07:31 +02:00
|
|
|
|
2014-09-05 18:33:46 +02:00
|
|
|
@pytest.mark.parametrize(('string', 'key', 'sep', 'value'), [
|
|
|
|
('path=c:\windows', 'path', '=', 'c:\windows'),
|
|
|
|
('path=c:\windows\\', 'path', '=', 'c:\windows\\'),
|
|
|
|
('path\==c:\windows', 'path=', '=', 'c:\windows'),
|
|
|
|
])
|
|
|
|
def test_backslash_before_non_special_character_does_not_escape(
|
|
|
|
self, string, key, sep, value):
|
|
|
|
expected = KeyValue(orig=string, key=key, sep=sep, value=value)
|
|
|
|
actual = self.key_value(string)
|
|
|
|
assert actual == expected
|
|
|
|
|
2014-04-24 14:07:31 +02:00
|
|
|
def test_escape_longsep(self):
|
2014-09-05 07:51:35 +02:00
|
|
|
items = input.parse_items([
|
2014-09-05 18:33:46 +02:00
|
|
|
self.key_value(r'bob\:==foo'),
|
2014-04-24 14:07:31 +02:00
|
|
|
])
|
2014-09-05 07:51:35 +02:00
|
|
|
assert items.params == {'bob:': 'foo'}
|
2014-04-24 14:07:31 +02:00
|
|
|
|
|
|
|
def test_valid_items(self):
|
2014-09-05 07:51:35 +02:00
|
|
|
items = input.parse_items([
|
2014-09-05 18:33:46 +02:00
|
|
|
self.key_value('string=value'),
|
2016-07-02 12:03:19 +02:00
|
|
|
self.key_value('Header:value'),
|
|
|
|
self.key_value('Unset-Header:'),
|
|
|
|
self.key_value('Empty-Header;'),
|
2014-09-05 18:33:46 +02:00
|
|
|
self.key_value('list:=["a", 1, {}, false]'),
|
|
|
|
self.key_value('obj:={"a": "b"}'),
|
|
|
|
self.key_value('ed='),
|
|
|
|
self.key_value('bool:=true'),
|
|
|
|
self.key_value('file@' + FILE_PATH_ARG),
|
|
|
|
self.key_value('query==value'),
|
|
|
|
self.key_value('string-embed=@' + FILE_PATH_ARG),
|
|
|
|
self.key_value('raw-json-embed:=@' + JSON_FILE_PATH_ARG),
|
2014-04-24 14:07:31 +02:00
|
|
|
])
|
|
|
|
|
|
|
|
# Parsed headers
|
|
|
|
# `requests.structures.CaseInsensitiveDict` => `dict`
|
2014-09-05 07:51:35 +02:00
|
|
|
headers = dict(items.headers._store.values())
|
2016-07-02 12:03:19 +02:00
|
|
|
assert headers == {
|
|
|
|
'Header': 'value',
|
|
|
|
'Unset-Header': None,
|
|
|
|
'Empty-Header': ''
|
|
|
|
}
|
2014-04-24 14:07:31 +02:00
|
|
|
|
|
|
|
# Parsed data
|
2014-09-05 07:51:35 +02:00
|
|
|
raw_json_embed = items.data.pop('raw-json-embed')
|
2014-04-26 18:41:28 +02:00
|
|
|
assert raw_json_embed == json.loads(JSON_FILE_CONTENT)
|
2014-09-05 07:51:35 +02:00
|
|
|
items.data['string-embed'] = items.data['string-embed'].strip()
|
|
|
|
assert dict(items.data) == {
|
2014-04-24 14:07:31 +02:00
|
|
|
"ed": "",
|
|
|
|
"string": "value",
|
|
|
|
"bool": True,
|
|
|
|
"list": ["a", 1, {}, False],
|
|
|
|
"obj": {"a": "b"},
|
|
|
|
"string-embed": FILE_CONTENT,
|
2014-04-24 14:58:15 +02:00
|
|
|
}
|
2014-04-24 14:07:31 +02:00
|
|
|
|
|
|
|
# Parsed query string parameters
|
2014-09-05 07:51:35 +02:00
|
|
|
assert items.params == {'query': 'value'}
|
2014-04-24 14:07:31 +02:00
|
|
|
|
|
|
|
# Parsed file fields
|
2014-09-05 07:51:35 +02:00
|
|
|
assert 'file' in items.files
|
|
|
|
assert (items.files['file'][1].read().strip().decode('utf8')
|
|
|
|
== FILE_CONTENT)
|
2014-04-24 14:07:31 +02:00
|
|
|
|
2014-10-20 14:40:55 +02:00
|
|
|
def test_multiple_file_fields_with_same_field_name(self):
|
|
|
|
items = input.parse_items([
|
|
|
|
self.key_value('file_field@' + FILE_PATH_ARG),
|
|
|
|
self.key_value('file_field@' + FILE_PATH_ARG),
|
|
|
|
])
|
|
|
|
assert len(items.files['file_field']) == 2
|
|
|
|
|
|
|
|
def test_multiple_text_fields_with_same_field_name(self):
|
|
|
|
items = input.parse_items(
|
|
|
|
[self.key_value('text_field=a'),
|
|
|
|
self.key_value('text_field=b')],
|
|
|
|
data_class=DataDict
|
|
|
|
)
|
|
|
|
assert items.data['text_field'] == ['a', 'b']
|
|
|
|
assert list(items.data.items()) == [
|
|
|
|
('text_field', 'a'),
|
|
|
|
('text_field', 'b'),
|
|
|
|
]
|
|
|
|
|
2014-04-24 14:07:31 +02:00
|
|
|
|
2014-04-25 11:39:59 +02:00
|
|
|
class TestQuerystring:
|
2014-06-28 16:35:57 +02:00
|
|
|
def test_query_string_params_in_url(self, httpbin):
|
|
|
|
r = http('--print=Hhb', 'GET', httpbin.url + '/get?a=1&b=2')
|
2014-04-24 14:07:31 +02:00
|
|
|
path = '/get?a=1&b=2'
|
2014-06-28 16:35:57 +02:00
|
|
|
url = httpbin.url + path
|
2014-04-24 14:58:15 +02:00
|
|
|
assert HTTP_OK in r
|
|
|
|
assert 'GET %s HTTP/1.1' % path in r
|
|
|
|
assert '"url": "%s"' % url in r
|
2014-04-24 14:07:31 +02:00
|
|
|
|
2014-06-28 16:35:57 +02:00
|
|
|
def test_query_string_params_items(self, httpbin):
|
|
|
|
r = http('--print=Hhb', 'GET', httpbin.url + '/get', 'a==1')
|
|
|
|
path = '/get?a=1'
|
|
|
|
url = httpbin.url + path
|
2014-04-24 14:58:15 +02:00
|
|
|
assert HTTP_OK in r
|
|
|
|
assert 'GET %s HTTP/1.1' % path in r
|
|
|
|
assert '"url": "%s"' % url in r
|
2014-04-24 14:07:31 +02:00
|
|
|
|
2014-06-28 16:35:57 +02:00
|
|
|
def test_query_string_params_in_url_and_items_with_duplicates(self,
|
|
|
|
httpbin):
|
|
|
|
r = http('--print=Hhb', 'GET',
|
|
|
|
httpbin.url + '/get?a=1&a=1', 'a==1', 'a==1')
|
|
|
|
path = '/get?a=1&a=1&a=1&a=1'
|
|
|
|
url = httpbin.url + path
|
2014-04-24 14:58:15 +02:00
|
|
|
assert HTTP_OK in r
|
|
|
|
assert 'GET %s HTTP/1.1' % path in r
|
|
|
|
assert '"url": "%s"' % url in r
|
2014-04-24 14:07:31 +02:00
|
|
|
|
|
|
|
|
2016-03-03 11:47:12 +01:00
|
|
|
class TestLocalhostShorthand:
|
2014-04-24 14:07:31 +02:00
|
|
|
def test_expand_localhost_shorthand(self):
|
|
|
|
args = parser.parse_args(args=[':'], env=TestEnvironment())
|
2014-04-24 14:58:15 +02:00
|
|
|
assert args.url == 'http://localhost'
|
2014-04-24 14:07:31 +02:00
|
|
|
|
|
|
|
def test_expand_localhost_shorthand_with_slash(self):
|
|
|
|
args = parser.parse_args(args=[':/'], env=TestEnvironment())
|
2014-04-24 14:58:15 +02:00
|
|
|
assert args.url == 'http://localhost/'
|
2014-04-24 14:07:31 +02:00
|
|
|
|
|
|
|
def test_expand_localhost_shorthand_with_port(self):
|
|
|
|
args = parser.parse_args(args=[':3000'], env=TestEnvironment())
|
2014-04-24 14:58:15 +02:00
|
|
|
assert args.url == 'http://localhost:3000'
|
2014-04-24 14:07:31 +02:00
|
|
|
|
|
|
|
def test_expand_localhost_shorthand_with_path(self):
|
|
|
|
args = parser.parse_args(args=[':/path'], env=TestEnvironment())
|
2014-04-24 14:58:15 +02:00
|
|
|
assert args.url == 'http://localhost/path'
|
2014-04-24 14:07:31 +02:00
|
|
|
|
|
|
|
def test_expand_localhost_shorthand_with_port_and_slash(self):
|
|
|
|
args = parser.parse_args(args=[':3000/'], env=TestEnvironment())
|
2014-04-24 14:58:15 +02:00
|
|
|
assert args.url == 'http://localhost:3000/'
|
2014-04-24 14:07:31 +02:00
|
|
|
|
|
|
|
def test_expand_localhost_shorthand_with_port_and_path(self):
|
|
|
|
args = parser.parse_args(args=[':3000/path'], env=TestEnvironment())
|
2014-04-24 14:58:15 +02:00
|
|
|
assert args.url == 'http://localhost:3000/path'
|
2014-04-24 14:07:31 +02:00
|
|
|
|
|
|
|
def test_dont_expand_shorthand_ipv6_as_shorthand(self):
|
|
|
|
args = parser.parse_args(args=['::1'], env=TestEnvironment())
|
2014-04-24 14:58:15 +02:00
|
|
|
assert args.url == 'http://::1'
|
2014-04-24 14:07:31 +02:00
|
|
|
|
|
|
|
def test_dont_expand_longer_ipv6_as_shorthand(self):
|
|
|
|
args = parser.parse_args(
|
|
|
|
args=['::ffff:c000:0280'],
|
|
|
|
env=TestEnvironment()
|
|
|
|
)
|
2014-04-24 14:58:15 +02:00
|
|
|
assert args.url == 'http://::ffff:c000:0280'
|
2014-04-24 14:07:31 +02:00
|
|
|
|
|
|
|
def test_dont_expand_full_ipv6_as_shorthand(self):
|
|
|
|
args = parser.parse_args(
|
|
|
|
args=['0000:0000:0000:0000:0000:0000:0000:0001'],
|
|
|
|
env=TestEnvironment()
|
|
|
|
)
|
2014-04-24 14:58:15 +02:00
|
|
|
assert args.url == 'http://0000:0000:0000:0000:0000:0000:0000:0001'
|
2014-04-24 14:07:31 +02:00
|
|
|
|
|
|
|
|
2014-04-25 11:39:59 +02:00
|
|
|
class TestArgumentParser:
|
|
|
|
|
|
|
|
def setup_method(self, method):
|
2016-02-28 12:01:54 +01:00
|
|
|
self.parser = input.HTTPieArgumentParser()
|
2014-04-24 14:07:31 +02:00
|
|
|
|
|
|
|
def test_guess_when_method_set_and_valid(self):
|
|
|
|
self.parser.args = argparse.Namespace()
|
|
|
|
self.parser.args.method = 'GET'
|
|
|
|
self.parser.args.url = 'http://example.com/'
|
|
|
|
self.parser.args.items = []
|
|
|
|
self.parser.args.ignore_stdin = False
|
|
|
|
|
|
|
|
self.parser.env = TestEnvironment()
|
|
|
|
|
|
|
|
self.parser._guess_method()
|
|
|
|
|
2014-04-24 14:58:15 +02:00
|
|
|
assert self.parser.args.method == 'GET'
|
|
|
|
assert self.parser.args.url == 'http://example.com/'
|
|
|
|
assert self.parser.args.items == []
|
2014-04-24 14:07:31 +02:00
|
|
|
|
|
|
|
def test_guess_when_method_not_set(self):
|
|
|
|
self.parser.args = argparse.Namespace()
|
|
|
|
self.parser.args.method = None
|
|
|
|
self.parser.args.url = 'http://example.com/'
|
|
|
|
self.parser.args.items = []
|
|
|
|
self.parser.args.ignore_stdin = False
|
|
|
|
self.parser.env = TestEnvironment()
|
|
|
|
|
|
|
|
self.parser._guess_method()
|
|
|
|
|
2014-04-24 14:58:15 +02:00
|
|
|
assert self.parser.args.method == 'GET'
|
|
|
|
assert self.parser.args.url == 'http://example.com/'
|
|
|
|
assert self.parser.args.items == []
|
2014-04-24 14:07:31 +02:00
|
|
|
|
|
|
|
def test_guess_when_method_set_but_invalid_and_data_field(self):
|
|
|
|
self.parser.args = argparse.Namespace()
|
|
|
|
self.parser.args.method = 'http://example.com/'
|
|
|
|
self.parser.args.url = 'data=field'
|
|
|
|
self.parser.args.items = []
|
|
|
|
self.parser.args.ignore_stdin = False
|
|
|
|
self.parser.env = TestEnvironment()
|
|
|
|
self.parser._guess_method()
|
|
|
|
|
2014-04-24 14:58:15 +02:00
|
|
|
assert self.parser.args.method == 'POST'
|
|
|
|
assert self.parser.args.url == 'http://example.com/'
|
|
|
|
assert self.parser.args.items == [
|
|
|
|
KeyValue(key='data',
|
2014-04-24 15:48:01 +02:00
|
|
|
value='field',
|
|
|
|
sep='=',
|
|
|
|
orig='data=field')
|
2014-04-24 14:58:15 +02:00
|
|
|
]
|
2014-04-24 14:07:31 +02:00
|
|
|
|
|
|
|
def test_guess_when_method_set_but_invalid_and_header_field(self):
|
|
|
|
self.parser.args = argparse.Namespace()
|
|
|
|
self.parser.args.method = 'http://example.com/'
|
|
|
|
self.parser.args.url = 'test:header'
|
|
|
|
self.parser.args.items = []
|
|
|
|
self.parser.args.ignore_stdin = False
|
|
|
|
|
|
|
|
self.parser.env = TestEnvironment()
|
|
|
|
|
|
|
|
self.parser._guess_method()
|
|
|
|
|
2014-04-24 14:58:15 +02:00
|
|
|
assert self.parser.args.method == 'GET'
|
|
|
|
assert self.parser.args.url == 'http://example.com/'
|
|
|
|
assert self.parser.args.items, [
|
|
|
|
KeyValue(key='test',
|
2014-04-24 15:48:01 +02:00
|
|
|
value='header',
|
|
|
|
sep=':',
|
|
|
|
orig='test:header')
|
2014-04-24 14:58:15 +02:00
|
|
|
]
|
2014-04-24 14:07:31 +02:00
|
|
|
|
|
|
|
def test_guess_when_method_set_but_invalid_and_item_exists(self):
|
|
|
|
self.parser.args = argparse.Namespace()
|
|
|
|
self.parser.args.method = 'http://example.com/'
|
|
|
|
self.parser.args.url = 'new_item=a'
|
|
|
|
self.parser.args.items = [
|
2014-04-24 14:58:15 +02:00
|
|
|
KeyValue(
|
2014-04-24 14:07:31 +02:00
|
|
|
key='old_item', value='b', sep='=', orig='old_item=b')
|
|
|
|
]
|
|
|
|
self.parser.args.ignore_stdin = False
|
|
|
|
|
|
|
|
self.parser.env = TestEnvironment()
|
|
|
|
|
|
|
|
self.parser._guess_method()
|
|
|
|
|
2014-04-24 14:58:15 +02:00
|
|
|
assert self.parser.args.items, [
|
|
|
|
KeyValue(key='new_item', value='a', sep='=', orig='new_item=a'),
|
|
|
|
KeyValue(
|
2014-04-24 14:07:31 +02:00
|
|
|
key='old_item', value='b', sep='=', orig='old_item=b'),
|
2014-04-24 14:58:15 +02:00
|
|
|
]
|
2014-04-24 14:07:31 +02:00
|
|
|
|
|
|
|
|
2014-04-25 11:39:59 +02:00
|
|
|
class TestNoOptions:
|
2014-06-28 16:35:57 +02:00
|
|
|
|
|
|
|
def test_valid_no_options(self, httpbin):
|
|
|
|
r = http('--verbose', '--no-verbose', 'GET', httpbin.url + '/get')
|
2014-04-24 14:58:15 +02:00
|
|
|
assert 'GET /get HTTP/1.1' not in r
|
2014-04-24 14:07:31 +02:00
|
|
|
|
2014-06-28 16:35:57 +02:00
|
|
|
def test_invalid_no_options(self, httpbin):
|
|
|
|
r = http('--no-war', 'GET', httpbin.url + '/get',
|
2014-04-26 15:06:51 +02:00
|
|
|
error_exit_ok=True)
|
2014-04-24 14:58:15 +02:00
|
|
|
assert r.exit_status == 1
|
|
|
|
assert 'unrecognized arguments: --no-war' in r.stderr
|
|
|
|
assert 'GET /get HTTP/1.1' not in r
|
2014-04-24 14:07:31 +02:00
|
|
|
|
|
|
|
|
2014-04-25 11:39:59 +02:00
|
|
|
class TestIgnoreStdin:
|
2014-06-28 16:35:57 +02:00
|
|
|
|
|
|
|
def test_ignore_stdin(self, httpbin):
|
2014-04-24 14:07:31 +02:00
|
|
|
with open(FILE_PATH) as f:
|
2014-04-24 15:48:01 +02:00
|
|
|
env = TestEnvironment(stdin=f, stdin_isatty=False)
|
2014-06-28 16:35:57 +02:00
|
|
|
r = http('--ignore-stdin', '--verbose', httpbin.url + '/get',
|
|
|
|
env=env)
|
2014-04-24 14:58:15 +02:00
|
|
|
assert HTTP_OK in r
|
|
|
|
assert 'GET /get HTTP' in r, "Don't default to POST."
|
|
|
|
assert FILE_CONTENT not in r, "Don't send stdin data."
|
2014-04-24 14:07:31 +02:00
|
|
|
|
2014-06-28 16:35:57 +02:00
|
|
|
def test_ignore_stdin_cannot_prompt_password(self, httpbin):
|
|
|
|
r = http('--ignore-stdin', '--auth=no-password', httpbin.url + '/get',
|
2014-04-26 15:06:51 +02:00
|
|
|
error_exit_ok=True)
|
2014-04-24 14:58:15 +02:00
|
|
|
assert r.exit_status == ExitStatus.ERROR
|
|
|
|
assert 'because --ignore-stdin' in r.stderr
|
2015-02-05 14:35:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TestSchemes:
|
|
|
|
|
|
|
|
def test_custom_scheme(self):
|
|
|
|
# InvalidSchema is expected because HTTPie
|
|
|
|
# shouldn't touch a formally valid scheme.
|
|
|
|
with pytest.raises(InvalidSchema):
|
|
|
|
http('foo+bar-BAZ.123://bah')
|