From 87806acc567fa9da4b48f05245f908bcc5288f81 Mon Sep 17 00:00:00 2001 From: Jakub Roztocil Date: Sat, 26 Apr 2014 23:04:45 +0200 Subject: [PATCH] Cleanup --- httpie/input.py | 12 ++++-------- tests/fixtures/__init__.py | 17 ++++++++++------- tests/test_auth.py | 33 ++++++++++++--------------------- tests/test_stream.py | 6 ------ 4 files changed, 26 insertions(+), 42 deletions(-) diff --git a/httpie/input.py b/httpie/input.py index f1ebcde7..11924cd5 100644 --- a/httpie/input.py +++ b/httpie/input.py @@ -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` diff --git a/tests/fixtures/__init__.py b/tests/fixtures/__init__.py index ad6e9343..de309c9d 100644 --- a/tests/fixtures/__init__.py +++ b/tests/fixtures/__init__.py @@ -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) diff --git a/tests/test_auth.py b/tests/test_auth.py index 1f405419..a0c249cd 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -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'} diff --git a/tests/test_stream.py b/tests/test_stream.py index a8e8b21e..b1762afd 100644 --- a/tests/test_stream.py +++ b/tests/test_stream.py @@ -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