def test_httpie_args_post(): c = Context('http://localhost/things') c.headers.update({'Authorization': 'ApiKey 1234', 'Accept': 'text/html'}) c.options['--form'] = None c.body_params.update({'name': 'Jane Doe', 'email': '*****@*****.**'}) args = c.httpie_args('post') assert args == [ '--form', 'POST', 'http://localhost/things', '[email protected]', "name=Jane Doe", 'Accept:text/html', "Authorization:ApiKey 1234", ] # Test quote option args = c.httpie_args('post', quote=True) assert args == [ '--form', 'POST', 'http://localhost/things', '[email protected]', "'name=Jane Doe'", 'Accept:text/html', "'Authorization:ApiKey 1234'", ]
def test_eq(): c1 = Context('http://localhost') c2 = Context('http://localhost') assert c1 == c2 c1.options['--verify'] = 'no' assert c1 != c2
def test_httpie_args_post(): c = Context('http://localhost/things') c.headers.update({ 'Authorization': 'ApiKey 1234', 'Accept': 'text/html' }) c.options['--form'] = None c.body_params.update({ 'name': 'Jane Doe', 'email': '*****@*****.**' }) args = c.httpie_args('post') assert args == [ '--form', 'POST', 'http://localhost/things', '[email protected]', "name=Jane Doe", 'Accept:text/html', "Authorization:ApiKey 1234", ] # Test quote option args = c.httpie_args('post', quote=True) assert args == [ '--form', 'POST', 'http://localhost/things', '[email protected]', "'name=Jane Doe'", 'Accept:text/html', "'Authorization:ApiKey 1234'", ]
def test_update(): c1 = Context('http://localhost') c1.headers['Accept'] = 'application/json' c1.querystring_params['flag'] = '1' c1.body_params.update({ 'name': 'John Doe', 'email': '*****@*****.**' }) c2 = Context('http://example.com') c2.headers['Content-Type'] = 'text/html' c2.body_params['name'] = 'John Smith' c1.update(c2) assert c1.url == 'http://example.com' assert c1.headers == { 'Accept': 'application/json', 'Content-Type': 'text/html' } assert c1.querystring_params == {'flag': '1'} assert c1.body_params == { 'name': 'John Smith', 'email': '*****@*****.**' }
def test_save_and_load(self): c = Context('http://example.com') c.headers['Authorization'] = 'apikey' c.querystring_params.update({ 'type': 'any', 'offset': '100' }) c.options.update({ '--verify': 'no', '--style': 'default', '--follow': None }) save_context(c) c2 = Context('http://example.com') load_context(c2) self.assertEqual(c2.headers, c.headers) self.assertEqual(c2.querystring_params, c.querystring_params) self.assertEqual(c2.body_params, c.body_params) # Make sure save_context() didn't save '--style' self.assertEqual(c2.options, { '--verify': 'no', '--follow': None })
def test_env_and_source_non_ascii(self): c = Context() c.url = 'http://localhost:8000/api' c.headers.update({ 'Accept': 'text/csv', 'Authorization': 'ApiKey 1234' }) c.querystring_params.update({'page': ['1'], 'limit': ['50']}) c.body_params.update({'name': '許 功蓋'}) c.options.update({'--verify': 'no', '--form': None}) c2 = c.copy() filename = self.make_tempfile() execute('env > %s' % filename, c) execute('rm *', c) self.assertFalse(c.headers) self.assertFalse(c.querystring_params) self.assertFalse(c.body_params) self.assertFalse(c.options) execute('source %s' % filename, c) self.assertEqual(c, c2)
def test_httpie_args_get(): c = Context('http://localhost/things') c.headers.update({'Authorization': 'ApiKey 1234', 'Accept': 'text/html'}) c.querystring_params.update({'page': '2', 'limit': '10'}) args = c.httpie_args('get') assert args == [ 'GET', 'http://localhost/things', 'limit==10', 'page==2', 'Accept:text/html', "Authorization:ApiKey 1234", ]
def execute_redirection(self, command): context = Context('http://httpbin.org') filename = self.make_tempfile() execute('%s > %s' % (command, filename), context) with open(filename, 'rb') as f: return f.read()
def test_save_and_load_context_non_ascii(self): c = Context('http://localhost') c.headers.update({ 'User-Agent': 'Ö', 'Authorization': '中文' }) save_context(c) c = Context('http://0.0.0.0') load_context(c) self.assertEqual(c.url, 'http://localhost') self.assertEqual(c.headers, { 'User-Agent': 'Ö', 'Authorization': '中文' })
def test_httpie_args_get(): c = Context('http://localhost/things') c.headers.update({ 'Authorization': 'ApiKey 1234', 'Accept': 'text/html' }) c.querystring_params.update({ 'page': '2', 'limit': '10' }) args = c.httpie_args('get') assert args == [ 'GET', 'http://localhost/things', 'limit==10', 'page==2', 'Accept:text/html', "Authorization:ApiKey 1234", ]
def test_creation_with_longer_url(): context = Context('http://example.com/a/b/c/index.html') assert context.url == 'http://example.com/a/b/c/index.html' assert context.options == {} assert context.headers == {} assert context.querystring_params == {} assert context.body_params == {} assert not context.should_exit
def setUp(self): self.listener = ExecutionListener({}) self.response = Response() self.response.cookies.update({'username': '******', 'sessionid': 'abcd'}) self.context = Context('http://localhost') self.context.headers['Cookie'] = 'name="John Doe"; sessionid=xyz'
def test_format_raw_json_string_to_http_prompt(): c = Context('http://localhost/things') c.body_json_params.update({ 'bar': 'baz', }) output = t.format_to_http_prompt(c) assert output == ("cd http://localhost/things\n" "bar:='\"baz\"'\n")
def test_creation(): context = Context('http://example.com') assert context.url == 'http://example.com' assert context.options == {} assert context.headers == {} assert context.querystring_params == {} assert context.body_params == {} assert not context.should_exit
def setUp(self): self.patchers = [ ('httpie_main', patch('http_prompt.execution.httpie_main')), ('click', patch('http_prompt.execution.click')), ] for attr_name, patcher in self.patchers: setattr(self, attr_name, patcher.start()) self.context = Context('http://localhost')
def test_different_ports(self): c = Context('http://example.com') c.headers['Authorization'] = 'apikey' c.querystring_params.update({'type': 'any', 'offset': '100'}) c.options.update({ '--verify': 'no', '--style': 'default', '--follow': None }) save_context(c) c2 = Context('http://example.com:8000') load_context(c2) self.assertFalse(c2.headers) self.assertFalse(c2.querystring_params) self.assertFalse(c2.body_params) self.assertFalse(c2.options)
def setUp(self): super(ExecutionTestCase, self).setUp() self.patchers = [('httpie_main', patch('http_prompt.execution.httpie_main')), ('echo_via_pager', patch('http_prompt.output.click.echo_via_pager')), ('secho', patch('http_prompt.execution.click.secho'))] for attr_name, patcher in self.patchers: setattr(self, attr_name, patcher.start()) self.context = Context('http://localhost')
def test_format_to_http_prompt_1(): c = Context('http://localhost/things') c.headers.update({'Authorization': 'ApiKey 1234', 'Accept': 'text/html'}) c.querystring_params.update({'page': '2', 'limit': '10'}) output = t.format_to_http_prompt(c) assert output == ("cd http://localhost/things\n" "limit==10\n" "page==2\n" "Accept:text/html\n" "'Authorization:ApiKey 1234'\n")
def test_save_and_load(self): c = Context('http://example.com') c.headers['Authorization'] = 'apikey' c.querystring_params.update({'type': 'any', 'offset': '100'}) c.options.update({ '--verify': 'no', '--style': 'default', '--follow': None }) save_context(c) c2 = Context('http://example.com') load_context(c2) self.assertEqual(c2.headers, c.headers) self.assertEqual(c2.querystring_params, c.querystring_params) self.assertEqual(c2.body_params, c.body_params) # Make sure save_context() didn't save '--style' self.assertEqual(c2.options, {'--verify': 'no', '--follow': None})
def test_json_serializing(): c = Context('http://example.com/api') c.headers.update({ 'Authorization': 'ApiKey 1234', 'Accept': 'text/html' }) c.options['--form'] = None c.body_params.update({ 'name': 'Jane Doe', 'email': '*****@*****.**' }) json_obj = c.json_obj() c2 = c.copy() c2.headers['Accept'] = 'application/json' c2.body_params['name'] = 'Jane Doe' assert c2 != c c2.load_from_json_obj(json_obj) assert c2 == c
def test_spec(): c = Context('http://localhost', spec={ 'paths': { '/users': { 'get': { 'parameters': [{ 'name': 'username', 'in': 'path' }, { 'name': 'since', 'in': 'query' }, { 'name': 'Accept' }] } }, '/orgs/{org}': { 'get': { 'parameters': [{ 'name': 'org', 'in': 'path' }, { 'name': 'featured', 'in': 'query' }, { 'name': 'X-Foo', 'in': 'header' }] } } } }) assert c.url == 'http://localhost' root_children = list(sorted(c.root.children)) assert len(root_children) == 2 assert root_children[0].name == 'orgs' assert root_children[1].name == 'users' orgs_children = list(sorted(root_children[0].children)) assert len(orgs_children) == 1 org_children = list(sorted(list(orgs_children)[0].children)) assert len(org_children) == 2 assert org_children[0].name == 'X-Foo' assert org_children[1].name == 'featured' users_children = list(sorted(root_children[1].children)) assert len(users_children) == 2 assert users_children[0].name == 'Accept' assert users_children[1].name == 'since'
def test_format_to_httpie_get(): c = Context('http://localhost/things') c.headers.update({'Authorization': 'ApiKey 1234', 'Accept': 'text/html'}) c.querystring_params.update({ 'page': '2', 'limit': '10', 'name': ['alice', 'bob bob'] }) output = t.format_to_httpie(c, method='get') assert output == ("http GET http://localhost/things " "limit==10 name==alice 'name==bob bob' page==2 " "Accept:text/html 'Authorization:ApiKey 1234'\n")
def test_different_ports(self): c = Context('http://example.com') c.headers['Authorization'] = 'apikey' c.querystring_params.update({ 'type': 'any', 'offset': '100' }) c.options.update({ '--verify': 'no', '--style': 'default', '--follow': None }) save_context(c) c2 = Context('http://example.com:8000') load_context(c2) self.assertFalse(c2.headers) self.assertFalse(c2.querystring_params) self.assertFalse(c2.body_params) self.assertFalse(c2.options)
def test_format_to_httpie_post(): c = Context('http://localhost/things') c.headers.update({'Authorization': 'ApiKey 1234', 'Accept': 'text/html'}) c.options.update({'--verify': 'no', '--form': None}) c.body_params.update({ 'full name': 'Jane Doe', 'email': '*****@*****.**' }) output = t.format_to_httpie(c, method='post') assert output == ("http --form --verify=no POST http://localhost/things " "[email protected] 'full name=Jane Doe' " "Accept:text/html 'Authorization:ApiKey 1234'\n")
def test_extract_args_for_httpie_main_post(): c = Context('http://localhost/things') c.headers.update({'Authorization': 'ApiKey 1234', 'Accept': 'text/html'}) c.options.update({'--verify': 'no', '--form': None}) c.body_params.update({ 'full name': 'Jane Doe', 'email': '*****@*****.**' }) args = t.extract_args_for_httpie_main(c, method='post') assert args == [ '--form', '--verify', 'no', 'POST', 'http://localhost/things', '[email protected]', 'full name=Jane Doe', 'Accept:text/html', 'Authorization:ApiKey 1234' ]
def test_extract_raw_json_args_for_httpie_main_post(): c = Context('http://localhost/things') c.body_json_params.update({ 'enabled': True, 'items': ['foo', 'bar'], 'object': { 'id': 10, 'name': 'test' } }) args = t.extract_args_for_httpie_main(c, method='post') assert args == [ 'POST', 'http://localhost/things', 'enabled:=true', 'items:=["foo", "bar"]', 'object:={"id": 10, "name": "test"}' ]
def setUp(self): self.context = Context('http://localhost', spec={ 'paths': { '/users': {}, '/users/{username}': {}, '/users/{username}/events': {}, '/users/{username}/orgs': {}, '/orgs': {}, '/orgs/{org}': {}, '/orgs/{org}/events': {}, '/orgs/{org}/members': {} } }) self.completer = HttpPromptCompleter(self.context) self.completer_event = None
def test_format_to_http_prompt_2(): c = Context('http://localhost/things') c.headers.update({'Authorization': 'ApiKey 1234', 'Accept': 'text/html'}) c.options.update({'--verify': 'no', '--form': None}) c.body_params.update({ 'full name': 'Jane Doe', 'email': '*****@*****.**' }) output = t.format_to_http_prompt(c) assert output == ("--form\n" "--verify=no\n" "cd http://localhost/things\n" "[email protected]\n" "'full name=Jane Doe'\n" "Accept:text/html\n" "'Authorization:ApiKey 1234'\n")
def test_override(): """Parameters can be defined at path level """ c = Context('http://localhost', spec={ 'paths': { '/users': { 'parameters': [{ 'name': 'username', 'in': 'query' }, { 'name': 'Accept', 'in': 'header' }], 'get': { 'parameters': [{ 'name': 'custom1', 'in': 'query' }] }, 'post': { 'parameters': [ { 'name': 'custom2', 'in': 'query' }, ] }, }, '/orgs': { 'parameters': [{ 'name': 'username', 'in': 'query' }, { 'name': 'Accept', 'in': 'header' }], 'get': {} } } }) assert c.url == 'http://localhost' root_children = list(sorted(c.root.children)) # one path assert len(root_children) == 2 assert root_children[0].name == 'orgs' assert root_children[1].name == 'users' orgs_methods = list(sorted(list(root_children)[0].children)) # path parameters are used even if no method parameter assert len(orgs_methods) == 2 assert next(filter(lambda i: i.name == 'username', orgs_methods), None) is not None assert next(filter(lambda i: i.name == 'Accept', orgs_methods), None) is not None users_methods = list(sorted(list(root_children)[1].children)) # path and methods parameters are merged assert len(users_methods) == 4 assert next(filter(lambda i: i.name == 'username', users_methods), None) is not None assert next(filter(lambda i: i.name == 'custom1', users_methods), None) is not None assert next(filter(lambda i: i.name == 'custom2', users_methods), None) is not None assert next(filter(lambda i: i.name == 'Accept', users_methods), None) is not None
def test_creation_with_longer_url(): context = Context('http://example.com/a/b/c/index.html') assert context.url == 'http://example.com/a/b/c/index.html'
def test_copy(): c1 = Context('http://localhost') c2 = c1.copy() assert c1 == c2 assert c1 is not c2
def setUp(self): self.context = Context() self.completer = HttpPromptCompleter(self.context) self.completer_event = None
def execute_pipe(self, command): context = Context('http://httpbin.org') execute(command, context)
def test_extract_httpie_options(): c = Context('http://localhost') c.options.update({'--verify': 'no', '--form': None}) output = t._extract_httpie_options(c, excluded_keys=['--form']) assert output == ['--verify', 'no']