Compare commits

..

17 Commits
0.2.1 ... 0.2.2

Author SHA1 Message Date
2f7921091c 0.2.2 2012-06-24 16:43:03 +02:00
180313d80c Impreved tests. 2012-06-24 04:20:45 +02:00
926d3f5caf Tests, docs, clean-up.
Closes #54.
2012-06-24 03:45:21 +02:00
4613d947a8 Default to POST also when stdin redirected.
+clean up
2012-06-24 01:25:30 +02:00
5a47f00bac Replaced mock.Mock with argparse.Namespace to reduce deps. 2012-06-23 23:54:59 +02:00
0e1affbbc4 Issue #54 Method suggestion proposal 2012-06-17 22:15:07 +04:00
d920f20847 Issue #54 Method suggestion proposal 2012-06-17 22:11:26 +04:00
bca36f0464 Issue #54 Method suggestion proposal 2012-06-17 21:46:56 +04:00
78fff98712 Issue #54 Method suggestion proposal 2012-06-16 20:08:31 +04:00
e06c448a75 README improvements. 2012-06-15 17:32:45 +02:00
9cdbd6b0ec Added a Contribute section to README. 2012-06-15 17:13:40 +02:00
cbc6d02127 Fixed --verbose --form.
Closes #53
2012-06-15 16:47:55 +02:00
284a75fa2f Merge pull request #51 from msabramo/testing
Added support for tox (http://tox.testrun.org/)
2012-06-13 07:48:09 -07:00
b3ea273a21 Add "pypy" to .travis.yml 2012-06-13 07:36:51 -07:00
0d129d5f69 Add tox.ini for tox (http://tox.testrun.org/) 2012-06-13 07:18:12 -07:00
1388206f1a Fix path to tests.py in setup.py to make python setup.py test work 2012-06-13 07:18:11 -07:00
28dbe9f76c Bump version to 0.2.2dev. 2012-06-13 16:02:30 +02:00
11 changed files with 379 additions and 113 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@ dist
httpie.egg-info
build
*.pyc
.tox

View File

@ -2,10 +2,9 @@ language: python
python:
- 2.6
- 2.7
- pypy
- 3.1
- 3.2
script: python tests/tests.py
script: python setup.py test
install:
- pip install requests pygments
- "if [[ $TRAVIS_PYTHON_VERSION == '2.6' ]] || [[ $TRAVIS_PYTHON_VERSION == '3.1' ]]; then pip install argparse; fi"
- pip install . --use-mirrors

View File

@ -28,17 +28,18 @@ Or, you can install the **development version** directly from GitHub:
pip install -U https://github.com/jkbr/httpie/tarball/master
HTTPie should also
Usage
-----
Hello world::
http GET httpie.org
http httpie.org
Synopsis::
http [flags] METHOD URL [items]
http [flags] [METHOD] URL [items]
There are four types of key-value pair items available:
@ -109,20 +110,23 @@ Most of the flags mirror the arguments understood by ``requests.request``. See `
[--style STYLE] [--auth AUTH] [--auth-type {basic,digest}]
[--verify VERIFY] [--proxy PROXY] [--allow-redirects]
[--timeout TIMEOUT]
METHOD URL [ITEM [ITEM ...]]
[METHOD] URL [ITEM [ITEM ...]]
HTTPie - cURL for humans. <http://httpie.org>
positional arguments:
METHOD The HTTP method to be used for the request (GET, POST,
PUT, DELETE, PATCH, ...).
PUT, DELETE, PATCH, ...). If this argument is omitted,
then HTTPie will guess the HTTP method. If there is
some data to be sent, then it will be POST, otherwise
GET.
URL The protocol defaults to http:// if the URL does not
include one.
ITEM A key-value pair whose type is defined by the
separator used. It can be an HTTP header
(header:value), a data field to be used in the request
body (field_name=value), a raw JSON data field
(field_name:=value) or a file field
(field_name:=value), or a file field
(field_name@/path/to/file). You can use a backslash to
escape a colliding separator in the field name.
@ -156,12 +160,12 @@ Most of the flags mirror the arguments understood by ``requests.request``. See `
--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. For this
option to work properly, please make sure that the
$TERM environment variable is set to "xterm-256color"
or similar (e.g., via `export TERM=xterm-256color' in
your ~/.bashrc).
monokai, murphy, native, pastie, perldoc, rrt,
solarized, tango, trac, vim, vs. Defaults to
solarized. For this option to work properly, please
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-type {basic,digest}
The authentication mechanism to be used. Defaults to
@ -179,21 +183,39 @@ Most of the flags mirror the arguments understood by ``requests.request``. See `
socket.setdefaulttimeout() as fallback).
Contributors
------------
Contribute
-----------
`View contributors on GitHub <https://github.com/jkbr/httpie/contributors>`_.
If you have found a bug or have a feature request, the `issue tracker <https://github.com/jkbr/httpie/issues?state=open>`_ is the place to start a discussion about it.
To contribute code or documentation, please first browse the existing issues to see if the feature/bug has previously been discussed. Then fork `the repository <https://github.com/jkbr/httpie>`_, make changes in your develop branch and submit a pull request. Note: Pull requests with tests and documentation are 53.6% more awesome :)
Before a pull requests is submitted, it's a good idea to run the existing suite of tests::
python setup.py test
`Tox <http://tox.testrun.org/>`_ can used to conveniently run tests in all of the `supported Python environments <https://github.com/jkbr/httpie/blob/master/tox.ini>`_::
# Install tox
pip install tox
# Run tests
tox
Changelog
---------
* `New in development version <https://github.com/jkbr/httpie/compare/0.2.1...master>`_
* 0.2.1 (2012-06-13)
* `0.2.3dev <https://github.com/jkbr/httpie/compare/0.2.2...master>`_
* `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.
* Added support for `Tox <http://tox.testrun.org/>`_.
* `0.2.1 <https://github.com/jkbr/httpie/compare/0.2.0...0.2.1>`_ (2012-06-13)
* Added compatibility with ``requests-0.12.1``.
* Dropped custom JSON and HTTP lexers in favor of the ones newly included in ``pygments-1.5``.
* `Complete changelog <https://github.com/jkbr/httpie/compare/0.2.0...0.2.1>`_
* 0.2.0 (2012-04-25)
* `0.2.0 <https://github.com/jkbr/httpie/compare/0.1.6...0.2.0>`_ (2012-04-25)
* Added Python 3 support.
* Added the ability to print the HTTP request as well as the response (see ``--print`` and ``--verbose``).
* Added support for Digest authentication.
@ -201,5 +223,4 @@ Changelog
* Improved syntax highlighting for JSON.
* Added support for field name escaping.
* Many bug fixes.
* `Complete changelog <https://github.com/jkbr/httpie/compare/0.1.6...0.2.0>`_
* `0.1.6 <https://github.com/jkbr/httpie/compare/0.1.4...0.1.6>`_ (2012-03-04)

View File

@ -3,5 +3,5 @@ HTTPie - cURL for humans.
"""
__author__ = 'Jakub Roztocil'
__version__ = '0.2.1'
__version__ = '0.2.2'
__licence__ = 'BSD'

View File

@ -18,12 +18,6 @@ TYPE_JSON = 'application/json; charset=utf-8'
def _get_response(parser, args, stdin, stdin_isatty):
if not stdin_isatty:
if args.data:
parser.error('Request body (stdin) and request '
'data (key=value) cannot be mixed.')
args.data = stdin.read()
if args.json or (not args.form and args.data):
# JSON
if not args.files and (
@ -114,7 +108,11 @@ def main(args=None,
stdin=sys.stdin, stdin_isatty=sys.stdin.isatty(),
stdout=sys.stdout, stdout_isatty=sys.stdout.isatty()):
parser = cli.parser
args = parser.parse_args(args if args is not None else sys.argv[1:])
args = parser.parse_args(
args=args if args is not None else sys.argv[1:],
stdin=stdin,
stdin_isatty=stdin_isatty
)
response = _get_response(parser, args, stdin, stdin_isatty)
output = _get_output(args, stdout_isatty, response)
output_bytes = output.encode('utf8')

View File

@ -14,7 +14,7 @@ def _(text):
desc = '%s <http://httpie.org>'
parser = cliparse.HTTPieArgumentParser(description=desc % __doc__.strip(),)
parser = cliparse.Parser(description=desc % __doc__.strip(),)
parser.add_argument('--version', action='version', version=__version__)
@ -171,9 +171,13 @@ parser.add_argument(
parser.add_argument(
'method', metavar='METHOD',
nargs='?',
default=None,
help=_('''
The HTTP method to be used for the request
(GET, POST, PUT, DELETE, PATCH, ...).
If this argument is omitted, then HTTPie will guess the HTTP method.
If there is some data to be sent, then it will be POST, otherwise GET.
''')
)
parser.add_argument(
@ -196,7 +200,7 @@ parser.add_argument(
A key-value pair whose type is defined by the separator used. It can be an
HTTP header (header:value),
a data field to be used in the request body (field_name=value),
a raw JSON data field (field_name:=value)
a raw JSON data field (field_name:=value),
or a file field (field_name@/path/to/file).
You can use a backslash to escape a colliding separator in the field name.
''')

View File

@ -3,6 +3,7 @@ CLI argument parsing logic.
"""
import os
import sys
import re
import json
import argparse
@ -24,6 +25,11 @@ SEP_HEADERS = SEP_COMMON
SEP_DATA = '='
SEP_DATA_RAW_JSON = ':='
SEP_FILES = '@'
DATA_ITEM_SEPARATORS = [
SEP_DATA,
SEP_DATA_RAW_JSON,
SEP_FILES
]
OUT_REQ_HEADERS = 'H'
@ -41,16 +47,71 @@ PRETTIFY_STDOUT_TTY_ONLY = object()
DEFAULT_UA = 'HTTPie/%s' % __version__
class HTTPieArgumentParser(argparse.ArgumentParser):
class Parser(argparse.ArgumentParser):
def parse_args(self, args=None, namespace=None):
args = super(HTTPieArgumentParser, self).parse_args(args, namespace)
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)
return args
def _process_stdin(self, args, stdin):
if args.data:
self.error('Request body (stdin) and request '
'data (key=value) cannot be mixed.')
args.data = stdin.read()
def _guess_method(self, args, stdin_isatty=sys.stdin.isatty()):
"""
Set `args.method`, if not specified, to either POST or GET
based on whether the request has data or not.
"""
if args.method is None:
# Invoked as `http URL'.
assert not args.items
if not stdin_isatty:
args.method = 'POST'
else:
args.method = 'GET'
# FIXME: False positive, e.g., "localhost" matches but is a valid URL.
elif not re.match('^[a-zA-Z]+$', args.method):
# Invoked as `http URL item+':
# - The URL is now in `args.method`.
# - The first item is now in `args.url`.
#
# So we need to:
# - Guess the HTTP method.
# - Set `args.url` correctly.
# - Parse the first item and move it to `args.items[0]`.
item = KeyValueType(
SEP_COMMON,
SEP_DATA,
SEP_DATA_RAW_JSON,
SEP_FILES).__call__(args.url)
args.url = args.method
args.items.insert(0, item)
has_data = not stdin_isatty or any(
item.sep in DATA_ITEM_SEPARATORS for item in args.items)
if has_data:
args.method = 'POST'
else:
args.method = 'GET'
def _parse_items(self, args):
"""
Parse `args.items` into `args.headers`, `args.data` and `args.files`.
"""
args.headers = CaseInsensitiveDict()
args.headers['User-Agent'] = DEFAULT_UA
args.data = OrderedDict()

