mirror of
https://github.com/httpie/cli.git
synced 2024-11-22 07:43:20 +01:00
Added first tests.
This commit is contained in:
parent
94c605fac1
commit
bb653bf1a9
14
README.md
14
README.md
@ -35,10 +35,10 @@ The data to be sent can also be passed via `stdin`:
|
|||||||
|
|
||||||
Most of the flags mirror the arguments you would use with `requests.request`. See `http -h`:
|
Most of the flags mirror the arguments you would use with `requests.request`. See `http -h`:
|
||||||
|
|
||||||
$ http -h
|
usage: httpie.py [-h] [--json | --form] [--traceback] [--ugly]
|
||||||
usage: http [-h] [--json | --form] [--traceback] [--ugly] [--headers | --body]
|
[--headers | --body] [--style STYLE] [--auth AUTH]
|
||||||
[--auth AUTH] [--verify VERIFY] [--proxy PROXY]
|
[--verify VERIFY] [--proxy PROXY] [--allow-redirects]
|
||||||
[--allow-redirects] [--file PATH] [--timeout TIMEOUT]
|
[--file PATH] [--timeout TIMEOUT]
|
||||||
method URL [item [item ...]]
|
method URL [item [item ...]]
|
||||||
|
|
||||||
HTTPie - cURL for humans.
|
HTTPie - cURL for humans.
|
||||||
@ -62,6 +62,11 @@ Most of the flags mirror the arguments you would use with `requests.request`. Se
|
|||||||
--ugly, -u Do not prettify the response.
|
--ugly, -u Do not prettify the response.
|
||||||
--headers, -t Print only the response headers.
|
--headers, -t Print only the response headers.
|
||||||
--body, -b Print only the response body.
|
--body, -b Print only the response body.
|
||||||
|
--style STYLE, -s STYLE
|
||||||
|
Output coloring style, one of autumn, borland, bw,
|
||||||
|
colorful, default, emacs, friendly, fruity, manni,
|
||||||
|
monokai, murphy, native, pastie, perldoc, solarized,
|
||||||
|
tango, trac, vim, vs. Defaults to solarized.
|
||||||
--auth AUTH, -a AUTH username:password
|
--auth AUTH, -a AUTH username:password
|
||||||
--verify VERIFY Set to "yes" to check the host's SSL certificate. You
|
--verify VERIFY Set to "yes" to check the host's SSL certificate. You
|
||||||
can also pass the path to a CA_BUNDLE file for private
|
can also pass the path to a CA_BUNDLE file for private
|
||||||
@ -74,4 +79,3 @@ Most of the flags mirror the arguments you would use with `requests.request`. Se
|
|||||||
--file PATH File to multipart upload
|
--file PATH File to multipart upload
|
||||||
--timeout TIMEOUT Float describes the timeout of the request (Use
|
--timeout TIMEOUT Float describes the timeout of the request (Use
|
||||||
socket.setdefaulttimeout() as fallback).
|
socket.setdefaulttimeout() as fallback).
|
||||||
|
|
||||||
|
@ -66,8 +66,10 @@ group_only.add_argument('--headers', '-t', dest='print_body',
|
|||||||
group_only.add_argument('--body', '-b', dest='print_headers',
|
group_only.add_argument('--body', '-b', dest='print_headers',
|
||||||
action='store_false', default=True,
|
action='store_false', default=True,
|
||||||
help='Print only the response body.')
|
help='Print only the response body.')
|
||||||
parser.add_argument('--style', '-s', dest='style', default='solarized',
|
parser.add_argument('--style', '-s', dest='style', default='solarized', metavar='STYLE',
|
||||||
help='Output coloring style')
|
choices=pretty.AVAILABLE_STYLES,
|
||||||
|
help='Output coloring style, one of %s. Defaults to solarized.'
|
||||||
|
% ', '.join(sorted(pretty.AVAILABLE_STYLES)))
|
||||||
|
|
||||||
|
|
||||||
# ``requests.request`` keyword arguments.
|
# ``requests.request`` keyword arguments.
|
||||||
@ -104,8 +106,13 @@ parser.add_argument('items', metavar='item', nargs='*',
|
|||||||
help='HTTP header (key:value) or data field (key=value)')
|
help='HTTP header (key:value) or data field (key=value)')
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main(args=None,
|
||||||
args = parser.parse_args()
|
stdin=sys.stdin,
|
||||||
|
stdin_isatty=sys.stdout.isatty(),
|
||||||
|
stdout=sys.stdout,
|
||||||
|
stdout_isatty=sys.stdout.isatty()):
|
||||||
|
|
||||||
|
args = parser.parse_args(args if args is not None else sys.argv[1:])
|
||||||
|
|
||||||
# Parse request headers and data from the command line.
|
# Parse request headers and data from the command line.
|
||||||
headers = CaseInsensitiveDict()
|
headers = CaseInsensitiveDict()
|
||||||
@ -115,18 +122,18 @@ def main():
|
|||||||
if item.sep == SEP_COMMON:
|
if item.sep == SEP_COMMON:
|
||||||
target = headers
|
target = headers
|
||||||
else:
|
else:
|
||||||
if not sys.stdin.isatty():
|
if not stdin_isatty:
|
||||||
parser.error('Request body (stdin) and request '
|
parser.error('Request body (stdin) and request '
|
||||||
'data (key=value) cannot be mixed.')
|
'data (key=value) cannot be mixed.')
|
||||||
target = data
|
target = data
|
||||||
target[item.key] = item.value
|
target[item.key] = item.value
|
||||||
|
|
||||||
if not sys.stdin.isatty():
|
if not stdin_isatty:
|
||||||
data = sys.stdin.read()
|
data = stdin.read()
|
||||||
|
|
||||||
# JSON/Form content type.
|
# JSON/Form content type.
|
||||||
if args.json or (not args.form and data):
|
if args.json or (not args.form and data):
|
||||||
if sys.stdin.isatty():
|
if stdin_isatty:
|
||||||
data = json.dumps(data)
|
data = json.dumps(data)
|
||||||
if 'Content-Type' not in headers and (data or args.json):
|
if 'Content-Type' not in headers and (data or args.json):
|
||||||
headers['Content-Type'] = TYPE_JSON
|
headers['Content-Type'] = TYPE_JSON
|
||||||
@ -167,19 +174,21 @@ def main():
|
|||||||
response.content.decode(encoding) if response.content else u''
|
response.content.decode(encoding) if response.content else u''
|
||||||
)
|
)
|
||||||
|
|
||||||
if args.prettify and sys.stdout.isatty():
|
if args.prettify and stdout_isatty:
|
||||||
|
prettify = pretty.PrettyHttp(args.style)
|
||||||
if args.print_headers:
|
if args.print_headers:
|
||||||
status_line = pretty.prettify_http(status_line, args.style).strip()
|
status_line = prettify.headers(status_line).strip()
|
||||||
headers = pretty.prettify_http(headers, args.style)
|
headers = prettify.headers(headers)
|
||||||
if args.print_body:
|
if args.print_body and 'content-type' in response.headers:
|
||||||
body = pretty.prettify_body(body,
|
body = prettify.body(body, response.headers['content-type'])
|
||||||
response.headers['content-type'], args.style)
|
|
||||||
|
|
||||||
if args.print_headers:
|
if args.print_headers:
|
||||||
print status_line
|
stdout.write(status_line)
|
||||||
print headers
|
stdout.write('\n')
|
||||||
|
stdout.write(headers)
|
||||||
|
stdout.write('\n')
|
||||||
if args.print_body:
|
if args.print_body:
|
||||||
print body
|
stdout.write(body)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
33
tests.py
33
tests.py
@ -1,3 +1,36 @@
|
|||||||
import unittest
|
import unittest
|
||||||
|
from StringIO import StringIO
|
||||||
|
from httpie import httpie
|
||||||
|
|
||||||
|
|
||||||
|
def http(*args, **kwargs):
|
||||||
|
stdout = StringIO()
|
||||||
|
httpie.main(args=args, stdout=stdout,
|
||||||
|
stdin_isatty=True,
|
||||||
|
stdout_isatty=False)
|
||||||
|
return stdout.getvalue()
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: moar!
|
||||||
|
|
||||||
|
class TestHTTPie(unittest.TestCase):
|
||||||
|
|
||||||
|
def test_get(self):
|
||||||
|
http('GET', 'http://httpbin.org/get')
|
||||||
|
|
||||||
|
def test_json(self):
|
||||||
|
response = http('POST', 'http://httpbin.org/post', 'foo=bar')
|
||||||
|
self.assertIn('"foo": "bar"', response)
|
||||||
|
|
||||||
|
def test_form(self):
|
||||||
|
response = http('POST', '--form', 'http://httpbin.org/post', 'foo=bar')
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
|
Loading…
Reference in New Issue
Block a user