From 7e38f9ccf025efb926971dc4413d940d38ba1f8c Mon Sep 17 00:00:00 2001 From: Nicolas Beltran Date: Tue, 14 Jul 2020 16:33:07 -0500 Subject: [PATCH] Added additional tests for flag and better documentation --- README.rst | 2 +- tests/test_downloads.py | 9 +++++--- tests/test_output.py | 47 +++++++++++++++++++++++++++++++++++++---- 3 files changed, 50 insertions(+), 8 deletions(-) diff --git a/README.rst b/README.rst index 000939ae..9a03b3e8 100644 --- a/README.rst +++ b/README.rst @@ -1156,7 +1156,7 @@ be printed via several options: ``--verbose, -v`` Print the whole HTTP exchange (request and response). This option also enables ``--all`` (see below). ``--print, -p`` Selects parts of the HTTP exchange. -``--quiet, -q`` Doesn't print anything. +``--quiet, -q`` Doesn't print anything. Overrides other output flags. ================= ===================================================== ``--verbose`` can often be useful for debugging the request and generating diff --git a/tests/test_downloads.py b/tests/test_downloads.py index ab6da161..48180180 100644 --- a/tests/test_downloads.py +++ b/tests/test_downloads.py @@ -5,6 +5,7 @@ from urllib.request import urlopen import pytest import mock +import requests from requests.structures import CaseInsensitiveDict from httpie.downloads import ( @@ -190,10 +191,12 @@ class TestDownloads: finally: os.chdir(orig_cwd) - def test_download_quietflag(self, httpbin_both, httpbin): + def test_download_with_quiet_flag(self, httpbin_both, httpbin): robots_txt = '/robots.txt' - body = urlopen(httpbin + robots_txt).read().decode() + body = requests.get(httpbin + robots_txt).text env = MockEnvironment(stdin_isatty=True, stdout_isatty=False) r = http('--quiet', '--download', httpbin_both.url + robots_txt, env=env) assert r.stderr == '' - assert body == r + assert env.devnull == env.stderr + assert env.devnull == env.stdout + diff --git a/tests/test_output.py b/tests/test_output.py index e0a395c1..4de77196 100644 --- a/tests/test_output.py +++ b/tests/test_output.py @@ -1,6 +1,8 @@ import argparse +import mock import json import os +import io from tempfile import gettempdir from urllib.request import urlopen @@ -33,13 +35,50 @@ def test_output_option(httpbin, stdout_isatty): class TestQuietFlag: - def test_quiet(self, httpbin): - r = http('--quiet', 'GET', httpbin.url + '/get') - # check stdin + + @pytest.mark.parametrize('argument_name', ['--quiet', '-q']) + def test_quiet(self, httpbin, argument_name): + env = MockEnvironment(stdin_isatty=True, stdout_isatty=True) + r = http(argument_name, 'GET', httpbin.url + '/get', env=env) + assert env.stdout == env.devnull + assert env.stderr == env.devnull assert r.stderr == '' - # Check stdout assert str(r) == '' + @pytest.mark.parametrize('argument_name', ['--quiet', '-q']) + def test_quiet_correct_message(self, httpbin, argument_name): + sim_devnull = io.BytesIO() + env = MockEnvironment(stdin_isatty=True, stdout_isatty=True) + env.devnull = sim_devnull + r = http(argument_name, 'GET', httpbin.url + '/get', env=env) + assert env.stdout == env.devnull + assert env.stderr == env.devnull + assert r.stderr == '' + assert HTTP_OK in r.devnull.decode() + assert str(r) == '' + + @mock.patch('httpie.cli.argtypes.AuthCredentials._getpass', + new=lambda self, prompt:'password') + def test_quiet_password_prompt(self, httpbin): + """ Tests whether httpie still prompts for password when request + requires authetication and only username is provided""" + env = MockEnvironment(stdin_isatty=True, stdout_isatty=True) + env.devnull = io.BytesIO() + r = http('--quiet', '--auth', 'user','GET', httpbin.url + '/basic-auth/user/password', env=env) + assert HTTP_OK in r.devnull.decode() + assert env.stdout == env.devnull + assert env.stderr == env.devnull + assert str(r) == '' + assert r.stderr == '' + + @pytest.mark.parametrize('argument_name', ['-h', '-b', '-v', '-p=hH']) + def test_quiet_flag_overrides_other_output_options(self, httpbin, argument_name): + env = MockEnvironment(stdin_isatty=True, stdout_isatty=True) + r = http('--quiet', argument_name, httpbin.url + '/GET', env=env) + assert env.stdout == env.devnull + assert env.stderr == env.devnull + assert str(r) == '' + assert r.stderr == '' class TestVerboseFlag: def test_verbose(self, httpbin):