View File

@ -25,6 +25,10 @@ def from_request(request):
# requests < 0.12.1
body = request._enc_data
if isinstance(body, dict):
# --form
body = request.__class__._encode_params(body)
return HTTPMessage(
line='{method} {path} HTTP/1.1'.format(
method=request.method,

View File

@ -5,12 +5,14 @@ import httpie
if sys.argv[-1] == 'test':
os.system('python tests.py')
sys.exit()
sys.exit(os.system('python tests/tests.py'))
requirements = [
# Debian has only requests==0.10.1 and httpie.deb depends on that.
requirements = ['requests>=0.10.1', 'Pygments>=1.5']
'requests>=0.10.1',
'Pygments>=1.5'
]
if sys.version_info[:2] in ((2, 6), (3, 1)):
# argparse has been added in Python 3.2 / 2.7
requirements.append('argparse>=1.2.1')

View File

@ -1,23 +1,33 @@
# coding:utf-8
import os
import sys
import unittest
import argparse
from requests.compat import is_py26
import os
import sys
import tempfile
from requests.compat import is_py26
#################################################################
# Utils/setup
#################################################################
# HACK: Prepend ../ to PYTHONPATH so that we can import httpie form there.
TESTS_ROOT = os.path.dirname(__file__)
sys.path.insert(0, os.path.realpath(os.path.join(TESTS_ROOT, '..')))
from httpie import __main__
from httpie import cliparse
from httpie import __main__, cliparse
TEST_FILE = os.path.join(TESTS_ROOT, 'file.txt')
TEST_FILE_PATH = os.path.join(TESTS_ROOT, 'file.txt')
TEST_FILE_CONTENT = open(TEST_FILE_PATH).read().strip()
TERMINAL_COLOR_PRESENCE_CHECK = '\x1b['
def http(*args, **kwargs):
"""
Invoke `httpie.__main__.main` with `args` and `kwargs`,
and return a unicode response.
"""
http_kwargs = {
'stdin_isatty': True,
'stdout_isatty': False
@ -31,7 +41,7 @@ def http(*args, **kwargs):
return response
class BaseTest(unittest.TestCase):
class BaseTestCase(unittest.TestCase):
if is_py26:
def assertIn(self, member, container, msg=None):
@ -45,7 +55,157 @@ class BaseTest(unittest.TestCase):
self.assertEqual(sorted(d1.values()), sorted(d2.values()), msg)
class TestItemParsing(BaseTest):
#################################################################
# High-level tests using httpbin.org.
#################################################################
class HTTPieTest(BaseTestCase):
def test_GET(self):
r = http('GET', 'http://httpbin.org/get')
self.assertIn('HTTP/1.1 200', r)
def test_DELETE(self):
r = http('DELETE', 'http://httpbin.org/delete')
self.assertIn('HTTP/1.1 200', r)
def test_PUT(self):
r = http('PUT', 'http://httpbin.org/put', 'foo=bar')
self.assertIn('HTTP/1.1 200', r)
self.assertIn('"foo": "bar"', r)
def test_POST_JSON_data(self):
r = http('POST', 'http://httpbin.org/post', 'foo=bar')
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)
self.assertIn('"foo": "bar"', r)
def test_POST_stdin(self):
r = http('--form', 'POST', 'http://httpbin.org/post',
stdin=open(TEST_FILE_PATH), stdin_isatty=False)
self.assertIn('HTTP/1.1 200', r)
self.assertIn(TEST_FILE_CONTENT, r)
def test_headers(self):
r = http('GET', 'http://httpbin.org/headers', 'Foo:bar')
self.assertIn('HTTP/1.1 200', r)
self.assertIn('"User-Agent": "HTTPie', r)
self.assertIn('"Foo": "bar"', r)
class ImplicitHTTPMethodTest(BaseTestCase):
def test_implicit_GET(self):
r = http('http://httpbin.org/get')
self.assertIn('HTTP/1.1 200', r)
def test_implicit_GET_with_headers(self):
r = http('http://httpbin.org/headers', 'Foo:bar')
self.assertIn('HTTP/1.1 200', r)
self.assertIn('"Foo": "bar"', r)
def test_implicit_POST_json(self):
r = http('http://httpbin.org/post', 'hello=world')
self.assertIn('HTTP/1.1 200', r)
self.assertIn('"hello": "world"', r)
def test_implicit_POST_form(self):
r = http('--form', 'http://httpbin.org/post', 'foo=bar')
self.assertIn('HTTP/1.1 200', r)
self.assertIn('"foo": "bar"', r)
def test_implicit_POST_stdin(self):
r = http('--form', 'http://httpbin.org/post',
stdin=open(TEST_FILE_PATH), stdin_isatty=False)
self.assertIn('HTTP/1.1 200', r)
class PrettyFlagTest(BaseTestCase):
"""Test the --pretty / --ugly flag handling."""
def test_pretty_enabled_by_default(self):
r = http('GET', 'http://httpbin.org/get', stdout_isatty=True)
self.assertIn(TERMINAL_COLOR_PRESENCE_CHECK, r)
def test_pretty_enabled_by_default_unless_stdin_redirected(self):
r = http('GET', 'http://httpbin.org/get', stdout_isatty=False)
self.assertNotIn(TERMINAL_COLOR_PRESENCE_CHECK, r)
def test_force_pretty(self):
r = http('--pretty', 'GET', 'http://httpbin.org/get', stdout_isatty=False)
self.assertIn(TERMINAL_COLOR_PRESENCE_CHECK, r)
def test_force_ugly(self):
r = http('--ugly', 'GET', 'http://httpbin.org/get', stdout_isatty=True)
self.assertNotIn(TERMINAL_COLOR_PRESENCE_CHECK, r)
class VerboseFlagTest(BaseTestCase):
def test_verbose(self):
r = http('--verbose', 'GET', 'http://httpbin.org/get', 'test-header:__test__')
self.assertIn('HTTP/1.1 200', r)
self.assertEqual(r.count('__test__'), 2)
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('HTTP/1.1 200', r)
self.assertIn('foo=bar&baz=bar', r)
class MultipartFormDataFileUploadTest(BaseTestCase):
def test_non_existent_file_raises_parse_error(self):
self.assertRaises(cliparse.ParseError, http,
'--form', '--traceback',
'POST', 'http://httpbin.org/post',
'foo@/__does_not_exist__')
def test_upload_ok(self):
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('"foo": "bar"', r)
class AuthTest(BaseTestCase):
def test_basic_auth(self):
r = http('--auth', 'user:password',
'GET', 'httpbin.org/basic-auth/user/password')
self.assertIn('HTTP/1.1 200', r)
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('HTTP/1.1 200', r)
self.assertIn('"authenticated": true', r)
self.assertIn('"user": "user"', r)
#################################################################
# CLI argument parsing related tests.
#################################################################
class ItemParsingTest(BaseTestCase):
def setUp(self):
self.key_value_type = cliparse.KeyValueType(
@ -69,7 +229,7 @@ class TestItemParsing(BaseTest):
# data
self.key_value_type('baz\\=bar=foo'),
# files
self.key_value_type('bar\\@baz@%s' % TEST_FILE)
self.key_value_type('bar\\@baz@%s' % TEST_FILE_PATH)
])
self.assertDictEqual(headers, {
'foo:bar': 'baz',
@ -97,7 +257,7 @@ class TestItemParsing(BaseTest):
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),
self.key_value_type('test-file@%s' % TEST_FILE_PATH),
])
self.assertDictEqual(headers, {
'header': 'value',
@ -113,80 +273,77 @@ class TestItemParsing(BaseTest):
self.assertIn('test-file', files)
class TestHTTPie(BaseTest):
class HTTPieArgumentParserTestCase(unittest.TestCase):
def test_get(self):
http('GET', 'http://httpbin.org/get')
def setUp(self):
self.parser = cliparse.Parser()
def test_verbose(self):
r = http('--verbose', 'GET', 'http://httpbin.org/get', 'test-header:__test__')
self.assertEqual(r.count('__test__'), 2)
def test_guess_when_method_set_and_valid(self):
args = argparse.Namespace()
args.method = 'GET'
args.url = 'http://example.com/'
args.items = []
def test_json(self):
response = http('POST', '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)
self.parser._guess_method(args)
def test_form(self):
response = http('--form', 'POST', 'http://httpbin.org/post', 'foo=bar')
self.assertIn('"foo": "bar"', response)
self.assertEquals(args.method, 'GET')
self.assertEquals(args.url, 'http://example.com/')
self.assertEquals(args.items, [])
def test_headers(self):
response = http('GET', 'http://httpbin.org/headers', 'Foo:bar')
self.assertIn('"User-Agent": "HTTPie', response)
self.assertIn('"Foo": "bar"', response)
def test_guess_when_method_not_set(self):
args = argparse.Namespace()
args.method = None
args.url = 'http://example.com/'
args.items = []
self.parser._guess_method(args)
class TestPrettyFlag(BaseTest):
"""Test the --pretty / --ugly flag handling."""
self.assertEquals(args.method, 'GET')
self.assertEquals(args.url, 'http://example.com/')
self.assertEquals(args.items, [])
def test_pretty_enabled_by_default(self):
r = http('GET', 'http://httpbin.org/get', stdout_isatty=True)
self.assertIn(TERMINAL_COLOR_PRESENCE_CHECK, r)
def test_guess_when_method_set_but_invalid_and_data_field(self):
args = argparse.Namespace()
args.method = 'http://example.com/'
args.url = 'data=field'
args.items = []
def test_pretty_enabled_by_default_unless_stdin_redirected(self):
r = http('GET', 'http://httpbin.org/get', stdout_isatty=False)
self.assertNotIn(TERMINAL_COLOR_PRESENCE_CHECK, r)
self.parser._guess_method(args)
def test_force_pretty(self):
r = http('--pretty', 'GET', 'http://httpbin.org/get', stdout_isatty=False)
self.assertIn(TERMINAL_COLOR_PRESENCE_CHECK, r)
self.assertEquals(args.method, 'POST')
self.assertEquals(args.url, 'http://example.com/')
self.assertEquals(
args.items,
[cliparse.KeyValue(key='data', value='field', sep='=', orig='data=field')])
def test_force_ugly(self):
r = http('--ugly', 'GET', 'http://httpbin.org/get', stdout_isatty=True)
self.assertNotIn(TERMINAL_COLOR_PRESENCE_CHECK, r)
def test_guess_when_method_set_but_invalid_and_header_field(self):
args = argparse.Namespace()
args.method = 'http://example.com/'
args.url = 'test:header'
args.items = []
self.parser._guess_method(args)
class TestFileUpload(BaseTest):
self.assertEquals(args.method, 'GET')
self.assertEquals(args.url, 'http://example.com/')
self.assertEquals(
args.items,
[cliparse.KeyValue(key='test', value='header', sep=':', orig='test:header')])
def test_non_existent_file_raises_parse_error(self):
self.assertRaises(cliparse.ParseError, http,
'--form', '--traceback',
'POST', 'http://httpbin.org/post',
'foo@/__does_not_exist__')
def test_guess_when_method_set_but_invalid_and_item_exists(self):
args = argparse.Namespace()
args.method = 'http://example.com/'
args.url = 'new_item=a'
args.items = [
cliparse.KeyValue(key='old_item', value='b', sep='=', orig='old_item=b')
]
def test_upload_ok(self):
r = http('--form', 'POST', 'http://httpbin.org/post',
'test-file@%s' % TEST_FILE)
self.assertIn('"test-file": "__test_file_content__', r)
self.parser._guess_method(args)
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)
self.assertEquals(args.items, [
cliparse.KeyValue(key='new_item', value='a', sep='=', orig='new_item=a'),
cliparse.KeyValue(key='old_item', value='b', sep='=', orig='old_item=b'),
])
if __name__ == '__main__':

19
tox.ini Normal file
View File

@ -0,0 +1,19 @@
# Tox (http://tox.testrun.org/) is a tool for running tests
# in multiple virtualenvs. This configuration file will run the
# test suite on all supported python versions. To use it, "pip install tox"
# and then run "tox" from this directory.
[tox]
envlist = py26, py27, py30, py31, py32, pypy
[testenv]
commands = {envpython} setup.py test
[testenv:py26]
deps = argparse
[testenv:py30]
deps = argparse
[testenv:py31]
deps = argparse