2012-03-15 00:11:49 +01:00
|
|
|
# coding:utf-8
|
2012-03-14 12:17:39 +01:00
|
|
|
import os
|
2012-03-04 12:02:30 +01:00
|
|
|
import sys
|
2012-03-02 01:39:22 +01:00
|
|
|
import unittest
|
2012-03-04 10:50:23 +01:00
|
|
|
import argparse
|
2012-04-25 00:08:40 +02:00
|
|
|
from requests.compat import is_py26
|
|
|
|
import tempfile
|
2012-03-15 00:11:49 +01:00
|
|
|
|
2012-03-14 12:17:39 +01:00
|
|
|
|
|
|
|
TESTS_ROOT = os.path.dirname(__file__)
|
2012-03-14 19:21:47 +01:00
|
|
|
sys.path.insert(0, os.path.realpath(os.path.join(TESTS_ROOT, '..')))
|
2012-03-02 18:35:33 +01:00
|
|
|
from httpie import __main__
|
2012-04-25 01:32:53 +02:00
|
|
|
from httpie import cliparse
|
2012-03-02 01:39:22 +01:00
|
|
|
|
|
|
|
|
2012-03-14 19:21:47 +01:00
|
|
|
TEST_FILE = os.path.join(TESTS_ROOT, 'file.txt')
|
2012-03-15 00:11:49 +01:00
|
|
|
TERMINAL_COLOR_PRESENCE_CHECK = '\x1b['
|
2012-03-04 01:36:39 +01:00
|
|
|
|
|
|
|
|
2012-03-02 01:40:40 +01:00
|
|
|
def http(*args, **kwargs):
|
2012-03-04 01:36:39 +01:00
|
|
|
http_kwargs = {
|
|
|
|
'stdin_isatty': True,
|
|
|
|
'stdout_isatty': False
|
|
|
|
}
|
|
|
|
http_kwargs.update(kwargs)
|
2012-04-25 00:08:40 +02:00
|
|
|
stdout = http_kwargs.setdefault('stdout', tempfile.TemporaryFile())
|
2012-03-04 03:13:50 +01:00
|
|
|
__main__.main(args=args, **http_kwargs)
|
2012-04-25 00:08:40 +02:00
|
|
|
stdout.seek(0)
|
|
|
|
response = stdout.read().decode('utf8')
|
|
|
|
stdout.close()
|
|
|
|
return response
|
2012-03-02 01:40:40 +01:00
|
|
|
|
|
|
|
|
2012-03-04 11:22:21 +01:00
|
|
|
class BaseTest(unittest.TestCase):
|
|
|
|
|
2012-03-15 00:11:49 +01:00
|
|
|
if is_py26:
|
2012-03-04 12:02:30 +01:00
|
|
|
def assertIn(self, member, container, msg=None):
|
2012-03-04 11:22:21 +01:00
|
|
|
self.assert_(member in container, msg)
|
|
|
|
|
2012-03-04 12:02:30 +01:00
|
|
|
def assertNotIn(self, member, container, msg=None):
|
2012-03-04 11:22:21 +01:00
|
|
|
self.assert_(member not in container, msg)
|
|
|
|
|
2012-03-04 12:02:30 +01:00
|
|
|
def assertDictEqual(self, d1, d2, msg=None):
|
2012-03-04 11:58:23 +01:00
|
|
|
self.assertEqual(set(d1.keys()), set(d2.keys()), msg)
|
|
|
|
self.assertEqual(sorted(d1.values()), sorted(d2.values()), msg)
|
2012-03-04 11:22:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TestItemParsing(BaseTest):
|
2012-03-04 10:50:23 +01:00
|
|
|
|
|
|
|
def setUp(self):
|
2012-04-25 01:32:53 +02:00
|
|
|
self.key_value_type = cliparse.KeyValueType(
|
|
|
|
cliparse.SEP_HEADERS,
|
|
|
|
cliparse.SEP_DATA,
|
|
|
|
cliparse.SEP_DATA_RAW_JSON,
|
|
|
|
cliparse.SEP_FILES,
|
2012-03-04 10:50:23 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_invalid_items(self):
|
|
|
|
items = ['no-separator']
|
|
|
|
for item in items:
|
2012-03-04 11:22:21 +01:00
|
|
|
self.assertRaises(argparse.ArgumentTypeError,
|
2012-04-25 01:32:53 +02:00
|
|
|
lambda: self.key_value_type(item))
|
2012-03-04 10:50:23 +01:00
|
|
|
|
2012-04-17 03:28:08 +02:00
|
|
|
def test_escape(self):
|
2012-04-25 01:32:53 +02:00
|
|
|
headers, data, files = cliparse.parse_items([
|
2012-04-17 03:28:08 +02:00
|
|
|
# headers
|
2012-04-25 01:32:53 +02:00
|
|
|
self.key_value_type('foo\\:bar:baz'),
|
|
|
|
self.key_value_type('jack\\@jill:hill'),
|
2012-04-17 03:28:08 +02:00
|
|
|
# data
|
2012-04-25 01:32:53 +02:00
|
|
|
self.key_value_type('baz\\=bar=foo'),
|
2012-04-17 03:28:08 +02:00
|
|
|
# files
|
2012-04-25 01:32:53 +02:00
|
|
|
self.key_value_type('bar\\@baz@%s' % TEST_FILE)
|
2012-04-17 03:28:08 +02:00
|
|
|
])
|
|
|
|
self.assertDictEqual(headers, {
|
|
|
|
'foo:bar': 'baz',
|
|
|
|
'jack@jill': 'hill',
|
|
|
|
})
|
|
|
|
self.assertDictEqual(data, {
|
|
|
|
'baz=bar': 'foo',
|
|
|
|
})
|
|
|
|
self.assertIn('bar@baz', files)
|
|
|
|
|
2012-04-19 01:18:00 +02:00
|
|
|
def test_escape_longsep(self):
|
2012-04-25 01:32:53 +02:00
|
|
|
headers, data, files = cliparse.parse_items([
|
|
|
|
self.key_value_type('bob\\:==foo'),
|
2012-04-19 01:18:00 +02:00
|
|
|
])
|
|
|
|
self.assertDictEqual(data, {
|
|
|
|
'bob:=': 'foo',
|
|
|
|
})
|
2012-04-25 00:08:40 +02:00
|
|
|
|
2012-03-04 10:50:23 +01:00
|
|
|
def test_valid_items(self):
|
2012-04-25 01:32:53 +02:00
|
|
|
headers, data, files = cliparse.parse_items([
|
|
|
|
self.key_value_type('string=value'),
|
|
|
|
self.key_value_type('header:value'),
|
|
|
|
self.key_value_type('list:=["a", 1, {}, false]'),
|
|
|
|
self.key_value_type('obj:={"a": "b"}'),
|
|
|
|
self.key_value_type('eh:'),
|
|
|
|
self.key_value_type('ed='),
|
|
|
|
self.key_value_type('bool:=true'),
|
|
|
|
self.key_value_type('test-file@%s' % TEST_FILE),
|
2012-03-04 10:50:23 +01:00
|
|
|
])
|
|
|
|
self.assertDictEqual(headers, {
|
|
|
|
'header': 'value',
|
|
|
|
'eh': ''
|
|
|
|
})
|
|
|
|
self.assertDictEqual(data, {
|
|
|
|
"ed": "",
|
|
|
|
"string": "value",
|
|
|
|
"bool": True,
|
|
|
|
"list": ["a", 1, {}, False],
|
|
|
|
"obj": {"a": "b"}
|
|
|
|
})
|
2012-03-14 19:21:47 +01:00
|
|
|
self.assertIn('test-file', files)
|
2012-03-04 10:50:23 +01:00
|
|
|
|
2012-03-02 01:40:40 +01:00
|
|
|
|
2012-03-04 11:22:21 +01:00
|
|
|
class TestHTTPie(BaseTest):
|
2012-03-02 01:40:40 +01:00
|
|
|
|
|
|
|
def test_get(self):
|
|
|
|
http('GET', 'http://httpbin.org/get')
|
|
|
|
|
2012-03-15 00:11:49 +01:00
|
|
|
def test_verbose(self):
|
|
|
|
r = http('--verbose', 'GET', 'http://httpbin.org/get', 'test-header:__test__')
|
|
|
|
self.assertEqual(r.count('__test__'), 2)
|
|
|
|
|
2012-06-15 16:47:55 +02:00
|
|
|
def test_verbose_form(self):
|
|
|
|
# https://github.com/jkbr/httpie/issues/53
|
|
|
|
r = http('--verbose', '--form', 'POST', 'http://httpbin.org/post', 'foo=bar', 'baz=bar')
|
|
|
|
self.assertIn('foo=bar&baz=bar', r)
|
|
|
|
|
2012-03-02 01:40:40 +01:00
|
|
|
def test_json(self):
|
|
|
|
response = http('POST', 'http://httpbin.org/post', 'foo=bar')
|
|
|
|
self.assertIn('"foo": "bar"', response)
|
2012-04-14 21:13:53 +02:00
|
|
|
response2 = http('-j', 'GET', 'http://httpbin.org/headers')
|
|
|
|
self.assertIn('"Accept": "application/json"', response2)
|
|
|
|
response3 = http('-j', 'GET', 'http://httpbin.org/headers', 'Accept:application/xml')
|
|
|
|
self.assertIn('"Accept": "application/xml"', response3)
|
2012-03-02 01:40:40 +01:00
|
|
|
|
|
|
|
def test_form(self):
|
2012-03-14 12:17:39 +01:00
|
|
|
response = http('--form', 'POST', 'http://httpbin.org/post', 'foo=bar')
|
2012-03-02 01:40:40 +01:00
|
|
|
self.assertIn('"foo": "bar"', response)
|
|
|
|
|
|
|
|
def test_headers(self):
|
|
|
|
response = http('GET', 'http://httpbin.org/headers', 'Foo:bar')
|
|
|
|
self.assertIn('"User-Agent": "HTTPie', response)
|
|
|
|
self.assertIn('"Foo": "bar"', response)
|
|
|
|
|
2012-06-17 19:46:56 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestHTTPieSuggestion(BaseTest):
|
|
|
|
def test_get(self):
|
2012-06-16 18:08:31 +02:00
|
|
|
http('http://httpbin.org/get')
|
|
|
|
|
2012-06-17 19:46:56 +02:00
|
|
|
def test_post(self):
|
|
|
|
r = http('http://httpbin.org/post', 'hello=world')
|
|
|
|
self.assertIn('"hello": "world"', r)
|
|
|
|
|
|
|
|
def test_verbose(self):
|
|
|
|
r = http('--verbose', 'http://httpbin.org/get', 'test-header:__test__')
|
|
|
|
self.assertEqual(r.count('__test__'), 2)
|
|
|
|
|
|
|
|
def test_verbose_form(self):
|
|
|
|
r = http('--verbose', '--form', 'http://httpbin.org/post', 'foo=bar', 'baz=bar')
|
|
|
|
self.assertIn('foo=bar&baz=bar', r)
|
|
|
|
|
|
|
|
def test_json(self):
|
|
|
|
response = http('http://httpbin.org/post', 'foo=bar')
|
|
|
|
self.assertIn('"foo": "bar"', response)
|
|
|
|
response2 = http('-j', 'GET', 'http://httpbin.org/headers')
|
|
|
|
self.assertIn('"Accept": "application/json"', response2)
|
|
|
|
response3 = http('-j', 'GET', 'http://httpbin.org/headers', 'Accept:application/xml')
|
|
|
|
self.assertIn('"Accept": "application/xml"', response3)
|
|
|
|
|
|
|
|
def test_form(self):
|
|
|
|
response = http('--form', 'http://httpbin.org/post', 'foo=bar')
|
|
|
|
self.assertIn('"foo": "bar"', response)
|
|
|
|
|
|
|
|
def test_headers(self):
|
|
|
|
response = http('http://httpbin.org/headers', 'Foo:bar')
|
|
|
|
self.assertIn('"User-Agent": "HTTPie', response)
|
|
|
|
self.assertIn('"Foo": "bar"', response)
|
2012-06-16 18:08:31 +02:00
|
|
|
|
2012-03-02 01:40:40 +01:00
|
|
|
|
2012-03-04 11:22:21 +01:00
|
|
|
class TestPrettyFlag(BaseTest):
|
2012-03-04 01:36:39 +01:00
|
|
|
"""Test the --pretty / --ugly flag handling."""
|
|
|
|
|
|
|
|
def test_pretty_enabled_by_default(self):
|
|
|
|
r = http('GET', 'http://httpbin.org/get', stdout_isatty=True)
|
2012-03-15 00:11:49 +01:00
|
|
|
self.assertIn(TERMINAL_COLOR_PRESENCE_CHECK, r)
|
2012-03-04 01:36:39 +01:00
|
|
|
|
|
|
|
def test_pretty_enabled_by_default_unless_stdin_redirected(self):
|
|
|
|
r = http('GET', 'http://httpbin.org/get', stdout_isatty=False)
|
2012-03-15 00:11:49 +01:00
|
|
|
self.assertNotIn(TERMINAL_COLOR_PRESENCE_CHECK, r)
|
2012-03-04 01:36:39 +01:00
|
|
|
|
|
|
|
def test_force_pretty(self):
|
2012-03-14 12:17:39 +01:00
|
|
|
r = http('--pretty', 'GET', 'http://httpbin.org/get', stdout_isatty=False)
|
2012-03-15 00:11:49 +01:00
|
|
|
self.assertIn(TERMINAL_COLOR_PRESENCE_CHECK, r)
|
2012-03-04 01:36:39 +01:00
|
|
|
|
|
|
|
def test_force_ugly(self):
|
2012-03-14 12:17:39 +01:00
|
|
|
r = http('--ugly', 'GET', 'http://httpbin.org/get', stdout_isatty=True)
|
2012-03-15 00:11:49 +01:00
|
|
|
self.assertNotIn(TERMINAL_COLOR_PRESENCE_CHECK, r)
|
2012-03-04 01:36:39 +01:00
|
|
|
|
|
|
|
|
2012-03-14 12:17:39 +01:00
|
|
|
class TestFileUpload(BaseTest):
|
|
|
|
|
|
|
|
def test_non_existent_file_raises_parse_error(self):
|
2012-04-25 01:32:53 +02:00
|
|
|
self.assertRaises(cliparse.ParseError, http,
|
2012-03-14 12:17:39 +01:00
|
|
|
'--form', '--traceback',
|
|
|
|
'POST', 'http://httpbin.org/post',
|
|
|
|
'foo@/__does_not_exist__')
|
|
|
|
|
|
|
|
def test_upload_ok(self):
|
|
|
|
r = http('--form', 'POST', 'http://httpbin.org/post',
|
2012-03-14 19:21:47 +01:00
|
|
|
'test-file@%s' % TEST_FILE)
|
2012-03-14 12:17:39 +01:00
|
|
|
self.assertIn('"test-file": "__test_file_content__', r)
|
|
|
|
|
|
|
|
|
2012-04-11 13:47:47 +02:00
|
|
|
class TestAuth(BaseTest):
|
|
|
|
|
|
|
|
def test_basic_auth(self):
|
|
|
|
r = http('--auth', 'user:password',
|
|
|
|
'GET', 'httpbin.org/basic-auth/user/password')
|
|
|
|
self.assertIn('"authenticated": true', r)
|
|
|
|
self.assertIn('"user": "user"', r)
|
|
|
|
|
|
|
|
def test_digest_auth(self):
|
|
|
|
r = http('--auth-type=digest', '--auth', 'user:password',
|
|
|
|
'GET', 'httpbin.org/digest-auth/auth/user/password')
|
|
|
|
self.assertIn('"authenticated": true', r)
|
|
|
|
self.assertIn('"user": "user"', r)
|
|
|
|
|
|
|
|
|
2012-03-02 01:40:40 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|