Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions httpie/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,22 @@ def request_body_read_callback(chunk: bytes):
)
prev_with_body = output_options.body

if (
final_response is not None
and final_response.status_code >= 400
and env.stdin
and not env.stdin_isatty
and not args.ignore_stdin
and args.raw is None
and initial_request is not None
and initial_request.body
):
env.log_error(
'stdin contents detected on a failed request. '
'Retry with --ignore-stdin if stdin was not intended as request body.',
level=LogLevel.WARNING,
)

# Cleanup
if force_separator:
separate()
Expand Down
62 changes: 62 additions & 0 deletions tests/test_ignore_stdin_warning.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""Regression tests for --ignore-stdin warning on failed requests with stdin (#1525)."""

import io
from unittest.mock import patch

import requests

from httpie.cli.definition import parser
from httpie.context import Environment
from httpie.core import program


def _make_pair(status_code: int, body=b'leaked-stdin'):
req = requests.Request('POST', 'http://example.com/api', data=body).prepare()
resp = requests.Response()
resp.status_code = status_code
resp.reason = 'Bad Request' if status_code == 400 else 'OK'
payload = b'Bad Request' if status_code == 400 else b'OK'
resp.raw = io.BytesIO(payload)
resp.raw.version = 11
resp.raw.status = status_code
resp.raw.reason = resp.reason
resp.request = req
resp.url = req.url
return [req, resp]


def _run_program(cli_args, status_code, stdin_isatty=False, stdin_bytes=b'leaked-stdin'):
stdout = io.BytesIO()
stderr = io.StringIO()
stdin = io.BytesIO(stdin_bytes)
env = Environment(
stdin=stdin,
stdin_isatty=stdin_isatty,
stdout=stdout,
stdout_isatty=False,
stderr=stderr,
stderr_isatty=False,
)
args = parser.parse_args(args=cli_args, env=env)
messages = _make_pair(
status_code,
body=stdin_bytes if not args.ignore_stdin and not stdin_isatty else None,
)
with patch('httpie.core.collect_messages', return_value=messages):
program(args=args, env=env)
return stderr.getvalue()


def test_warns_about_ignore_stdin_on_failed_request_with_stdin():
err = _run_program(['http://example.com/api'], status_code=400, stdin_isatty=False)
assert '--ignore-stdin' in err


def test_no_warning_when_ignore_stdin_is_passed():
err = _run_program(['--ignore-stdin', 'http://example.com/api'], status_code=400, stdin_isatty=False)
assert '--ignore-stdin' not in err


def test_no_warning_on_successful_request_with_stdin():
err = _run_program(['http://example.com/api'], status_code=200, stdin_isatty=False)
assert '--ignore-stdin' not in err
Loading