Skip to content

Commit

Permalink
Added --ignore-stdin
Browse files Browse the repository at this point in the history
Closes #150
  • Loading branch information
jkbrzt committed Aug 23, 2013
1 parent 00de49f commit f7b703b
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 8 deletions.
15 changes: 12 additions & 3 deletions README.rst
Expand Up @@ -717,6 +717,10 @@ on the command line:
$ echo 'data' | http POST example.org more=data # This is invalid
To prevent HTTPie from reading ``stdin`` data you can use the
``--ignore-stdin`` option.
-------------------------
Body Data From a Filename
-------------------------
Expand Down Expand Up @@ -1060,14 +1064,18 @@ When using HTTPie from **shell scripts**, it can be handy to set the
``--check-status`` flag. It instructs HTTPie to exit with an error if the
HTTP status is one of ``3xx``, ``4xx``, or ``5xx``. The exit status will
be ``3`` (unless ``--follow`` is set), ``4``, or ``5``,
respectively. Also, the ``--timeout`` option allows to overwrite the default
30s timeout:
respectively.
The ``--ignore-stdin`` option prevents HTTPie from reading data from ``stdin``,
which is usually not desirable during non-interactive invocations.
Also, the ``--timeout`` option allows to overwrite the default 30s timeout:
.. code-block:: bash
#!/bin/bash
if http --timeout=2.5 --check-status HEAD example.org/health &> /dev/null; then
if http --check-status --ignore-stdin --timeout=2.5 HEAD example.org/health &> /dev/null; then
echo 'OK!'
else
case $? in
Expand Down Expand Up @@ -1197,6 +1205,7 @@ Changelog
*You can click a version name to see a diff with the previous one.*
* `0.7.0-dev`_
* Added ``--ignore-stdin``.
* `0.6.0`_ (2013-06-03)
* XML data is now formatted.
* ``--session`` and ``--session-read-only`` now also accept paths to
Expand Down
9 changes: 9 additions & 0 deletions httpie/cli.py
Expand Up @@ -466,6 +466,15 @@ def _split_lines(self, text, width):

troubleshooting = parser.add_argument_group(title='Troubleshooting')

troubleshooting.add_argument(
'--ignore-stdin',
action='store_true',
default=False,
help="""
Do not attempt to read stdin.
"""
)
troubleshooting.add_argument(
'--help',
action='help',
Expand Down
16 changes: 11 additions & 5 deletions httpie/input.py
Expand Up @@ -115,7 +115,7 @@ def parse_args(self, env, args=None, namespace=None):
self._process_pretty_options()
self._guess_method()
self._parse_items()
if not env.stdin_isatty:
if not self.args.ignore_stdin and not env.stdin_isatty:
self._body_from_file(self.env.stdin)
if not (self.args.url.startswith((HTTP, HTTPS))):
# Default to 'https://' if invoked as `https args`.
Expand Down Expand Up @@ -184,6 +184,9 @@ def _process_auth(self):
if self.args.auth:
if not self.args.auth.has_password():
# Stdin already read (if not a tty) so it's save to prompt.
if self.args.ignore_stdin:
self.error('Unable to prompt for passwords because'
' --ignore-stdin is set.')
self.args.auth.prompt_password(url.netloc)

elif url.username is not None:
Expand Down Expand Up @@ -241,7 +244,7 @@ def _guess_method(self):
if self.args.method is None:
# Invoked as `http URL'.
assert not self.args.items
if not self.env.stdin_isatty:
if not self.args.ignore_stdin and not self.env.stdin_isatty:
self.args.method = HTTP_POST
else:
self.args.method = HTTP_GET
Expand All @@ -266,9 +269,12 @@ def _guess_method(self):
# Set the URL correctly
self.args.url = self.args.method
# Infer the method
has_data = not self.env.stdin_isatty or any(
item.sep in SEP_GROUP_DATA_ITEMS
for item in self.args.items
has_data = (
(not self.args.ignore_stdin and
not self.env.stdin_isatty) or any(
item.sep in SEP_GROUP_DATA_ITEMS
for item in self.args.items
)
)
self.args.method = HTTP_POST if has_data else HTTP_GET

Expand Down
30 changes: 30 additions & 0 deletions tests/tests.py
Expand Up @@ -812,6 +812,7 @@ def test_request_body_from_file_by_path(self):
# FIXME: *sometimes* fails on py33, the content-type is form.
# https://github.com/jkbr/httpie/issues/140
r = http(
'--verbose',
'POST',
httpbin('/post'),
'@' + FILE_PATH_ARG
Expand Down Expand Up @@ -1082,6 +1083,30 @@ def test_redirected_stream(self):
self.assertIn(BIN_FILE_CONTENT, r)


class IgnoreStdinTest(BaseTestCase):

def test_ignore_stdin(self):
with open(FILE_PATH) as f:
r = http(
'--ignore-stdin',
'--verbose',
httpbin('/get'),
env=TestEnvironment(stdin=f, stdin_isatty=False)
)
self.assertIn(OK, r)
self.assertIn('GET /get HTTP', r) # Don't default to POST.
self.assertNotIn(FILE_CONTENT, r) # Don't send stdin data.

def test_ignore_stdin_cannot_prompt_password(self):
r = http(
'--ignore-stdin',
'--auth=username-without-password',
httpbin('/get'),
)
self.assertEqual(r.exit_status, ExitStatus.ERROR)
self.assertIn('because --ignore-stdin', r.stderr)


class LineEndingsTest(BaseTestCase):
"""Test that CRLF is properly used in headers and
as the headers/body separator."""
Expand Down Expand Up @@ -1234,6 +1259,7 @@ def test_guess_when_method_set_and_valid(self):
self.parser.args.method = 'GET'
self.parser.args.url = 'http://example.com/'
self.parser.args.items = []
self.parser.args.ignore_stdin = False

self.parser.env = TestEnvironment()

Expand All @@ -1249,6 +1275,7 @@ def test_guess_when_method_not_set(self):
self.parser.args.method = None
self.parser.args.url = 'http://example.com/'
self.parser.args.items = []
self.parser.args.ignore_stdin = False
self.parser.env = TestEnvironment()

self.parser._guess_method()
Expand All @@ -1262,6 +1289,7 @@ def test_guess_when_method_set_but_invalid_and_data_field(self):
self.parser.args.method = 'http://example.com/'
self.parser.args.url = 'data=field'
self.parser.args.items = []
self.parser.args.ignore_stdin = False
self.parser.env = TestEnvironment()
self.parser._guess_method()

Expand All @@ -1277,6 +1305,7 @@ def test_guess_when_method_set_but_invalid_and_header_field(self):
self.parser.args.method = 'http://example.com/'
self.parser.args.url = 'test:header'
self.parser.args.items = []
self.parser.args.ignore_stdin = False

self.parser.env = TestEnvironment()

Expand All @@ -1297,6 +1326,7 @@ def test_guess_when_method_set_but_invalid_and_item_exists(self):
input.KeyValue(
key='old_item', value='b', sep='=', orig='old_item=b')
]
self.parser.args.ignore_stdin = False

self.parser.env = TestEnvironment()

Expand Down

0 comments on commit f7b703b

Please sign in to comment.