diff --git a/tests/test_downloads.py b/tests/test_downloads.py index 21623b56..494ea65a 100644 --- a/tests/test_downloads.py +++ b/tests/test_downloads.py @@ -191,11 +191,44 @@ class TestDownloads: finally: os.chdir(orig_cwd) - def test_download_with_quiet_flag(self, httpbin_both, httpbin): + def test_download_with_quiet_flag(self, httpbin): robots_txt = '/robots.txt' - 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 env.devnull == env.stderr - assert env.devnull == env.stdout + expected_body = requests.get(httpbin + robots_txt).text + expected_filename = 'robots.txt' + env = MockEnvironment(stdin_isatty=True, stdout_isatty=True) + orig_cwd = os.getcwd() + os.chdir(tempfile.mkdtemp(prefix='httpie_download_quiet_test_')) + try: + assert os.listdir('.') == [] + r = http('--quiet', '--download', httpbin + robots_txt, env=env) + assert os.listdir('.') == [expected_filename] + assert r.stderr == '' + assert env.devnull == env.stderr + assert env.devnull == env.stdout + with open(expected_filename, 'r') as f: + assert f.read() == expected_body + finally: + os.chdir(orig_cwd) + + def test_download_with_quiet_and_output_flag(self, httpbin): + robots_txt = '/robots.txt' + expected_body = requests.get(httpbin + robots_txt).text + expected_filename = 'test.txt' + env = MockEnvironment(stdin_isatty=True, stdout_isatty=True) + orig_cwd = os.getcwd() + os.chdir(tempfile.mkdtemp(prefix='httpie_download_quiet_test_')) + try: + assert os.listdir('.') == [] + r = http('--quiet', + '--download', + '--output=' + expected_filename, + httpbin + robots_txt, + env=env) + assert os.listdir('.') == [expected_filename] + assert r.stderr == '' + assert env.devnull == env.stderr + assert env.devnull == env.stdout + with open(expected_filename, 'r') as f: + assert f.read() == expected_body + finally: + os.chdir(orig_cwd) diff --git a/tests/test_output.py b/tests/test_output.py index 41ef5d3f..52b6b724 100644 --- a/tests/test_output.py +++ b/tests/test_output.py @@ -2,11 +2,13 @@ import argparse import mock import json import os +import tempfile import io from tempfile import gettempdir from urllib.request import urlopen import pytest +import requests from httpie.cli.argtypes import ( PARSED_DEFAULT_FORMAT_OPTIONS, @@ -80,6 +82,28 @@ class TestQuietFlag: assert str(r) == '' assert r.stderr == '' + def test_quiet_flag_with_output_redirection(self, httpbin): + robots_txt = '/robots.txt' + expected_body = requests.get(httpbin + robots_txt).text + expected_filename = 'test.txt' + env = MockEnvironment(stdin_isatty=True, stdout_isatty=True) + orig_cwd = os.getcwd() + os.chdir(tempfile.mkdtemp(prefix='httpie_download_quiet_test_')) + try: + assert os.listdir('.') == [] + r = http('--quiet', + '--output=' + expected_filename, + httpbin + robots_txt, + env=env) + assert os.listdir('.') == [expected_filename] + assert r.stderr == '' + assert env.devnull == env.stderr + assert env.devnull != env.stdout + with open(expected_filename, 'r') as f: + assert f.read() == expected_body + finally: + os.chdir(orig_cwd) + class TestVerboseFlag: def test_verbose(self, httpbin):