def test_httpie_with_full_https_url(self): execute('httpie post https://httpbin.org/post name=alice', self.context) self.click.echo.assert_called_with( 'http POST https://httpbin.org/post name=alice') self.assertEqual(self.context.url, 'http://localhost') self.assertFalse(self.context.body_params)
def test_get(self): execute('get > data.json', self.context) self.assert_httpie_main_called_with(['GET', 'http://localhost']) env = self.httpie_main.call_args[1]['env'] self.assertFalse(env.stdout_isatty) self.assertEqual(env.stdout.fp.name, 'data.json')
def test_body_reset(self): self.context.body_params.update({ 'first_name': 'alice', 'last_name': 'bryne' }) execute('rm -b *', self.context) self.assertFalse(self.context.body_params)
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_long_short_option_names_mixed(self): execute('--style=default -j --stream', self.context) self.assertEqual(self.context.options, { '-j': None, '--stream': None, '--style': 'default' })
def test_headers_reset(self): self.context.headers.update({ 'Content-Type': 'text/html', 'Accept': 'application/json' }) execute('rm -h *', self.context) self.assertFalse(self.context.headers)
def test_multi_querystring(self): execute('name==john name==doe', self.context) self.assertEqual(self.context.querystring_params, {'name': ['john', 'doe']}) execute('name==jane', self.context) self.assertEqual(self.context.querystring_params, {'name': ['jane']})
def test_post_with_relative_path(self): self.context.url = 'http://localhost/api/v3' execute('post ../v2/movie id=8', self.context) self.assert_httpie_main_called_with([ 'POST', 'http://localhost/api/v2/movie', 'id=8']) self.assertFalse(self.context.body_params) self.assertEqual(self.context.url, 'http://localhost/api/v3')
def test_long_short_option_names_mixed(self): execute('--style=default -j --stream', self.context) self.assertEqual(self.context.options, { '-j': None, '--stream': None, '--style': 'default' })
def test_httpie_with_full_https_url(self): execute('httpie post https://httpbin.org/post name=alice', self.context) self.click.echo.assert_called_with( 'http POST https://httpbin.org/post name=alice') self.assertEqual(self.context.url, 'http://localhost') self.assertFalse(self.context.body_params)
def test_querystring_reset(self): self.context.querystring_params.update({ 'first_name': 'alice', 'last_name': 'bryne' }) execute('rm -q *', self.context) self.assertFalse(self.context.querystring_params)
def test_post_with_relative_path(self): self.context.url = 'http://localhost/api/v3' execute('post ../v2/movie id=8', self.context) self.assert_httpie_main_called_with([ 'POST', 'http://localhost/api/v2/movie', 'id=8']) self.assertFalse(self.context.body_params) self.assertEqual(self.context.url, 'http://localhost/api/v3')
def test_option_and_body_param(self): execute('--form name="John Doe"', self.context) self.assertEqual(self.context.options, { '--form': None }) self.assertEqual(self.context.body_params, { 'name': 'John Doe' })
def test_env_grep(self): self.context.body_params = { 'username': '******', 'name': 'Jane', 'password': '******' } execute('env | grep name', self.context) self.assert_stdout('name=Jane\nusername=jane\n')
def test_option_and_body_param(self): execute('--form name="John Doe"', self.context) self.assertEqual(self.context.options, { '--form': None }) self.assertEqual(self.context.body_params, { 'name': 'John Doe' })
def test_spaces(self): execute(' \t \t ', self.context) self.assertEqual(self.context.url, 'http://localhost') self.assertFalse(self.context.options) self.assertFalse(self.context.headers) self.assertFalse(self.context.querystring_params) self.assertFalse(self.context.body_params) self.assertFalse(self.context.should_exit)
def test_long_option_names_with_its_prefix(self): execute('--auth-type basic --auth user:pass --session user --session-read-only user', self.context) self.assertEqual(self.context.options, { '--auth-type': 'basic', '--auth': 'user:pass', '--session-read-only': 'user', '--session': 'user' })
def test_spaces(self): execute(' \t \t ', self.context) self.assertEqual(self.context.url, 'http://localhost') self.assertFalse(self.context.options) self.assertFalse(self.context.headers) self.assertFalse(self.context.querystring_params) self.assertFalse(self.context.body_params) self.assertFalse(self.context.should_exit)
def test_env(self): execute('env', self.context) self.assert_stdout("--form\n--verify=no\n" "cd http://localhost:8000/api\n" "limit==50\npage==1\n" "'name=John Doe'\n" "Accept:text/csv\n" "'Authorization:ApiKey 1234'\n")
def test_env_non_ascii(self): self.context.body_params['name'] = '許 功蓋' execute('env', self.context) self.assert_stdout("--form\n--verify=no\n" "cd http://localhost:8000/api\n" "limit==50\npage==1\n" "'name=許 功蓋'\n" "Accept:text/csv\n" "'Authorization:ApiKey 1234'\n")
def test_unquoted_header(self): execute("`echo 'X-Greeting'`:`echo 'hello world'`", self.context) if sys.platform == 'win32': expected_key = "'X-Greeting'" expected_value = "'hello world'" else: expected_key = 'X-Greeting' expected_value = 'hello world' self.assertEqual(self.context.headers, {expected_key: expected_value})
def test_multi_querystring(self): execute('name==john name==doe', self.context) self.assertEqual(self.context.querystring_params, { 'name': ['john', 'doe'] }) execute('name==jane', self.context) self.assertEqual(self.context.querystring_params, { 'name': ['jane'] })
def test_long_option_names_with_its_prefix(self): execute( '--auth-type basic --auth user:pass --session user ' '--session-read-only user', self.context) self.assertEqual( self.context.options, { '--auth-type': 'basic', '--auth': 'user:pass', '--session-read-only': 'user', '--session': 'user' })
def test_httpie_with_quotes(self): execute(r'httpie post http://httpbin.org/post name="john doe" ' r"apikey==abc\ 123 'Authorization:ApiKey 1234'", self.context) self.click.echo.assert_called_with( "http POST http://httpbin.org/post 'apikey==abc 123' " "'name=john doe' 'Authorization:ApiKey 1234'") self.assertEqual(self.context.url, 'http://localhost') self.assertFalse(self.context.body_params) self.assertFalse(self.context.querystring_params) self.assertFalse(self.context.headers)
def test_httpie_with_quotes(self): execute(r'httpie post http://httpbin.org/post name="john doe" ' r"apikey==abc\ 123 'Authorization:ApiKey 1234'", self.context) self.click.echo.assert_called_with( "http POST http://httpbin.org/post 'apikey==abc 123' " "'name=john doe' 'Authorization:ApiKey 1234'") self.assertEqual(self.context.url, 'http://localhost') self.assertFalse(self.context.body_params) self.assertFalse(self.context.querystring_params) self.assertFalse(self.context.headers)
def test_httpie_redirect_write(self): filename = self.make_tempfile() # Write something first to make sure it's a full overwrite with open(filename, 'w') as f: f.write('hello world\n') execute('httpie > %s' % filename, self.context) with open(filename) as f: content = f.read() self.assertEqual(content, 'http http://localhost\n')
def test_httpie_redirect_append_quoted_filename(self): filename = self.make_tempfile() # Write something first to make sure it's an append with open(filename, 'w') as f: f.write('hello world\n') execute("httpie >> '%s'" % filename, self.context) with open(filename) as f: content = f.read() self.assertEqual(content, 'hello world\nhttp http://localhost\n')
def test_httpie_redirect_write_with_args(self): filename = self.make_tempfile() # Write something first to make sure it's a full overwrite with open(filename, 'w') as f: f.write('hello world\n') execute('httpie post http://example.org name=john > %s' % filename, self.context) with open(filename) as f: content = f.read() self.assertEqual(content, 'http POST http://example.org name=john\n')
def test_exec_quoted_filename(self): execute("exec '%s'" % self.filename, self.context) self.assertEqual(self.context.url, 'http://localhost:8000/api/v2/user') self.assertEqual(self.context.headers, { 'Authorization': 'ApiKey 5678', 'Language': 'en' }) self.assertEqual(self.context.querystring_params, {'limit': ['25']}) self.assertEqual(self.context.body_params, { 'name': 'Jane Doe', 'username': '******' })
def test_exec_non_existing_file(self): c = self.context.copy() execute('exec no_such_file.txt', self.context) self.assertEqual(self.context, c) # Try to get the error message when opening a non-existing file try: with io.open('no_such_file.txt'): pass except IOError as err: err_msg = str(err) else: assert False, 'what?! no_such_file.txt exists!' self.assert_stderr(err_msg)
def test_source_non_existing_file(self): c = self.context.copy() execute('source no_such_file.txt', self.context) self.assertEqual(self.context, c) # Expect the error message would be the same as when we open the # non-existing file try: with io.open('no_such_file.txt'): pass except IOError as err: err_msg = str(err) else: assert False, 'what?! no_such_file.txt exists!' self.assert_stderr(err_msg)
def test_exec_escaped_filename(self): new_filename = self.filename + r' copy' shutil.copyfile(self.filename, new_filename) new_filename = new_filename.replace(' ', r'\ ') execute('exec %s' % new_filename, self.context) self.assertEqual(self.context.url, 'http://localhost:8000/api/v2/user') self.assertEqual(self.context.headers, { 'Authorization': 'ApiKey 5678', 'Language': 'en' }) self.assertEqual(self.context.querystring_params, {'limit': ['25']}) self.assertEqual(self.context.body_params, { 'name': 'Jane Doe', 'username': '******' })
def test_source_quoted_filename(self): execute('source "%s"' % self.filename, self.context) self.assertEqual(self.context.url, 'http://localhost:8000/api/v2/user') self.assertEqual(self.context.headers, { 'Accept': 'text/csv', 'Authorization': 'ApiKey 5678', 'Language': 'en' }) self.assertEqual(self.context.querystring_params, { 'page': ['1'], 'limit': ['25'] }) self.assertEqual(self.context.body_params, { 'name': 'Jane Doe', 'username': '******' }) self.assertEqual(self.context.options, {'--verify': 'no'})
def test_env_write_to_quoted_filename(self): filename = self.make_tempfile() # Write something first to make sure it's a full overwrite with open(filename, 'w') as f: f.write('hello world\n') execute("env > '%s'" % filename, self.context) with open(filename) as f: content = f.read() self.assertEqual( content, "--form\n--verify=no\n" "cd http://localhost:8000/api\n" "limit==50\npage==1\n" "'name=John Doe'\n" "Accept:text/csv\n" "'Authorization:ApiKey 1234'\n")
def test_env_non_ascii_and_write_to_file(self): filename = self.make_tempfile() # write something first to make sure it's a full overwrite with open(filename, 'w') as f: f.write('hello world\n') self.context.body_params['name'] = '許 功蓋' execute('env > %s' % filename, self.context) with io.open(filename, encoding='utf-8') as f: content = f.read() self.assertEqual( content, "--form\n--verify=no\n" "cd http://localhost:8000/api\n" "limit==50\npage==1\n" "'name=許 功蓋'\n" "Accept:text/csv\n" "'Authorization:ApiKey 1234'\n")
def test_mixed(self): execute(' --form name="John Doe" password=1234\ 5678 ' 'User-Agent:HTTP\ Prompt -a \'john:1234 5678\' ' '"Accept:text/html" ', self.context) self.assertEqual(self.context.options, { '--form': None, '-a': 'john:1234 5678' }) self.assertEqual(self.context.headers, { 'User-Agent': 'HTTP Prompt', 'Accept': 'text/html' }) self.assertEqual(self.context.options, { '--form': None, '-a': 'john:1234 5678' }) self.assertEqual(self.context.body_params, { 'name': 'John Doe', 'password': '******' })
def test_mixed(self): execute(' --form name="John Doe" password=1234\ 5678 ' 'User-Agent:HTTP\ Prompt -a \'john:1234 5678\' ' '"Accept:text/html" ', self.context) self.assertEqual(self.context.options, { '--form': None, '-a': 'john:1234 5678' }) self.assertEqual(self.context.headers, { 'User-Agent': 'HTTP Prompt', 'Accept': 'text/html' }) self.assertEqual(self.context.options, { '--form': None, '-a': 'john:1234 5678' }) self.assertEqual(self.context.body_params, { 'name': 'John Doe', 'password': '******' })
def test_reset(self): self.context.options.update({'--form': None, '--verify': 'no'}) self.context.headers.update({ 'Accept': 'dontcare', 'Content-Type': 'dontcare' }) self.context.querystring_params.update({ 'name': 'dontcare', 'email': 'dontcare' }) self.context.body_params.update({ 'name': 'dontcare', 'email': 'dontcare' }) execute('rm *', self.context) self.assertFalse(self.context.options) self.assertFalse(self.context.headers) self.assertFalse(self.context.querystring_params) self.assertFalse(self.context.body_params)
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_dot_dot(self): execute('cd api/v1', self.context) self.assertEqual(self.context.url, 'http://localhost/api/v1') execute('cd ..', self.context) self.assertEqual(self.context.url, 'http://localhost/api') execute('cd ../rest/api', self.context) self.assertEqual(self.context.url, 'http://localhost/rest/api')
def test_reset(self): self.context.options.update({ '--form': None, '--verify': 'no' }) self.context.headers.update({ 'Accept': 'dontcare', 'Content-Type': 'dontcare' }) self.context.querystring_params.update({ 'name': 'dontcare', 'email': 'dontcare' }) self.context.body_params.update({ 'name': 'dontcare', 'email': 'dontcare' }) execute('rm *', self.context) self.assertFalse(self.context.options) self.assertFalse(self.context.headers) self.assertFalse(self.context.querystring_params) self.assertFalse(self.context.body_params)
def test_url_with_trailing_slash(self): self.context.url = 'http://localhost/' execute('cd api', self.context) self.assertEqual(self.context.url, 'http://localhost/api') execute('cd v2/', self.context) self.assertEqual(self.context.url, 'http://localhost/api/v2/') execute('cd /objects/', self.context) self.assertEqual(self.context.url, 'http://localhost/objects/')
def test_dot_dot(self): execute('cd api/v1', self.context) self.assertEqual(self.context.url, 'http://localhost/api/v1') execute('cd ..', self.context) self.assertEqual(self.context.url, 'http://localhost/api') # If dot-dot has a trailing slash, the resulting URL should have a # trailing slash execute('cd ../rest/api/', self.context) self.assertEqual(self.context.url, 'http://localhost/rest/api/')
def test_many_levels(self): execute('cd api/v2/movie/50', self.context) self.assertEqual(self.context.url, 'http://localhost/api/v2/movie/50')
def test_change_base(self): execute('cd //example.com/api', self.context) self.assertEqual(self.context.url, 'http://example.com/api')
def test_exit_with_spaces(self): execute(' exit ', self.context) self.assertTrue(self.context.should_exit)
def test_single_level(self): execute('cd api', self.context) self.assertEqual(self.context.url, 'http://localhost/api')
def test_help_with_spaces(self): execute(' help ', self.context) help_text = self.click.echo_via_pager.call_args[0][0] self.assertTrue(help_text.startswith('Commands:\n\tcd'))
def test_exit(self): execute('exit', self.context) self.assertTrue(self.context.should_exit)
def test_post(self): execute('post page==1', self.context) self.assert_httpie_main_called_with(['POST', 'http://localhost', 'page==1']) self.assertFalse(self.context.querystring_params)
def test_delete_uppercase(self): execute('DELETE', self.context) self.assert_httpie_main_called_with(['DELETE', 'http://localhost'])
def test_httpie_without_args(self): execute('httpie', self.context) self.click.echo.assert_called_with('http http://localhost')
def test_httpie_with_absolute_path(self): execute('httpie post /api name=alice', self.context) self.click.echo.assert_called_with( 'http POST http://localhost/api name=alice') self.assertFalse(self.context.body_params)
def test_post_with_absolute_path(self): execute('post /api/v3 name=bob', self.context) self.assert_httpie_main_called_with(['POST', 'http://localhost/api/v3', 'name=bob']) self.assertFalse(self.context.body_params) self.assertEqual(self.context.url, 'http://localhost')
def test_head_uppercase(self): execute('HEAD', self.context) self.assert_httpie_main_called_with(['HEAD', 'http://localhost'])
def test_patch_uppercase(self): execute('PATCH', self.context) self.assert_httpie_main_called_with(['PATCH', 'http://localhost'])
def test_root(self): execute('cd /api/v2', self.context) self.assertEqual(self.context.url, 'http://localhost/api/v2') execute('cd /index.html', self.context) self.assertEqual(self.context.url, 'http://localhost/index.html')
def test_get(self): execute('get', self.context) self.assert_httpie_main_called_with(['GET', 'http://localhost'])
def test_post_with_full_https_url(self): execute('post https://httpbin.org/post id=9', self.context) self.assert_httpie_main_called_with([ 'POST', 'https://httpbin.org/post', 'id=9']) self.assertFalse(self.context.body_params) self.assertEqual(self.context.url, 'http://localhost')
def test_post_uppercase(self): execute('POST content=text', self.context) self.assert_httpie_main_called_with(['POST', 'http://localhost', 'content=text']) self.assertFalse(self.context.body_params)