1
0
mirror of https://github.com/httpie/cli.git synced 2024-11-24 08:22:22 +02:00
This commit is contained in:
Jakub Roztocil 2014-04-26 23:04:45 +02:00
parent 1169a3eb23
commit 87806acc56
4 changed files with 26 additions and 42 deletions

View File

@ -170,27 +170,23 @@ class Parser(ArgumentParser):
if not self.env.stdout_isatty and self.args.output_file:
self.error('Cannot use --output, -o with redirected output.')
# FIXME: Come up with a cleaner solution.
if self.args.download:
# FIXME: Come up with a cleaner solution.
if not self.env.stdout_isatty:
# Use stdout as tge download output file.
# Use stdout as the download output file.
self.args.output_file = self.env.stdout
# With `--download`, we write everything that would normally go to
# `stdout` to `stderr` instead. Let's replace the stream so that
# we don't have to use many `if`s throughout the codebase.
# The response body will be treated separately.
self.env.stdout = self.env.stderr
self.env.stdout_isatty = self.env.stderr_isatty
elif self.args.output_file:
# When not `--download`ing, then `--output` simply replaces
# `stdout`. The file is opened for appending, which isn't what
# we want in this case.
self.args.output_file.seek(0)
self.args.output_file.truncate()
self.env.stdout = self.args.output_file
self.env.stdout_isatty = False
@ -289,7 +285,7 @@ class Parser(ArgumentParser):
except ArgumentTypeError as e:
if self.args.traceback:
raise
self.error(e.message)
self.error(e.args[0])
else:
# Set the URL correctly
@ -323,7 +319,7 @@ class Parser(ArgumentParser):
except ParseError as e:
if self.args.traceback:
raise
self.error(e.message)
self.error(e.args[0])
if self.args.files and not self.args.form:
# `http url @/path/to/file`

View File

@ -1,18 +1,21 @@
import os
import codecs
from tests import TESTS_ROOT
def patharg(path):
"""Back slashes need to be escaped in ITEM args, even in Windows paths."""
"""
Back slashes need to be escaped in ITEM args,
even in Windows paths.
"""
return path.replace('\\', '\\\\\\')
### Test files
FILE_PATH = os.path.join(TESTS_ROOT, 'fixtures', 'test.txt')
JSON_FILE_PATH = os.path.join(TESTS_ROOT, 'fixtures', 'test.json')
BIN_FILE_PATH = os.path.join(TESTS_ROOT, 'fixtures', 'test.bin')
FIXTURES_ROOT = os.path.abspath(os.path.dirname(__file__))
FILE_PATH = os.path.join(FIXTURES_ROOT, 'test.txt')
JSON_FILE_PATH = os.path.join(FIXTURES_ROOT, 'test.json')
BIN_FILE_PATH = os.path.join(FIXTURES_ROOT, 'test.bin')
FILE_PATH_ARG = patharg(FILE_PATH)
BIN_FILE_PATH_ARG = patharg(BIN_FILE_PATH)

View File

@ -1,6 +1,4 @@
"""HTTP authentication-related tests."""
from unittest import TestCase
import requests
import pytest
@ -8,45 +6,38 @@ from tests import http, httpbin, HTTP_OK
import httpie.input
class AuthTest(TestCase):
class TestAuth:
def test_basic_auth(self):
r = http('--auth=user:password', 'GET',
httpbin('/basic-auth/user/password'))
r = http('--auth=user:password',
'GET', httpbin('/basic-auth/user/password'))
assert HTTP_OK in r
assert '"authenticated": true' in r
assert '"user": "user"' in r
assert r.json == {'authenticated': True, 'user': 'user'}
@pytest.mark.skipif(
requests.__version__ == '0.13.6',
reason='Redirects with prefetch=False are broken in Requests 0.13.6')
def test_digest_auth(self):
r = http('--auth-type=digest', '--auth=user:password', 'GET',
httpbin('/digest-auth/auth/user/password'))
r = http('--auth-type=digest', '--auth=user:password',
'GET', httpbin('/digest-auth/auth/user/password'))
assert HTTP_OK in r
assert r'"authenticated": true' in r
assert r'"user": "user"', r
assert r.json == {'authenticated': True, 'user': 'user'}
def test_password_prompt(self):
httpie.input.AuthCredentials._getpass = lambda self, prompt: 'password'
r = http('--auth', 'user', 'GET', httpbin('/basic-auth/user/password'))
assert HTTP_OK in r
assert '"authenticated": true' in r
assert '"user": "user"' in r
assert r.json == {'authenticated': True, 'user': 'user'}
def test_credentials_in_url(self):
url = httpbin('/basic-auth/user/password')
url = 'http://user:password@' + url.split('http://', 1)[1]
url = httpbin('/basic-auth/user/password', auth='user:password')
r = http('GET', url)
assert HTTP_OK in r
assert '"authenticated": true' in r
assert '"user": "user"' in r
assert r.json == {'authenticated': True, 'user': 'user'}
def test_credentials_in_url_auth_flag_has_priority(self):
"""When credentials are passed in URL and via -a at the same time,
then the ones from -a are used."""
url = httpbin('/basic-auth/user/password')
url = 'http://user:wrong_password@' + url.split('http://', 1)[1]
url = httpbin('/basic-auth/user/password', auth='user:wrong')
r = http('--auth=user:password', 'GET', url)
assert HTTP_OK in r
assert '"authenticated": true' in r
assert '"user": "user"' in r
assert r.json == {'authenticated': True, 'user': 'user'}

View File

@ -20,8 +20,6 @@ class TestStream:
r = http('--verbose', '--pretty=all', '--stream', 'GET',
httpbin('/get'), env=env)
assert BINARY_SUPPRESSED_NOTICE.decode() in r
# We get 'Bad Request' but it's okay.
#self.assertIn(OK_COLOR, r)
def test_encoded_stream(self):
"""Test that --stream works with non-prettified
@ -31,8 +29,6 @@ class TestStream:
r = http('--pretty=none', '--stream', '--verbose', 'GET',
httpbin('/get'), env=env)
assert BINARY_SUPPRESSED_NOTICE.decode() in r
# We get 'Bad Request' but it's okay.
#self.assertIn(OK, r)
def test_redirected_stream(self):
"""Test that --stream works with non-prettified
@ -43,6 +39,4 @@ class TestStream:
stdin=f)
r = http('--pretty=none', '--stream', '--verbose', 'GET',
httpbin('/get'), env=env)
# We get 'Bad Request' but it's okay.
#self.assertIn(OK.encode(), r)
assert BIN_FILE_CONTENT in r