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

View File

@ -1,18 +1,21 @@
import os import os
import codecs import codecs
from tests import TESTS_ROOT
def patharg(path): 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('\\', '\\\\\\') return path.replace('\\', '\\\\\\')
### Test files FIXTURES_ROOT = os.path.abspath(os.path.dirname(__file__))
FILE_PATH = os.path.join(TESTS_ROOT, 'fixtures', 'test.txt') FILE_PATH = os.path.join(FIXTURES_ROOT, 'test.txt')
JSON_FILE_PATH = os.path.join(TESTS_ROOT, 'fixtures', 'test.json') JSON_FILE_PATH = os.path.join(FIXTURES_ROOT, 'test.json')
BIN_FILE_PATH = os.path.join(TESTS_ROOT, 'fixtures', 'test.bin') BIN_FILE_PATH = os.path.join(FIXTURES_ROOT, 'test.bin')
FILE_PATH_ARG = patharg(FILE_PATH) FILE_PATH_ARG = patharg(FILE_PATH)
BIN_FILE_PATH_ARG = patharg(BIN_FILE_PATH) BIN_FILE_PATH_ARG = patharg(BIN_FILE_PATH)

View File

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

View File

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