diff --git a/httpie/output/writer.py b/httpie/output/writer.py index 4a2949bce2..41de5738ed 100644 --- a/httpie/output/writer.py +++ b/httpie/output/writer.py @@ -46,7 +46,7 @@ def write_message( 'flush': env.stdout_isatty or processing_options.stream } try: - if env.is_windows and 'colors' in processing_options.get_prettify(env): + if env.is_windows and env.stdout_isatty and 'colors' in processing_options.get_prettify(env): write_stream_with_colors_win(**write_stream_kwargs) else: write_stream(**write_stream_kwargs) diff --git a/tests/test_windows.py b/tests/test_windows.py index 80d392ae8f..42c7fc9e63 100644 --- a/tests/test_windows.py +++ b/tests/test_windows.py @@ -1,3 +1,5 @@ +from unittest import mock + import pytest from httpie.context import Environment @@ -23,3 +25,18 @@ def test_output_file_pretty_not_allowed_on_windows(self, tmp_path, httpbin): '--pretty=all', 'GET', httpbin + '/get', env=env, tolerate_error_exit_status=True) assert 'Only terminal output can be colorized on Windows' in r.stderr + + def test_windows_piped_pretty_uses_raw_stream(self, httpbin): + """When stdout is piped on Windows, write_stream() should be used + instead of write_stream_with_colors_win() so ANSI codes pass + through to pagers like `less -r`. Regression test for #799.""" + env = MockEnvironment( + is_windows=True, + stdout_isatty=False, + colors=256, + ) + with mock.patch('httpie.output.writer.write_stream') as write_stream, \ + mock.patch('httpie.output.writer.write_stream_with_colors_win') as write_colors_win: + http('--pretty=all', 'GET', httpbin + '/get', env=env) + write_stream.assert_called() + write_colors_win.assert_not_called()