mirror of
https://github.com/httpie/cli.git
synced 2025-08-10 08:08:41 +02:00
Compare commits
28 Commits
Author | SHA1 | Date | |
---|---|---|---|
1ce02ebbd5 | |||
8a7f4c0d6e | |||
f29c458611 | |||
2d7df0afb4 | |||
16a7d0a719 | |||
0cffda86f6 | |||
f42ee6da85 | |||
deeb7cbbac | |||
12f2fb4a92 | |||
489bd64295 | |||
9b8cb42efd | |||
2036337a53 | |||
5ca8bec9ff | |||
df79792fd9 | |||
5a82c79fdf | |||
05b321d38f | |||
681b652bf9 | |||
85b3a016eb | |||
929ead437a | |||
36de166b28 | |||
7bc2de2f9d | |||
cb7ead04e2 | |||
cd2ca41f48 | |||
c71de95505 | |||
6ab03b21b4 | |||
50196be0f2 | |||
41d640920c | |||
3179631603 |
49
README.rst
49
README.rst
@ -28,7 +28,9 @@ Or, you can install the **development version** directly from GitHub:
|
||||
|
||||
pip install -U https://github.com/jkbr/httpie/tarball/master
|
||||
|
||||
HTTPie should also
|
||||
|
||||
There are packages available for `Ubuntu <http://packages.ubuntu.com/quantal/httpie>`_ and `Debian <http://packages.debian.org/wheezy/httpie>`_.
|
||||
|
||||
|
||||
Usage
|
||||
-----
|
||||
@ -73,7 +75,7 @@ The following request is issued::
|
||||
{"name": "John", "email": "john@example.org", "age": 29}
|
||||
|
||||
|
||||
It can easily be changed to a 'form' request using the ``-f`` (or ``--form``) flag, which produces::
|
||||
It can easily be changed to a **form** request using the ``-f`` (or ``--form``) flag, which produces::
|
||||
|
||||
PATCH /person/1 HTTP/1.1
|
||||
User-Agent: HTTPie/0.1
|
||||
@ -82,7 +84,7 @@ It can easily be changed to a 'form' request using the ``-f`` (or ``--form``) fl
|
||||
|
||||
age=29&name=John&email=john%40example.org
|
||||
|
||||
It is also possible to send ``multipart/form-data`` requests, i.e., to simulate a file upload form submission. It is done using the ``--form`` / ``-f`` flag and passing one or more file fields::
|
||||
It is also possible to send ``multipart/form-data`` requests, i.e., to simulate a **file upload form** submission. It is done using the ``--form`` / ``-f`` flag and passing one or more file fields::
|
||||
|
||||
http -f POST example.com/jobs name=John cv@~/Documents/cv.pdf
|
||||
|
||||
@ -93,17 +95,30 @@ The above will send the same request as if the following HTML form were submitte
|
||||
<input type="file" name="cv" />
|
||||
</form>
|
||||
|
||||
A whole request body can be passed in via ``stdin`` instead::
|
||||
A whole request body can be passed in via **``stdin``** instead, in which case it will be used with no further processing::
|
||||
|
||||
echo '{"name": "John"}' | http PATCH example.com/person/1 X-API-Token:123
|
||||
# Or:
|
||||
http POST example.com/person/1 X-API-Token:123 < person.json
|
||||
|
||||
That can be used for **piping services together**. The following example ``GET``s JSON data from the Github API and ``POST``s it to httpbin.org::
|
||||
|
||||
http -b GET https://api.github.com/repos/jkbr/httpie | http POST httpbin.org/post
|
||||
|
||||
The above can be further simplified by omitting ``GET`` and ``POST`` because they are both default here. The first command has no request data, whereas the second one does via ``stdin``::
|
||||
|
||||
http -b https://api.github.com/repos/jkbr/httpie | http httpbin.org/post
|
||||
|
||||
An alternative to ``stdin`` is to pass a file name whose content will be used as the request body. It has the advantage that the ``Content-Type`` header will automatically be set to the appropriate value based on the filename extension (using the ``mimetypes`` module). Therefore, the following will request will send the verbatim contents of the file with ``Content-Type: application/xml``::
|
||||
|
||||
http PUT httpbin.org/put @/data/file.xml
|
||||
|
||||
|
||||
Flags
|
||||
^^^^^
|
||||
Most of the flags mirror the arguments understood by ``requests.request``. See ``http -h`` for more details::
|
||||
|
||||
$ http --help
|
||||
usage: http [-h] [--version] [--json | --form] [--traceback]
|
||||
[--pretty | --ugly]
|
||||
[--print OUTPUT_OPTIONS | --verbose | --headers | --body]
|
||||
@ -133,13 +148,15 @@ Most of the flags mirror the arguments understood by ``requests.request``. See `
|
||||
optional arguments:
|
||||
-h, --help show this help message and exit
|
||||
--version show program's version number and exit
|
||||
--json, -j (default) Data items are serialized as a JSON object.
|
||||
The Content-Type and Accept headers are set to
|
||||
application/json (if not set via the command line).
|
||||
--form, -f Data items are serialized as form fields. The Content-
|
||||
Type is set to application/x-www-form-urlencoded (if
|
||||
not specifid). The presence of any file fields results
|
||||
into a multipart/form-data request.
|
||||
--json, -j (default) Data items from the command line are
|
||||
serialized as a JSON object. The Content-Type and
|
||||
Accept headers are set to application/json (if not
|
||||
specified).
|
||||
--form, -f Data items from the command line are serialized as
|
||||
form fields. The Content-Type is set to application/x
|
||||
-www-form-urlencoded (if not specified). The presence
|
||||
of any file fields results into a multipart/form-data
|
||||
request.
|
||||
--traceback Print exception traceback should one occur.
|
||||
--pretty If stdout is a terminal, the response is prettified by
|
||||
default (colorized and indented if it is JSON). This
|
||||
@ -166,7 +183,8 @@ Most of the flags mirror the arguments understood by ``requests.request``. See `
|
||||
make sure that the $TERM environment variable is set
|
||||
to "xterm-256color" or similar (e.g., via `export TERM
|
||||
=xterm-256color' in your ~/.bashrc).
|
||||
--auth AUTH, -a AUTH username:password
|
||||
--auth AUTH, -a AUTH username:password. If the password is omitted (-a
|
||||
username), HTTPie will prompt for it.
|
||||
--auth-type {basic,digest}
|
||||
The authentication mechanism to be used. Defaults to
|
||||
"basic".
|
||||
@ -207,7 +225,12 @@ Before a pull requests is submitted, it's a good idea to run the existing suite
|
||||
Changelog
|
||||
---------
|
||||
|
||||
* `0.2.3dev <https://github.com/jkbr/httpie/compare/0.2.2...master>`_
|
||||
* `0.2.5 <https://github.com/jkbr/httpie/compare/0.2.2...0.2.5>`_ (2012-07-17)
|
||||
* Unicode characters in prettified JSON now don't get escaped to improve readability.
|
||||
* --auth now prompts for a password if only a username provided.
|
||||
* Added support for request payloads from a file path with automatic ``Content-Type`` (``http URL @/path``).
|
||||
* Fixed missing query string when displaing the request headers via ``--verbose``.
|
||||
* Fixed Content-Type for requests with no data.
|
||||
* `0.2.2 <https://github.com/jkbr/httpie/compare/0.2.1...0.2.2>`_ (2012-06-24)
|
||||
* The ``METHOD`` positional argument can now be omitted (defaults to ``GET``, or to ``POST`` with data).
|
||||
* Fixed --verbose --form.
|
||||
|
@ -3,5 +3,5 @@ HTTPie - cURL for humans.
|
||||
|
||||
"""
|
||||
__author__ = 'Jakub Roztocil'
|
||||
__version__ = '0.2.2'
|
||||
__version__ = '0.2.5'
|
||||
__licence__ = 'BSD'
|
||||
|
@ -16,23 +16,29 @@ TYPE_FORM = 'application/x-www-form-urlencoded; charset=utf-8'
|
||||
TYPE_JSON = 'application/json; charset=utf-8'
|
||||
|
||||
|
||||
def _get_response(parser, args, stdin, stdin_isatty):
|
||||
def _get_response(args):
|
||||
|
||||
if args.json or (not args.form and args.data):
|
||||
auto_json = args.data and not args.form
|
||||
if args.json or auto_json:
|
||||
# JSON
|
||||
if not args.files and (
|
||||
'Content-Type' not in args.headers
|
||||
and (args.data or args.json)):
|
||||
args.headers['Content-Type'] = TYPE_JSON
|
||||
if stdin_isatty:
|
||||
# Serialize the parsed data.
|
||||
args.data = json.dumps(args.data)
|
||||
if 'Content-Type' not in args.headers:
|
||||
args.headers['Content-Type'] = TYPE_JSON
|
||||
|
||||
if 'Accept' not in args.headers:
|
||||
# Default Accept to JSON as well.
|
||||
args.headers['Accept'] = 'application/json'
|
||||
elif not args.files and 'Content-Type' not in args.headers:
|
||||
|
||||
if isinstance(args.data, dict):
|
||||
# If not empty, serialize the data `dict` parsed from arguments.
|
||||
# Otherwise set it to `None` avoid sending "{}".
|
||||
args.data = json.dumps(args.data) if args.data else None
|
||||
|
||||
elif args.form:
|
||||
# Form
|
||||
args.headers['Content-Type'] = TYPE_FORM
|
||||
if not args.files and 'Content-Type' not in args.headers:
|
||||
# If sending files, `requests` will set
|
||||
# the `Content-Type` for us.
|
||||
args.headers['Content-Type'] = TYPE_FORM
|
||||
|
||||
# Fire the request.
|
||||
try:
|
||||
@ -113,7 +119,7 @@ def main(args=None,
|
||||
stdin=stdin,
|
||||
stdin_isatty=stdin_isatty
|
||||
)
|
||||
response = _get_response(parser, args, stdin, stdin_isatty)
|
||||
response = _get_response(args)
|
||||
output = _get_output(args, stdout_isatty, response)
|
||||
output_bytes = output.encode('utf8')
|
||||
f = (stdout.buffer if hasattr(stdout, 'buffer') else stdout)
|
||||
|
@ -25,16 +25,16 @@ group_type = parser.add_mutually_exclusive_group(required=False)
|
||||
group_type.add_argument(
|
||||
'--json', '-j', action='store_true',
|
||||
help=_('''
|
||||
(default) Data items are serialized as a JSON object.
|
||||
(default) Data items from the command line are serialized as a JSON object.
|
||||
The Content-Type and Accept headers
|
||||
are set to application/json (if not set via the command line).
|
||||
are set to application/json (if not specified).
|
||||
''')
|
||||
)
|
||||
group_type.add_argument(
|
||||
'--form', '-f', action='store_true',
|
||||
help=_('''
|
||||
Data items are serialized as form fields.
|
||||
The Content-Type is set to application/x-www-form-urlencoded (if not specifid).
|
||||
Data items from the command line are serialized as form fields.
|
||||
The Content-Type is set to application/x-www-form-urlencoded (if not specified).
|
||||
The presence of any file fields results into a multipart/form-data request.
|
||||
''')
|
||||
)
|
||||
@ -123,8 +123,11 @@ parser.add_argument(
|
||||
|
||||
# ``requests.request`` keyword arguments.
|
||||
parser.add_argument(
|
||||
'--auth', '-a', help='username:password',
|
||||
type=cliparse.KeyValueType(cliparse.SEP_COMMON)
|
||||
'--auth', '-a', type=cliparse.AuthCredentialsType(cliparse.SEP_COMMON),
|
||||
help=_('''
|
||||
username:password.
|
||||
If only the username is provided (-a username), HTTPie will prompt for the password.
|
||||
'''),
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
|
@ -7,8 +7,8 @@ import sys
|
||||
import re
|
||||
import json
|
||||
import argparse
|
||||
|
||||
from collections import namedtuple
|
||||
import mimetypes
|
||||
import getpass
|
||||
|
||||
try:
|
||||
from collections import OrderedDict
|
||||
@ -52,20 +52,29 @@ class Parser(argparse.ArgumentParser):
|
||||
def parse_args(self, args=None, namespace=None,
|
||||
stdin=sys.stdin,
|
||||
stdin_isatty=sys.stdin.isatty()):
|
||||
|
||||
args = super(Parser, self).parse_args(args, namespace)
|
||||
|
||||
self._validate_output_options(args)
|
||||
self._validate_auth_options(args)
|
||||
self._guess_method(args, stdin_isatty)
|
||||
self._parse_items(args)
|
||||
|
||||
if not stdin_isatty:
|
||||
self._process_stdin(args, stdin)
|
||||
self._body_from_file(args, stdin)
|
||||
|
||||
if args.auth and not args.auth.has_password():
|
||||
# stdin has already been read (if not a tty) so
|
||||
# it's save to prompt now.
|
||||
args.auth.prompt_password()
|
||||
|
||||
return args
|
||||
|
||||
def _process_stdin(self, args, stdin):
|
||||
def _body_from_file(self, args, f):
|
||||
if args.data:
|
||||
self.error('Request body (stdin) and request '
|
||||
self.error('Request body (from stdin or a file) and request '
|
||||
'data (key=value) cannot be mixed.')
|
||||
args.data = stdin.read()
|
||||
args.data = f.read()
|
||||
|
||||
def _guess_method(self, args, stdin_isatty=sys.stdin.isatty()):
|
||||
"""
|
||||
@ -125,12 +134,25 @@ class Parser(argparse.ArgumentParser):
|
||||
self.error(e.message)
|
||||
|
||||
if args.files and not args.form:
|
||||
# We could just switch to --form automatically here,
|
||||
# but I think it's better to make it explicit.
|
||||
self.error(
|
||||
' You need to set the --form / -f flag to'
|
||||
' to issue a multipart request. File fields: %s'
|
||||
% ','.join(args.files.keys()))
|
||||
# `http url @/path/to/file`
|
||||
# It's not --form so the file contents will be used as the
|
||||
# body of the requests. Also, we try to detect the appropriate
|
||||
# Content-Type.
|
||||
if len(args.files) > 1:
|
||||
self.error(
|
||||
'Only one file can be specified unless'
|
||||
' --form is used. File fields: %s'
|
||||
% ','.join(args.files.keys()))
|
||||
f = list(args.files.values())[0]
|
||||
self._body_from_file(args, f)
|
||||
args.files = {}
|
||||
if 'Content-Type' not in args.headers:
|
||||
mime, encoding = mimetypes.guess_type(f.name, strict=False)
|
||||
if mime:
|
||||
content_type = mime
|
||||
if encoding:
|
||||
content_type = '%s; charset=%s' % (mime, encoding)
|
||||
args.headers['Content-Type'] = content_type
|
||||
|
||||
def _validate_output_options(self, args):
|
||||
unknown_output_options = set(args.output_options) - set(OUTPUT_OPTIONS)
|
||||
@ -146,12 +168,24 @@ class ParseError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
KeyValue = namedtuple('KeyValue', ['key', 'value', 'sep', 'orig'])
|
||||
class KeyValue(object):
|
||||
"""Base key-value pair parsed from CLI."""
|
||||
|
||||
def __init__(self, key, value, sep, orig):
|
||||
self.key = key
|
||||
self.value = value
|
||||
self.sep = sep
|
||||
self.orig = orig
|
||||
|
||||
def __eq__(self, other):
|
||||
return self.__dict__ == other.__dict__
|
||||
|
||||
|
||||
class KeyValueType(object):
|
||||
"""A type used with `argparse`."""
|
||||
|
||||
key_value_class = KeyValue
|
||||
|
||||
def __init__(self, *separators):
|
||||
self.separators = separators
|
||||
self.escapes = ['\\\\' + sep for sep in separators]
|
||||
@ -187,7 +221,44 @@ class KeyValueType(object):
|
||||
for sepstr in self.separators:
|
||||
key = key.replace('\\' + sepstr, sepstr)
|
||||
value = value.replace('\\' + sepstr, sepstr)
|
||||
return KeyValue(key=key, value=value, sep=sep, orig=string)
|
||||
return self.key_value_class(key=key, value=value, sep=sep, orig=string)
|
||||
|
||||
|
||||
class AuthCredentials(KeyValue):
|
||||
"""
|
||||
Represents parsed credentials.
|
||||
|
||||
"""
|
||||
def _getpass(self, prompt):
|
||||
# To allow mocking.
|
||||
return getpass.getpass(prompt)
|
||||
|
||||
def has_password(self):
|
||||
return self.value is not None
|
||||
|
||||
def prompt_password(self):
|
||||
try:
|
||||
self.value = self._getpass("Password for user '%s': " % self.key)
|
||||
except (EOFError, KeyboardInterrupt):
|
||||
sys.stderr.write('\n')
|
||||
sys.exit(0)
|
||||
|
||||
|
||||
class AuthCredentialsType(KeyValueType):
|
||||
|
||||
key_value_class = AuthCredentials
|
||||
|
||||
def __call__(self, string):
|
||||
try:
|
||||
return super(AuthCredentialsType, self).__call__(string)
|
||||
except argparse.ArgumentTypeError:
|
||||
# No password provided, will prompt for it later.
|
||||
return self.key_value_class(
|
||||
key=string,
|
||||
value=None,
|
||||
sep=SEP_COMMON,
|
||||
orig=string
|
||||
)
|
||||
|
||||
|
||||
def parse_items(items, data=None, headers=None, files=None):
|
||||
|
@ -30,9 +30,10 @@ def from_request(request):
|
||||
body = request.__class__._encode_params(body)
|
||||
|
||||
return HTTPMessage(
|
||||
line='{method} {path} HTTP/1.1'.format(
|
||||
line='{method} {path}{query} HTTP/1.1'.format(
|
||||
method=request.method,
|
||||
path=url.path or '/'),
|
||||
path=url.path or '/',
|
||||
query='' if url.query is '' else '?' + url.query),
|
||||
headers='\n'.join(str('%s: %s') % (name, value)
|
||||
for name, value
|
||||
in request_headers.items()),
|
||||
|
@ -1,4 +1,5 @@
|
||||
import os
|
||||
import re
|
||||
import json
|
||||
|
||||
import pygments
|
||||
@ -18,6 +19,8 @@ FORMATTER = (Terminal256Formatter
|
||||
if '256color' in os.environ.get('TERM', '')
|
||||
else TerminalFormatter)
|
||||
|
||||
application_content_type_re = re.compile(r'application/(.+\+)(json|xml)$')
|
||||
|
||||
|
||||
class PrettyHttp(object):
|
||||
|
||||
@ -33,16 +36,23 @@ class PrettyHttp(object):
|
||||
|
||||
def body(self, content, content_type):
|
||||
content_type = content_type.split(';')[0]
|
||||
application_match = re.match(application_content_type_re, content_type)
|
||||
if application_match:
|
||||
# Strip vendor and extensions from Content-Type
|
||||
vendor, extension = application_match.groups()
|
||||
content_type = content_type.replace(vendor, '')
|
||||
|
||||
try:
|
||||
lexer = get_lexer_for_mimetype(content_type)
|
||||
except ClassNotFound:
|
||||
return content
|
||||
|
||||
if content_type == 'application/json':
|
||||
if content_type == "application/json":
|
||||
try:
|
||||
# Indent and sort the JSON data.
|
||||
content = json.dumps(json.loads(content),
|
||||
sort_keys=True, indent=4)
|
||||
sort_keys=True, indent=4,
|
||||
ensure_ascii=False)
|
||||
except:
|
||||
pass
|
||||
|
||||
|
3
setup.py
3
setup.py
@ -5,7 +5,8 @@ import httpie
|
||||
|
||||
|
||||
if sys.argv[-1] == 'test':
|
||||
sys.exit(os.system('python tests/tests.py'))
|
||||
status = os.system('python tests/tests.py')
|
||||
sys.exit(1 if status > 127 else status)
|
||||
|
||||
|
||||
requirements = [
|
||||
|
1
tests/file2.txt
Normal file
1
tests/file2.txt
Normal file
@ -0,0 +1 @@
|
||||
__test_file_content__
|
176
tests/tests.py
176
tests/tests.py
@ -1,9 +1,12 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import unittest
|
||||
import argparse
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
from requests.compat import is_py26
|
||||
import json
|
||||
from requests.compat import is_py26, is_py3
|
||||
from requests import Response
|
||||
|
||||
|
||||
#################################################################
|
||||
@ -18,6 +21,7 @@ from httpie import __main__, cliparse
|
||||
|
||||
|
||||
TEST_FILE_PATH = os.path.join(TESTS_ROOT, 'file.txt')
|
||||
TEST_FILE2_PATH = os.path.join(TESTS_ROOT, 'file2.txt')
|
||||
TEST_FILE_CONTENT = open(TEST_FILE_PATH).read().strip()
|
||||
TERMINAL_COLOR_PRESENCE_CHECK = '\x1b['
|
||||
|
||||
@ -79,16 +83,6 @@ class HTTPieTest(BaseTestCase):
|
||||
self.assertIn('HTTP/1.1 200', r)
|
||||
self.assertIn('"foo": "bar"', r)
|
||||
|
||||
def test_GET_JSON_implicit_accept(self):
|
||||
r = http('-j', 'GET', 'http://httpbin.org/headers')
|
||||
self.assertIn('HTTP/1.1 200', r)
|
||||
self.assertIn('"Accept": "application/json"', r)
|
||||
|
||||
def test_GET_JSON_explicit_accept(self):
|
||||
r = http('-j', 'GET', 'http://httpbin.org/headers', 'Accept:application/xml')
|
||||
self.assertIn('HTTP/1.1 200', r)
|
||||
self.assertIn('"Accept": "application/xml"', r)
|
||||
|
||||
def test_POST_form(self):
|
||||
r = http('--form', 'POST', 'http://httpbin.org/post', 'foo=bar')
|
||||
self.assertIn('HTTP/1.1 200', r)
|
||||
@ -107,6 +101,69 @@ class HTTPieTest(BaseTestCase):
|
||||
self.assertIn('"Foo": "bar"', r)
|
||||
|
||||
|
||||
class AutoContentTypeAndAcceptHeadersTest(BaseTestCase):
|
||||
"""
|
||||
Test that Accept and Content-Type correctly defaults to JSON,
|
||||
but can still be overridden. The same with Content-Type when --form
|
||||
-f is used.
|
||||
|
||||
"""
|
||||
def test_GET_no_data_no_auto_headers(self):
|
||||
# https://github.com/jkbr/httpie/issues/62
|
||||
r = http('GET', 'http://httpbin.org/headers')
|
||||
self.assertIn('HTTP/1.1 200', r)
|
||||
self.assertIn('"Accept": "*/*"', r)
|
||||
# Although an empty header is present in the response from httpbin,
|
||||
# it's not included in the request.
|
||||
self.assertIn('"Content-Type": ""', r)
|
||||
|
||||
def test_POST_no_data_no_auto_headers(self):
|
||||
# JSON headers shouldn't be automatically set for POST with no data.
|
||||
r = http('POST', 'http://httpbin.org/post')
|
||||
self.assertIn('HTTP/1.1 200', r)
|
||||
self.assertIn('"Accept": "*/*"', r)
|
||||
# Although an empty header is present in the response from httpbin,
|
||||
# it's not included in the request.
|
||||
self.assertIn(' "Content-Type": ""', r)
|
||||
|
||||
def test_POST_with_data_auto_JSON_headers(self):
|
||||
r = http('POST', 'http://httpbin.org/post', 'a=b')
|
||||
self.assertIn('HTTP/1.1 200', r)
|
||||
self.assertIn('"Accept": "application/json"', r)
|
||||
self.assertIn('"Content-Type": "application/json; charset=utf-8', r)
|
||||
|
||||
def test_GET_with_data_auto_JSON_headers(self):
|
||||
# JSON headers should automatically be set also for GET with data.
|
||||
r = http('POST', 'http://httpbin.org/post', 'a=b')
|
||||
self.assertIn('HTTP/1.1 200', r)
|
||||
self.assertIn('"Accept": "application/json"', r)
|
||||
self.assertIn('"Content-Type": "application/json; charset=utf-8', r)
|
||||
|
||||
def test_POST_explicit_JSON_auto_JSON_headers(self):
|
||||
r = http('-j', 'POST', 'http://httpbin.org/post')
|
||||
self.assertIn('HTTP/1.1 200', r)
|
||||
self.assertIn('"Accept": "application/json"', r)
|
||||
self.assertIn('"Content-Type": "application/json; charset=utf-8', r)
|
||||
|
||||
def test_GET_explicit_JSON_explicit_headers(self):
|
||||
r = http('-j', 'GET', 'http://httpbin.org/headers',
|
||||
'Accept:application/xml',
|
||||
'Content-Type:application/xml')
|
||||
self.assertIn('HTTP/1.1 200', r)
|
||||
self.assertIn('"Accept": "application/xml"', r)
|
||||
self.assertIn('"Content-Type": "application/xml"', r)
|
||||
|
||||
def test_POST_form_auto_Content_Type(self):
|
||||
r = http('-f', 'POST', 'http://httpbin.org/post')
|
||||
self.assertIn('HTTP/1.1 200', r)
|
||||
self.assertIn('"Content-Type": "application/x-www-form-urlencoded; charset=utf-8"', r)
|
||||
|
||||
def test_POST_form_Content_Type_override(self):
|
||||
r = http('-f', 'POST', 'http://httpbin.org/post', 'Content-Type:application/xml')
|
||||
self.assertIn('HTTP/1.1 200', r)
|
||||
self.assertIn('"Content-Type": "application/xml"', r)
|
||||
|
||||
|
||||
class ImplicitHTTPMethodTest(BaseTestCase):
|
||||
|
||||
def test_implicit_GET(self):
|
||||
@ -180,10 +237,42 @@ class MultipartFormDataFileUploadTest(BaseTestCase):
|
||||
r = http('--form', 'POST', 'http://httpbin.org/post',
|
||||
'test-file@%s' % TEST_FILE_PATH, 'foo=bar')
|
||||
self.assertIn('HTTP/1.1 200', r)
|
||||
self.assertIn('"test-file": "__test_file_content__', r)
|
||||
self.assertIn('"test-file": "%s' % TEST_FILE_CONTENT, r)
|
||||
self.assertIn('"foo": "bar"', r)
|
||||
|
||||
|
||||
class RequestBodyFromFilePathTest(BaseTestCase):
|
||||
"""
|
||||
`http URL @file'
|
||||
|
||||
"""
|
||||
def test_request_body_from_file_by_path(self):
|
||||
r = http('POST', 'http://httpbin.org/post', '@' + TEST_FILE_PATH)
|
||||
self.assertIn('HTTP/1.1 200', r)
|
||||
self.assertIn(TEST_FILE_CONTENT, r)
|
||||
self.assertIn('"Content-Type": "text/plain"', r)
|
||||
|
||||
def test_request_body_from_file_by_path_with_explicit_content_type(self):
|
||||
r = http('POST', 'http://httpbin.org/post', '@' + TEST_FILE_PATH, 'Content-Type:x-foo/bar')
|
||||
self.assertIn('HTTP/1.1 200', r)
|
||||
self.assertIn(TEST_FILE_CONTENT, r)
|
||||
self.assertIn('"Content-Type": "x-foo/bar"', r)
|
||||
|
||||
def test_request_body_from_file_by_path_only_one_file_allowed(self):
|
||||
self.assertRaises(SystemExit, lambda: http(
|
||||
'POST',
|
||||
'http://httpbin.org/post',
|
||||
'@' + TEST_FILE_PATH,
|
||||
'@' + TEST_FILE2_PATH))
|
||||
|
||||
def test_request_body_from_file_by_path_no_data_items_allowed(self):
|
||||
self.assertRaises(SystemExit, lambda: http(
|
||||
'POST',
|
||||
'http://httpbin.org/post',
|
||||
'@' + TEST_FILE_PATH,
|
||||
'foo=bar'))
|
||||
|
||||
|
||||
class AuthTest(BaseTestCase):
|
||||
|
||||
def test_basic_auth(self):
|
||||
@ -200,6 +289,16 @@ class AuthTest(BaseTestCase):
|
||||
self.assertIn('"authenticated": true', r)
|
||||
self.assertIn('"user": "user"', r)
|
||||
|
||||
def test_password_prompt(self):
|
||||
cliparse.AuthCredentials._getpass = lambda self, prompt: 'password'
|
||||
|
||||
r = http('--auth', 'user',
|
||||
'GET', 'httpbin.org/basic-auth/user/password')
|
||||
|
||||
self.assertIn('HTTP/1.1 200', r)
|
||||
self.assertIn('"authenticated": true', r)
|
||||
self.assertIn('"user": "user"', r)
|
||||
|
||||
|
||||
#################################################################
|
||||
# CLI argument parsing related tests.
|
||||
@ -273,7 +372,7 @@ class ItemParsingTest(BaseTestCase):
|
||||
self.assertIn('test-file', files)
|
||||
|
||||
|
||||
class HTTPieArgumentParserTestCase(unittest.TestCase):
|
||||
class ArgumentParserTestCase(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.parser = cliparse.Parser()
|
||||
@ -346,5 +445,56 @@ class HTTPieArgumentParserTestCase(unittest.TestCase):
|
||||
])
|
||||
|
||||
|
||||
class FakeResponse(Response):
|
||||
|
||||
class Mock(object):
|
||||
|
||||
def __getattr__(self, item):
|
||||
return self
|
||||
|
||||
def __repr__(self):
|
||||
return 'Mock string'
|
||||
|
||||
def __unicode__(self):
|
||||
return self.__repr__()
|
||||
|
||||
def __init__(self, content=None, encoding='utf-8'):
|
||||
super(FakeResponse, self).__init__()
|
||||
self.headers['Content-Type'] = 'application/json'
|
||||
self.encoding = encoding
|
||||
self._content = content.encode(encoding)
|
||||
self.raw = self.Mock()
|
||||
|
||||
|
||||
class UnicodeOutputTestCase(BaseTestCase):
|
||||
|
||||
def test_unicode_output(self):
|
||||
# some cyrillic and simplified chinese symbols
|
||||
response_dict = {'Привет': 'Мир!',
|
||||
'Hello': '世界'}
|
||||
if not is_py3:
|
||||
response_dict = dict(
|
||||
(k.decode('utf8'), v.decode('utf8'))
|
||||
for k, v in response_dict.items()
|
||||
)
|
||||
response_body = json.dumps(response_dict)
|
||||
# emulate response
|
||||
response = FakeResponse(response_body)
|
||||
|
||||
# emulate cli arguments
|
||||
args = argparse.Namespace()
|
||||
args.prettify = True
|
||||
args.output_options = 'b'
|
||||
args.forced_content_type = None
|
||||
args.style = 'default'
|
||||
|
||||
# colorized output contains escape sequences
|
||||
output = __main__._get_output(args, True, response)
|
||||
|
||||
for key, value in response_dict.items():
|
||||
self.assertIn(key, output)
|
||||
self.assertIn(value, output)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
Reference in New Issue
Block a user