mirror of
https://github.com/httpie/cli.git
synced 2025-08-14 08:58:47 +02:00
Compare commits
319 Commits
Author | SHA1 | Date | |
---|---|---|---|
747be30d2e | |||
88a9583f4c | |||
fd6e87914c | |||
6dee49357d | |||
df36d6255d | |||
e92b831e6e | |||
fd44f1af93 | |||
b6309547d5 | |||
3a46149de1 | |||
b7c8bf0800 | |||
69d010a11b | |||
42ff243400 | |||
933b438e5f | |||
358342d1c9 | |||
c591a3810d | |||
0eba037037 | |||
3898129e9c | |||
b88e88d2e3 | |||
d1407baf76 | |||
d5032ca859 | |||
f6a19cf552 | |||
74979f3b33 | |||
698eb51e60 | |||
ae8030c930 | |||
2e96d7ffbb | |||
b5625e3d75 | |||
932d3224f4 | |||
b596fedf13 | |||
96444f3345 | |||
89b66f1608 | |||
a7d570916d | |||
ab5a50cee8 | |||
91961c6b51 | |||
256ea7d49d | |||
2cd6ea3050 | |||
37dddf5bf7 | |||
e508c631f2 | |||
55530c8c6d | |||
eb929cbc04 | |||
2490bb25ca | |||
2038fa02e3 | |||
59d51ad513 | |||
61568f1def | |||
f93f4fa7c7 | |||
bf73b5701e | |||
7917f1b40c | |||
a50660cc70 | |||
749b1e2aca | |||
137889a267 | |||
c9c6f0fae5 | |||
6fd1ea0e5a | |||
8f7676a2a9 | |||
87e661c5f1 | |||
8ca333dda0 | |||
0f4dce98c7 | |||
05547224ce | |||
6301fee3d2 | |||
a803e845a5 | |||
11be041e06 | |||
7f5fd130c5 | |||
ec899d70b7 | |||
4d3b4fa0be | |||
27c557e983 | |||
7f24f7d34c | |||
4b61108005 | |||
8b189725fd | |||
1719ebded6 | |||
c5d6a4ad8e | |||
91e1fe2d0f | |||
ca7f41de53 | |||
46e24dd6b5 | |||
803127e8c9 | |||
4c138959ea | |||
91a28973bd | |||
02b28093a8 | |||
d64e7d8a6a | |||
8841b8bf46 | |||
6472ca55e1 | |||
37c3307018 | |||
0aab796960 | |||
95c33e31a2 | |||
9af833da30 | |||
dfe6245cd6 | |||
555761f3cb | |||
643735ef23 | |||
7a45f14542 | |||
e993f83355 | |||
d726a4cd92 | |||
8d3f09497b | |||
31c78c2885 | |||
9776a6dea0 | |||
f1d4861fae | |||
d99e1ff492 | |||
a196d1d451 | |||
02209c2db1 | |||
9886f01f91 | |||
a4f796fe69 | |||
c948f98b05 | |||
b0fde07cfd | |||
f74670fac1 | |||
7321b9fa4e | |||
cf8d5eb3e8 | |||
64af72eb88 | |||
de38f86730 | |||
244ad15c92 | |||
586f45e634 | |||
b1b4743663 | |||
5600b4a2d3 | |||
9261167a1f | |||
519654e21b | |||
4840499a43 | |||
ee6cdf4ab3 | |||
98003f545d | |||
0046ed73c6 | |||
66a6475064 | |||
97804802c0 | |||
c9296a9a45 | |||
64a41c2601 | |||
0af6ae1be4 | |||
d0fc10cf1a | |||
fe1d0b0a1e | |||
f133dbf22c | |||
9d93b07a9d | |||
761cdbf8be | |||
3a3aecca45 | |||
fb3a26586a | |||
cc9083f541 | |||
9ae86f3b4f | |||
3a6fd074a1 | |||
da59381b0b | |||
6de2d6c2cb | |||
b9b033ed0c | |||
64d6363565 | |||
923b7acbe6 | |||
2efc0db8d4 | |||
2bf71af286 | |||
0b84180485 | |||
5a1bd4ba83 | |||
3f7ed35238 | |||
47fd392c74 | |||
54a63a810e | |||
a49774d3ab | |||
b879d38b07 | |||
0913e8b2ef | |||
4fef4b9a75 | |||
bfc23b1412 | |||
6267f21f21 | |||
e9aba543b1 | |||
9b23a4ac9a | |||
b96eba336d | |||
48a6d234cb | |||
c6f2b32e36 | |||
64f6f69037 | |||
6bdfc7a071 | |||
497a91711a | |||
f515ef72d0 | |||
22a2fddc79 | |||
1847eaa299 | |||
e387c1d43e | |||
fc6d89913f | |||
d584686744 | |||
b565be4318 | |||
87e44ae639 | |||
0d08732397 | |||
c53a778f60 | |||
5efc9010cc | |||
08e883fcfe | |||
c4b309164f | |||
8e96238323 | |||
8a9206eceb | |||
8ac3c5961c | |||
487c7a9221 | |||
6d65668355 | |||
3e5115e4a2 | |||
2b8b572f22 | |||
af737fd338 | |||
ee375b6942 | |||
6b06d92a59 | |||
becb63de9a | |||
86c8abc485 | |||
8f6bee9196 | |||
9c2c058ae5 | |||
6238b59e72 | |||
702c21aa91 | |||
aab5cd9da0 | |||
8c0f0b578c | |||
bb4881a873 | |||
3a1726b4ed | |||
e1fa57d228 | |||
bfc64bce21 | |||
595dc51b2d | |||
83fa772247 | |||
49a0fb6e0f | |||
41e822ca2f | |||
1124d68946 | |||
c3735d0422 | |||
364b91cbc4 | |||
c8e06b55e1 | |||
5acbc904b7 | |||
0c7c248dce | |||
caf60cbc65 | |||
2b0e642842 | |||
e25948f6a0 | |||
b565b4628e | |||
65081b2f12 | |||
963b2746f5 | |||
098257c0be | |||
30eb0c2f26 | |||
9fbe745987 | |||
01a546eedd | |||
eba6b63c55 | |||
ec245a1e80 | |||
33eb9acd92 | |||
293295cad6 | |||
557911b606 | |||
5300b0b490 | |||
001bda1945 | |||
7c68d87c10 | |||
35a99fe04b | |||
76e15b227c | |||
8881ebf033 | |||
25d1e8e418 | |||
7ce6eb148e | |||
6e1dbadff9 | |||
a6ebc44a48 | |||
5e03aeceb7 | |||
13ee9389aa | |||
bb49a1f979 | |||
4e574e6b8e | |||
529981af7a | |||
6731cb881a | |||
f7d1b739e2 | |||
5bdf4a3bae | |||
2d9414d34c | |||
20823c1702 | |||
5dbd104c3b | |||
13a979ad11 | |||
4cfa143bfe | |||
d24f30d0af | |||
66e168b2af | |||
564670566c | |||
ecbbad816a | |||
0432694661 | |||
dc4da527db | |||
38e8ef14ec | |||
c73dcaf63d | |||
fb85509e91 | |||
a2dca1e3bb | |||
c2dae62af0 | |||
ae7008ee96 | |||
f6824f7ade | |||
7fd46e0b0d | |||
d4067fcb6d | |||
20f01709ea | |||
56afd1adb9 | |||
5e87a2d7e5 | |||
d30e28c2c7 | |||
0d2d24eac7 | |||
e2751e5fa3 | |||
2a25d71aa4 | |||
01ca7f0eb2 | |||
4f8d6c013b | |||
e83e554ffb | |||
345f5a02a2 | |||
f96f0ef9ed | |||
74e4d0b678 | |||
0fc1f61f3d | |||
c50413a9c1 | |||
9f8c452e7e | |||
776328c818 | |||
9312fabc01 | |||
48ce934dfa | |||
3625bb6fa1 | |||
a97f0d52f6 | |||
41b0286f37 | |||
fee54b04d8 | |||
73e0455896 | |||
3b217daddc | |||
e5e5d0ce6d | |||
f43e473de1 | |||
0a002ec554 | |||
576ee83d82 | |||
e42f7b8fc9 | |||
b44e16ed0f | |||
ed08ab133e | |||
5408fb0fb9 | |||
e18b609ef7 | |||
356e043651 | |||
c6d4f6cdf6 | |||
dc1371d4d6 | |||
e2235e56dc | |||
763935b77f | |||
6435532f72 | |||
11a37067e7 | |||
25f0156502 | |||
0f8d04b4df | |||
e385ed6a99 | |||
01fdab55e9 | |||
1127557742 | |||
5898879395 | |||
8c33e5e3d3 | |||
10da7b63a3 | |||
df193a373f | |||
c2f8c36952 | |||
56f498c153 | |||
59e22b16b8 | |||
d32d6f29a9 | |||
274dddfb45 | |||
deb7b747cc | |||
018e1f68de | |||
ac69d4311b | |||
c75c4fa2a6 | |||
a6a79e92e4 | |||
ea76542150 | |||
c6690e0182 | |||
c82c9f0ae4 | |||
84b81c00ea | |||
34c6958dc8 | |||
6259b5dd3b |
12
.github/FUNDING.yml
vendored
Normal file
12
.github/FUNDING.yml
vendored
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
# These are supported funding model platforms
|
||||||
|
|
||||||
|
github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
|
||||||
|
patreon: # Replace with a single Patreon username
|
||||||
|
open_collective: # Replace with a single Open Collective username
|
||||||
|
ko_fi: # Replace with a single Ko-fi username
|
||||||
|
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
|
||||||
|
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
|
||||||
|
liberapay: # Replace with a single Liberapay username
|
||||||
|
issuehunt: # Replace with a single IssueHunt username
|
||||||
|
otechie: # Replace with a single Otechie username
|
||||||
|
custom: https://paypal.me/roztocil
|
18
.gitignore
vendored
18
.gitignore
vendored
@ -1,11 +1,13 @@
|
|||||||
dist
|
.DS_Store
|
||||||
httpie.egg-info
|
.idea/
|
||||||
build
|
__pycache__/
|
||||||
|
dist/
|
||||||
|
build/
|
||||||
|
*.egg-info
|
||||||
|
.cache/
|
||||||
|
.tox/
|
||||||
|
.coverage
|
||||||
*.pyc
|
*.pyc
|
||||||
*.egg
|
*.egg
|
||||||
.tox
|
|
||||||
README.html
|
|
||||||
.coverage
|
|
||||||
htmlcov
|
htmlcov
|
||||||
.idea
|
.pytest_cache/
|
||||||
.DS_Store
|
|
||||||
|
96
.travis.yml
96
.travis.yml
@ -1,20 +1,96 @@
|
|||||||
# https://travis-ci.org/jkbrzt/httpie
|
# <https://travis-ci.org/jakubroztocil/httpie>
|
||||||
sudo: false
|
sudo: false
|
||||||
|
language: python
|
||||||
os:
|
os:
|
||||||
- linux
|
- linux
|
||||||
# - osx
|
|
||||||
env:
|
env:
|
||||||
global:
|
global:
|
||||||
- NEWEST_PYTHON=3.4
|
- NEWEST_PYTHON=3.7
|
||||||
language: python
|
|
||||||
python:
|
python:
|
||||||
- 2.6
|
# <https://docs.travis-ci.com/user/languages/python/>
|
||||||
|
|
||||||
- 2.7
|
- 2.7
|
||||||
|
|
||||||
|
# Python 3.4 fails installing packages
|
||||||
|
# <https://travis-ci.org/jakubroztocil/httpie/jobs/403263566#L636>
|
||||||
|
# - 3.4
|
||||||
|
|
||||||
|
- 3.5
|
||||||
|
- 3.6
|
||||||
|
# - 3.7 # is done in the matrix below as described in travis-ci/travis-ci#9069
|
||||||
- pypy
|
- pypy
|
||||||
- 3.3
|
|
||||||
- 3.4
|
# pypy3 currently fails because of a Flask issue
|
||||||
- pypy3
|
# - pypy3
|
||||||
|
|
||||||
|
cache: pip
|
||||||
|
matrix:
|
||||||
|
include:
|
||||||
|
# Add manually defined OS X builds
|
||||||
|
# <https://docs.travis-ci.com/user/multi-os/#Python-example-(unsupported-languages)>
|
||||||
|
- os: osx
|
||||||
|
language: generic
|
||||||
|
env:
|
||||||
|
# Stock OSX Python
|
||||||
|
- TOXENV=py27-osx-builtin
|
||||||
|
- BREW_PYTHON_PACKAGE=
|
||||||
|
- os: osx
|
||||||
|
language: generic
|
||||||
|
env:
|
||||||
|
# Latest Python 2.7 from Homebrew
|
||||||
|
- TOXENV=py27
|
||||||
|
- BREW_PYTHON_PACKAGE=python@2
|
||||||
|
- os: osx
|
||||||
|
language: generic
|
||||||
|
env:
|
||||||
|
# Latest Python 3.x from Homebrew
|
||||||
|
- TOXENV=py37 # <= needs to be kept up-to-date to reflect latest minor version
|
||||||
|
- BREW_PYTHON_PACKAGE=python@3
|
||||||
|
# Travis Python 3.7 must run sudo on
|
||||||
|
- os: linux
|
||||||
|
python: 3.7
|
||||||
|
env: TOXENV=py37
|
||||||
|
sudo: true # Required for Python 3.7
|
||||||
|
dist: xenial # Required for Python 3.7
|
||||||
|
# Add a codestyle-only build
|
||||||
|
- os: linux
|
||||||
|
python: 3.6
|
||||||
|
env: CODESTYLE_ONLY=true
|
||||||
|
install:
|
||||||
|
- |
|
||||||
|
if [[ $TRAVIS_OS_NAME == 'osx' ]]; then
|
||||||
|
if [[ -n "$BREW_PYTHON_PACKAGE" ]]; then
|
||||||
|
brew update
|
||||||
|
if ! brew list --versions "$BREW_PYTHON_PACKAGE" >/dev/null; then
|
||||||
|
brew install "$BREW_PYTHON_PACKAGE"
|
||||||
|
elif ! brew outdated "$BREW_PYTHON_PACKAGE"; then
|
||||||
|
brew upgrade "$BREW_PYTHON_PACKAGE"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
sudo pip2 install tox
|
||||||
|
fi
|
||||||
script:
|
script:
|
||||||
- make
|
- |
|
||||||
|
if [[ $TRAVIS_OS_NAME == 'linux' ]]; then
|
||||||
|
if [[ $CODESTYLE_ONLY ]]; then
|
||||||
|
make pycodestyle
|
||||||
|
else
|
||||||
|
make test
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
PATH="/usr/local/bin:$PATH" tox -e "$TOXENV"
|
||||||
|
fi
|
||||||
after_success:
|
after_success:
|
||||||
- if [[ $TRAVIS_PYTHON_VERSION == $NEWEST_PYTHON ]]; then pip install python-coveralls && coveralls; fi
|
- |
|
||||||
|
if [[ $TRAVIS_PYTHON_VERSION == $NEWEST_PYTHON && $TRAVIS_OS_NAME == 'linux' ]]; then
|
||||||
|
make coveralls
|
||||||
|
fi
|
||||||
|
notifications:
|
||||||
|
webhooks:
|
||||||
|
# options: [always|never|change] default: always
|
||||||
|
on_success: always
|
||||||
|
on_failure: always
|
||||||
|
on_start: always
|
||||||
|
urls:
|
||||||
|
# https://gitter.im/jkbrzt/httpie
|
||||||
|
- https://webhooks.gitter.im/e/c42fcd359a110d02830b
|
||||||
|
@ -2,12 +2,14 @@
|
|||||||
HTTPie authors
|
HTTPie authors
|
||||||
==============
|
==============
|
||||||
|
|
||||||
* `Jakub Roztocil <https://github.com/jkbrzt>`_
|
* `Jakub Roztocil <https://github.com/jakubroztocil>`_
|
||||||
|
|
||||||
|
|
||||||
Patches and ideas
|
Patches and ideas
|
||||||
-----------------
|
-----------------
|
||||||
|
|
||||||
|
`Complete list of contributors on GitHub <https://github.com/jakubroztocil/httpie/graphs/contributors>`_
|
||||||
|
|
||||||
* `Cláudia T. Delgado <https://github.com/claudiatd>`_ (logo)
|
* `Cláudia T. Delgado <https://github.com/claudiatd>`_ (logo)
|
||||||
* `Hank Gay <https://github.com/gthank>`_
|
* `Hank Gay <https://github.com/gthank>`_
|
||||||
* `Jake Basile <https://github.com/jakebasile>`_
|
* `Jake Basile <https://github.com/jakebasile>`_
|
||||||
@ -34,3 +36,5 @@ Patches and ideas
|
|||||||
* `Dennis Brakhane <https://github.com/brakhane>`_
|
* `Dennis Brakhane <https://github.com/brakhane>`_
|
||||||
* `Matt Layman <https://github.com/mblayman>`_
|
* `Matt Layman <https://github.com/mblayman>`_
|
||||||
* `Edward Yang <https://github.com/honorabrutroll>`_
|
* `Edward Yang <https://github.com/honorabrutroll>`_
|
||||||
|
|
||||||
|
|
||||||
|
182
CHANGELOG.rst
182
CHANGELOG.rst
@ -6,9 +6,128 @@ This document records all notable changes to `HTTPie <http://httpie.org>`_.
|
|||||||
This project adheres to `Semantic Versioning <http://semver.org/>`_.
|
This project adheres to `Semantic Versioning <http://semver.org/>`_.
|
||||||
|
|
||||||
|
|
||||||
`0.9.3`_ (2016-01-01)
|
`1.0.3`_ (2019-08-26)
|
||||||
-------------------------
|
-------------------------
|
||||||
|
|
||||||
|
* Fixed CVE-2019-10751 — the way the output filename is generated for
|
||||||
|
``--download`` requests without ``--output`` resulting in a redirect has
|
||||||
|
been changed to only consider the initial URL as the base for the generated
|
||||||
|
filename, and not the final one. This fixes a potential security issue under
|
||||||
|
the following scenario:
|
||||||
|
|
||||||
|
1. A ``--download`` request with no explicit ``--output`` is made (e.g.,
|
||||||
|
``$ http -d example.org/file.txt``), instructing httpie to
|
||||||
|
`generate the output filename <https://httpie.org/doc#downloaded-file-name>`_
|
||||||
|
from the ``Content-Disposition`` response, or from the URL if the header
|
||||||
|
is not provided.
|
||||||
|
2. The server handling the request has been modified by an attacker and
|
||||||
|
instead of the expected response the URL returns a redirect to another
|
||||||
|
URL, e.g., ``attacker.example.org/.bash_profile``, whose response does
|
||||||
|
not provide a ``Content-Disposition`` header (i.e., the base for the
|
||||||
|
generated filename becomes ``.bash_profile`` instead of ``file.txt``).
|
||||||
|
3. Your current directory doesn’t already contain ``.bash_profile``
|
||||||
|
(i.e., no unique suffix is added to the generated filename).
|
||||||
|
4. You don’t notice the potentially unexpected output filename
|
||||||
|
as reported by httpie in the console output
|
||||||
|
(e.g., ``Downloading 100.00 B to ".bash_profile"``).
|
||||||
|
|
||||||
|
Reported by Raul Onitza and Giulio Comi.
|
||||||
|
|
||||||
|
|
||||||
|
`1.0.2`_ (2018-11-14)
|
||||||
|
-------------------------
|
||||||
|
|
||||||
|
* Fixed tests for installation with pyOpenSSL.
|
||||||
|
|
||||||
|
|
||||||
|
`1.0.1`_ (2018-11-14)
|
||||||
|
-------------------------
|
||||||
|
|
||||||
|
* Removed external URL calls from tests.
|
||||||
|
|
||||||
|
|
||||||
|
`1.0.0`_ (2018-11-02)
|
||||||
|
-------------------------
|
||||||
|
|
||||||
|
* Added ``--style=auto`` which follows the terminal ANSI color styles.
|
||||||
|
* Added support for selecting TLS 1.3 via ``--ssl=tls1.3``
|
||||||
|
(available once implemented in upstream libraries).
|
||||||
|
* Added ``true``/``false`` as valid values for ``--verify``
|
||||||
|
(in addition to ``yes``/``no``) and the boolean value is case-insensitive.
|
||||||
|
* Changed the default ``--style`` from ``solarized`` to ``auto`` (on Windows it stays ``fruity``).
|
||||||
|
* Fixed default headers being incorrectly case-sensitive.
|
||||||
|
* Removed Python 2.6 support.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
`0.9.9`_ (2016-12-08)
|
||||||
|
---------------------
|
||||||
|
|
||||||
|
* Fixed README.
|
||||||
|
|
||||||
|
|
||||||
|
`0.9.8`_ (2016-12-08)
|
||||||
|
---------------------
|
||||||
|
|
||||||
|
* Extended auth plugin API.
|
||||||
|
* Added exit status code ``7`` for plugin errors.
|
||||||
|
* Added support for ``curses``-less Python installations.
|
||||||
|
* Fixed ``REQUEST_ITEM`` arg incorrectly being reported as required.
|
||||||
|
* Improved ``CTRL-C`` interrupt handling.
|
||||||
|
* Added the standard exit status code ``130`` for keyboard interrupts.
|
||||||
|
|
||||||
|
|
||||||
|
`0.9.6`_ (2016-08-13)
|
||||||
|
---------------------
|
||||||
|
|
||||||
|
* Added Python 3 as a dependency for Homebrew installations
|
||||||
|
to ensure some of the newer HTTP features work out of the box
|
||||||
|
for macOS users (starting with HTTPie 0.9.4.).
|
||||||
|
* Added the ability to unset a request header with ``Header:``, and send an
|
||||||
|
empty value with ``Header;``.
|
||||||
|
* Added ``--default-scheme <URL_SCHEME>`` to enable things like
|
||||||
|
``$ alias https='http --default-scheme=https``.
|
||||||
|
* Added ``-I`` as a shortcut for ``--ignore-stdin``.
|
||||||
|
* Added fish shell completion (located in ``extras/httpie-completion.fish``
|
||||||
|
in the Github repo).
|
||||||
|
* Updated ``requests`` to 2.10.0 so that SOCKS support can be added via
|
||||||
|
``pip install requests[socks]``.
|
||||||
|
* Changed the default JSON ``Accept`` header from ``application/json``
|
||||||
|
to ``application/json, */*``.
|
||||||
|
* Changed the pre-processing of request HTTP headers so that any leading
|
||||||
|
and trailing whitespace is removed.
|
||||||
|
|
||||||
|
|
||||||
|
`0.9.4`_ (2016-07-01)
|
||||||
|
---------------------
|
||||||
|
|
||||||
|
* Added ``Content-Type`` of files uploaded in ``multipart/form-data`` requests
|
||||||
|
* Added ``--ssl=<PROTOCOL>`` to specify the desired SSL/TLS protocol version
|
||||||
|
to use for HTTPS requests.
|
||||||
|
* Added JSON detection with ``--json, -j`` to work around incorrect
|
||||||
|
``Content-Type``
|
||||||
|
* Added ``--all`` to show intermediate responses such as redirects (with ``--follow``)
|
||||||
|
* Added ``--history-print, -P WHAT`` to specify formatting of intermediate responses
|
||||||
|
* Added ``--max-redirects=N`` (default 30)
|
||||||
|
* Added ``-A`` as short name for ``--auth-type``
|
||||||
|
* Added ``-F`` as short name for ``--follow``
|
||||||
|
* Removed the ``implicit_content_type`` config option
|
||||||
|
(use ``"default_options": ["--form"]`` instead)
|
||||||
|
* Redirected ``stdout`` doesn't trigger an error anymore when ``--output FILE``
|
||||||
|
is set
|
||||||
|
* Changed the default ``--style`` back to ``solarized`` for better support
|
||||||
|
of light and dark terminals
|
||||||
|
* Improved ``--debug`` output
|
||||||
|
* Fixed ``--session`` when used with ``--download``
|
||||||
|
* Fixed ``--download`` to trim too long filenames before saving the file
|
||||||
|
* Fixed the handling of ``Content-Type`` with multiple ``+subtype`` parts
|
||||||
|
* Removed the XML formatter as the implementation suffered from multiple issues
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
`0.9.3`_ (2016-01-01)
|
||||||
|
---------------------
|
||||||
|
|
||||||
* Changed the default color ``--style`` from ``solarized`` to ``monokai``
|
* Changed the default color ``--style`` from ``solarized`` to ``monokai``
|
||||||
* Added basic Bash autocomplete support (need to be installed manually)
|
* Added basic Bash autocomplete support (need to be installed manually)
|
||||||
* Added request details to connection error messages
|
* Added request details to connection error messages
|
||||||
@ -30,8 +149,8 @@ This project adheres to `Semantic Versioning <http://semver.org/>`_.
|
|||||||
---------------------
|
---------------------
|
||||||
|
|
||||||
* Added support for Requests transport adapter plugins
|
* Added support for Requests transport adapter plugins
|
||||||
(see `httpie-unixsocket <https://github.com/msabramo/httpie-unixsocket>`_
|
(see `httpie-unixsocket <https://github.com/httpie/httpie-unixsocket>`_
|
||||||
and `httpie-http2 <https://github.com/jkbrzt/httpie-http2>`_)
|
and `httpie-http2 <https://github.com/httpie/httpie-http2>`_)
|
||||||
|
|
||||||
|
|
||||||
`0.9.0`_ (2015-01-31)
|
`0.9.0`_ (2015-01-31)
|
||||||
@ -229,32 +348,39 @@ This project adheres to `Semantic Versioning <http://semver.org/>`_.
|
|||||||
* Many improvements and bug fixes
|
* Many improvements and bug fixes
|
||||||
|
|
||||||
|
|
||||||
`0.1`_ (2012-02-25)
|
`0.1.0`_ (2012-02-25)
|
||||||
-------------------
|
---------------------
|
||||||
|
|
||||||
* Initial public release
|
* Initial public release
|
||||||
|
|
||||||
|
|
||||||
.. _`0.1`: https://github.com/jkbrzt/httpie/commit/b966efa
|
.. _`0.1.0`: https://github.com/jakubroztocil/httpie/commit/b966efa
|
||||||
.. _0.1.4: https://github.com/jkbrzt/httpie/compare/b966efa...0.1.4
|
.. _0.1.4: https://github.com/jakubroztocil/httpie/compare/b966efa...0.1.4
|
||||||
.. _0.1.5: https://github.com/jkbrzt/httpie/compare/0.1.4...0.1.5
|
.. _0.1.5: https://github.com/jakubroztocil/httpie/compare/0.1.4...0.1.5
|
||||||
.. _0.1.6: https://github.com/jkbrzt/httpie/compare/0.1.5...0.1.6
|
.. _0.1.6: https://github.com/jakubroztocil/httpie/compare/0.1.5...0.1.6
|
||||||
.. _0.2.0: https://github.com/jkbrzt/httpie/compare/0.1.6...0.2.0
|
.. _0.2.0: https://github.com/jakubroztocil/httpie/compare/0.1.6...0.2.0
|
||||||
.. _0.2.1: https://github.com/jkbrzt/httpie/compare/0.2.0...0.2.1
|
.. _0.2.1: https://github.com/jakubroztocil/httpie/compare/0.2.0...0.2.1
|
||||||
.. _0.2.2: https://github.com/jkbrzt/httpie/compare/0.2.1...0.2.2
|
.. _0.2.2: https://github.com/jakubroztocil/httpie/compare/0.2.1...0.2.2
|
||||||
.. _0.2.5: https://github.com/jkbrzt/httpie/compare/0.2.2...0.2.5
|
.. _0.2.5: https://github.com/jakubroztocil/httpie/compare/0.2.2...0.2.5
|
||||||
.. _0.2.6: https://github.com/jkbrzt/httpie/compare/0.2.5...0.2.6
|
.. _0.2.6: https://github.com/jakubroztocil/httpie/compare/0.2.5...0.2.6
|
||||||
.. _0.2.7: https://github.com/jkbrzt/httpie/compare/0.2.5...0.2.7
|
.. _0.2.7: https://github.com/jakubroztocil/httpie/compare/0.2.5...0.2.7
|
||||||
.. _0.3.0: https://github.com/jkbrzt/httpie/compare/0.2.7...0.3.0
|
.. _0.3.0: https://github.com/jakubroztocil/httpie/compare/0.2.7...0.3.0
|
||||||
.. _0.4.0: https://github.com/jkbrzt/httpie/compare/0.3.0...0.4.0
|
.. _0.4.0: https://github.com/jakubroztocil/httpie/compare/0.3.0...0.4.0
|
||||||
.. _0.4.1: https://github.com/jkbrzt/httpie/compare/0.4.0...0.4.1
|
.. _0.4.1: https://github.com/jakubroztocil/httpie/compare/0.4.0...0.4.1
|
||||||
.. _0.5.0: https://github.com/jkbrzt/httpie/compare/0.4.1...0.5.0
|
.. _0.5.0: https://github.com/jakubroztocil/httpie/compare/0.4.1...0.5.0
|
||||||
.. _0.5.1: https://github.com/jkbrzt/httpie/compare/0.5.0...0.5.1
|
.. _0.5.1: https://github.com/jakubroztocil/httpie/compare/0.5.0...0.5.1
|
||||||
.. _0.6.0: https://github.com/jkbrzt/httpie/compare/0.5.1...0.6.0
|
.. _0.6.0: https://github.com/jakubroztocil/httpie/compare/0.5.1...0.6.0
|
||||||
.. _0.7.1: https://github.com/jkbrzt/httpie/compare/0.6.0...0.7.1
|
.. _0.7.1: https://github.com/jakubroztocil/httpie/compare/0.6.0...0.7.1
|
||||||
.. _0.8.0: https://github.com/jkbrzt/httpie/compare/0.7.1...0.8.0
|
.. _0.8.0: https://github.com/jakubroztocil/httpie/compare/0.7.1...0.8.0
|
||||||
.. _0.9.0: https://github.com/jkbrzt/httpie/compare/0.8.0...0.9.0
|
.. _0.9.0: https://github.com/jakubroztocil/httpie/compare/0.8.0...0.9.0
|
||||||
.. _0.9.1: https://github.com/jkbrzt/httpie/compare/0.9.0...0.9.1
|
.. _0.9.1: https://github.com/jakubroztocil/httpie/compare/0.9.0...0.9.1
|
||||||
.. _0.9.2: https://github.com/jkbrzt/httpie/compare/0.9.1...0.9.2
|
.. _0.9.2: https://github.com/jakubroztocil/httpie/compare/0.9.1...0.9.2
|
||||||
.. _0.9.3: https://github.com/jkbrzt/httpie/compare/0.9.2...0.9.3
|
.. _0.9.3: https://github.com/jakubroztocil/httpie/compare/0.9.2...0.9.3
|
||||||
.. _1.0.0-dev: https://github.com/jkbrzt/httpie/compare/0.9.3...master
|
.. _0.9.4: https://github.com/jakubroztocil/httpie/compare/0.9.3...0.9.4
|
||||||
|
.. _0.9.6: https://github.com/jakubroztocil/httpie/compare/0.9.4...0.9.6
|
||||||
|
.. _0.9.8: https://github.com/jakubroztocil/httpie/compare/0.9.6...0.9.8
|
||||||
|
.. _0.9.9: https://github.com/jakubroztocil/httpie/compare/0.9.8...0.9.9
|
||||||
|
.. _1.0.0: https://github.com/jakubroztocil/httpie/compare/0.9.9...1.0.0
|
||||||
|
.. _1.0.1: https://github.com/jakubroztocil/httpie/compare/1.0.0...1.0.1
|
||||||
|
.. _1.0.2: https://github.com/jakubroztocil/httpie/compare/1.0.1...1.0.2
|
||||||
|
.. _1.0.3: https://github.com/jakubroztocil/httpie/compare/1.0.2...1.0.3
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
|
######################
|
||||||
Contributing to HTTPie
|
Contributing to HTTPie
|
||||||
######################
|
######################
|
||||||
|
|
||||||
Bug reports and code and documentation patches are greatly appreciated. You can
|
Bug reports and code and documentation patches are welcome. You can
|
||||||
also help by using the development version of HTTPie and reporting any bugs you
|
help this project also by using the development version of HTTPie
|
||||||
might encounter.
|
and by reporting any bugs you might encounter.
|
||||||
|
|
||||||
Bug Reports
|
1. Reporting bugs
|
||||||
===========
|
=================
|
||||||
|
|
||||||
**It's important that you provide the full command argument list
|
**It's important that you provide the full command argument list
|
||||||
as well as the output of the failing command.**
|
as well as the output of the failing command.**
|
||||||
@ -15,21 +16,24 @@ to your bug report, e.g.:
|
|||||||
|
|
||||||
.. code-block:: bash
|
.. code-block:: bash
|
||||||
|
|
||||||
$ http --debug [arguments that trigger the error]
|
$ http --debug [COMPLETE ARGUMENT LIST THAT TRIGGERS THE ERROR]
|
||||||
[complete output]
|
[COMPLETE OUTPUT]
|
||||||
|
|
||||||
|
|
||||||
Contributing Code and Documentation
|
2. Contributing Code and Docs
|
||||||
===================================
|
=============================
|
||||||
|
|
||||||
Before working on a new feature or a bug, please browse `existing issues`_
|
Before working on a new feature or a bug, please browse `existing issues`_
|
||||||
to see whether it has been previously discussed. If the change in question
|
to see whether it has been previously discussed. If the change in question
|
||||||
is a bigger one, it's always good to discuss before your starting working on
|
is a bigger one, it's always good to discuss before you start working on
|
||||||
it.
|
it.
|
||||||
|
|
||||||
|
|
||||||
Development Environment
|
Creating Development Environment
|
||||||
-----------------------
|
--------------------------------
|
||||||
|
|
||||||
|
Go to https://github.com/jakubroztocil/httpie and fork the project repository.
|
||||||
|
|
||||||
|
|
||||||
.. code-block:: bash
|
.. code-block:: bash
|
||||||
|
|
||||||
@ -43,53 +47,71 @@ Development Environment
|
|||||||
|
|
||||||
# Install dev. requirements and also HTTPie (in editable mode
|
# Install dev. requirements and also HTTPie (in editable mode
|
||||||
# so that the `http' command will point to your working copy):
|
# so that the `http' command will point to your working copy):
|
||||||
make
|
make init
|
||||||
|
|
||||||
|
|
||||||
Making Changes
|
Making Changes
|
||||||
--------------
|
--------------
|
||||||
|
|
||||||
Please make sure your changes conform to `Style Guide for Python Code`_ (PEP8).
|
Please make sure your changes conform to `Style Guide for Python Code`_ (PEP8)
|
||||||
|
and that ``make pycodestyle`` passes.
|
||||||
|
|
||||||
|
|
||||||
Tests
|
Testing
|
||||||
-----
|
-------
|
||||||
|
|
||||||
Before opening a pull requests, please make sure the `test suite`_ passes
|
Before opening a pull requests, please make sure the `test suite`_ passes
|
||||||
in all of the `supported Python environments`_. You should also **add tests
|
in all of the `supported Python environments`_. You should also add tests
|
||||||
for any new features and bug fixes**.
|
for any new features and bug fixes.
|
||||||
|
|
||||||
HTTPie uses `pytest`_ and `Tox`_.
|
HTTPie uses `pytest`_ and `Tox`_ for testing.
|
||||||
|
|
||||||
|
|
||||||
|
Running all tests:
|
||||||
|
******************
|
||||||
|
|
||||||
.. code-block:: bash
|
.. code-block:: bash
|
||||||
|
|
||||||
### Running all tests:
|
# Run all tests on the current Python interpreter with coverage
|
||||||
|
|
||||||
# Current Python
|
|
||||||
make test
|
make test
|
||||||
|
|
||||||
# Current Python with coverage
|
# Run all tests in all of the supported and available Pythons via Tox
|
||||||
make test-cover
|
|
||||||
|
|
||||||
# All the supported and available Pythons via Tox
|
|
||||||
make test-tox
|
make test-tox
|
||||||
|
|
||||||
### Running specific tests:
|
# Run all tests for code as well as packaging, etc.
|
||||||
|
make test-all
|
||||||
|
|
||||||
# Current Python
|
# Test PEP8 compliance
|
||||||
pytest tests/test_uploads.py
|
make pycodestyle
|
||||||
|
|
||||||
# All Pythons
|
|
||||||
|
Running specific tests:
|
||||||
|
***********************
|
||||||
|
|
||||||
|
.. code-block:: bash
|
||||||
|
|
||||||
|
# Run specific tests on the current Python
|
||||||
|
py.test tests/test_uploads.py
|
||||||
|
py.test tests/test_uploads.py::TestMultipartFormDataFileUpload
|
||||||
|
py.test tests/test_uploads.py::TestMultipartFormDataFileUpload::test_upload_ok
|
||||||
|
|
||||||
|
# Run specific tests on the on all Pythons via Tox
|
||||||
|
# (change to `tox -e py37' to limit Python version)
|
||||||
tox -- tests/test_uploads.py --verbose
|
tox -- tests/test_uploads.py --verbose
|
||||||
|
tox -- tests/test_uploads.py::TestMultipartFormDataFileUpload --verbose
|
||||||
|
tox -- tests/test_uploads.py::TestMultipartFormDataFileUpload::test_upload_ok --verbose
|
||||||
|
|
||||||
|
-----
|
||||||
|
|
||||||
Don't forget to add yourself to `AUTHORS.rst`_.
|
See `Makefile`_ for additional development utilities.
|
||||||
|
Don't forget to add yourself to `AUTHORS`_!
|
||||||
|
|
||||||
|
|
||||||
.. _Tox: http://tox.testrun.org
|
.. _Tox: http://tox.testrun.org
|
||||||
.. _supported Python environments: https://github.com/jkbrzt/httpie/blob/master/tox.ini
|
.. _supported Python environments: https://github.com/jakubroztocil/httpie/blob/master/tox.ini
|
||||||
.. _existing issues: https://github.com/jkbrzt/httpie/issues?state=open
|
.. _existing issues: https://github.com/jakubroztocil/httpie/issues?state=open
|
||||||
.. _AUTHORS.rst: https://github.com/jkbrzt/httpie/blob/master/AUTHORS.rst
|
.. _AUTHORS: https://github.com/jakubroztocil/httpie/blob/master/AUTHORS.rst
|
||||||
|
.. _Makefile: https://github.com/jakubroztocil/httpie/blob/master/Makefile
|
||||||
.. _pytest: http://pytest.org/
|
.. _pytest: http://pytest.org/
|
||||||
.. _Style Guide for Python Code: http://python.org/dev/peps/pep-0008/
|
.. _Style Guide for Python Code: http://python.org/dev/peps/pep-0008/
|
||||||
.. _test suite: https://github.com/jkbrzt/httpie/tree/master/tests
|
.. _test suite: https://github.com/jakubroztocil/httpie/tree/master/tests
|
||||||
|
2
LICENSE
2
LICENSE
@ -1,4 +1,4 @@
|
|||||||
Copyright © 2012-2015 Jakub Roztocil <jakub@roztocil.co>
|
Copyright © 2012-2019 Jakub Roztocil <jakub@roztocil.co>
|
||||||
|
|
||||||
Redistribution and use in source and binary forms, with or without
|
Redistribution and use in source and binary forms, with or without
|
||||||
modification, are permitted provided that the following conditions are met:
|
modification, are permitted provided that the following conditions are met:
|
||||||
|
129
Makefile
129
Makefile
@ -1,38 +1,57 @@
|
|||||||
|
###############################################################################
|
||||||
|
# See ./CONTRIBUTING.rst
|
||||||
|
###############################################################################
|
||||||
|
|
||||||
VERSION=$(shell grep __version__ httpie/__init__.py)
|
VERSION=$(shell grep __version__ httpie/__init__.py)
|
||||||
REQUIREMENTS="requirements-dev.txt"
|
REQUIREMENTS="requirements-dev.txt"
|
||||||
TAG="\n\n\033[0;32m\#\#\# "
|
TAG="\n\n\033[0;32m\#\#\# "
|
||||||
END=" \#\#\# \033[0m\n"
|
END=" \#\#\# \033[0m\n"
|
||||||
|
|
||||||
|
|
||||||
all: test
|
all: test
|
||||||
|
|
||||||
uninstall-httpie:
|
|
||||||
@echo $(TAG)Removing existing installation of HTTPie$(END)
|
|
||||||
- pip uninstall --yes httpie >/dev/null
|
|
||||||
! which http
|
|
||||||
@echo
|
|
||||||
|
|
||||||
uninstall-all: uninstall-httpie
|
|
||||||
- pip uninstall --yes -r $(REQUIREMENTS)
|
|
||||||
|
|
||||||
init: uninstall-httpie
|
init: uninstall-httpie
|
||||||
@echo $(TAG)Installing dev requirements$(END)
|
@echo $(TAG)Installing dev requirements$(END)
|
||||||
pip install --upgrade -r $(REQUIREMENTS)
|
pip install --upgrade -r $(REQUIREMENTS)
|
||||||
|
|
||||||
@echo $(TAG)Installing HTTPie$(END)
|
@echo $(TAG)Installing HTTPie$(END)
|
||||||
pip install --upgrade --editable .
|
pip install --upgrade --editable .
|
||||||
|
|
||||||
@echo
|
@echo
|
||||||
|
|
||||||
|
clean:
|
||||||
|
@echo $(TAG)Cleaning up$(END)
|
||||||
|
rm -rf .tox *.egg dist build .coverage .cache .pytest_cache httpie.egg-info
|
||||||
|
find . -name '__pycache__' -delete -print -o -name '*.pyc' -delete -print
|
||||||
|
@echo
|
||||||
|
|
||||||
|
|
||||||
|
###############################################################################
|
||||||
|
# Testing
|
||||||
|
###############################################################################
|
||||||
|
|
||||||
|
|
||||||
test: init
|
test: init
|
||||||
@echo $(TAG)Running tests in on current Python with coverage $(END)
|
@echo $(TAG)Running tests on the current Python interpreter with coverage $(END)
|
||||||
py.test --cov ./httpie --cov ./tests --doctest-modules --verbose ./httpie ./tests
|
py.test --cov ./httpie --cov ./tests --doctest-modules --verbose ./httpie ./tests
|
||||||
@echo
|
@echo
|
||||||
|
|
||||||
|
|
||||||
|
# test-all is meant to test everything — even this Makefile
|
||||||
|
test-all: uninstall-all clean init test test-tox test-dist pycodestyle
|
||||||
|
@echo
|
||||||
|
|
||||||
|
|
||||||
|
test-dist: test-sdist test-bdist-wheel
|
||||||
|
@echo
|
||||||
|
|
||||||
|
|
||||||
test-tox: init
|
test-tox: init
|
||||||
@echo $(TAG)Running tests on all Pythons via Tox$(END)
|
@echo $(TAG)Running tests on all Pythons via Tox$(END)
|
||||||
tox
|
tox
|
||||||
@echo
|
@echo
|
||||||
|
|
||||||
test-dist: test-sdist test-bdist-wheel
|
|
||||||
@echo
|
|
||||||
|
|
||||||
test-sdist: clean uninstall-httpie
|
test-sdist: clean uninstall-httpie
|
||||||
@echo $(TAG)Testing sdist build an installation$(END)
|
@echo $(TAG)Testing sdist build an installation$(END)
|
||||||
@ -41,6 +60,7 @@ test-sdist: clean uninstall-httpie
|
|||||||
which http
|
which http
|
||||||
@echo
|
@echo
|
||||||
|
|
||||||
|
|
||||||
test-bdist-wheel: clean uninstall-httpie
|
test-bdist-wheel: clean uninstall-httpie
|
||||||
@echo $(TAG)Testing wheel build an installation$(END)
|
@echo $(TAG)Testing wheel build an installation$(END)
|
||||||
python setup.py bdist_wheel
|
python setup.py bdist_wheel
|
||||||
@ -48,20 +68,85 @@ test-bdist-wheel: clean uninstall-httpie
|
|||||||
which http
|
which http
|
||||||
@echo
|
@echo
|
||||||
|
|
||||||
# This tests everything, even this Makefile.
|
|
||||||
test-all: uninstall-all clean init test test-tox test-dist
|
|
||||||
|
|
||||||
publish: test-all
|
pycodestyle:
|
||||||
|
which pycodestyle || pip install pycodestyle
|
||||||
|
pycodestyle
|
||||||
|
@echo
|
||||||
|
|
||||||
|
|
||||||
|
coveralls:
|
||||||
|
which coveralls || pip install python-coveralls
|
||||||
|
coveralls
|
||||||
|
@echo
|
||||||
|
|
||||||
|
|
||||||
|
###############################################################################
|
||||||
|
# Publishing to PyPi
|
||||||
|
###############################################################################
|
||||||
|
|
||||||
|
|
||||||
|
publish: test-all publish-no-test
|
||||||
|
|
||||||
|
|
||||||
|
publish-no-test:
|
||||||
@echo $(TAG)Testing wheel build an installation$(END)
|
@echo $(TAG)Testing wheel build an installation$(END)
|
||||||
@echo "$(VERSION)"
|
@echo "$(VERSION)"
|
||||||
@echo "$(VERSION)" | grep -q "dev" && echo "!!!Not publishing dev version!!!" && exit 1
|
@echo "$(VERSION)" | grep -q "dev" && echo '!!!Not publishing dev version!!!' && exit 1 || echo ok
|
||||||
python setup.py register
|
python setup.py sdist bdist_wheel
|
||||||
python setup.py sdist upload
|
twine upload dist/*
|
||||||
python setup.py bdist_wheel upload
|
|
||||||
@echo
|
@echo
|
||||||
|
|
||||||
clean:
|
|
||||||
@echo $(TAG)Cleaning up$(END)
|
|
||||||
rm -rf .tox *.egg dist build .coverage
|
###############################################################################
|
||||||
find . -name '__pycache__' -delete -print -o -name '*.pyc' -delete -print
|
# Uninstalling
|
||||||
|
###############################################################################
|
||||||
|
|
||||||
|
uninstall-httpie:
|
||||||
|
@echo $(TAG)Uninstalling httpie$(END)
|
||||||
|
- pip uninstall --yes httpie &2>/dev/null
|
||||||
|
|
||||||
|
@echo "Verifying…"
|
||||||
|
cd .. && ! python -m httpie --version &2>/dev/null
|
||||||
|
|
||||||
|
@echo "Done"
|
||||||
@echo
|
@echo
|
||||||
|
|
||||||
|
|
||||||
|
uninstall-all: uninstall-httpie
|
||||||
|
|
||||||
|
@echo $(TAG)Uninstalling httpie requirements$(END)
|
||||||
|
- pip uninstall --yes pygments requests
|
||||||
|
|
||||||
|
@echo $(TAG)Uninstalling development requirements$(END)
|
||||||
|
- pip uninstall --yes -r $(REQUIREMENTS)
|
||||||
|
|
||||||
|
|
||||||
|
###############################################################################
|
||||||
|
# Docs
|
||||||
|
###############################################################################
|
||||||
|
|
||||||
|
pdf:
|
||||||
|
# NOTE: rst2pdf needs to be installed manually and against a Python 2
|
||||||
|
@echo "Converting README.rst to PDF…"
|
||||||
|
rst2pdf \
|
||||||
|
--strip-elements-with-class=no-pdf \
|
||||||
|
README.rst \
|
||||||
|
-o README.pdf
|
||||||
|
@echo "Done"
|
||||||
|
@echo
|
||||||
|
|
||||||
|
|
||||||
|
###############################################################################
|
||||||
|
# Homebrew
|
||||||
|
###############################################################################
|
||||||
|
|
||||||
|
brew-deps:
|
||||||
|
extras/brew-deps.py
|
||||||
|
|
||||||
|
brew-test:
|
||||||
|
- brew uninstall httpie
|
||||||
|
brew install --build-from-source ./extras/httpie.rb
|
||||||
|
brew test httpie
|
||||||
|
brew audit --strict httpie
|
||||||
|
1021
README.rst
1021
README.rst
File diff suppressed because it is too large
Load Diff
18
appveyor.yml
18
appveyor.yml
@ -1,18 +0,0 @@
|
|||||||
# https://ci.appveyor.com/project/jkbrzt/httpie
|
|
||||||
build: false
|
|
||||||
environment:
|
|
||||||
matrix:
|
|
||||||
- PYTHON: "C:/Python27"
|
|
||||||
- PYTHON: "C:/Python34"
|
|
||||||
init:
|
|
||||||
- "ECHO %PYTHON%"
|
|
||||||
- ps: "ls C:/Python*"
|
|
||||||
install:
|
|
||||||
# - ps: (new-object net.webclient).DownloadFile('https://bootstrap.pypa.io/get-pip.py', 'C:/get-pip.py')
|
|
||||||
# - "%PYTHON%/python.exe C:/get-pip.py"
|
|
||||||
- "%PYTHON%/Scripts/pip.exe install -U pip setuptools"
|
|
||||||
- "%PYTHON%/Scripts/pip.exe install -e ."
|
|
||||||
test_script:
|
|
||||||
- "%PYTHON%/Scripts/pip.exe --version"
|
|
||||||
- "%PYTHON%/Scripts/http.exe --debug"
|
|
||||||
- "%PYTHON%/python.exe setup.py test"
|
|
64
extras/brew-deps.py
Executable file
64
extras/brew-deps.py
Executable file
@ -0,0 +1,64 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""
|
||||||
|
Generate Ruby code with URLs and file hashes for packages from PyPi
|
||||||
|
(i.e., httpie itself as well as its dependencies) to be included
|
||||||
|
in the Homebrew formula after a new release of HTTPie has been published
|
||||||
|
on PyPi.
|
||||||
|
|
||||||
|
<https://github.com/Homebrew/homebrew-core/blob/master/Formula/httpie.rb>
|
||||||
|
|
||||||
|
"""
|
||||||
|
import hashlib
|
||||||
|
import requests
|
||||||
|
|
||||||
|
|
||||||
|
PACKAGES = [
|
||||||
|
'httpie',
|
||||||
|
'Pygments',
|
||||||
|
'requests',
|
||||||
|
'certifi',
|
||||||
|
'urllib3',
|
||||||
|
'idna',
|
||||||
|
'chardet',
|
||||||
|
'PySocks',
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def get_package_meta(package_name):
|
||||||
|
api_url = 'https://pypi.python.org/pypi/{}/json'.format(package_name)
|
||||||
|
resp = requests.get(api_url).json()
|
||||||
|
hasher = hashlib.sha256()
|
||||||
|
for release in resp['urls']:
|
||||||
|
download_url = release['url']
|
||||||
|
if download_url.endswith('.tar.gz'):
|
||||||
|
hasher.update(requests.get(download_url).content)
|
||||||
|
return {
|
||||||
|
'name': package_name,
|
||||||
|
'url': download_url,
|
||||||
|
'sha256': hasher.hexdigest(),
|
||||||
|
}
|
||||||
|
else:
|
||||||
|
raise RuntimeError(
|
||||||
|
'{}: download not found: {}'.format(package_name, resp))
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
package_meta_map = {
|
||||||
|
package_name: get_package_meta(package_name)
|
||||||
|
for package_name in PACKAGES
|
||||||
|
}
|
||||||
|
httpie_meta = package_meta_map.pop('httpie')
|
||||||
|
print()
|
||||||
|
print(' url "{url}"'.format(url=httpie_meta['url']))
|
||||||
|
print(' sha256 "{sha256}"'.format(sha256=httpie_meta['sha256']))
|
||||||
|
print()
|
||||||
|
for dep_meta in package_meta_map.values():
|
||||||
|
print(' resource "{name}" do'.format(name=dep_meta['name']))
|
||||||
|
print(' url "{url}"'.format(url=dep_meta['url']))
|
||||||
|
print(' sha256 "{sha256}"'.format(sha256=dep_meta['sha256']))
|
||||||
|
print(' end')
|
||||||
|
print('')
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
59
extras/httpie-completion.fish
Normal file
59
extras/httpie-completion.fish
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
function __fish_httpie_auth_types
|
||||||
|
echo "basic"\t"Basic HTTP auth"
|
||||||
|
echo "digest"\t"Digest HTTP auth"
|
||||||
|
end
|
||||||
|
|
||||||
|
function __fish_httpie_styles
|
||||||
|
echo "autumn"
|
||||||
|
echo "borland"
|
||||||
|
echo "bw"
|
||||||
|
echo "colorful"
|
||||||
|
echo "default"
|
||||||
|
echo "emacs"
|
||||||
|
echo "friendly"
|
||||||
|
echo "fruity"
|
||||||
|
echo "igor"
|
||||||
|
echo "manni"
|
||||||
|
echo "monokai"
|
||||||
|
echo "murphy"
|
||||||
|
echo "native"
|
||||||
|
echo "paraiso-dark"
|
||||||
|
echo "paraiso-light"
|
||||||
|
echo "pastie"
|
||||||
|
echo "perldoc"
|
||||||
|
echo "rrt"
|
||||||
|
echo "solarized"
|
||||||
|
echo "tango"
|
||||||
|
echo "trac"
|
||||||
|
echo "vim"
|
||||||
|
echo "vs"
|
||||||
|
echo "xcode"
|
||||||
|
end
|
||||||
|
|
||||||
|
complete -x -c http -s s -l style -d 'Output coloring style (default is "monokai")' -A -a '(__fish_httpie_styles)'
|
||||||
|
complete -c http -s f -l form -d 'Data items from the command line are serialized as form fields'
|
||||||
|
complete -c http -s j -l json -d '(default) Data items from the command line are serialized as a JSON object'
|
||||||
|
complete -x -c http -l pretty -d 'Controls output processing' -a "all colors format none" -A
|
||||||
|
complete -x -c http -s p -l print -d 'String specifying what the output should contain'
|
||||||
|
complete -c http -s v -l verbose -d 'Print the whole request as well as the response'
|
||||||
|
complete -c http -s h -l headers -d 'Print only the response headers'
|
||||||
|
complete -c http -s b -l body -d 'Print only the response body'
|
||||||
|
complete -c http -s S -l stream -d 'Always stream the output by line'
|
||||||
|
complete -c http -s o -l output -d 'Save output to FILE'
|
||||||
|
complete -c http -s d -l download -d 'Do not print the response body to stdout'
|
||||||
|
complete -c http -s c -l continue -d 'Resume an interrupted download'
|
||||||
|
complete -x -c http -l session -d 'Create, or reuse and update a session'
|
||||||
|
complete -x -c http -s a -l auth -d 'If only the username is provided (-a username), HTTPie will prompt for the password'
|
||||||
|
complete -x -c http -l auth-type -d 'The authentication mechanism to be used' -a '(__fish_httpie_auth_types)' -A
|
||||||
|
complete -x -c http -l proxy -d 'String mapping protocol to the URL of the proxy'
|
||||||
|
complete -c http -l follow -d 'Allow full redirects'
|
||||||
|
complete -x -c http -l verify -d 'SSL cert verification'
|
||||||
|
complete -c http -l cert -d 'SSL cert'
|
||||||
|
complete -c http -l cert-key -d 'Private SSL cert key'
|
||||||
|
complete -x -c http -l timeout -d 'Connection timeout in seconds'
|
||||||
|
complete -c http -l check-status -d 'Error with non-200 HTTP status code'
|
||||||
|
complete -c http -l ignore-stdin -d 'Do not attempt to read stdin'
|
||||||
|
complete -c http -l help -d 'Show help'
|
||||||
|
complete -c http -l version -d 'Show version'
|
||||||
|
complete -c http -l traceback -d 'Prints exception traceback should one occur'
|
||||||
|
complete -c http -l debug -d 'Show debugging information'
|
68
extras/httpie.rb
Normal file
68
extras/httpie.rb
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
# The latest Homebrew formula as submitted to Homebrew/homebrew-core.
|
||||||
|
# Only useful for testing until it gets accepted by homebrew maintainers.
|
||||||
|
# (It will need to be updated from the repo version before next release.)
|
||||||
|
#
|
||||||
|
# https://github.com/Homebrew/homebrew-core/blob/master/Formula/httpie.rb
|
||||||
|
#
|
||||||
|
class Httpie < Formula
|
||||||
|
include Language::Python::Virtualenv
|
||||||
|
|
||||||
|
desc "User-friendly cURL replacement (command-line HTTP client)"
|
||||||
|
homepage "https://httpie.org/"
|
||||||
|
url "https://files.pythonhosted.org/packages/09/8d/581ef7bd9a09dc30b621638a4fa805a2073bbfb45fa06ed37f998f172419/httpie-1.0.2.tar.gz"
|
||||||
|
sha256 "fc676c85febdf3d80abc1ef6fa71ec3764d8b838806a7ae4e55e5e5aa014a2ab"
|
||||||
|
head "https://github.com/jakubroztocil/httpie.git"
|
||||||
|
|
||||||
|
bottle do
|
||||||
|
cellar :any_skip_relocation
|
||||||
|
sha256 "158258be68ac93de13860be2bef02da6fd8b68aa24b2e6609bcff1ec3f93e7a0" => :mojave
|
||||||
|
sha256 "54352116b6fa2c3bd65f26136fdcb57986dbff8a52de5febf7aea59c126d29e1" => :high_sierra
|
||||||
|
sha256 "9cce71768fe388808e11b26d651b44a6b54219f5406845b4273b5099f5c1f76f" => :sierra
|
||||||
|
end
|
||||||
|
|
||||||
|
depends_on "python"
|
||||||
|
|
||||||
|
resource "Pygments" do
|
||||||
|
url "https://files.pythonhosted.org/packages/64/69/413708eaf3a64a6abb8972644e0f20891a55e621c6759e2c3f3891e05d63/Pygments-2.3.1.tar.gz"
|
||||||
|
sha256 "5ffada19f6203563680669ee7f53b64dabbeb100eb51b61996085e99c03b284a"
|
||||||
|
end
|
||||||
|
|
||||||
|
resource "requests" do
|
||||||
|
url "https://files.pythonhosted.org/packages/52/2c/514e4ac25da2b08ca5a464c50463682126385c4272c18193876e91f4bc38/requests-2.21.0.tar.gz"
|
||||||
|
sha256 "502a824f31acdacb3a35b6690b5fbf0bc41d63a24a45c4004352b0242707598e"
|
||||||
|
end
|
||||||
|
|
||||||
|
resource "certifi" do
|
||||||
|
url "https://files.pythonhosted.org/packages/55/54/3ce77783acba5979ce16674fc98b1920d00b01d337cfaaf5db22543505ed/certifi-2018.11.29.tar.gz"
|
||||||
|
sha256 "47f9c83ef4c0c621eaef743f133f09fa8a74a9b75f037e8624f83bd1b6626cb7"
|
||||||
|
end
|
||||||
|
|
||||||
|
resource "urllib3" do
|
||||||
|
url "https://files.pythonhosted.org/packages/b1/53/37d82ab391393565f2f831b8eedbffd57db5a718216f82f1a8b4d381a1c1/urllib3-1.24.1.tar.gz"
|
||||||
|
sha256 "de9529817c93f27c8ccbfead6985011db27bd0ddfcdb2d86f3f663385c6a9c22"
|
||||||
|
end
|
||||||
|
|
||||||
|
resource "idna" do
|
||||||
|
url "https://files.pythonhosted.org/packages/ad/13/eb56951b6f7950cadb579ca166e448ba77f9d24efc03edd7e55fa57d04b7/idna-2.8.tar.gz"
|
||||||
|
sha256 "c357b3f628cf53ae2c4c05627ecc484553142ca23264e593d327bcde5e9c3407"
|
||||||
|
end
|
||||||
|
|
||||||
|
resource "chardet" do
|
||||||
|
url "https://files.pythonhosted.org/packages/fc/bb/a5768c230f9ddb03acc9ef3f0d4a3cf93462473795d18e9535498c8f929d/chardet-3.0.4.tar.gz"
|
||||||
|
sha256 "84ab92ed1c4d4f16916e05906b6b75a6c0fb5db821cc65e70cbd64a3e2a5eaae"
|
||||||
|
end
|
||||||
|
|
||||||
|
resource "PySocks" do
|
||||||
|
url "https://files.pythonhosted.org/packages/53/12/6bf1d764f128636cef7408e8156b7235b150ea31650d0260969215bb8e7d/PySocks-1.6.8.tar.gz"
|
||||||
|
sha256 "3fe52c55890a248676fd69dc9e3c4e811718b777834bcaab7a8125cf9deac672"
|
||||||
|
end
|
||||||
|
|
||||||
|
def install
|
||||||
|
virtualenv_install_with_resources
|
||||||
|
end
|
||||||
|
|
||||||
|
test do
|
||||||
|
raw_url = "https://raw.githubusercontent.com/Homebrew/homebrew-core/master/Formula/httpie.rb"
|
||||||
|
assert_match "PYTHONPATH", shell_output("#{bin}/http --ignore-stdin #{raw_url}")
|
||||||
|
end
|
||||||
|
end
|
BIN
httpie.gif
Normal file
BIN
httpie.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 1019 KiB |
BIN
httpie.png
BIN
httpie.png
Binary file not shown.
Before Width: | Height: | Size: 182 KiB After Width: | Height: | Size: 681 KiB |
@ -2,18 +2,31 @@
|
|||||||
HTTPie - a CLI, cURL-like tool for humans.
|
HTTPie - a CLI, cURL-like tool for humans.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
__version__ = '1.0.3'
|
||||||
__author__ = 'Jakub Roztocil'
|
__author__ = 'Jakub Roztocil'
|
||||||
__version__ = '0.9.3'
|
|
||||||
__licence__ = 'BSD'
|
__licence__ = 'BSD'
|
||||||
|
|
||||||
|
|
||||||
class ExitStatus:
|
class ExitStatus:
|
||||||
"""Exit status code constants."""
|
"""Program exit code constants."""
|
||||||
OK = 0
|
SUCCESS = 0
|
||||||
ERROR = 1
|
ERROR = 1
|
||||||
|
PLUGIN_ERROR = 7
|
||||||
|
|
||||||
|
# 128+2 SIGINT <http://www.tldp.org/LDP/abs/html/exitcodes.html>
|
||||||
|
ERROR_CTRL_C = 130
|
||||||
|
|
||||||
ERROR_TIMEOUT = 2
|
ERROR_TIMEOUT = 2
|
||||||
|
ERROR_TOO_MANY_REDIRECTS = 6
|
||||||
|
|
||||||
# Used only when requested with --check-status:
|
# Used only when requested with --check-status:
|
||||||
ERROR_HTTP_3XX = 3
|
ERROR_HTTP_3XX = 3
|
||||||
ERROR_HTTP_4XX = 4
|
ERROR_HTTP_4XX = 4
|
||||||
ERROR_HTTP_5XX = 5
|
ERROR_HTTP_5XX = 5
|
||||||
|
|
||||||
|
|
||||||
|
EXIT_STATUS_LABELS = {
|
||||||
|
value: key
|
||||||
|
for key, value in ExitStatus.__dict__.items()
|
||||||
|
if key.isupper()
|
||||||
|
}
|
||||||
|
@ -3,8 +3,16 @@
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
import sys
|
import sys
|
||||||
from .core import main
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
try:
|
||||||
|
from .core import main
|
||||||
|
sys.exit(main())
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
from . import ExitStatus
|
||||||
|
sys.exit(ExitStatus.ERROR_CTRL_C)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
sys.exit(main())
|
main()
|
||||||
|
228
httpie/cli.py
228
httpie/cli.py
@ -3,23 +3,29 @@
|
|||||||
NOTE: the CLI interface may change before reaching v1.0.
|
NOTE: the CLI interface may change before reaching v1.0.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
from textwrap import dedent, wrap
|
|
||||||
# noinspection PyCompatibility
|
# noinspection PyCompatibility
|
||||||
from argparse import (RawDescriptionHelpFormatter, FileType,
|
from argparse import (
|
||||||
OPTIONAL, ZERO_OR_MORE, SUPPRESS)
|
RawDescriptionHelpFormatter, FileType,
|
||||||
|
OPTIONAL, ZERO_OR_MORE, SUPPRESS
|
||||||
|
)
|
||||||
|
from textwrap import dedent, wrap
|
||||||
|
|
||||||
from httpie import __doc__, __version__
|
from httpie import __doc__, __version__
|
||||||
from httpie.plugins.builtin import BuiltinAuthPlugin
|
from httpie.input import (
|
||||||
|
HTTPieArgumentParser, KeyValueArgType,
|
||||||
|
SEP_PROXY, SEP_GROUP_ALL_ITEMS,
|
||||||
|
OUT_REQ_HEAD, OUT_REQ_BODY, OUT_RESP_HEAD,
|
||||||
|
OUT_RESP_BODY, OUTPUT_OPTIONS,
|
||||||
|
OUTPUT_OPTIONS_DEFAULT, PRETTY_MAP,
|
||||||
|
PRETTY_STDOUT_TTY_ONLY, SessionNameValidator,
|
||||||
|
readable_file_arg, SSL_VERSION_ARG_MAPPING
|
||||||
|
)
|
||||||
|
from httpie.output.formatters.colors import (
|
||||||
|
AVAILABLE_STYLES, DEFAULT_STYLE, AUTO_STYLE
|
||||||
|
)
|
||||||
from httpie.plugins import plugin_manager
|
from httpie.plugins import plugin_manager
|
||||||
|
from httpie.plugins.builtin import BuiltinAuthPlugin
|
||||||
from httpie.sessions import DEFAULT_SESSIONS_DIR
|
from httpie.sessions import DEFAULT_SESSIONS_DIR
|
||||||
from httpie.output.formatters.colors import AVAILABLE_STYLES, DEFAULT_STYLE
|
|
||||||
from httpie.input import (Parser, AuthCredentialsArgType, KeyValueArgType,
|
|
||||||
SEP_PROXY, SEP_CREDENTIALS, SEP_GROUP_ALL_ITEMS,
|
|
||||||
OUT_REQ_HEAD, OUT_REQ_BODY, OUT_RESP_HEAD,
|
|
||||||
OUT_RESP_BODY, OUTPUT_OPTIONS,
|
|
||||||
OUTPUT_OPTIONS_DEFAULT, PRETTY_MAP,
|
|
||||||
PRETTY_STDOUT_TTY_ONLY, SessionNameValidator,
|
|
||||||
readable_file_arg)
|
|
||||||
|
|
||||||
|
|
||||||
class HTTPieHelpFormatter(RawDescriptionHelpFormatter):
|
class HTTPieHelpFormatter(RawDescriptionHelpFormatter):
|
||||||
@ -40,7 +46,9 @@ class HTTPieHelpFormatter(RawDescriptionHelpFormatter):
|
|||||||
text = dedent(text).strip() + '\n\n'
|
text = dedent(text).strip() + '\n\n'
|
||||||
return text.splitlines()
|
return text.splitlines()
|
||||||
|
|
||||||
parser = Parser(
|
|
||||||
|
parser = HTTPieArgumentParser(
|
||||||
|
prog='http',
|
||||||
formatter_class=HTTPieHelpFormatter,
|
formatter_class=HTTPieHelpFormatter,
|
||||||
description='%s <http://httpie.org>' % __doc__.strip(),
|
description='%s <http://httpie.org>' % __doc__.strip(),
|
||||||
epilog=dedent("""
|
epilog=dedent("""
|
||||||
@ -49,9 +57,9 @@ parser = Parser(
|
|||||||
|
|
||||||
Suggestions and bug reports are greatly appreciated:
|
Suggestions and bug reports are greatly appreciated:
|
||||||
|
|
||||||
https://github.com/jkbrzt/httpie/issues
|
https://github.com/jakubroztocil/httpie/issues
|
||||||
|
|
||||||
""")
|
"""),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -88,6 +96,7 @@ positional.add_argument(
|
|||||||
metavar='URL',
|
metavar='URL',
|
||||||
help="""
|
help="""
|
||||||
The scheme defaults to 'http://' if the URL does not include one.
|
The scheme defaults to 'http://' if the URL does not include one.
|
||||||
|
(You can override this with: --default-scheme=https)
|
||||||
|
|
||||||
You can also use a shorthand for localhost
|
You can also use a shorthand for localhost
|
||||||
|
|
||||||
@ -100,6 +109,7 @@ positional.add_argument(
|
|||||||
'items',
|
'items',
|
||||||
metavar='REQUEST_ITEM',
|
metavar='REQUEST_ITEM',
|
||||||
nargs=ZERO_OR_MORE,
|
nargs=ZERO_OR_MORE,
|
||||||
|
default=None,
|
||||||
type=KeyValueArgType(*SEP_GROUP_ALL_ITEMS),
|
type=KeyValueArgType(*SEP_GROUP_ALL_ITEMS),
|
||||||
help=r"""
|
help=r"""
|
||||||
Optional key-value pairs to be included in the request. The separator used
|
Optional key-value pairs to be included in the request. The separator used
|
||||||
@ -202,18 +212,21 @@ output_processing.add_argument(
|
|||||||
help="""
|
help="""
|
||||||
Output coloring style (default is "{default}"). One of:
|
Output coloring style (default is "{default}"). One of:
|
||||||
|
|
||||||
{available}
|
{available_styles}
|
||||||
|
|
||||||
For this option to work properly, please make sure that the $TERM
|
The "{auto_style}" style follows your terminal's ANSI color styles.
|
||||||
environment variable is set to "xterm-256color" or similar
|
|
||||||
|
For non-{auto_style} styles to work properly, please make sure that the
|
||||||
|
$TERM environment variable is set to "xterm-256color" or similar
|
||||||
(e.g., via `export TERM=xterm-256color' in your ~/.bashrc).
|
(e.g., via `export TERM=xterm-256color' in your ~/.bashrc).
|
||||||
|
|
||||||
""".format(
|
""".format(
|
||||||
default=DEFAULT_STYLE,
|
default=DEFAULT_STYLE,
|
||||||
available='\n'.join(
|
available_styles='\n'.join(
|
||||||
'{0}{1}'.format(8*' ', line.strip())
|
'{0}{1}'.format(8 * ' ', line.strip())
|
||||||
for line in wrap(', '.join(sorted(AVAILABLE_STYLES)), 60)
|
for line in wrap(', '.join(sorted(AVAILABLE_STYLES)), 60)
|
||||||
).rstrip(),
|
).rstrip(),
|
||||||
|
auto_style=AUTO_STYLE,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -249,17 +262,6 @@ output_options.add_argument(
|
|||||||
default=OUTPUT_OPTIONS_DEFAULT,
|
default=OUTPUT_OPTIONS_DEFAULT,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
output_options.add_argument(
|
|
||||||
'--verbose', '-v',
|
|
||||||
dest='output_options',
|
|
||||||
action='store_const',
|
|
||||||
const=''.join(OUTPUT_OPTIONS),
|
|
||||||
help="""
|
|
||||||
Print the whole request as well as the response. Shortcut for --print={0}.
|
|
||||||
|
|
||||||
"""
|
|
||||||
.format(''.join(OUTPUT_OPTIONS))
|
|
||||||
)
|
|
||||||
output_options.add_argument(
|
output_options.add_argument(
|
||||||
'--headers', '-h',
|
'--headers', '-h',
|
||||||
dest='output_options',
|
dest='output_options',
|
||||||
@ -283,6 +285,42 @@ output_options.add_argument(
|
|||||||
.format(OUT_RESP_BODY)
|
.format(OUT_RESP_BODY)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
output_options.add_argument(
|
||||||
|
'--verbose', '-v',
|
||||||
|
dest='verbose',
|
||||||
|
action='store_true',
|
||||||
|
help="""
|
||||||
|
Verbose output. Print the whole request as well as the response. Also print
|
||||||
|
any intermediary requests/responses (such as redirects).
|
||||||
|
It's a shortcut for: --all --print={0}
|
||||||
|
|
||||||
|
"""
|
||||||
|
.format(''.join(OUTPUT_OPTIONS))
|
||||||
|
)
|
||||||
|
output_options.add_argument(
|
||||||
|
'--all',
|
||||||
|
default=False,
|
||||||
|
action='store_true',
|
||||||
|
help="""
|
||||||
|
By default, only the final request/response is shown. Use this flag to show
|
||||||
|
any intermediary requests/responses as well. Intermediary requests include
|
||||||
|
followed redirects (with --follow), the first unauthorized request when
|
||||||
|
Digest auth is used (--auth=digest), etc.
|
||||||
|
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
output_options.add_argument(
|
||||||
|
'--history-print', '-P',
|
||||||
|
dest='output_options_history',
|
||||||
|
metavar='WHAT',
|
||||||
|
help="""
|
||||||
|
The same as --print, -p but applies only to intermediary requests/responses
|
||||||
|
(such as redirects) when their inclusion is enabled with --all. If this
|
||||||
|
options is not specified, then they are formatted the same way as the final
|
||||||
|
response.
|
||||||
|
|
||||||
|
"""
|
||||||
|
)
|
||||||
output_options.add_argument(
|
output_options.add_argument(
|
||||||
'--stream', '-S',
|
'--stream', '-S',
|
||||||
action='store_true',
|
action='store_true',
|
||||||
@ -307,8 +345,9 @@ output_options.add_argument(
|
|||||||
dest='output_file',
|
dest='output_file',
|
||||||
metavar='FILE',
|
metavar='FILE',
|
||||||
help="""
|
help="""
|
||||||
Save output to FILE. If --download is set, then only the response body is
|
Save output to FILE instead of stdout. If --download is also set, then only
|
||||||
saved to the file. Other parts of the HTTP exchange are printed to stderr.
|
the response body is saved to FILE. Other parts of the HTTP exchange are
|
||||||
|
printed to stderr.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@ -377,7 +416,6 @@ sessions.add_argument(
|
|||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
#######################################################################
|
#######################################################################
|
||||||
# Authentication
|
# Authentication
|
||||||
#######################################################################
|
#######################################################################
|
||||||
@ -386,8 +424,8 @@ sessions.add_argument(
|
|||||||
auth = parser.add_argument_group(title='Authentication')
|
auth = parser.add_argument_group(title='Authentication')
|
||||||
auth.add_argument(
|
auth.add_argument(
|
||||||
'--auth', '-a',
|
'--auth', '-a',
|
||||||
|
default=None,
|
||||||
metavar='USER[:PASS]',
|
metavar='USER[:PASS]',
|
||||||
type=AuthCredentialsArgType(SEP_CREDENTIALS),
|
|
||||||
help="""
|
help="""
|
||||||
If only the username is provided (-a username), HTTPie will prompt
|
If only the username is provided (-a username), HTTPie will prompt
|
||||||
for the password.
|
for the password.
|
||||||
@ -395,11 +433,22 @@ auth.add_argument(
|
|||||||
""",
|
""",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class _AuthTypeLazyChoices(object):
|
||||||
|
# Needed for plugin testing
|
||||||
|
|
||||||
|
def __contains__(self, item):
|
||||||
|
return item in plugin_manager.get_auth_plugin_mapping()
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
return iter(sorted(plugin_manager.get_auth_plugin_mapping().keys()))
|
||||||
|
|
||||||
|
|
||||||
_auth_plugins = plugin_manager.get_auth_plugins()
|
_auth_plugins = plugin_manager.get_auth_plugins()
|
||||||
auth.add_argument(
|
auth.add_argument(
|
||||||
'--auth-type',
|
'--auth-type', '-A',
|
||||||
choices=[plugin.auth_type for plugin in _auth_plugins],
|
choices=_AuthTypeLazyChoices(),
|
||||||
default=_auth_plugins[0].auth_type,
|
default=None,
|
||||||
help="""
|
help="""
|
||||||
The authentication mechanism to be used. Defaults to "{default}".
|
The authentication mechanism to be used. Defaults to "{default}".
|
||||||
|
|
||||||
@ -444,45 +493,21 @@ network.add_argument(
|
|||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
network.add_argument(
|
network.add_argument(
|
||||||
'--follow',
|
'--follow', '-F',
|
||||||
default=False,
|
default=False,
|
||||||
action='store_true',
|
action='store_true',
|
||||||
help="""
|
help="""
|
||||||
Set this flag if full redirects are allowed (e.g. re-POST-ing of data at
|
Follow 30x Location redirects.
|
||||||
new Location).
|
|
||||||
|
|
||||||
"""
|
|
||||||
)
|
|
||||||
network.add_argument(
|
|
||||||
'--verify',
|
|
||||||
default='yes',
|
|
||||||
help="""
|
|
||||||
Set to "no" to skip checking the host's SSL certificate. You can also pass
|
|
||||||
the path to a CA_BUNDLE file for private certs. You can also set the
|
|
||||||
REQUESTS_CA_BUNDLE environment variable. Defaults to "yes".
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
|
|
||||||
network.add_argument(
|
network.add_argument(
|
||||||
'--cert',
|
'--max-redirects',
|
||||||
default=None,
|
type=int,
|
||||||
type=readable_file_arg,
|
default=30,
|
||||||
help="""
|
help="""
|
||||||
You can specify a local cert to use as client side SSL certificate.
|
By default, requests have a limit of 30 redirects (works with --follow).
|
||||||
This file may either contain both private key and certificate or you may
|
|
||||||
specify --cert-key separately.
|
|
||||||
|
|
||||||
"""
|
|
||||||
)
|
|
||||||
|
|
||||||
network.add_argument(
|
|
||||||
'--cert-key',
|
|
||||||
default=None,
|
|
||||||
type=readable_file_arg,
|
|
||||||
help="""
|
|
||||||
The private key to use with SSL. Only needed if --cert is given and the
|
|
||||||
certificate file does not contain the private key.
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
@ -516,6 +541,57 @@ network.add_argument(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
#######################################################################
|
||||||
|
# SSL
|
||||||
|
#######################################################################
|
||||||
|
|
||||||
|
ssl = parser.add_argument_group(title='SSL')
|
||||||
|
ssl.add_argument(
|
||||||
|
'--verify',
|
||||||
|
default='yes',
|
||||||
|
help="""
|
||||||
|
Set to "no" (or "false") to skip checking the host's SSL certificate.
|
||||||
|
Defaults to "yes" ("true"). You can also pass the path to a CA_BUNDLE file
|
||||||
|
for private certs. (Or you can set the REQUESTS_CA_BUNDLE environment
|
||||||
|
variable instead.)
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
ssl.add_argument(
|
||||||
|
'--ssl', # TODO: Maybe something more general, such as --secure-protocol?
|
||||||
|
dest='ssl_version',
|
||||||
|
choices=list(sorted(SSL_VERSION_ARG_MAPPING.keys())),
|
||||||
|
help="""
|
||||||
|
The desired protocol version to use. This will default to
|
||||||
|
SSL v2.3 which will negotiate the highest protocol that both
|
||||||
|
the server and your installation of OpenSSL support. Available protocols
|
||||||
|
may vary depending on OpenSSL installation (only the supported ones
|
||||||
|
are shown here).
|
||||||
|
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
ssl.add_argument(
|
||||||
|
'--cert',
|
||||||
|
default=None,
|
||||||
|
type=readable_file_arg,
|
||||||
|
help="""
|
||||||
|
You can specify a local cert to use as client side SSL certificate.
|
||||||
|
This file may either contain both private key and certificate or you may
|
||||||
|
specify --cert-key separately.
|
||||||
|
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
ssl.add_argument(
|
||||||
|
'--cert-key',
|
||||||
|
default=None,
|
||||||
|
type=readable_file_arg,
|
||||||
|
help="""
|
||||||
|
The private key to use with SSL. Only needed if --cert is given and the
|
||||||
|
certificate file does not contain the private key.
|
||||||
|
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
#######################################################################
|
#######################################################################
|
||||||
# Troubleshooting
|
# Troubleshooting
|
||||||
#######################################################################
|
#######################################################################
|
||||||
@ -523,7 +599,7 @@ network.add_argument(
|
|||||||
troubleshooting = parser.add_argument_group(title='Troubleshooting')
|
troubleshooting = parser.add_argument_group(title='Troubleshooting')
|
||||||
|
|
||||||
troubleshooting.add_argument(
|
troubleshooting.add_argument(
|
||||||
'--ignore-stdin',
|
'--ignore-stdin', '-I',
|
||||||
action='store_true',
|
action='store_true',
|
||||||
default=False,
|
default=False,
|
||||||
help="""
|
help="""
|
||||||
@ -554,7 +630,15 @@ troubleshooting.add_argument(
|
|||||||
action='store_true',
|
action='store_true',
|
||||||
default=False,
|
default=False,
|
||||||
help="""
|
help="""
|
||||||
Prints exception traceback should one occur.
|
Prints the exception traceback should one occur.
|
||||||
|
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
troubleshooting.add_argument(
|
||||||
|
'--default-scheme',
|
||||||
|
default="http",
|
||||||
|
help="""
|
||||||
|
The default scheme to use if not specified in the URL.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
@ -563,8 +647,8 @@ troubleshooting.add_argument(
|
|||||||
action='store_true',
|
action='store_true',
|
||||||
default=False,
|
default=False,
|
||||||
help="""
|
help="""
|
||||||
Prints exception traceback should one occur, and also other information
|
Prints the exception traceback should one occur, as well as other
|
||||||
that is useful for debugging HTTPie itself and for reporting bugs.
|
information useful for debugging HTTPie itself and for reporting bugs.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
|
@ -1,20 +1,23 @@
|
|||||||
import json
|
import json
|
||||||
import sys
|
import sys
|
||||||
from pprint import pformat
|
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
from requests.packages import urllib3
|
from requests.adapters import HTTPAdapter
|
||||||
|
from requests.structures import CaseInsensitiveDict
|
||||||
|
|
||||||
from httpie import sessions
|
from httpie import sessions
|
||||||
from httpie import __version__
|
from httpie import __version__
|
||||||
from httpie.compat import str
|
from httpie.compat import str
|
||||||
|
from httpie.input import SSL_VERSION_ARG_MAPPING
|
||||||
from httpie.plugins import plugin_manager
|
from httpie.plugins import plugin_manager
|
||||||
|
from httpie.utils import repr_dict_nice
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# https://urllib3.readthedocs.org/en/latest/security.html
|
# https://urllib3.readthedocs.io/en/latest/security.html
|
||||||
|
# noinspection PyPackageRequirements
|
||||||
|
import urllib3
|
||||||
urllib3.disable_warnings()
|
urllib3.disable_warnings()
|
||||||
except AttributeError:
|
except (ImportError, AttributeError):
|
||||||
# In some rare cases, the user may have an old version of the requests
|
# In some rare cases, the user may have an old version of the requests
|
||||||
# or urllib3, and there is no method called "disable_warnings." In these
|
# or urllib3, and there is no method called "disable_warnings." In these
|
||||||
# cases, we don't need to call the method.
|
# cases, we don't need to call the method.
|
||||||
@ -22,13 +25,29 @@ except AttributeError:
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
FORM = 'application/x-www-form-urlencoded; charset=utf-8'
|
FORM_CONTENT_TYPE = 'application/x-www-form-urlencoded; charset=utf-8'
|
||||||
JSON = 'application/json'
|
JSON_CONTENT_TYPE = 'application/json'
|
||||||
|
JSON_ACCEPT = '{0}, */*'.format(JSON_CONTENT_TYPE)
|
||||||
DEFAULT_UA = 'HTTPie/%s' % __version__
|
DEFAULT_UA = 'HTTPie/%s' % __version__
|
||||||
|
|
||||||
|
|
||||||
def get_requests_session():
|
class HTTPieHTTPAdapter(HTTPAdapter):
|
||||||
|
|
||||||
|
def __init__(self, ssl_version=None, **kwargs):
|
||||||
|
self._ssl_version = ssl_version
|
||||||
|
super(HTTPieHTTPAdapter, self).__init__(**kwargs)
|
||||||
|
|
||||||
|
def init_poolmanager(self, *args, **kwargs):
|
||||||
|
kwargs['ssl_version'] = self._ssl_version
|
||||||
|
super(HTTPieHTTPAdapter, self).init_poolmanager(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
def get_requests_session(ssl_version):
|
||||||
requests_session = requests.Session()
|
requests_session = requests.Session()
|
||||||
|
requests_session.mount(
|
||||||
|
'https://',
|
||||||
|
HTTPieHTTPAdapter(ssl_version=ssl_version)
|
||||||
|
)
|
||||||
for cls in plugin_manager.get_transport_plugins():
|
for cls in plugin_manager.get_transport_plugins():
|
||||||
transport_plugin = cls()
|
transport_plugin = cls()
|
||||||
requests_session.mount(prefix=transport_plugin.prefix,
|
requests_session.mount(prefix=transport_plugin.prefix,
|
||||||
@ -39,7 +58,12 @@ def get_requests_session():
|
|||||||
def get_response(args, config_dir):
|
def get_response(args, config_dir):
|
||||||
"""Send the request and return a `request.Response`."""
|
"""Send the request and return a `request.Response`."""
|
||||||
|
|
||||||
requests_session = get_requests_session()
|
ssl_version = None
|
||||||
|
if args.ssl_version:
|
||||||
|
ssl_version = SSL_VERSION_ARG_MAPPING[args.ssl_version]
|
||||||
|
|
||||||
|
requests_session = get_requests_session(ssl_version)
|
||||||
|
requests_session.max_redirects = args.max_redirects
|
||||||
|
|
||||||
if not args.session and not args.session_read_only:
|
if not args.session and not args.session_read_only:
|
||||||
kwargs = get_requests_kwargs(args)
|
kwargs = get_requests_kwargs(args)
|
||||||
@ -60,34 +84,43 @@ def get_response(args, config_dir):
|
|||||||
|
|
||||||
def dump_request(kwargs):
|
def dump_request(kwargs):
|
||||||
sys.stderr.write('\n>>> requests.request(**%s)\n\n'
|
sys.stderr.write('\n>>> requests.request(**%s)\n\n'
|
||||||
% pformat(kwargs))
|
% repr_dict_nice(kwargs))
|
||||||
|
|
||||||
|
|
||||||
def encode_headers(headers):
|
def finalize_headers(headers):
|
||||||
# This allows for unicode headers which is non-standard but practical.
|
final_headers = {}
|
||||||
# See: https://github.com/jkbrzt/httpie/issues/212
|
for name, value in headers.items():
|
||||||
return dict(
|
if value is not None:
|
||||||
(name, value.encode('utf8') if isinstance(value, str) else value)
|
|
||||||
for name, value in headers.items()
|
# >leading or trailing LWS MAY be removed without
|
||||||
)
|
# >changing the semantics of the field value"
|
||||||
|
# -https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html
|
||||||
|
# Also, requests raises `InvalidHeader` for leading spaces.
|
||||||
|
value = value.strip()
|
||||||
|
|
||||||
|
if isinstance(value, str):
|
||||||
|
# See: https://github.com/jakubroztocil/httpie/issues/212
|
||||||
|
value = value.encode('utf8')
|
||||||
|
|
||||||
|
final_headers[name] = value
|
||||||
|
return final_headers
|
||||||
|
|
||||||
|
|
||||||
def get_default_headers(args):
|
def get_default_headers(args):
|
||||||
default_headers = {
|
default_headers = CaseInsensitiveDict({
|
||||||
'User-Agent': DEFAULT_UA
|
'User-Agent': DEFAULT_UA
|
||||||
}
|
})
|
||||||
|
|
||||||
auto_json = args.data and not args.form
|
auto_json = args.data and not args.form
|
||||||
# FIXME: Accept is set to JSON with `http url @./file.txt`.
|
|
||||||
if args.json or auto_json:
|
if args.json or auto_json:
|
||||||
default_headers['Accept'] = 'application/json'
|
default_headers['Accept'] = JSON_ACCEPT
|
||||||
if args.json or (auto_json and args.data):
|
if args.json or (auto_json and args.data):
|
||||||
default_headers['Content-Type'] = JSON
|
default_headers['Content-Type'] = JSON_CONTENT_TYPE
|
||||||
|
|
||||||
elif args.form and not args.files:
|
elif args.form and not args.files:
|
||||||
# If sending files, `requests` will set
|
# If sending files, `requests` will set
|
||||||
# the `Content-Type` for us.
|
# the `Content-Type` for us.
|
||||||
default_headers['Content-Type'] = FORM
|
default_headers['Content-Type'] = FORM_CONTENT_TYPE
|
||||||
return default_headers
|
return default_headers
|
||||||
|
|
||||||
|
|
||||||
@ -112,12 +145,7 @@ def get_requests_kwargs(args, base_headers=None):
|
|||||||
if base_headers:
|
if base_headers:
|
||||||
headers.update(base_headers)
|
headers.update(base_headers)
|
||||||
headers.update(args.headers)
|
headers.update(args.headers)
|
||||||
headers = encode_headers(headers)
|
headers = finalize_headers(headers)
|
||||||
|
|
||||||
credentials = None
|
|
||||||
if args.auth:
|
|
||||||
auth_plugin = plugin_manager.get_auth_plugin(args.auth_type)()
|
|
||||||
credentials = auth_plugin.get_auth(args.auth.key, args.auth.value)
|
|
||||||
|
|
||||||
cert = None
|
cert = None
|
||||||
if args.cert:
|
if args.cert:
|
||||||
@ -133,12 +161,14 @@ def get_requests_kwargs(args, base_headers=None):
|
|||||||
'data': data,
|
'data': data,
|
||||||
'verify': {
|
'verify': {
|
||||||
'yes': True,
|
'yes': True,
|
||||||
'no': False
|
'true': True,
|
||||||
}.get(args.verify, args.verify),
|
'no': False,
|
||||||
|
'false': False,
|
||||||
|
}.get(args.verify.lower(), args.verify),
|
||||||
'cert': cert,
|
'cert': cert,
|
||||||
'timeout': args.timeout,
|
'timeout': args.timeout,
|
||||||
'auth': credentials,
|
'auth': args.auth,
|
||||||
'proxies': dict((p.key, p.value) for p in args.proxy),
|
'proxies': {p.key: p.value for p in args.proxy},
|
||||||
'files': args.files,
|
'files': args.files,
|
||||||
'allow_redirects': args.follow,
|
'allow_redirects': args.follow,
|
||||||
'params': args.params,
|
'params': args.params,
|
||||||
|
146
httpie/compat.py
146
httpie/compat.py
@ -1,12 +1,11 @@
|
|||||||
"""
|
"""
|
||||||
Python 2.6, 2.7, and 3.x compatibility.
|
Python 2.7, and 3.x compatibility.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
||||||
is_py2 = sys.version_info[0] == 2
|
is_py2 = sys.version_info[0] == 2
|
||||||
is_py26 = sys.version_info[:2] == (2, 6)
|
|
||||||
is_py27 = sys.version_info[:2] == (2, 7)
|
is_py27 = sys.version_info[:2] == (2, 7)
|
||||||
is_py3 = sys.version_info[0] == 3
|
is_py3 = sys.version_info[0] == 3
|
||||||
is_pypy = 'pypy' in sys.version.lower()
|
is_pypy = 'pypy' in sys.version.lower()
|
||||||
@ -14,10 +13,14 @@ is_windows = 'win32' in str(sys.platform).lower()
|
|||||||
|
|
||||||
|
|
||||||
if is_py2:
|
if is_py2:
|
||||||
|
# noinspection PyShadowingBuiltins
|
||||||
bytes = str
|
bytes = str
|
||||||
|
# noinspection PyUnresolvedReferences,PyShadowingBuiltins
|
||||||
str = unicode
|
str = unicode
|
||||||
elif is_py3:
|
elif is_py3:
|
||||||
|
# noinspection PyShadowingBuiltins
|
||||||
str = str
|
str = str
|
||||||
|
# noinspection PyShadowingBuiltins
|
||||||
bytes = bytes
|
bytes = bytes
|
||||||
|
|
||||||
|
|
||||||
@ -32,142 +35,5 @@ try: # pragma: no cover
|
|||||||
# noinspection PyCompatibility
|
# noinspection PyCompatibility
|
||||||
from urllib.request import urlopen
|
from urllib.request import urlopen
|
||||||
except ImportError: # pragma: no cover
|
except ImportError: # pragma: no cover
|
||||||
# noinspection PyCompatibility
|
# noinspection PyCompatibility,PyUnresolvedReferences
|
||||||
from urllib2 import urlopen
|
from urllib2 import urlopen
|
||||||
|
|
||||||
try: # pragma: no cover
|
|
||||||
from collections import OrderedDict
|
|
||||||
except ImportError: # pragma: no cover
|
|
||||||
# Python 2.6 OrderedDict class, needed for headers, parameters, etc .###
|
|
||||||
# <https://pypi.python.org/pypi/ordereddict/1.1>
|
|
||||||
# noinspection PyCompatibility
|
|
||||||
from UserDict import DictMixin
|
|
||||||
|
|
||||||
# noinspection PyShadowingBuiltins
|
|
||||||
class OrderedDict(dict, DictMixin):
|
|
||||||
# Copyright (c) 2009 Raymond Hettinger
|
|
||||||
#
|
|
||||||
# Permission is hereby granted, free of charge, to any person
|
|
||||||
# obtaining a copy of this software and associated documentation files
|
|
||||||
# (the "Software"), to deal in the Software without restriction,
|
|
||||||
# including without limitation the rights to use, copy, modify, merge,
|
|
||||||
# publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
# and to permit persons to whom the Software is furnished to do so,
|
|
||||||
# subject to the following conditions:
|
|
||||||
#
|
|
||||||
# The above copyright notice and this permission notice shall be
|
|
||||||
# included in all copies or substantial portions of the Software.
|
|
||||||
#
|
|
||||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
||||||
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
||||||
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
||||||
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
||||||
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
||||||
# OTHER DEALINGS IN THE SOFTWARE.
|
|
||||||
# noinspection PyMissingConstructor
|
|
||||||
def __init__(self, *args, **kwds):
|
|
||||||
if len(args) > 1:
|
|
||||||
raise TypeError('expected at most 1 arguments, got %d'
|
|
||||||
% len(args))
|
|
||||||
try:
|
|
||||||
self.__end
|
|
||||||
except AttributeError:
|
|
||||||
self.clear()
|
|
||||||
self.update(*args, **kwds)
|
|
||||||
|
|
||||||
def clear(self):
|
|
||||||
self.__end = end = []
|
|
||||||
# noinspection PyUnusedLocal
|
|
||||||
end += [None, end, end] # sentinel node for doubly linked list
|
|
||||||
self.__map = {} # key --> [key, prev, next]
|
|
||||||
dict.clear(self)
|
|
||||||
|
|
||||||
def __setitem__(self, key, value):
|
|
||||||
if key not in self:
|
|
||||||
end = self.__end
|
|
||||||
curr = end[1]
|
|
||||||
curr[2] = end[1] = self.__map[key] = [key, curr, end]
|
|
||||||
dict.__setitem__(self, key, value)
|
|
||||||
|
|
||||||
def __delitem__(self, key):
|
|
||||||
dict.__delitem__(self, key)
|
|
||||||
key, prev, next = self.__map.pop(key)
|
|
||||||
prev[2] = next
|
|
||||||
next[1] = prev
|
|
||||||
|
|
||||||
def __iter__(self):
|
|
||||||
end = self.__end
|
|
||||||
curr = end[2]
|
|
||||||
while curr is not end:
|
|
||||||
yield curr[0]
|
|
||||||
curr = curr[2]
|
|
||||||
|
|
||||||
def __reversed__(self):
|
|
||||||
end = self.__end
|
|
||||||
curr = end[1]
|
|
||||||
while curr is not end:
|
|
||||||
yield curr[0]
|
|
||||||
curr = curr[1]
|
|
||||||
|
|
||||||
def popitem(self, last=True):
|
|
||||||
if not self:
|
|
||||||
raise KeyError('dictionary is empty')
|
|
||||||
if last:
|
|
||||||
key = reversed(self).next()
|
|
||||||
else:
|
|
||||||
key = iter(self).next()
|
|
||||||
value = self.pop(key)
|
|
||||||
return key, value
|
|
||||||
|
|
||||||
def __reduce__(self):
|
|
||||||
items = [[k, self[k]] for k in self]
|
|
||||||
tmp = self.__map, self.__end
|
|
||||||
del self.__map, self.__end
|
|
||||||
inst_dict = vars(self).copy()
|
|
||||||
self.__map, self.__end = tmp
|
|
||||||
if inst_dict:
|
|
||||||
return self.__class__, (items,), inst_dict
|
|
||||||
return self.__class__, (items,)
|
|
||||||
|
|
||||||
def keys(self):
|
|
||||||
return list(self)
|
|
||||||
|
|
||||||
setdefault = DictMixin.setdefault
|
|
||||||
update = DictMixin.update
|
|
||||||
pop = DictMixin.pop
|
|
||||||
values = DictMixin.values
|
|
||||||
items = DictMixin.items
|
|
||||||
iterkeys = DictMixin.iterkeys
|
|
||||||
itervalues = DictMixin.itervalues
|
|
||||||
iteritems = DictMixin.iteritems
|
|
||||||
|
|
||||||
def __repr__(self):
|
|
||||||
if not self:
|
|
||||||
return '%s()' % (self.__class__.__name__,)
|
|
||||||
return '%s(%r)' % (self.__class__.__name__, self.items())
|
|
||||||
|
|
||||||
def copy(self):
|
|
||||||
return self.__class__(self)
|
|
||||||
|
|
||||||
# noinspection PyMethodOverriding
|
|
||||||
@classmethod
|
|
||||||
def fromkeys(cls, iterable, value=None):
|
|
||||||
d = cls()
|
|
||||||
for key in iterable:
|
|
||||||
d[key] = value
|
|
||||||
return d
|
|
||||||
|
|
||||||
def __eq__(self, other):
|
|
||||||
if isinstance(other, OrderedDict):
|
|
||||||
if len(self) != len(other):
|
|
||||||
return False
|
|
||||||
for p, q in zip(self.items(), other.items()):
|
|
||||||
if p != q:
|
|
||||||
return False
|
|
||||||
return True
|
|
||||||
return dict.__eq__(self, other)
|
|
||||||
|
|
||||||
def __ne__(self, other):
|
|
||||||
return not self == other
|
|
||||||
|
@ -6,11 +6,11 @@ from httpie import __version__
|
|||||||
from httpie.compat import is_windows
|
from httpie.compat import is_windows
|
||||||
|
|
||||||
|
|
||||||
DEFAULT_CONFIG_DIR = os.environ.get(
|
DEFAULT_CONFIG_DIR = str(os.environ.get(
|
||||||
'HTTPIE_CONFIG_DIR',
|
'HTTPIE_CONFIG_DIR',
|
||||||
os.path.expanduser('~/.httpie') if not is_windows else
|
os.path.expanduser('~/.httpie') if not is_windows else
|
||||||
os.path.expandvars(r'%APPDATA%\\httpie')
|
os.path.expandvars(r'%APPDATA%\\httpie')
|
||||||
)
|
))
|
||||||
|
|
||||||
|
|
||||||
class BaseConfigDict(dict):
|
class BaseConfigDict(dict):
|
||||||
@ -80,11 +80,10 @@ class BaseConfigDict(dict):
|
|||||||
class Config(BaseConfigDict):
|
class Config(BaseConfigDict):
|
||||||
|
|
||||||
name = 'config'
|
name = 'config'
|
||||||
helpurl = 'https://github.com/jkbrzt/httpie#config'
|
helpurl = 'https://httpie.org/doc#config'
|
||||||
about = 'HTTPie configuration file'
|
about = 'HTTPie configuration file'
|
||||||
|
|
||||||
DEFAULTS = {
|
DEFAULTS = {
|
||||||
'implicit_content_type': 'json',
|
|
||||||
'default_options': []
|
'default_options': []
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -93,5 +92,21 @@ class Config(BaseConfigDict):
|
|||||||
self.update(self.DEFAULTS)
|
self.update(self.DEFAULTS)
|
||||||
self.directory = directory
|
self.directory = directory
|
||||||
|
|
||||||
|
def load(self):
|
||||||
|
super(Config, self).load()
|
||||||
|
self._migrate_implicit_content_type()
|
||||||
|
|
||||||
def _get_path(self):
|
def _get_path(self):
|
||||||
return os.path.join(self.directory, self.name + '.json')
|
return os.path.join(self.directory, self.name + '.json')
|
||||||
|
|
||||||
|
def _migrate_implicit_content_type(self):
|
||||||
|
"""Migrate the removed implicit_content_type config option"""
|
||||||
|
try:
|
||||||
|
implicit_content_type = self.pop('implicit_content_type')
|
||||||
|
except KeyError:
|
||||||
|
self.save()
|
||||||
|
else:
|
||||||
|
if implicit_content_type == 'form':
|
||||||
|
self['default_options'].insert(0, '--form')
|
||||||
|
self.save()
|
||||||
|
self.load()
|
||||||
|
@ -1,8 +1,14 @@
|
|||||||
import sys
|
import sys
|
||||||
|
try:
|
||||||
|
import curses
|
||||||
|
except ImportError:
|
||||||
|
curses = None # Compiled w/o curses
|
||||||
|
|
||||||
from httpie.compat import is_windows
|
from httpie.compat import is_windows
|
||||||
from httpie.config import DEFAULT_CONFIG_DIR, Config
|
from httpie.config import DEFAULT_CONFIG_DIR, Config
|
||||||
|
|
||||||
|
from httpie.utils import repr_dict_nice
|
||||||
|
|
||||||
|
|
||||||
class Environment(object):
|
class Environment(object):
|
||||||
"""
|
"""
|
||||||
@ -26,17 +32,12 @@ class Environment(object):
|
|||||||
stderr_isatty = stderr.isatty()
|
stderr_isatty = stderr.isatty()
|
||||||
colors = 256
|
colors = 256
|
||||||
if not is_windows:
|
if not is_windows:
|
||||||
import curses
|
if curses:
|
||||||
try:
|
|
||||||
curses.setupterm()
|
|
||||||
try:
|
try:
|
||||||
|
curses.setupterm()
|
||||||
colors = curses.tigetnum('colors')
|
colors = curses.tigetnum('colors')
|
||||||
except TypeError:
|
except curses.error:
|
||||||
# pypy3 (2.4.0)
|
pass
|
||||||
colors = curses.tigetnum(b'colors')
|
|
||||||
except curses.error:
|
|
||||||
pass
|
|
||||||
del curses
|
|
||||||
else:
|
else:
|
||||||
# noinspection PyUnresolvedReferences
|
# noinspection PyUnresolvedReferences
|
||||||
import colorama.initialise
|
import colorama.initialise
|
||||||
@ -82,3 +83,17 @@ class Environment(object):
|
|||||||
else:
|
else:
|
||||||
self._config.load()
|
self._config.load()
|
||||||
return self._config
|
return self._config
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
defaults = dict(type(self).__dict__)
|
||||||
|
actual = dict(defaults)
|
||||||
|
actual.update(self.__dict__)
|
||||||
|
actual['config'] = self.config
|
||||||
|
return repr_dict_nice(dict(
|
||||||
|
(key, value)
|
||||||
|
for key, value in actual.items()
|
||||||
|
if not key.startswith('_'))
|
||||||
|
)
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return '<{0} {1}>'.format(type(self).__name__, str(self))
|
||||||
|
288
httpie/core.py
288
httpie/core.py
@ -12,6 +12,7 @@ Invocation flow:
|
|||||||
"""
|
"""
|
||||||
import sys
|
import sys
|
||||||
import errno
|
import errno
|
||||||
|
import platform
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
from requests import __version__ as requests_version
|
from requests import __version__ as requests_version
|
||||||
@ -20,12 +21,13 @@ from pygments import __version__ as pygments_version
|
|||||||
from httpie import __version__ as httpie_version, ExitStatus
|
from httpie import __version__ as httpie_version, ExitStatus
|
||||||
from httpie.compat import str, bytes, is_py3
|
from httpie.compat import str, bytes, is_py3
|
||||||
from httpie.client import get_response
|
from httpie.client import get_response
|
||||||
from httpie.downloads import Download
|
from httpie.downloads import Downloader
|
||||||
from httpie.context import Environment
|
from httpie.context import Environment
|
||||||
from httpie.plugins import plugin_manager
|
from httpie.plugins import plugin_manager
|
||||||
from httpie.output.streams import (
|
from httpie.output.streams import (
|
||||||
build_output_stream,
|
build_output_stream,
|
||||||
write, write_with_colors_win_py3
|
write_stream,
|
||||||
|
write_stream_with_colors_win_py3
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -41,22 +43,25 @@ def get_exit_status(http_status, follow=False):
|
|||||||
# Server Error
|
# Server Error
|
||||||
return ExitStatus.ERROR_HTTP_5XX
|
return ExitStatus.ERROR_HTTP_5XX
|
||||||
else:
|
else:
|
||||||
return ExitStatus.OK
|
return ExitStatus.SUCCESS
|
||||||
|
|
||||||
|
|
||||||
def print_debug_info(env):
|
def print_debug_info(env):
|
||||||
env.stderr.writelines([
|
env.stderr.writelines([
|
||||||
'HTTPie %s\n' % httpie_version,
|
'HTTPie %s\n' % httpie_version,
|
||||||
'HTTPie data: %s\n' % env.config.directory,
|
|
||||||
'Requests %s\n' % requests_version,
|
'Requests %s\n' % requests_version,
|
||||||
'Pygments %s\n' % pygments_version,
|
'Pygments %s\n' % pygments_version,
|
||||||
'Python %s %s\n' % (sys.version, sys.platform)
|
'Python %s\n%s\n' % (sys.version, sys.executable),
|
||||||
|
'%s %s' % (platform.system(), platform.release()),
|
||||||
])
|
])
|
||||||
|
env.stderr.write('\n\n')
|
||||||
|
env.stderr.write(repr(env))
|
||||||
|
env.stderr.write('\n')
|
||||||
|
|
||||||
|
|
||||||
def decode_args(args, stdin_encoding):
|
def decode_args(args, stdin_encoding):
|
||||||
"""
|
"""
|
||||||
Convert all bytes ags to str
|
Convert all bytes args to str
|
||||||
by decoding them using stdin encoding.
|
by decoding them using stdin encoding.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@ -67,8 +72,109 @@ def decode_args(args, stdin_encoding):
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
def main(args=sys.argv[1:], env=Environment(), error=None):
|
def program(args, env, log_error):
|
||||||
"""Run the main program and write the output to ``env.stdout``.
|
"""
|
||||||
|
The main program without error handling
|
||||||
|
|
||||||
|
:param args: parsed args (argparse.Namespace)
|
||||||
|
:type env: Environment
|
||||||
|
:param log_error: error log function
|
||||||
|
:return: status code
|
||||||
|
|
||||||
|
"""
|
||||||
|
exit_status = ExitStatus.SUCCESS
|
||||||
|
downloader = None
|
||||||
|
show_traceback = args.debug or args.traceback
|
||||||
|
|
||||||
|
try:
|
||||||
|
if args.download:
|
||||||
|
args.follow = True # --download implies --follow.
|
||||||
|
downloader = Downloader(
|
||||||
|
output_file=args.output_file,
|
||||||
|
progress_file=env.stderr,
|
||||||
|
resume=args.download_resume
|
||||||
|
)
|
||||||
|
downloader.pre_request(args.headers)
|
||||||
|
|
||||||
|
final_response = get_response(args, config_dir=env.config.directory)
|
||||||
|
if args.all:
|
||||||
|
responses = final_response.history + [final_response]
|
||||||
|
else:
|
||||||
|
responses = [final_response]
|
||||||
|
|
||||||
|
for response in responses:
|
||||||
|
|
||||||
|
if args.check_status or downloader:
|
||||||
|
exit_status = get_exit_status(
|
||||||
|
http_status=response.status_code,
|
||||||
|
follow=args.follow
|
||||||
|
)
|
||||||
|
if not env.stdout_isatty and exit_status != ExitStatus.SUCCESS:
|
||||||
|
log_error(
|
||||||
|
'HTTP %s %s', response.raw.status, response.raw.reason,
|
||||||
|
level='warning'
|
||||||
|
)
|
||||||
|
|
||||||
|
write_stream_kwargs = {
|
||||||
|
'stream': build_output_stream(
|
||||||
|
args=args,
|
||||||
|
env=env,
|
||||||
|
request=response.request,
|
||||||
|
response=response,
|
||||||
|
output_options=(
|
||||||
|
args.output_options
|
||||||
|
if response is final_response
|
||||||
|
else args.output_options_history
|
||||||
|
)
|
||||||
|
),
|
||||||
|
# NOTE: `env.stdout` will in fact be `stderr` with `--download`
|
||||||
|
'outfile': env.stdout,
|
||||||
|
'flush': env.stdout_isatty or args.stream
|
||||||
|
}
|
||||||
|
try:
|
||||||
|
if env.is_windows and is_py3 and 'colors' in args.prettify:
|
||||||
|
write_stream_with_colors_win_py3(**write_stream_kwargs)
|
||||||
|
else:
|
||||||
|
write_stream(**write_stream_kwargs)
|
||||||
|
except IOError as e:
|
||||||
|
if not show_traceback and e.errno == errno.EPIPE:
|
||||||
|
# Ignore broken pipes unless --traceback.
|
||||||
|
env.stderr.write('\n')
|
||||||
|
else:
|
||||||
|
raise
|
||||||
|
|
||||||
|
if downloader and exit_status == ExitStatus.SUCCESS:
|
||||||
|
# Last response body download.
|
||||||
|
download_stream, download_to = downloader.start(final_response)
|
||||||
|
write_stream(
|
||||||
|
stream=download_stream,
|
||||||
|
outfile=download_to,
|
||||||
|
flush=False,
|
||||||
|
)
|
||||||
|
downloader.finish()
|
||||||
|
if downloader.interrupted:
|
||||||
|
exit_status = ExitStatus.ERROR
|
||||||
|
log_error('Incomplete download: size=%d; downloaded=%d' % (
|
||||||
|
downloader.status.total_size,
|
||||||
|
downloader.status.downloaded
|
||||||
|
))
|
||||||
|
return exit_status
|
||||||
|
|
||||||
|
finally:
|
||||||
|
if downloader and not downloader.finished:
|
||||||
|
downloader.failed()
|
||||||
|
|
||||||
|
if (not isinstance(args, list) and args.output_file
|
||||||
|
and args.output_file_specified):
|
||||||
|
args.output_file.close()
|
||||||
|
|
||||||
|
|
||||||
|
def main(args=sys.argv[1:], env=Environment(), custom_log_error=None):
|
||||||
|
"""
|
||||||
|
The main function.
|
||||||
|
|
||||||
|
Pre-process args, handle some special types of invocations,
|
||||||
|
and run the main program with error handling.
|
||||||
|
|
||||||
Return exit status code.
|
Return exit status code.
|
||||||
|
|
||||||
@ -76,127 +182,79 @@ def main(args=sys.argv[1:], env=Environment(), error=None):
|
|||||||
args = decode_args(args, env.stdin_encoding)
|
args = decode_args(args, env.stdin_encoding)
|
||||||
plugin_manager.load_installed_plugins()
|
plugin_manager.load_installed_plugins()
|
||||||
|
|
||||||
|
def log_error(msg, *args, **kwargs):
|
||||||
|
msg = msg % args
|
||||||
|
level = kwargs.get('level', 'error')
|
||||||
|
assert level in ['error', 'warning']
|
||||||
|
env.stderr.write('\nhttp: %s: %s\n' % (level, msg))
|
||||||
|
|
||||||
from httpie.cli import parser
|
from httpie.cli import parser
|
||||||
|
|
||||||
if env.config.default_options:
|
if env.config.default_options:
|
||||||
args = env.config.default_options + args
|
args = env.config.default_options + args
|
||||||
|
|
||||||
def _error(msg, *args, **kwargs):
|
if custom_log_error:
|
||||||
msg = msg % args
|
log_error = custom_log_error
|
||||||
level = kwargs.get('level', 'error')
|
|
||||||
env.stderr.write('\nhttp: %s: %s\n' % (level, msg))
|
|
||||||
|
|
||||||
if error is None:
|
include_debug_info = '--debug' in args
|
||||||
error = _error
|
include_traceback = include_debug_info or '--traceback' in args
|
||||||
|
|
||||||
debug = '--debug' in args
|
if include_debug_info:
|
||||||
traceback = debug or '--traceback' in args
|
|
||||||
exit_status = ExitStatus.OK
|
|
||||||
|
|
||||||
if debug:
|
|
||||||
print_debug_info(env)
|
print_debug_info(env)
|
||||||
if args == ['--debug']:
|
if args == ['--debug']:
|
||||||
return exit_status
|
return ExitStatus.SUCCESS
|
||||||
|
|
||||||
download = None
|
exit_status = ExitStatus.SUCCESS
|
||||||
|
|
||||||
try:
|
try:
|
||||||
args = parser.parse_args(args=args, env=env)
|
parsed_args = parser.parse_args(args=args, env=env)
|
||||||
|
|
||||||
if args.download:
|
|
||||||
args.follow = True # --download implies --follow.
|
|
||||||
download = Download(
|
|
||||||
output_file=args.output_file,
|
|
||||||
progress_file=env.stderr,
|
|
||||||
resume=args.download_resume
|
|
||||||
)
|
|
||||||
download.pre_request(args.headers)
|
|
||||||
|
|
||||||
response = get_response(args, config_dir=env.config.directory)
|
|
||||||
|
|
||||||
if args.check_status or download:
|
|
||||||
|
|
||||||
exit_status = get_exit_status(
|
|
||||||
http_status=response.status_code,
|
|
||||||
follow=args.follow
|
|
||||||
)
|
|
||||||
|
|
||||||
if not env.stdout_isatty and exit_status != ExitStatus.OK:
|
|
||||||
error('HTTP %s %s',
|
|
||||||
response.raw.status,
|
|
||||||
response.raw.reason,
|
|
||||||
level='warning')
|
|
||||||
|
|
||||||
write_kwargs = {
|
|
||||||
'stream': build_output_stream(
|
|
||||||
args, env, response.request, response),
|
|
||||||
|
|
||||||
# This will in fact be `stderr` with `--download`
|
|
||||||
'outfile': env.stdout,
|
|
||||||
|
|
||||||
'flush': env.stdout_isatty or args.stream
|
|
||||||
}
|
|
||||||
|
|
||||||
try:
|
|
||||||
|
|
||||||
if env.is_windows and is_py3 and 'colors' in args.prettify:
|
|
||||||
write_with_colors_win_py3(**write_kwargs)
|
|
||||||
else:
|
|
||||||
write(**write_kwargs)
|
|
||||||
|
|
||||||
if download and exit_status == ExitStatus.OK:
|
|
||||||
# Response body download.
|
|
||||||
download_stream, download_to = download.start(response)
|
|
||||||
write(
|
|
||||||
stream=download_stream,
|
|
||||||
outfile=download_to,
|
|
||||||
flush=False,
|
|
||||||
)
|
|
||||||
download.finish()
|
|
||||||
if download.interrupted:
|
|
||||||
exit_status = ExitStatus.ERROR
|
|
||||||
error('Incomplete download: size=%d; downloaded=%d' % (
|
|
||||||
download.status.total_size,
|
|
||||||
download.status.downloaded
|
|
||||||
))
|
|
||||||
|
|
||||||
except IOError as e:
|
|
||||||
if not traceback and e.errno == errno.EPIPE:
|
|
||||||
# Ignore broken pipes unless --traceback.
|
|
||||||
env.stderr.write('\n')
|
|
||||||
else:
|
|
||||||
raise
|
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
if traceback:
|
|
||||||
raise
|
|
||||||
env.stderr.write('\n')
|
env.stderr.write('\n')
|
||||||
exit_status = ExitStatus.ERROR
|
if include_traceback:
|
||||||
except SystemExit as e:
|
|
||||||
if e.code != ExitStatus.OK:
|
|
||||||
if traceback:
|
|
||||||
raise
|
|
||||||
env.stderr.write('\n')
|
|
||||||
exit_status = ExitStatus.ERROR
|
|
||||||
except requests.Timeout:
|
|
||||||
exit_status = ExitStatus.ERROR_TIMEOUT
|
|
||||||
error('Request timed out (%ss).', args.timeout)
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
# TODO: Better distinction between expected and unexpected errors.
|
|
||||||
# Network errors vs. bugs, etc.
|
|
||||||
if traceback:
|
|
||||||
raise
|
raise
|
||||||
msg = str(e)
|
exit_status = ExitStatus.ERROR_CTRL_C
|
||||||
if hasattr(e, 'request'):
|
except SystemExit as e:
|
||||||
request = e.request
|
if e.code != ExitStatus.SUCCESS:
|
||||||
if hasattr(request, 'url'):
|
env.stderr.write('\n')
|
||||||
msg += ' while doing %s request to URL: %s' % (
|
if include_traceback:
|
||||||
request.method, request.url)
|
raise
|
||||||
error('%s: %s', type(e).__name__, msg)
|
exit_status = ExitStatus.ERROR
|
||||||
exit_status = ExitStatus.ERROR
|
else:
|
||||||
|
try:
|
||||||
finally:
|
exit_status = program(
|
||||||
if download and not download.finished:
|
args=parsed_args,
|
||||||
download.failed()
|
env=env,
|
||||||
|
log_error=log_error,
|
||||||
|
)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
env.stderr.write('\n')
|
||||||
|
if include_traceback:
|
||||||
|
raise
|
||||||
|
exit_status = ExitStatus.ERROR_CTRL_C
|
||||||
|
except SystemExit as e:
|
||||||
|
if e.code != ExitStatus.SUCCESS:
|
||||||
|
env.stderr.write('\n')
|
||||||
|
if include_traceback:
|
||||||
|
raise
|
||||||
|
exit_status = ExitStatus.ERROR
|
||||||
|
except requests.Timeout:
|
||||||
|
exit_status = ExitStatus.ERROR_TIMEOUT
|
||||||
|
log_error('Request timed out (%ss).', parsed_args.timeout)
|
||||||
|
except requests.TooManyRedirects:
|
||||||
|
exit_status = ExitStatus.ERROR_TOO_MANY_REDIRECTS
|
||||||
|
log_error('Too many redirects (--max-redirects=%s).',
|
||||||
|
parsed_args.max_redirects)
|
||||||
|
except Exception as e:
|
||||||
|
# TODO: Further distinction between expected and unexpected errors.
|
||||||
|
msg = str(e)
|
||||||
|
if hasattr(e, 'request'):
|
||||||
|
request = e.request
|
||||||
|
if hasattr(request, 'url'):
|
||||||
|
msg += ' while doing %s request to URL: %s' % (
|
||||||
|
request.method, request.url)
|
||||||
|
log_error('%s: %s', type(e).__name__, msg)
|
||||||
|
if include_traceback:
|
||||||
|
raise
|
||||||
|
exit_status = ExitStatus.ERROR
|
||||||
|
|
||||||
return exit_status
|
return exit_status
|
||||||
|
@ -7,6 +7,7 @@ from __future__ import division
|
|||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
|
import errno
|
||||||
import mimetypes
|
import mimetypes
|
||||||
import threading
|
import threading
|
||||||
from time import sleep, time
|
from time import sleep, time
|
||||||
@ -53,8 +54,8 @@ def parse_content_range(content_range, resumed_from):
|
|||||||
raise ContentRangeError('Missing Content-Range')
|
raise ContentRangeError('Missing Content-Range')
|
||||||
|
|
||||||
pattern = (
|
pattern = (
|
||||||
'^bytes (?P<first_byte_pos>\d+)-(?P<last_byte_pos>\d+)'
|
r'^bytes (?P<first_byte_pos>\d+)-(?P<last_byte_pos>\d+)'
|
||||||
'/(\*|(?P<instance_length>\d+))$'
|
r'/(\*|(?P<instance_length>\d+))$'
|
||||||
)
|
)
|
||||||
match = re.match(pattern, content_range)
|
match = re.match(pattern, content_range)
|
||||||
|
|
||||||
@ -84,8 +85,8 @@ def parse_content_range(content_range, resumed_from):
|
|||||||
'Invalid Content-Range returned: %r' % content_range)
|
'Invalid Content-Range returned: %r' % content_range)
|
||||||
|
|
||||||
if (first_byte_pos != resumed_from
|
if (first_byte_pos != resumed_from
|
||||||
or (instance_length is not None
|
or (instance_length is not None
|
||||||
and last_byte_pos + 1 != instance_length)):
|
and last_byte_pos + 1 != instance_length)):
|
||||||
# Not what we asked for.
|
# Not what we asked for.
|
||||||
raise ContentRangeError(
|
raise ContentRangeError(
|
||||||
'Unexpected Content-Range returned (%r)'
|
'Unexpected Content-Range returned (%r)'
|
||||||
@ -104,7 +105,7 @@ def filename_from_content_disposition(content_disposition):
|
|||||||
:return: the filename if present and valid, otherwise `None`
|
:return: the filename if present and valid, otherwise `None`
|
||||||
|
|
||||||
"""
|
"""
|
||||||
# attachment; filename=jkbrzt-httpie-0.4.1-20-g40bd8f6.tar.gz
|
# attachment; filename=jakubroztocil-httpie-0.4.1-20-g40bd8f6.tar.gz
|
||||||
|
|
||||||
msg = Message('Content-Disposition: %s' % content_disposition)
|
msg = Message('Content-Disposition: %s' % content_disposition)
|
||||||
filename = msg.get_filename()
|
filename = msg.get_filename()
|
||||||
@ -135,16 +136,51 @@ def filename_from_url(url, content_type):
|
|||||||
return fn
|
return fn
|
||||||
|
|
||||||
|
|
||||||
|
def trim_filename(filename, max_len):
|
||||||
|
if len(filename) > max_len:
|
||||||
|
trim_by = len(filename) - max_len
|
||||||
|
name, ext = os.path.splitext(filename)
|
||||||
|
if trim_by >= len(name):
|
||||||
|
filename = filename[:-trim_by]
|
||||||
|
else:
|
||||||
|
filename = name[:-trim_by] + ext
|
||||||
|
return filename
|
||||||
|
|
||||||
|
|
||||||
|
def get_filename_max_length(directory):
|
||||||
|
max_len = 255
|
||||||
|
try:
|
||||||
|
pathconf = os.pathconf
|
||||||
|
except AttributeError:
|
||||||
|
pass # non-posix
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
max_len = pathconf(directory, 'PC_NAME_MAX')
|
||||||
|
except OSError as e:
|
||||||
|
if e.errno != errno.EINVAL:
|
||||||
|
raise
|
||||||
|
return max_len
|
||||||
|
|
||||||
|
|
||||||
|
def trim_filename_if_needed(filename, directory='.', extra=0):
|
||||||
|
max_len = get_filename_max_length(directory) - extra
|
||||||
|
if len(filename) > max_len:
|
||||||
|
filename = trim_filename(filename, max_len)
|
||||||
|
return filename
|
||||||
|
|
||||||
|
|
||||||
def get_unique_filename(filename, exists=os.path.exists):
|
def get_unique_filename(filename, exists=os.path.exists):
|
||||||
attempt = 0
|
attempt = 0
|
||||||
while True:
|
while True:
|
||||||
suffix = '-' + str(attempt) if attempt > 0 else ''
|
suffix = '-' + str(attempt) if attempt > 0 else ''
|
||||||
if not exists(filename + suffix):
|
try_filename = trim_filename_if_needed(filename, extra=len(suffix))
|
||||||
return filename + suffix
|
try_filename += suffix
|
||||||
|
if not exists(try_filename):
|
||||||
|
return try_filename
|
||||||
attempt += 1
|
attempt += 1
|
||||||
|
|
||||||
|
|
||||||
class Download(object):
|
class Downloader(object):
|
||||||
|
|
||||||
def __init__(self, output_file=None,
|
def __init__(self, output_file=None,
|
||||||
resume=False, progress_file=sys.stderr):
|
resume=False, progress_file=sys.stderr):
|
||||||
@ -178,8 +214,8 @@ class Download(object):
|
|||||||
:type request_headers: dict
|
:type request_headers: dict
|
||||||
|
|
||||||
"""
|
"""
|
||||||
# Disable content encoding so that we can resume, etc.
|
# Ask the server not to encode the content so that we can resume, etc.
|
||||||
request_headers['Accept-Encoding'] = None
|
request_headers['Accept-Encoding'] = 'identity'
|
||||||
if self._resume:
|
if self._resume:
|
||||||
bytes_have = os.path.getsize(self._output_file.name)
|
bytes_have = os.path.getsize(self._output_file.name)
|
||||||
if bytes_have:
|
if bytes_have:
|
||||||
@ -188,28 +224,34 @@ class Download(object):
|
|||||||
request_headers['Range'] = 'bytes=%d-' % bytes_have
|
request_headers['Range'] = 'bytes=%d-' % bytes_have
|
||||||
self._resumed_from = bytes_have
|
self._resumed_from = bytes_have
|
||||||
|
|
||||||
def start(self, response):
|
def start(self, final_response):
|
||||||
"""
|
"""
|
||||||
Initiate and return a stream for `response` body with progress
|
Initiate and return a stream for `response` body with progress
|
||||||
callback attached. Can be called only once.
|
callback attached. Can be called only once.
|
||||||
|
|
||||||
:param response: Initiated response object with headers already fetched
|
:param final_response: Initiated response object with headers already fetched
|
||||||
:type response: requests.models.Response
|
:type final_response: requests.models.Response
|
||||||
|
|
||||||
:return: RawStream, output_file
|
:return: RawStream, output_file
|
||||||
|
|
||||||
"""
|
"""
|
||||||
assert not self.status.time_started
|
assert not self.status.time_started
|
||||||
|
|
||||||
|
# FIXME: some servers still might sent Content-Encoding: gzip
|
||||||
|
# <https://github.com/jakubroztocil/httpie/issues/423>
|
||||||
try:
|
try:
|
||||||
total_size = int(response.headers['Content-Length'])
|
total_size = int(final_response.headers['Content-Length'])
|
||||||
except (KeyError, ValueError, TypeError):
|
except (KeyError, ValueError, TypeError):
|
||||||
total_size = None
|
total_size = None
|
||||||
|
|
||||||
if self._output_file:
|
if not self._output_file:
|
||||||
if self._resume and response.status_code == PARTIAL_CONTENT:
|
self._output_file = self._get_output_file_from_response(
|
||||||
|
final_response)
|
||||||
|
else:
|
||||||
|
# `--output, -o` provided
|
||||||
|
if self._resume and final_response.status_code == PARTIAL_CONTENT:
|
||||||
total_size = parse_content_range(
|
total_size = parse_content_range(
|
||||||
response.headers.get('Content-Range'),
|
final_response.headers.get('Content-Range'),
|
||||||
self._resumed_from
|
self._resumed_from
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -220,19 +262,6 @@ class Download(object):
|
|||||||
self._output_file.truncate()
|
self._output_file.truncate()
|
||||||
except IOError:
|
except IOError:
|
||||||
pass # stdout
|
pass # stdout
|
||||||
else:
|
|
||||||
# TODO: Should the filename be taken from response.history[0].url?
|
|
||||||
# Output file not specified. Pick a name that doesn't exist yet.
|
|
||||||
filename = None
|
|
||||||
if 'Content-Disposition' in response.headers:
|
|
||||||
filename = filename_from_content_disposition(
|
|
||||||
response.headers['Content-Disposition'])
|
|
||||||
if not filename:
|
|
||||||
filename = filename_from_url(
|
|
||||||
url=response.url,
|
|
||||||
content_type=response.headers.get('Content-Type'),
|
|
||||||
)
|
|
||||||
self._output_file = open(get_unique_filename(filename), mode='a+b')
|
|
||||||
|
|
||||||
self.status.started(
|
self.status.started(
|
||||||
resumed_from=self._resumed_from,
|
resumed_from=self._resumed_from,
|
||||||
@ -240,7 +269,7 @@ class Download(object):
|
|||||||
)
|
)
|
||||||
|
|
||||||
stream = RawStream(
|
stream = RawStream(
|
||||||
msg=HTTPResponse(response),
|
msg=HTTPResponse(final_response),
|
||||||
with_headers=False,
|
with_headers=False,
|
||||||
with_body=True,
|
with_body=True,
|
||||||
on_body_chunk_downloaded=self.chunk_downloaded,
|
on_body_chunk_downloaded=self.chunk_downloaded,
|
||||||
@ -286,6 +315,25 @@ class Download(object):
|
|||||||
"""
|
"""
|
||||||
self.status.chunk_downloaded(len(chunk))
|
self.status.chunk_downloaded(len(chunk))
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _get_output_file_from_response(final_response):
|
||||||
|
# Output file not specified. Pick a name that doesn't exist yet.
|
||||||
|
filename = None
|
||||||
|
if 'Content-Disposition' in final_response.headers:
|
||||||
|
filename = filename_from_content_disposition(
|
||||||
|
final_response.headers['Content-Disposition'])
|
||||||
|
if not filename:
|
||||||
|
initial_response = (
|
||||||
|
final_response.history[0] if final_response.history
|
||||||
|
else final_response
|
||||||
|
)
|
||||||
|
filename = filename_from_url(
|
||||||
|
url=initial_response.url,
|
||||||
|
content_type=final_response.headers.get('Content-Type'),
|
||||||
|
)
|
||||||
|
unique_filename = get_unique_filename(filename)
|
||||||
|
return open(unique_filename, mode='a+b')
|
||||||
|
|
||||||
|
|
||||||
class Status(object):
|
class Status(object):
|
||||||
"""Holds details about the downland status."""
|
"""Holds details about the downland status."""
|
||||||
@ -299,8 +347,7 @@ class Status(object):
|
|||||||
|
|
||||||
def started(self, resumed_from=0, total_size=None):
|
def started(self, resumed_from=0, total_size=None):
|
||||||
assert self.time_started is None
|
assert self.time_started is None
|
||||||
if total_size is not None:
|
self.total_size = total_size
|
||||||
self.total_size = total_size
|
|
||||||
self.downloaded = self.resumed_from = resumed_from
|
self.downloaded = self.resumed_from = resumed_from
|
||||||
self.time_started = time()
|
self.time_started = time()
|
||||||
|
|
||||||
@ -410,8 +457,8 @@ class ProgressReporterThread(threading.Thread):
|
|||||||
else 0)
|
else 0)
|
||||||
|
|
||||||
def sum_up(self):
|
def sum_up(self):
|
||||||
actually_downloaded = (self.status.downloaded
|
actually_downloaded = (
|
||||||
- self.status.resumed_from)
|
self.status.downloaded - self.status.resumed_from)
|
||||||
time_taken = self.status.time_finished - self.status.time_started
|
time_taken = self.status.time_finished - self.status.time_started
|
||||||
|
|
||||||
self.output.write(CLEAR_LINE)
|
self.output.write(CLEAR_LINE)
|
||||||
|
218
httpie/input.py
218
httpie/input.py
@ -2,21 +2,23 @@
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
import os
|
import os
|
||||||
|
import ssl
|
||||||
import sys
|
import sys
|
||||||
import re
|
import re
|
||||||
import errno
|
import errno
|
||||||
import mimetypes
|
import mimetypes
|
||||||
import getpass
|
import getpass
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from collections import namedtuple, Iterable
|
from collections import namedtuple, Iterable, OrderedDict
|
||||||
# noinspection PyCompatibility
|
# noinspection PyCompatibility
|
||||||
from argparse import ArgumentParser, ArgumentTypeError, ArgumentError
|
from argparse import ArgumentParser, ArgumentTypeError, ArgumentError
|
||||||
|
|
||||||
# TODO: Use MultiDict for headers once added to `requests`.
|
# TODO: Use MultiDict for headers once added to `requests`.
|
||||||
# https://github.com/jkbrzt/httpie/issues/130
|
# https://github.com/jakubroztocil/httpie/issues/130
|
||||||
|
from httpie.plugins import plugin_manager
|
||||||
from requests.structures import CaseInsensitiveDict
|
from requests.structures import CaseInsensitiveDict
|
||||||
|
|
||||||
from httpie.compat import OrderedDict, urlsplit, str, is_pypy, is_py27
|
from httpie.compat import urlsplit, str, is_pypy, is_py27
|
||||||
from httpie.sessions import VALID_SESSION_NAME_PATTERN
|
from httpie.sessions import VALID_SESSION_NAME_PATTERN
|
||||||
from httpie.utils import load_json_preserve_order
|
from httpie.utils import load_json_preserve_order
|
||||||
|
|
||||||
@ -27,12 +29,11 @@ URL_SCHEME_RE = re.compile(r'^[a-z][a-z0-9.+-]*://', re.IGNORECASE)
|
|||||||
|
|
||||||
HTTP_POST = 'POST'
|
HTTP_POST = 'POST'
|
||||||
HTTP_GET = 'GET'
|
HTTP_GET = 'GET'
|
||||||
HTTP = 'http://'
|
|
||||||
HTTPS = 'https://'
|
|
||||||
|
|
||||||
|
|
||||||
# Various separators used in args
|
# Various separators used in args
|
||||||
SEP_HEADERS = ':'
|
SEP_HEADERS = ':'
|
||||||
|
SEP_HEADERS_EMPTY = ';'
|
||||||
SEP_CREDENTIALS = ':'
|
SEP_CREDENTIALS = ':'
|
||||||
SEP_PROXY = ':'
|
SEP_PROXY = ':'
|
||||||
SEP_DATA = '='
|
SEP_DATA = '='
|
||||||
@ -66,6 +67,7 @@ SEP_GROUP_RAW_JSON_ITEMS = frozenset([
|
|||||||
# Separators allowed in ITEM arguments
|
# Separators allowed in ITEM arguments
|
||||||
SEP_GROUP_ALL_ITEMS = frozenset([
|
SEP_GROUP_ALL_ITEMS = frozenset([
|
||||||
SEP_HEADERS,
|
SEP_HEADERS,
|
||||||
|
SEP_HEADERS_EMPTY,
|
||||||
SEP_QUERY,
|
SEP_QUERY,
|
||||||
SEP_DATA,
|
SEP_DATA,
|
||||||
SEP_DATA_RAW_JSON,
|
SEP_DATA_RAW_JSON,
|
||||||
@ -103,7 +105,22 @@ OUTPUT_OPTIONS_DEFAULT = OUT_RESP_HEAD + OUT_RESP_BODY
|
|||||||
OUTPUT_OPTIONS_DEFAULT_STDOUT_REDIRECTED = OUT_RESP_BODY
|
OUTPUT_OPTIONS_DEFAULT_STDOUT_REDIRECTED = OUT_RESP_BODY
|
||||||
|
|
||||||
|
|
||||||
class Parser(ArgumentParser):
|
SSL_VERSION_ARG_MAPPING = {
|
||||||
|
'ssl2.3': 'PROTOCOL_SSLv23',
|
||||||
|
'ssl3': 'PROTOCOL_SSLv3',
|
||||||
|
'tls1': 'PROTOCOL_TLSv1',
|
||||||
|
'tls1.1': 'PROTOCOL_TLSv1_1',
|
||||||
|
'tls1.2': 'PROTOCOL_TLSv1_2',
|
||||||
|
'tls1.3': 'PROTOCOL_TLSv1_3',
|
||||||
|
}
|
||||||
|
SSL_VERSION_ARG_MAPPING = {
|
||||||
|
cli_arg: getattr(ssl, ssl_constant)
|
||||||
|
for cli_arg, ssl_constant in SSL_VERSION_ARG_MAPPING.items()
|
||||||
|
if hasattr(ssl, ssl_constant)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class HTTPieArgumentParser(ArgumentParser):
|
||||||
"""Adds additional logic to `argparse.ArgumentParser`.
|
"""Adds additional logic to `argparse.ArgumentParser`.
|
||||||
|
|
||||||
Handles all input (CLI args, file args, stdin), applies defaults,
|
Handles all input (CLI args, file args, stdin), applies defaults,
|
||||||
@ -113,13 +130,13 @@ class Parser(ArgumentParser):
|
|||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
kwargs['add_help'] = False
|
kwargs['add_help'] = False
|
||||||
super(Parser, self).__init__(*args, **kwargs)
|
super(HTTPieArgumentParser, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
# noinspection PyMethodOverriding
|
# noinspection PyMethodOverriding
|
||||||
def parse_args(self, env, args=None, namespace=None):
|
def parse_args(self, env, args=None, namespace=None):
|
||||||
|
|
||||||
self.env = env
|
self.env = env
|
||||||
self.args, no_options = super(Parser, self)\
|
self.args, no_options = super(HTTPieArgumentParser, self)\
|
||||||
.parse_known_args(args, namespace)
|
.parse_known_args(args, namespace)
|
||||||
|
|
||||||
if self.args.debug:
|
if self.args.debug:
|
||||||
@ -127,7 +144,6 @@ class Parser(ArgumentParser):
|
|||||||
|
|
||||||
# Arguments processing and environment setup.
|
# Arguments processing and environment setup.
|
||||||
self._apply_no_options(no_options)
|
self._apply_no_options(no_options)
|
||||||
self._apply_config()
|
|
||||||
self._validate_download_options()
|
self._validate_download_options()
|
||||||
self._setup_standard_streams()
|
self._setup_standard_streams()
|
||||||
self._process_output_options()
|
self._process_output_options()
|
||||||
@ -137,7 +153,7 @@ class Parser(ArgumentParser):
|
|||||||
if not self.args.ignore_stdin and not env.stdin_isatty:
|
if not self.args.ignore_stdin and not env.stdin_isatty:
|
||||||
self._body_from_file(self.env.stdin)
|
self._body_from_file(self.env.stdin)
|
||||||
if not URL_SCHEME_RE.match(self.args.url):
|
if not URL_SCHEME_RE.match(self.args.url):
|
||||||
scheme = HTTP
|
scheme = self.args.default_scheme + "://"
|
||||||
|
|
||||||
# See if we're using curl style shorthand for localhost (:3000/foo)
|
# See if we're using curl style shorthand for localhost (:3000/foo)
|
||||||
shorthand = re.match(r'^:(?!:)(\d*)(/?.*)$', self.args.url)
|
shorthand = re.match(r'^:(?!:)(\d*)(/?.*)$', self.args.url)
|
||||||
@ -164,19 +180,17 @@ class Parser(ArgumentParser):
|
|||||||
}.get(file, file)
|
}.get(file, file)
|
||||||
if not hasattr(file, 'buffer') and isinstance(message, str):
|
if not hasattr(file, 'buffer') and isinstance(message, str):
|
||||||
message = message.encode(self.env.stdout_encoding)
|
message = message.encode(self.env.stdout_encoding)
|
||||||
super(Parser, self)._print_message(message, file)
|
super(HTTPieArgumentParser, self)._print_message(message, file)
|
||||||
|
|
||||||
def _setup_standard_streams(self):
|
def _setup_standard_streams(self):
|
||||||
"""
|
"""
|
||||||
Modify `env.stdout` and `env.stdout_isatty` based on args, if needed.
|
Modify `env.stdout` and `env.stdout_isatty` based on args, if needed.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
if not self.env.stdout_isatty and self.args.output_file:
|
self.args.output_file_specified = bool(self.args.output_file)
|
||||||
self.error('Cannot use --output, -o with redirected output.')
|
|
||||||
|
|
||||||
if self.args.download:
|
if self.args.download:
|
||||||
# FIXME: Come up with a cleaner solution.
|
# FIXME: Come up with a cleaner solution.
|
||||||
if not self.env.stdout_isatty:
|
if not self.args.output_file and not self.env.stdout_isatty:
|
||||||
# Use stdout as the download output file.
|
# Use stdout as the download output file.
|
||||||
self.args.output_file = self.env.stdout
|
self.args.output_file = self.env.stdout
|
||||||
# With `--download`, we write everything that would normally go to
|
# With `--download`, we write everything that would normally go to
|
||||||
@ -201,37 +215,59 @@ class Parser(ArgumentParser):
|
|||||||
self.env.stdout = self.args.output_file
|
self.env.stdout = self.args.output_file
|
||||||
self.env.stdout_isatty = False
|
self.env.stdout_isatty = False
|
||||||
|
|
||||||
def _apply_config(self):
|
|
||||||
if (not self.args.json
|
|
||||||
and self.env.config.implicit_content_type == 'form'):
|
|
||||||
self.args.form = True
|
|
||||||
|
|
||||||
def _process_auth(self):
|
def _process_auth(self):
|
||||||
"""
|
# TODO: refactor
|
||||||
If only a username provided via --auth, then ask for a password.
|
self.args.auth_plugin = None
|
||||||
Or, take credentials from the URL, if provided.
|
default_auth_plugin = plugin_manager.get_auth_plugins()[0]
|
||||||
|
auth_type_set = self.args.auth_type is not None
|
||||||
"""
|
|
||||||
url = urlsplit(self.args.url)
|
url = urlsplit(self.args.url)
|
||||||
|
|
||||||
if self.args.auth:
|
if self.args.auth is None and not auth_type_set:
|
||||||
if not self.args.auth.has_password():
|
if url.username is not None:
|
||||||
# Stdin already read (if not a tty) so it's save to prompt.
|
# Handle http://username:password@hostname/
|
||||||
if self.args.ignore_stdin:
|
username = url.username
|
||||||
self.error('Unable to prompt for passwords because'
|
password = url.password or ''
|
||||||
' --ignore-stdin is set.')
|
self.args.auth = AuthCredentials(
|
||||||
self.args.auth.prompt_password(url.netloc)
|
key=username,
|
||||||
|
value=password,
|
||||||
|
sep=SEP_CREDENTIALS,
|
||||||
|
orig=SEP_CREDENTIALS.join([username, password])
|
||||||
|
)
|
||||||
|
|
||||||
elif url.username is not None:
|
if self.args.auth is not None or auth_type_set:
|
||||||
# Handle http://username:password@hostname/
|
if not self.args.auth_type:
|
||||||
username = url.username
|
self.args.auth_type = default_auth_plugin.auth_type
|
||||||
password = url.password or ''
|
plugin = plugin_manager.get_auth_plugin(self.args.auth_type)()
|
||||||
self.args.auth = AuthCredentials(
|
|
||||||
key=username,
|
if plugin.auth_require and self.args.auth is None:
|
||||||
value=password,
|
self.error('--auth required')
|
||||||
sep=SEP_CREDENTIALS,
|
|
||||||
orig=SEP_CREDENTIALS.join([username, password])
|
plugin.raw_auth = self.args.auth
|
||||||
)
|
self.args.auth_plugin = plugin
|
||||||
|
already_parsed = isinstance(self.args.auth, AuthCredentials)
|
||||||
|
|
||||||
|
if self.args.auth is None or not plugin.auth_parse:
|
||||||
|
self.args.auth = plugin.get_auth()
|
||||||
|
else:
|
||||||
|
if already_parsed:
|
||||||
|
# from the URL
|
||||||
|
credentials = self.args.auth
|
||||||
|
else:
|
||||||
|
credentials = parse_auth(self.args.auth)
|
||||||
|
|
||||||
|
if (not credentials.has_password()
|
||||||
|
and plugin.prompt_password):
|
||||||
|
if self.args.ignore_stdin:
|
||||||
|
# Non-tty stdin read by now
|
||||||
|
self.error(
|
||||||
|
'Unable to prompt for passwords because'
|
||||||
|
' --ignore-stdin is set.'
|
||||||
|
)
|
||||||
|
credentials.prompt_password(url.netloc)
|
||||||
|
self.args.auth = plugin.get_auth(
|
||||||
|
username=credentials.key,
|
||||||
|
password=credentials.value,
|
||||||
|
)
|
||||||
|
|
||||||
def _apply_no_options(self, no_options):
|
def _apply_no_options(self, no_options):
|
||||||
"""For every `--no-OPTION` in `no_options`, set `args.OPTION` to
|
"""For every `--no-OPTION` in `no_options`, set `args.OPTION` to
|
||||||
@ -267,7 +303,8 @@ class Parser(ArgumentParser):
|
|||||||
"""
|
"""
|
||||||
if self.args.data:
|
if self.args.data:
|
||||||
self.error('Request body (from stdin or a file) and request '
|
self.error('Request body (from stdin or a file) and request '
|
||||||
'data (key=value) cannot be mixed.')
|
'data (key=value) cannot be mixed. Pass '
|
||||||
|
'--ignore-stdin to let key/value take priority.')
|
||||||
self.args.data = getattr(fd, 'buffer', fd).read()
|
self.args.data = getattr(fd, 'buffer', fd).read()
|
||||||
|
|
||||||
def _guess_method(self):
|
def _guess_method(self):
|
||||||
@ -303,8 +340,10 @@ class Parser(ArgumentParser):
|
|||||||
# Infer the method
|
# Infer the method
|
||||||
has_data = (
|
has_data = (
|
||||||
(not self.args.ignore_stdin and not self.env.stdin_isatty)
|
(not self.args.ignore_stdin and not self.env.stdin_isatty)
|
||||||
or any(item.sep in SEP_GROUP_DATA_ITEMS
|
or any(
|
||||||
for item in self.args.items)
|
item.sep in SEP_GROUP_DATA_ITEMS
|
||||||
|
for item in self.args.items
|
||||||
|
)
|
||||||
)
|
)
|
||||||
self.args.method = HTTP_POST if has_data else HTTP_GET
|
self.args.method = HTTP_POST if has_data else HTTP_GET
|
||||||
|
|
||||||
@ -336,17 +375,14 @@ class Parser(ArgumentParser):
|
|||||||
'Invalid file fields (perhaps you meant --form?): %s'
|
'Invalid file fields (perhaps you meant --form?): %s'
|
||||||
% ','.join(file_fields))
|
% ','.join(file_fields))
|
||||||
|
|
||||||
fn, fd = self.args.files['']
|
fn, fd, ct = self.args.files['']
|
||||||
self.args.files = {}
|
self.args.files = {}
|
||||||
|
|
||||||
self._body_from_file(fd)
|
self._body_from_file(fd)
|
||||||
|
|
||||||
if 'Content-Type' not in self.args.headers:
|
if 'Content-Type' not in self.args.headers:
|
||||||
mime, encoding = mimetypes.guess_type(fn, strict=False)
|
content_type = get_content_type(fn)
|
||||||
if mime:
|
if content_type:
|
||||||
content_type = mime
|
|
||||||
if encoding:
|
|
||||||
content_type = '%s; charset=%s' % (mime, encoding)
|
|
||||||
self.args.headers['Content-Type'] = content_type
|
self.args.headers['Content-Type'] = content_type
|
||||||
|
|
||||||
def _process_output_options(self):
|
def _process_output_options(self):
|
||||||
@ -355,18 +391,32 @@ class Parser(ArgumentParser):
|
|||||||
The default output options are stdout-type-sensitive.
|
The default output options are stdout-type-sensitive.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
if not self.args.output_options:
|
def check_options(value, option):
|
||||||
self.args.output_options = (
|
unknown = set(value) - OUTPUT_OPTIONS
|
||||||
OUTPUT_OPTIONS_DEFAULT
|
if unknown:
|
||||||
if self.env.stdout_isatty
|
self.error('Unknown output options: {0}={1}'.format(
|
||||||
else OUTPUT_OPTIONS_DEFAULT_STDOUT_REDIRECTED
|
option,
|
||||||
)
|
','.join(unknown)
|
||||||
|
))
|
||||||
|
|
||||||
unknown_output_options = set(self.args.output_options) - OUTPUT_OPTIONS
|
if self.args.verbose:
|
||||||
if unknown_output_options:
|
self.args.all = True
|
||||||
self.error(
|
|
||||||
'Unknown output options: %s' % ','.join(unknown_output_options)
|
if self.args.output_options is None:
|
||||||
)
|
if self.args.verbose:
|
||||||
|
self.args.output_options = ''.join(OUTPUT_OPTIONS)
|
||||||
|
else:
|
||||||
|
self.args.output_options = (
|
||||||
|
OUTPUT_OPTIONS_DEFAULT
|
||||||
|
if self.env.stdout_isatty
|
||||||
|
else OUTPUT_OPTIONS_DEFAULT_STDOUT_REDIRECTED
|
||||||
|
)
|
||||||
|
|
||||||
|
if self.args.output_options_history is None:
|
||||||
|
self.args.output_options_history = self.args.output_options
|
||||||
|
|
||||||
|
check_options(self.args.output_options, '--print')
|
||||||
|
check_options(self.args.output_options_history, '--history-print')
|
||||||
|
|
||||||
if self.args.download and OUT_RESP_BODY in self.args.output_options:
|
if self.args.download and OUT_RESP_BODY in self.args.output_options:
|
||||||
# Response body is always downloaded with --download and it goes
|
# Response body is always downloaded with --download and it goes
|
||||||
@ -378,8 +428,8 @@ class Parser(ArgumentParser):
|
|||||||
if self.args.prettify == PRETTY_STDOUT_TTY_ONLY:
|
if self.args.prettify == PRETTY_STDOUT_TTY_ONLY:
|
||||||
self.args.prettify = PRETTY_MAP[
|
self.args.prettify = PRETTY_MAP[
|
||||||
'all' if self.env.stdout_isatty else 'none']
|
'all' if self.env.stdout_isatty else 'none']
|
||||||
elif (self.args.prettify and self.env.is_windows and
|
elif (self.args.prettify and self.env.is_windows
|
||||||
self.args.output_file):
|
and self.args.output_file):
|
||||||
self.error('Only terminal output can be colorized on Windows.')
|
self.error('Only terminal output can be colorized on Windows.')
|
||||||
else:
|
else:
|
||||||
# noinspection PyTypeChecker
|
# noinspection PyTypeChecker
|
||||||
@ -457,7 +507,7 @@ class KeyValueArgType(object):
|
|||||||
"""Represents an escaped character."""
|
"""Represents an escaped character."""
|
||||||
|
|
||||||
def tokenize(string):
|
def tokenize(string):
|
||||||
"""Tokenize `string`. There are only two token types - strings
|
r"""Tokenize `string`. There are only two token types - strings
|
||||||
and escaped characters:
|
and escaped characters:
|
||||||
|
|
||||||
tokenize(r'foo\=bar\\baz')
|
tokenize(r'foo\=bar\\baz')
|
||||||
@ -559,6 +609,9 @@ class AuthCredentialsArgType(KeyValueArgType):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
parse_auth = AuthCredentialsArgType(SEP_CREDENTIALS)
|
||||||
|
|
||||||
|
|
||||||
class RequestItemsDict(OrderedDict):
|
class RequestItemsDict(OrderedDict):
|
||||||
"""Multi-value dict for URL parameters and form data."""
|
"""Multi-value dict for URL parameters and form data."""
|
||||||
|
|
||||||
@ -609,6 +662,21 @@ RequestItems = namedtuple('RequestItems',
|
|||||||
['headers', 'data', 'files', 'params'])
|
['headers', 'data', 'files', 'params'])
|
||||||
|
|
||||||
|
|
||||||
|
def get_content_type(filename):
|
||||||
|
"""
|
||||||
|
Return the content type for ``filename`` in format appropriate
|
||||||
|
for Content-Type headers, or ``None`` if the file type is unknown
|
||||||
|
to ``mimetypes``.
|
||||||
|
|
||||||
|
"""
|
||||||
|
mime, encoding = mimetypes.guess_type(filename, strict=False)
|
||||||
|
if mime:
|
||||||
|
content_type = mime
|
||||||
|
if encoding:
|
||||||
|
content_type = '%s; charset=%s' % (mime, encoding)
|
||||||
|
return content_type
|
||||||
|
|
||||||
|
|
||||||
def parse_items(items,
|
def parse_items(items,
|
||||||
headers_class=CaseInsensitiveDict,
|
headers_class=CaseInsensitiveDict,
|
||||||
data_class=OrderedDict,
|
data_class=OrderedDict,
|
||||||
@ -622,11 +690,20 @@ def parse_items(items,
|
|||||||
data = []
|
data = []
|
||||||
files = []
|
files = []
|
||||||
params = []
|
params = []
|
||||||
|
|
||||||
for item in items:
|
for item in items:
|
||||||
value = item.value
|
value = item.value
|
||||||
|
|
||||||
if item.sep == SEP_HEADERS:
|
if item.sep == SEP_HEADERS:
|
||||||
|
if value == '':
|
||||||
|
# No value => unset the header
|
||||||
|
value = None
|
||||||
|
target = headers
|
||||||
|
elif item.sep == SEP_HEADERS_EMPTY:
|
||||||
|
if item.value:
|
||||||
|
raise ParseError(
|
||||||
|
'Invalid item "%s" '
|
||||||
|
'(to specify an empty header use `Header;`)'
|
||||||
|
% item.orig
|
||||||
|
)
|
||||||
target = headers
|
target = headers
|
||||||
elif item.sep == SEP_QUERY:
|
elif item.sep == SEP_QUERY:
|
||||||
target = params
|
target = params
|
||||||
@ -634,7 +711,8 @@ def parse_items(items,
|
|||||||
try:
|
try:
|
||||||
with open(os.path.expanduser(value), 'rb') as f:
|
with open(os.path.expanduser(value), 'rb') as f:
|
||||||
value = (os.path.basename(value),
|
value = (os.path.basename(value),
|
||||||
BytesIO(f.read()))
|
BytesIO(f.read()),
|
||||||
|
get_content_type(value))
|
||||||
except IOError as e:
|
except IOError as e:
|
||||||
raise ParseError('"%s": %s' % (item.orig, e))
|
raise ParseError('"%s": %s' % (item.orig, e))
|
||||||
target = files
|
target = files
|
||||||
@ -674,7 +752,7 @@ def parse_items(items,
|
|||||||
|
|
||||||
def readable_file_arg(filename):
|
def readable_file_arg(filename):
|
||||||
try:
|
try:
|
||||||
open(filename, 'rb')
|
with open(filename, 'rb'):
|
||||||
|
return filename
|
||||||
except IOError as ex:
|
except IOError as ex:
|
||||||
raise ArgumentTypeError('%s: %s' % (filename, ex.args[1]))
|
raise ArgumentTypeError('%s: %s' % (filename, ex.args[1]))
|
||||||
return filename
|
|
||||||
|
@ -48,7 +48,7 @@ class HTTPResponse(HTTPMessage):
|
|||||||
def iter_lines(self, chunk_size):
|
def iter_lines(self, chunk_size):
|
||||||
return ((line, b'\n') for line in self._orig.iter_lines(chunk_size))
|
return ((line, b'\n') for line in self._orig.iter_lines(chunk_size))
|
||||||
|
|
||||||
#noinspection PyProtectedMember
|
# noinspection PyProtectedMember
|
||||||
@property
|
@property
|
||||||
def headers(self):
|
def headers(self):
|
||||||
original = self._orig.raw._original_response
|
original = self._orig.raw._original_response
|
||||||
|
@ -1,3 +1,6 @@
|
|||||||
|
from __future__ import absolute_import
|
||||||
|
import json
|
||||||
|
|
||||||
import pygments.lexer
|
import pygments.lexer
|
||||||
import pygments.token
|
import pygments.token
|
||||||
import pygments.styles
|
import pygments.styles
|
||||||
@ -5,16 +8,26 @@ import pygments.lexers
|
|||||||
import pygments.style
|
import pygments.style
|
||||||
from pygments.formatters.terminal import TerminalFormatter
|
from pygments.formatters.terminal import TerminalFormatter
|
||||||
from pygments.formatters.terminal256 import Terminal256Formatter
|
from pygments.formatters.terminal256 import Terminal256Formatter
|
||||||
|
from pygments.lexers.special import TextLexer
|
||||||
|
from pygments.lexers.text import HttpLexer as PygmentsHttpLexer
|
||||||
from pygments.util import ClassNotFound
|
from pygments.util import ClassNotFound
|
||||||
|
|
||||||
|
from httpie.compat import is_windows
|
||||||
from httpie.plugins import FormatterPlugin
|
from httpie.plugins import FormatterPlugin
|
||||||
|
|
||||||
|
|
||||||
# Colors on Windows via colorama don't look that
|
AUTO_STYLE = 'auto' # Follows terminal ANSI color styles
|
||||||
# great and fruity seems to give the best result there.
|
DEFAULT_STYLE = AUTO_STYLE
|
||||||
AVAILABLE_STYLES = set(pygments.styles.STYLE_MAP.keys())
|
SOLARIZED_STYLE = 'solarized' # Bundled here
|
||||||
AVAILABLE_STYLES.add('solarized')
|
if is_windows:
|
||||||
DEFAULT_STYLE = 'monokai'
|
# Colors on Windows via colorama don't look that
|
||||||
|
# great and fruity seems to give the best result there.
|
||||||
|
DEFAULT_STYLE = 'fruity'
|
||||||
|
|
||||||
|
|
||||||
|
AVAILABLE_STYLES = set(pygments.styles.get_all_styles())
|
||||||
|
AVAILABLE_STYLES.add(SOLARIZED_STYLE)
|
||||||
|
AVAILABLE_STYLES.add(AUTO_STYLE)
|
||||||
|
|
||||||
|
|
||||||
class ColorFormatter(FormatterPlugin):
|
class ColorFormatter(FormatterPlugin):
|
||||||
@ -27,58 +40,81 @@ class ColorFormatter(FormatterPlugin):
|
|||||||
"""
|
"""
|
||||||
group_name = 'colors'
|
group_name = 'colors'
|
||||||
|
|
||||||
def __init__(self, env, color_scheme=DEFAULT_STYLE, **kwargs):
|
def __init__(self, env, explicit_json=False,
|
||||||
|
color_scheme=DEFAULT_STYLE, **kwargs):
|
||||||
super(ColorFormatter, self).__init__(**kwargs)
|
super(ColorFormatter, self).__init__(**kwargs)
|
||||||
|
|
||||||
if not env.colors:
|
if not env.colors:
|
||||||
self.enabled = False
|
self.enabled = False
|
||||||
return
|
return
|
||||||
|
|
||||||
# Cache to speed things up when we process streamed body by line.
|
use_auto_style = color_scheme == AUTO_STYLE
|
||||||
self.lexer_cache = {}
|
has_256_colors = env.colors == 256
|
||||||
|
if use_auto_style or not has_256_colors:
|
||||||
try:
|
http_lexer = PygmentsHttpLexer()
|
||||||
style_class = pygments.styles.get_style_by_name(color_scheme)
|
formatter = TerminalFormatter()
|
||||||
except ClassNotFound:
|
|
||||||
style_class = Solarized256Style
|
|
||||||
|
|
||||||
if env.colors == 256:
|
|
||||||
fmt_class = Terminal256Formatter
|
|
||||||
else:
|
else:
|
||||||
fmt_class = TerminalFormatter
|
http_lexer = SimplifiedHTTPLexer()
|
||||||
self.formatter = fmt_class(style=style_class)
|
formatter = Terminal256Formatter(
|
||||||
|
style=self.get_style_class(color_scheme)
|
||||||
|
)
|
||||||
|
|
||||||
|
self.explicit_json = explicit_json # --json
|
||||||
|
self.formatter = formatter
|
||||||
|
self.http_lexer = http_lexer
|
||||||
|
|
||||||
def format_headers(self, headers):
|
def format_headers(self, headers):
|
||||||
return pygments.highlight(headers, HTTPLexer(), self.formatter).strip()
|
return pygments.highlight(
|
||||||
|
code=headers,
|
||||||
|
lexer=self.http_lexer,
|
||||||
|
formatter=self.formatter,
|
||||||
|
).strip()
|
||||||
|
|
||||||
def format_body(self, body, mime):
|
def format_body(self, body, mime):
|
||||||
lexer = self.get_lexer(mime)
|
lexer = self.get_lexer_for_body(mime, body)
|
||||||
if lexer:
|
if lexer:
|
||||||
body = pygments.highlight(body, lexer, self.formatter)
|
body = pygments.highlight(
|
||||||
|
code=body,
|
||||||
|
lexer=lexer,
|
||||||
|
formatter=self.formatter,
|
||||||
|
)
|
||||||
return body.strip()
|
return body.strip()
|
||||||
|
|
||||||
def get_lexer(self, mime):
|
def get_lexer_for_body(self, mime, body):
|
||||||
if mime in self.lexer_cache:
|
return get_lexer(
|
||||||
return self.lexer_cache[mime]
|
mime=mime,
|
||||||
self.lexer_cache[mime] = get_lexer(mime)
|
explicit_json=self.explicit_json,
|
||||||
return self.lexer_cache[mime]
|
body=body,
|
||||||
|
)
|
||||||
|
|
||||||
|
def get_style_class(self, color_scheme):
|
||||||
|
try:
|
||||||
|
return pygments.styles.get_style_by_name(color_scheme)
|
||||||
|
except ClassNotFound:
|
||||||
|
return Solarized256Style
|
||||||
|
|
||||||
|
|
||||||
def get_lexer(mime):
|
def get_lexer(mime, explicit_json=False, body=''):
|
||||||
|
|
||||||
|
# Build candidate mime type and lexer names.
|
||||||
mime_types, lexer_names = [mime], []
|
mime_types, lexer_names = [mime], []
|
||||||
type_, subtype = mime.split('/')
|
type_, subtype = mime.split('/', 1)
|
||||||
if '+' not in subtype:
|
if '+' not in subtype:
|
||||||
lexer_names.append(subtype)
|
lexer_names.append(subtype)
|
||||||
else:
|
else:
|
||||||
subtype_name, subtype_suffix = subtype.split('+')
|
subtype_name, subtype_suffix = subtype.split('+', 1)
|
||||||
lexer_names.extend([subtype_name, subtype_suffix])
|
lexer_names.extend([subtype_name, subtype_suffix])
|
||||||
mime_types.extend([
|
mime_types.extend([
|
||||||
'%s/%s' % (type_, subtype_name),
|
'%s/%s' % (type_, subtype_name),
|
||||||
'%s/%s' % (type_, subtype_suffix)
|
'%s/%s' % (type_, subtype_suffix)
|
||||||
])
|
])
|
||||||
# as a last resort, if no lexer feels responsible, and
|
|
||||||
|
# As a last resort, if no lexer feels responsible, and
|
||||||
# the subtype contains 'json', take the JSON lexer
|
# the subtype contains 'json', take the JSON lexer
|
||||||
if 'json' in subtype:
|
if 'json' in subtype:
|
||||||
lexer_names.append('json')
|
lexer_names.append('json')
|
||||||
|
|
||||||
|
# Try to resolve the right lexer.
|
||||||
lexer = None
|
lexer = None
|
||||||
for mime_type in mime_types:
|
for mime_type in mime_types:
|
||||||
try:
|
try:
|
||||||
@ -92,10 +128,20 @@ def get_lexer(mime):
|
|||||||
lexer = pygments.lexers.get_lexer_by_name(name)
|
lexer = pygments.lexers.get_lexer_by_name(name)
|
||||||
except ClassNotFound:
|
except ClassNotFound:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
if explicit_json and body and (not lexer or isinstance(lexer, TextLexer)):
|
||||||
|
# JSON response with an incorrect Content-Type?
|
||||||
|
try:
|
||||||
|
json.loads(body) # FIXME: the body also gets parsed in json.py
|
||||||
|
except ValueError:
|
||||||
|
pass # Nope
|
||||||
|
else:
|
||||||
|
lexer = pygments.lexers.get_lexer_by_name('json')
|
||||||
|
|
||||||
return lexer
|
return lexer
|
||||||
|
|
||||||
|
|
||||||
class HTTPLexer(pygments.lexer.RegexLexer):
|
class SimplifiedHTTPLexer(pygments.lexer.RegexLexer):
|
||||||
"""Simplified HTTP lexer for Pygments.
|
"""Simplified HTTP lexer for Pygments.
|
||||||
|
|
||||||
It only operates on headers and provides a stronger contrast between
|
It only operates on headers and provides a stronger contrast between
|
||||||
@ -143,8 +189,6 @@ class HTTPLexer(pygments.lexer.RegexLexer):
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# TODO: As Solarized is not the default theme any longer, it should be removed
|
|
||||||
# or bundled directly with Pygments so that we don't need to support it.
|
|
||||||
class Solarized256Style(pygments.style.Style):
|
class Solarized256Style(pygments.style.Style):
|
||||||
"""
|
"""
|
||||||
solarized256
|
solarized256
|
||||||
|
@ -10,17 +10,24 @@ DEFAULT_INDENT = 4
|
|||||||
class JSONFormatter(FormatterPlugin):
|
class JSONFormatter(FormatterPlugin):
|
||||||
|
|
||||||
def format_body(self, body, mime):
|
def format_body(self, body, mime):
|
||||||
if 'json' in mime:
|
maybe_json = [
|
||||||
|
'json',
|
||||||
|
'javascript',
|
||||||
|
'text',
|
||||||
|
]
|
||||||
|
if (self.kwargs['explicit_json']
|
||||||
|
or any(token in mime for token in maybe_json)):
|
||||||
try:
|
try:
|
||||||
obj = json.loads(body)
|
obj = json.loads(body)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
# Invalid JSON, ignore.
|
pass # Invalid JSON, ignore.
|
||||||
pass
|
|
||||||
else:
|
else:
|
||||||
# Indent, sort keys by name, and avoid
|
# Indent, sort keys by name, and avoid
|
||||||
# unicode escapes to improve readability.
|
# unicode escapes to improve readability.
|
||||||
body = json.dumps(obj,
|
body = json.dumps(
|
||||||
sort_keys=True,
|
obj=obj,
|
||||||
ensure_ascii=False,
|
sort_keys=True,
|
||||||
indent=DEFAULT_INDENT)
|
ensure_ascii=False,
|
||||||
|
indent=DEFAULT_INDENT
|
||||||
|
)
|
||||||
return body
|
return body
|
||||||
|
@ -1,61 +0,0 @@
|
|||||||
from __future__ import absolute_import
|
|
||||||
import re
|
|
||||||
from xml.etree import ElementTree
|
|
||||||
|
|
||||||
from httpie.plugins import FormatterPlugin
|
|
||||||
|
|
||||||
|
|
||||||
DECLARATION_RE = re.compile('<\?xml[^\n]+?\?>', flags=re.I)
|
|
||||||
DOCTYPE_RE = re.compile('<!DOCTYPE[^\n]+?>', flags=re.I)
|
|
||||||
|
|
||||||
|
|
||||||
DEFAULT_INDENT = 4
|
|
||||||
|
|
||||||
|
|
||||||
def indent(elem, indent_text=' ' * DEFAULT_INDENT):
|
|
||||||
"""
|
|
||||||
In-place prettyprint formatter
|
|
||||||
C.f. http://effbot.org/zone/element-lib.htm#prettyprint
|
|
||||||
|
|
||||||
"""
|
|
||||||
def _indent(elem, level=0):
|
|
||||||
i = "\n" + level * indent_text
|
|
||||||
if len(elem):
|
|
||||||
if not elem.text or not elem.text.strip():
|
|
||||||
elem.text = i + indent_text
|
|
||||||
if not elem.tail or not elem.tail.strip():
|
|
||||||
elem.tail = i
|
|
||||||
for elem in elem:
|
|
||||||
_indent(elem, level + 1)
|
|
||||||
if not elem.tail or not elem.tail.strip():
|
|
||||||
elem.tail = i
|
|
||||||
else:
|
|
||||||
if level and (not elem.tail or not elem.tail.strip()):
|
|
||||||
elem.tail = i
|
|
||||||
|
|
||||||
return _indent(elem)
|
|
||||||
|
|
||||||
|
|
||||||
class XMLFormatter(FormatterPlugin):
|
|
||||||
# TODO: tests
|
|
||||||
|
|
||||||
def format_body(self, body, mime):
|
|
||||||
if 'xml' in mime:
|
|
||||||
# FIXME: orig NS names get forgotten during the conversion, etc.
|
|
||||||
try:
|
|
||||||
root = ElementTree.fromstring(body.encode('utf8'))
|
|
||||||
except ElementTree.ParseError:
|
|
||||||
# Ignore invalid XML errors (skips attempting to pretty print)
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
indent(root)
|
|
||||||
# Use the original declaration
|
|
||||||
declaration = DECLARATION_RE.match(body)
|
|
||||||
doctype = DOCTYPE_RE.match(body)
|
|
||||||
body = ElementTree.tostring(root, encoding='utf-8')\
|
|
||||||
.decode('utf8')
|
|
||||||
if doctype:
|
|
||||||
body = '%s\n%s' % (doctype.group(0), body)
|
|
||||||
if declaration:
|
|
||||||
body = '%s\n%s' % (declaration.group(0), body)
|
|
||||||
return body
|
|
@ -24,7 +24,7 @@ class BinarySuppressedError(Exception):
|
|||||||
message = BINARY_SUPPRESSED_NOTICE
|
message = BINARY_SUPPRESSED_NOTICE
|
||||||
|
|
||||||
|
|
||||||
def write(stream, outfile, flush):
|
def write_stream(stream, outfile, flush):
|
||||||
"""Write the output stream."""
|
"""Write the output stream."""
|
||||||
try:
|
try:
|
||||||
# Writing bytes so we use the buffer interface (Python 3).
|
# Writing bytes so we use the buffer interface (Python 3).
|
||||||
@ -38,7 +38,7 @@ def write(stream, outfile, flush):
|
|||||||
outfile.flush()
|
outfile.flush()
|
||||||
|
|
||||||
|
|
||||||
def write_with_colors_win_py3(stream, outfile, flush):
|
def write_stream_with_colors_win_py3(stream, outfile, flush):
|
||||||
"""Like `write`, but colorized chunks are written as text
|
"""Like `write`, but colorized chunks are written as text
|
||||||
directly to `outfile` to ensure it gets processed by colorama.
|
directly to `outfile` to ensure it gets processed by colorama.
|
||||||
Applies only to Windows with Python 3 and colorized terminal output.
|
Applies only to Windows with Python 3 and colorized terminal output.
|
||||||
@ -55,15 +55,15 @@ def write_with_colors_win_py3(stream, outfile, flush):
|
|||||||
outfile.flush()
|
outfile.flush()
|
||||||
|
|
||||||
|
|
||||||
def build_output_stream(args, env, request, response):
|
def build_output_stream(args, env, request, response, output_options):
|
||||||
"""Build and return a chain of iterators over the `request`-`response`
|
"""Build and return a chain of iterators over the `request`-`response`
|
||||||
exchange each of which yields `bytes` chunks.
|
exchange each of which yields `bytes` chunks.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
req_h = OUT_REQ_HEAD in args.output_options
|
req_h = OUT_REQ_HEAD in output_options
|
||||||
req_b = OUT_REQ_BODY in args.output_options
|
req_b = OUT_REQ_BODY in output_options
|
||||||
resp_h = OUT_RESP_HEAD in args.output_options
|
resp_h = OUT_RESP_HEAD in output_options
|
||||||
resp_b = OUT_RESP_BODY in args.output_options
|
resp_b = OUT_RESP_BODY in output_options
|
||||||
req = req_h or req_b
|
req = req_h or req_b
|
||||||
resp = resp_h or resp_b
|
resp = resp_h or resp_b
|
||||||
|
|
||||||
@ -112,8 +112,12 @@ def get_stream_type(env, args):
|
|||||||
PrettyStream if args.stream else BufferedPrettyStream,
|
PrettyStream if args.stream else BufferedPrettyStream,
|
||||||
env=env,
|
env=env,
|
||||||
conversion=Conversion(),
|
conversion=Conversion(),
|
||||||
formatting=Formatting(env=env, groups=args.prettify,
|
formatting=Formatting(
|
||||||
color_scheme=args.style),
|
env=env,
|
||||||
|
groups=args.prettify,
|
||||||
|
color_scheme=args.style,
|
||||||
|
explicit_json=args.json,
|
||||||
|
),
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
Stream = partial(EncodedStream, env=env)
|
Stream = partial(EncodedStream, env=env)
|
||||||
|
@ -11,7 +11,6 @@ from httpie.plugins.manager import PluginManager
|
|||||||
from httpie.plugins.builtin import BasicAuthPlugin, DigestAuthPlugin
|
from httpie.plugins.builtin import BasicAuthPlugin, DigestAuthPlugin
|
||||||
from httpie.output.formatters.headers import HeadersFormatter
|
from httpie.output.formatters.headers import HeadersFormatter
|
||||||
from httpie.output.formatters.json import JSONFormatter
|
from httpie.output.formatters.json import JSONFormatter
|
||||||
from httpie.output.formatters.xml import XMLFormatter
|
|
||||||
from httpie.output.formatters.colors import ColorFormatter
|
from httpie.output.formatters.colors import ColorFormatter
|
||||||
|
|
||||||
|
|
||||||
@ -20,5 +19,4 @@ plugin_manager.register(BasicAuthPlugin,
|
|||||||
DigestAuthPlugin)
|
DigestAuthPlugin)
|
||||||
plugin_manager.register(HeadersFormatter,
|
plugin_manager.register(HeadersFormatter,
|
||||||
JSONFormatter,
|
JSONFormatter,
|
||||||
XMLFormatter,
|
|
||||||
ColorFormatter)
|
ColorFormatter)
|
||||||
|
@ -15,15 +15,41 @@ class AuthPlugin(BasePlugin):
|
|||||||
"""
|
"""
|
||||||
Base auth plugin class.
|
Base auth plugin class.
|
||||||
|
|
||||||
See <https://github.com/jkbrzt/httpie-ntlm> for an example auth plugin.
|
See <https://github.com/httpie/httpie-ntlm> for an example auth plugin.
|
||||||
|
|
||||||
|
See also `test_auth_plugins.py`
|
||||||
|
|
||||||
"""
|
"""
|
||||||
# The value that should be passed to --auth-type
|
# The value that should be passed to --auth-type
|
||||||
# to use this auth plugin. Eg. "my-auth"
|
# to use this auth plugin. Eg. "my-auth"
|
||||||
auth_type = None
|
auth_type = None
|
||||||
|
|
||||||
def get_auth(self, username, password):
|
# Set to `False` to make it possible to invoke this auth
|
||||||
|
# plugin without requiring the user to specify credentials
|
||||||
|
# through `--auth, -a`.
|
||||||
|
auth_require = True
|
||||||
|
|
||||||
|
# By default the `-a` argument is parsed for `username:password`.
|
||||||
|
# Set this to `False` to disable the parsing and error handling.
|
||||||
|
auth_parse = True
|
||||||
|
|
||||||
|
# If both `auth_parse` and `prompt_password` are set to `True`,
|
||||||
|
# and the value of `-a` lacks the password part,
|
||||||
|
# then the user will be prompted to type the password in.
|
||||||
|
prompt_password = True
|
||||||
|
|
||||||
|
# Will be set to the raw value of `-a` (if provided) before
|
||||||
|
# `get_auth()` gets called.
|
||||||
|
raw_auth = None
|
||||||
|
|
||||||
|
def get_auth(self, username=None, password=None):
|
||||||
"""
|
"""
|
||||||
|
If `auth_parse` is set to `True`, then `username`
|
||||||
|
and `password` contain the parsed credentials.
|
||||||
|
|
||||||
|
Use `self.raw_auth` to access the raw value passed through
|
||||||
|
`--auth, -a`.
|
||||||
|
|
||||||
Return a ``requests.auth.AuthBase`` subclass instance.
|
Return a ``requests.auth.AuthBase`` subclass instance.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
@ -5,6 +5,7 @@ import requests.auth
|
|||||||
from httpie.plugins.base import AuthPlugin
|
from httpie.plugins.base import AuthPlugin
|
||||||
|
|
||||||
|
|
||||||
|
# noinspection PyAbstractClass
|
||||||
class BuiltinAuthPlugin(AuthPlugin):
|
class BuiltinAuthPlugin(AuthPlugin):
|
||||||
|
|
||||||
package_name = '(builtin)'
|
package_name = '(builtin)'
|
||||||
@ -16,7 +17,7 @@ class HTTPBasicAuth(requests.auth.HTTPBasicAuth):
|
|||||||
"""
|
"""
|
||||||
Override username/password serialization to allow unicode.
|
Override username/password serialization to allow unicode.
|
||||||
|
|
||||||
See https://github.com/jkbrzt/httpie/issues/212
|
See https://github.com/jakubroztocil/httpie/issues/212
|
||||||
|
|
||||||
"""
|
"""
|
||||||
r.headers['Authorization'] = type(self).make_header(
|
r.headers['Authorization'] = type(self).make_header(
|
||||||
@ -35,6 +36,7 @@ class BasicAuthPlugin(BuiltinAuthPlugin):
|
|||||||
name = 'Basic HTTP auth'
|
name = 'Basic HTTP auth'
|
||||||
auth_type = 'basic'
|
auth_type = 'basic'
|
||||||
|
|
||||||
|
# noinspection PyMethodOverriding
|
||||||
def get_auth(self, username, password):
|
def get_auth(self, username, password):
|
||||||
return HTTPBasicAuth(username, password)
|
return HTTPBasicAuth(username, password)
|
||||||
|
|
||||||
@ -44,5 +46,6 @@ class DigestAuthPlugin(BuiltinAuthPlugin):
|
|||||||
name = 'Digest HTTP auth'
|
name = 'Digest HTTP auth'
|
||||||
auth_type = 'digest'
|
auth_type = 'digest'
|
||||||
|
|
||||||
|
# noinspection PyMethodOverriding
|
||||||
def get_auth(self, username, password):
|
def get_auth(self, username, password):
|
||||||
return requests.auth.HTTPDigestAuth(username, password)
|
return requests.auth.HTTPDigestAuth(username, password)
|
||||||
|
@ -24,6 +24,9 @@ class PluginManager(object):
|
|||||||
for plugin in plugins:
|
for plugin in plugins:
|
||||||
self._plugins.append(plugin)
|
self._plugins.append(plugin)
|
||||||
|
|
||||||
|
def unregister(self, plugin):
|
||||||
|
self._plugins.remove(plugin)
|
||||||
|
|
||||||
def load_installed_plugins(self):
|
def load_installed_plugins(self):
|
||||||
for entry_point_name in ENTRY_POINT_NAMES:
|
for entry_point_name in ENTRY_POINT_NAMES:
|
||||||
for entry_point in iter_entry_points(entry_point_name):
|
for entry_point in iter_entry_points(entry_point_name):
|
||||||
@ -36,8 +39,7 @@ class PluginManager(object):
|
|||||||
return [plugin for plugin in self if issubclass(plugin, AuthPlugin)]
|
return [plugin for plugin in self if issubclass(plugin, AuthPlugin)]
|
||||||
|
|
||||||
def get_auth_plugin_mapping(self):
|
def get_auth_plugin_mapping(self):
|
||||||
return dict((plugin.auth_type, plugin)
|
return {plugin.auth_type: plugin for plugin in self.get_auth_plugins()}
|
||||||
for plugin in self.get_auth_plugins())
|
|
||||||
|
|
||||||
def get_auth_plugin(self, auth_type):
|
def get_auth_plugin(self, auth_type):
|
||||||
return self.get_auth_plugin_mapping()[auth_type]
|
return self.get_auth_plugin_mapping()[auth_type]
|
||||||
|
@ -22,7 +22,7 @@ SESSION_IGNORED_HEADER_PREFIXES = ['Content-', 'If-']
|
|||||||
|
|
||||||
def get_response(requests_session, session_name,
|
def get_response(requests_session, session_name,
|
||||||
config_dir, args, read_only=False):
|
config_dir, args, read_only=False):
|
||||||
"""Like `client.get_response`, but applies permanent
|
"""Like `client.get_responses`, but applies permanent
|
||||||
aspects of the session to the request.
|
aspects of the session to the request.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@ -51,11 +51,10 @@ def get_response(requests_session, session_name,
|
|||||||
dump_request(kwargs)
|
dump_request(kwargs)
|
||||||
session.update_headers(kwargs['headers'])
|
session.update_headers(kwargs['headers'])
|
||||||
|
|
||||||
if args.auth:
|
if args.auth_plugin:
|
||||||
session.auth = {
|
session.auth = {
|
||||||
'type': args.auth_type,
|
'type': args.auth_plugin.auth_type,
|
||||||
'username': args.auth.key,
|
'raw_auth': args.auth_plugin.raw_auth,
|
||||||
'password': args.auth.value,
|
|
||||||
}
|
}
|
||||||
elif session.auth:
|
elif session.auth:
|
||||||
kwargs['auth'] = session.auth
|
kwargs['auth'] = session.auth
|
||||||
@ -75,7 +74,7 @@ def get_response(requests_session, session_name,
|
|||||||
|
|
||||||
|
|
||||||
class Session(BaseConfigDict):
|
class Session(BaseConfigDict):
|
||||||
helpurl = 'https://github.com/jkbrzt/httpie#sessions'
|
helpurl = 'https://httpie.org/doc#sessions'
|
||||||
about = 'HTTPie session file'
|
about = 'HTTPie session file'
|
||||||
|
|
||||||
def __init__(self, path, *args, **kwargs):
|
def __init__(self, path, *args, **kwargs):
|
||||||
@ -101,6 +100,10 @@ class Session(BaseConfigDict):
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
for name, value in request_headers.items():
|
for name, value in request_headers.items():
|
||||||
|
|
||||||
|
if value is None:
|
||||||
|
continue # Ignore explicitely unset headers
|
||||||
|
|
||||||
value = value.decode('utf8')
|
value = value.decode('utf8')
|
||||||
if name == 'User-Agent' and value.startswith('HTTPie/'):
|
if name == 'User-Agent' and value.startswith('HTTPie/'):
|
||||||
continue
|
continue
|
||||||
@ -133,20 +136,41 @@ class Session(BaseConfigDict):
|
|||||||
stored_attrs = ['value', 'path', 'secure', 'expires']
|
stored_attrs = ['value', 'path', 'secure', 'expires']
|
||||||
self['cookies'] = {}
|
self['cookies'] = {}
|
||||||
for cookie in jar:
|
for cookie in jar:
|
||||||
self['cookies'][cookie.name] = dict(
|
self['cookies'][cookie.name] = {
|
||||||
(attname, getattr(cookie, attname))
|
attname: getattr(cookie, attname)
|
||||||
for attname in stored_attrs
|
for attname in stored_attrs
|
||||||
)
|
}
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def auth(self):
|
def auth(self):
|
||||||
auth = self.get('auth', None)
|
auth = self.get('auth', None)
|
||||||
if not auth or not auth['type']:
|
if not auth or not auth['type']:
|
||||||
return
|
return
|
||||||
auth_plugin = plugin_manager.get_auth_plugin(auth['type'])()
|
|
||||||
return auth_plugin.get_auth(auth['username'], auth['password'])
|
plugin = plugin_manager.get_auth_plugin(auth['type'])()
|
||||||
|
|
||||||
|
credentials = {'username': None, 'password': None}
|
||||||
|
try:
|
||||||
|
# New style
|
||||||
|
plugin.raw_auth = auth['raw_auth']
|
||||||
|
except KeyError:
|
||||||
|
# Old style
|
||||||
|
credentials = {
|
||||||
|
'username': auth['username'],
|
||||||
|
'password': auth['password'],
|
||||||
|
}
|
||||||
|
else:
|
||||||
|
if plugin.auth_parse:
|
||||||
|
from httpie.input import parse_auth
|
||||||
|
parsed = parse_auth(plugin.raw_auth)
|
||||||
|
credentials = {
|
||||||
|
'username': parsed.key,
|
||||||
|
'password': parsed.value,
|
||||||
|
}
|
||||||
|
|
||||||
|
return plugin.get_auth(**credentials)
|
||||||
|
|
||||||
@auth.setter
|
@auth.setter
|
||||||
def auth(self, auth):
|
def auth(self, auth):
|
||||||
assert set(['type', 'username', 'password']) == set(auth.keys())
|
assert set(['type', 'raw_auth']) == set(auth.keys())
|
||||||
self['auth'] = auth
|
self['auth'] = auth
|
||||||
|
@ -1,15 +1,28 @@
|
|||||||
from __future__ import division
|
from __future__ import division
|
||||||
import json
|
import json
|
||||||
|
from collections import OrderedDict
|
||||||
from httpie.compat import is_py26, OrderedDict
|
|
||||||
|
|
||||||
|
|
||||||
def load_json_preserve_order(s):
|
def load_json_preserve_order(s):
|
||||||
if is_py26:
|
|
||||||
return json.loads(s)
|
|
||||||
return json.loads(s, object_pairs_hook=OrderedDict)
|
return json.loads(s, object_pairs_hook=OrderedDict)
|
||||||
|
|
||||||
|
|
||||||
|
def repr_dict_nice(d):
|
||||||
|
def prepare_dict(d):
|
||||||
|
for k, v in d.items():
|
||||||
|
if isinstance(v, dict):
|
||||||
|
v = dict(prepare_dict(v))
|
||||||
|
elif isinstance(v, bytes):
|
||||||
|
v = v.decode('utf8')
|
||||||
|
elif not isinstance(v, (int, str)):
|
||||||
|
v = repr(v)
|
||||||
|
yield k, v
|
||||||
|
return json.dumps(
|
||||||
|
dict(prepare_dict(d)),
|
||||||
|
indent=4, sort_keys=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def humanize_bytes(n, precision=2):
|
def humanize_bytes(n, precision=2):
|
||||||
# Author: Doug Latornell
|
# Author: Doug Latornell
|
||||||
# Licence: MIT
|
# Licence: MIT
|
||||||
|
@ -1,3 +0,0 @@
|
|||||||
[pytest]
|
|
||||||
addopts = --tb=native
|
|
||||||
norecursedirs = tests/fixtures
|
|
@ -5,3 +5,5 @@ pytest-cov
|
|||||||
pytest-httpbin>=0.0.6
|
pytest-httpbin>=0.0.6
|
||||||
docutils
|
docutils
|
||||||
wheel
|
wheel
|
||||||
|
pycodestyle
|
||||||
|
twine
|
||||||
|
17
setup.cfg
17
setup.cfg
@ -1,2 +1,19 @@
|
|||||||
[wheel]
|
[wheel]
|
||||||
universal = 1
|
universal = 1
|
||||||
|
|
||||||
|
|
||||||
|
[tool:pytest]
|
||||||
|
# <https://docs.pytest.org/en/latest/customize.html>
|
||||||
|
norecursedirs = tests/fixtures
|
||||||
|
|
||||||
|
|
||||||
|
[pycodestyle]
|
||||||
|
# <http://pycodestyle.pycqa.org/en/latest/intro.html#configuration>
|
||||||
|
|
||||||
|
exclude = .git,.idea,__pycache__,build,dist,.tox,.pytest_cache,*.egg-info
|
||||||
|
|
||||||
|
# <http://pycodestyle.pycqa.org/en/latest/intro.html#error-codes>
|
||||||
|
# E241 - multiple spaces after ‘,’
|
||||||
|
# E501 - line too long
|
||||||
|
# W503 - line break before binary operator
|
||||||
|
ignore = E241,E501,W503
|
||||||
|
18
setup.py
18
setup.py
@ -35,10 +35,11 @@ tests_require = [
|
|||||||
|
|
||||||
|
|
||||||
install_requires = [
|
install_requires = [
|
||||||
'requests>=2.3.0',
|
'requests>=2.21.0',
|
||||||
'Pygments>=1.5'
|
'Pygments>=2.3.1'
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
# Conditional dependencies:
|
# Conditional dependencies:
|
||||||
|
|
||||||
# sdist
|
# sdist
|
||||||
@ -56,10 +57,8 @@ if 'bdist_wheel' not in sys.argv:
|
|||||||
|
|
||||||
# bdist_wheel
|
# bdist_wheel
|
||||||
extras_require = {
|
extras_require = {
|
||||||
# http://wheel.readthedocs.org/en/latest/#defining-conditional-dependencies
|
# http://wheel.readthedocs.io/en/latest/#defining-conditional-dependencies
|
||||||
':python_version == "2.6"'
|
'python_version == "3.0" or python_version == "3.1"': ['argparse>=1.2.1'],
|
||||||
' or python_version == "3.0"'
|
|
||||||
' or python_version == "3.1" ': ['argparse>=1.2.1'],
|
|
||||||
':sys_platform == "win32"': ['colorama>=0.2.4'],
|
':sys_platform == "win32"': ['colorama>=0.2.4'],
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,13 +67,14 @@ def long_description():
|
|||||||
with codecs.open('README.rst', encoding='utf8') as f:
|
with codecs.open('README.rst', encoding='utf8') as f:
|
||||||
return f.read()
|
return f.read()
|
||||||
|
|
||||||
|
|
||||||
setup(
|
setup(
|
||||||
name='httpie',
|
name='httpie',
|
||||||
version=httpie.__version__,
|
version=httpie.__version__,
|
||||||
description=httpie.__doc__.strip(),
|
description=httpie.__doc__.strip(),
|
||||||
long_description=long_description(),
|
long_description=long_description(),
|
||||||
url='http://httpie.org/',
|
url='http://httpie.org/',
|
||||||
download_url='https://github.com/jkbrzt/httpie',
|
download_url='https://github.com/jakubroztocil/httpie',
|
||||||
author=httpie.__author__,
|
author=httpie.__author__,
|
||||||
author_email='jakub@roztocil.co',
|
author_email='jakub@roztocil.co',
|
||||||
license=httpie.__licence__,
|
license=httpie.__licence__,
|
||||||
@ -91,14 +91,14 @@ setup(
|
|||||||
classifiers=[
|
classifiers=[
|
||||||
'Development Status :: 5 - Production/Stable',
|
'Development Status :: 5 - Production/Stable',
|
||||||
'Programming Language :: Python',
|
'Programming Language :: Python',
|
||||||
'Programming Language :: Python :: 2',
|
|
||||||
'Programming Language :: Python :: 2.6',
|
|
||||||
'Programming Language :: Python :: 2.7',
|
'Programming Language :: Python :: 2.7',
|
||||||
'Programming Language :: Python :: 3',
|
'Programming Language :: Python :: 3',
|
||||||
'Programming Language :: Python :: 3.1',
|
'Programming Language :: Python :: 3.1',
|
||||||
'Programming Language :: Python :: 3.2',
|
'Programming Language :: Python :: 3.2',
|
||||||
'Programming Language :: Python :: 3.3',
|
'Programming Language :: Python :: 3.3',
|
||||||
'Programming Language :: Python :: 3.4',
|
'Programming Language :: Python :: 3.4',
|
||||||
|
'Programming Language :: Python :: 3.5',
|
||||||
|
'Programming Language :: Python :: 3.6',
|
||||||
'Environment :: Console',
|
'Environment :: Console',
|
||||||
'Intended Audience :: Developers',
|
'Intended Audience :: Developers',
|
||||||
'Intended Audience :: System Administrators',
|
'Intended Audience :: System Administrators',
|
||||||
|
@ -5,4 +5,4 @@ HTTPie Test Suite
|
|||||||
Please see `CONTRIBUTING`_.
|
Please see `CONTRIBUTING`_.
|
||||||
|
|
||||||
|
|
||||||
.. _CONTRIBUTING: https://github.com/jkbrzt/httpie/blob/master/CONTRIBUTING.rst
|
.. _CONTRIBUTING: https://github.com/jakubroztocil/httpie/blob/master/CONTRIBUTING.rst
|
||||||
|
24
tests/conftest.py
Normal file
24
tests/conftest.py
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
import pytest
|
||||||
|
from pytest_httpbin import certs
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope='function', autouse=True)
|
||||||
|
def httpbin_add_ca_bundle(monkeypatch):
|
||||||
|
"""
|
||||||
|
Make pytest-httpbin's CA trusted by default.
|
||||||
|
|
||||||
|
(Same as `httpbin_ca_bundle`, just auto-used.).
|
||||||
|
|
||||||
|
"""
|
||||||
|
monkeypatch.setenv('REQUESTS_CA_BUNDLE', certs.where())
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope='function')
|
||||||
|
def httpbin_secure_untrusted(monkeypatch, httpbin_secure):
|
||||||
|
"""
|
||||||
|
Like the `httpbin_secure` fixture, but without the
|
||||||
|
make-CA-trusted-by-default.
|
||||||
|
|
||||||
|
"""
|
||||||
|
monkeypatch.delenv('REQUESTS_CA_BUNDLE')
|
||||||
|
return httpbin_secure
|
@ -12,7 +12,7 @@ def patharg(path):
|
|||||||
return path.replace('\\', '\\\\\\')
|
return path.replace('\\', '\\\\\\')
|
||||||
|
|
||||||
|
|
||||||
FIXTURES_ROOT = path.join(path.abspath(path.dirname(__file__)), 'fixtures')
|
FIXTURES_ROOT = path.join(path.abspath(path.dirname(__file__)))
|
||||||
FILE_PATH = path.join(FIXTURES_ROOT, 'test.txt')
|
FILE_PATH = path.join(FIXTURES_ROOT, 'test.txt')
|
||||||
JSON_FILE_PATH = path.join(FIXTURES_ROOT, 'test.json')
|
JSON_FILE_PATH = path.join(FIXTURES_ROOT, 'test.json')
|
||||||
BIN_FILE_PATH = path.join(FIXTURES_ROOT, 'test.bin')
|
BIN_FILE_PATH = path.join(FIXTURES_ROOT, 'test.bin')
|
@ -1,61 +1,75 @@
|
|||||||
"""HTTP authentication-related tests."""
|
"""HTTP authentication-related tests."""
|
||||||
import requests
|
import mock
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from utils import http, add_auth, HTTP_OK, TestEnvironment
|
from utils import http, add_auth, HTTP_OK, MockEnvironment
|
||||||
import httpie.input
|
import httpie.input
|
||||||
import httpie.cli
|
import httpie.cli
|
||||||
|
|
||||||
|
|
||||||
class TestAuth:
|
def test_basic_auth(httpbin_both):
|
||||||
def test_basic_auth(self, httpbin):
|
r = http('--auth=user:password',
|
||||||
r = http('--auth=user:password',
|
'GET', httpbin_both + '/basic-auth/user/password')
|
||||||
'GET', httpbin.url + '/basic-auth/user/password')
|
assert HTTP_OK in r
|
||||||
assert HTTP_OK in r
|
assert r.json == {'authenticated': True, 'user': 'user'}
|
||||||
assert r.json == {'authenticated': True, 'user': 'user'}
|
|
||||||
|
|
||||||
@pytest.mark.skipif(
|
|
||||||
requests.__version__ == '0.13.6',
|
|
||||||
reason='Redirects with prefetch=False are broken in Requests 0.13.6')
|
|
||||||
def test_digest_auth(self, httpbin):
|
|
||||||
r = http('--auth-type=digest', '--auth=user:password',
|
|
||||||
'GET', httpbin.url + '/digest-auth/auth/user/password')
|
|
||||||
assert HTTP_OK in r
|
|
||||||
assert r.json == {'authenticated': True, 'user': 'user'}
|
|
||||||
|
|
||||||
def test_password_prompt(self, httpbin):
|
@pytest.mark.parametrize('argument_name', ['--auth-type', '-A'])
|
||||||
httpie.input.AuthCredentials._getpass = lambda self, prompt: 'password'
|
def test_digest_auth(httpbin_both, argument_name):
|
||||||
r = http('--auth', 'user',
|
r = http(argument_name + '=digest', '--auth=user:password',
|
||||||
'GET', httpbin.url + '/basic-auth/user/password')
|
'GET', httpbin_both.url + '/digest-auth/auth/user/password')
|
||||||
assert HTTP_OK in r
|
assert HTTP_OK in r
|
||||||
assert r.json == {'authenticated': True, 'user': 'user'}
|
assert r.json == {'authenticated': True, 'user': 'user'}
|
||||||
|
|
||||||
def test_credentials_in_url(self, httpbin):
|
|
||||||
url = add_auth(httpbin.url + '/basic-auth/user/password',
|
|
||||||
auth='user:password')
|
|
||||||
r = http('GET', url)
|
|
||||||
assert HTTP_OK in r
|
|
||||||
assert r.json == {'authenticated': True, 'user': 'user'}
|
|
||||||
|
|
||||||
def test_credentials_in_url_auth_flag_has_priority(self, httpbin):
|
@mock.patch('httpie.input.AuthCredentials._getpass',
|
||||||
"""When credentials are passed in URL and via -a at the same time,
|
new=lambda self, prompt: 'password')
|
||||||
then the ones from -a are used."""
|
def test_password_prompt(httpbin):
|
||||||
url = add_auth(httpbin.url + '/basic-auth/user/password',
|
r = http('--auth', 'user',
|
||||||
auth='user:wrong')
|
'GET', httpbin.url + '/basic-auth/user/password')
|
||||||
r = http('--auth=user:password', 'GET', url)
|
assert HTTP_OK in r
|
||||||
assert HTTP_OK in r
|
assert r.json == {'authenticated': True, 'user': 'user'}
|
||||||
assert r.json == {'authenticated': True, 'user': 'user'}
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('url', [
|
|
||||||
'username@example.org',
|
|
||||||
'username:@example.org',
|
|
||||||
])
|
|
||||||
def test_only_username_in_url(self, url):
|
|
||||||
"""
|
|
||||||
https://github.com/jkbrzt/httpie/issues/242
|
|
||||||
|
|
||||||
"""
|
def test_credentials_in_url(httpbin_both):
|
||||||
args = httpie.cli.parser.parse_args(args=[url], env=TestEnvironment())
|
url = add_auth(httpbin_both.url + '/basic-auth/user/password',
|
||||||
assert args.auth
|
auth='user:password')
|
||||||
assert args.auth.key == 'username'
|
r = http('GET', url)
|
||||||
assert args.auth.value == ''
|
assert HTTP_OK in r
|
||||||
|
assert r.json == {'authenticated': True, 'user': 'user'}
|
||||||
|
|
||||||
|
|
||||||
|
def test_credentials_in_url_auth_flag_has_priority(httpbin_both):
|
||||||
|
"""When credentials are passed in URL and via -a at the same time,
|
||||||
|
then the ones from -a are used."""
|
||||||
|
url = add_auth(httpbin_both.url + '/basic-auth/user/password',
|
||||||
|
auth='user:wrong')
|
||||||
|
r = http('--auth=user:password', 'GET', url)
|
||||||
|
assert HTTP_OK in r
|
||||||
|
assert r.json == {'authenticated': True, 'user': 'user'}
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('url', [
|
||||||
|
'username@example.org',
|
||||||
|
'username:@example.org',
|
||||||
|
])
|
||||||
|
def test_only_username_in_url(url):
|
||||||
|
"""
|
||||||
|
https://github.com/jakubroztocil/httpie/issues/242
|
||||||
|
|
||||||
|
"""
|
||||||
|
args = httpie.cli.parser.parse_args(args=[url], env=MockEnvironment())
|
||||||
|
assert args.auth
|
||||||
|
assert args.auth.username == 'username'
|
||||||
|
assert args.auth.password == ''
|
||||||
|
|
||||||
|
|
||||||
|
def test_missing_auth(httpbin):
|
||||||
|
r = http(
|
||||||
|
'--auth-type=basic',
|
||||||
|
'GET',
|
||||||
|
httpbin + '/basic-auth/user/password',
|
||||||
|
error_exit_ok=True
|
||||||
|
)
|
||||||
|
assert HTTP_OK not in r
|
||||||
|
assert '--auth required' in r.stderr
|
||||||
|
133
tests/test_auth_plugins.py
Normal file
133
tests/test_auth_plugins.py
Normal file
@ -0,0 +1,133 @@
|
|||||||
|
from mock import mock
|
||||||
|
|
||||||
|
from httpie.input import SEP_CREDENTIALS
|
||||||
|
from httpie.plugins import AuthPlugin, plugin_manager
|
||||||
|
from utils import http, HTTP_OK
|
||||||
|
|
||||||
|
# TODO: run all these tests in session mode as well
|
||||||
|
|
||||||
|
USERNAME = 'user'
|
||||||
|
PASSWORD = 'password'
|
||||||
|
# Basic auth encoded `USERNAME` and `PASSWORD`
|
||||||
|
# noinspection SpellCheckingInspection
|
||||||
|
BASIC_AUTH_HEADER_VALUE = 'Basic dXNlcjpwYXNzd29yZA=='
|
||||||
|
BASIC_AUTH_URL = '/basic-auth/{0}/{1}'.format(USERNAME, PASSWORD)
|
||||||
|
AUTH_OK = {'authenticated': True, 'user': USERNAME}
|
||||||
|
|
||||||
|
|
||||||
|
def basic_auth(header=BASIC_AUTH_HEADER_VALUE):
|
||||||
|
|
||||||
|
def inner(r):
|
||||||
|
r.headers['Authorization'] = header
|
||||||
|
return r
|
||||||
|
|
||||||
|
return inner
|
||||||
|
|
||||||
|
|
||||||
|
def test_auth_plugin_parse_auth_false(httpbin):
|
||||||
|
|
||||||
|
class Plugin(AuthPlugin):
|
||||||
|
auth_type = 'test-parse-false'
|
||||||
|
auth_parse = False
|
||||||
|
|
||||||
|
def get_auth(self, username=None, password=None):
|
||||||
|
assert username is None
|
||||||
|
assert password is None
|
||||||
|
assert self.raw_auth == BASIC_AUTH_HEADER_VALUE
|
||||||
|
return basic_auth(self.raw_auth)
|
||||||
|
|
||||||
|
plugin_manager.register(Plugin)
|
||||||
|
try:
|
||||||
|
r = http(
|
||||||
|
httpbin + BASIC_AUTH_URL,
|
||||||
|
'--auth-type',
|
||||||
|
Plugin.auth_type,
|
||||||
|
'--auth',
|
||||||
|
BASIC_AUTH_HEADER_VALUE,
|
||||||
|
)
|
||||||
|
assert HTTP_OK in r
|
||||||
|
assert r.json == AUTH_OK
|
||||||
|
finally:
|
||||||
|
plugin_manager.unregister(Plugin)
|
||||||
|
|
||||||
|
|
||||||
|
def test_auth_plugin_require_auth_false(httpbin):
|
||||||
|
|
||||||
|
class Plugin(AuthPlugin):
|
||||||
|
auth_type = 'test-require-false'
|
||||||
|
auth_require = False
|
||||||
|
|
||||||
|
def get_auth(self, username=None, password=None):
|
||||||
|
assert self.raw_auth is None
|
||||||
|
assert username is None
|
||||||
|
assert password is None
|
||||||
|
return basic_auth()
|
||||||
|
|
||||||
|
plugin_manager.register(Plugin)
|
||||||
|
try:
|
||||||
|
r = http(
|
||||||
|
httpbin + BASIC_AUTH_URL,
|
||||||
|
'--auth-type',
|
||||||
|
Plugin.auth_type,
|
||||||
|
)
|
||||||
|
assert HTTP_OK in r
|
||||||
|
assert r.json == AUTH_OK
|
||||||
|
finally:
|
||||||
|
plugin_manager.unregister(Plugin)
|
||||||
|
|
||||||
|
|
||||||
|
def test_auth_plugin_require_auth_false_and_auth_provided(httpbin):
|
||||||
|
|
||||||
|
class Plugin(AuthPlugin):
|
||||||
|
auth_type = 'test-require-false-yet-provided'
|
||||||
|
auth_require = False
|
||||||
|
|
||||||
|
def get_auth(self, username=None, password=None):
|
||||||
|
assert self.raw_auth == USERNAME + SEP_CREDENTIALS + PASSWORD
|
||||||
|
assert username == USERNAME
|
||||||
|
assert password == PASSWORD
|
||||||
|
return basic_auth()
|
||||||
|
|
||||||
|
plugin_manager.register(Plugin)
|
||||||
|
try:
|
||||||
|
r = http(
|
||||||
|
httpbin + BASIC_AUTH_URL,
|
||||||
|
'--auth-type',
|
||||||
|
Plugin.auth_type,
|
||||||
|
'--auth',
|
||||||
|
USERNAME + SEP_CREDENTIALS + PASSWORD,
|
||||||
|
)
|
||||||
|
assert HTTP_OK in r
|
||||||
|
assert r.json == AUTH_OK
|
||||||
|
finally:
|
||||||
|
plugin_manager.unregister(Plugin)
|
||||||
|
|
||||||
|
|
||||||
|
@mock.patch('httpie.input.AuthCredentials._getpass',
|
||||||
|
new=lambda self, prompt: 'UNEXPECTED_PROMPT_RESPONSE')
|
||||||
|
def test_auth_plugin_prompt_password_false(httpbin):
|
||||||
|
|
||||||
|
class Plugin(AuthPlugin):
|
||||||
|
auth_type = 'test-prompt-false'
|
||||||
|
prompt_password = False
|
||||||
|
|
||||||
|
def get_auth(self, username=None, password=None):
|
||||||
|
assert self.raw_auth == USERNAME
|
||||||
|
assert username == USERNAME
|
||||||
|
assert password is None
|
||||||
|
return basic_auth()
|
||||||
|
|
||||||
|
plugin_manager.register(Plugin)
|
||||||
|
|
||||||
|
try:
|
||||||
|
r = http(
|
||||||
|
httpbin + BASIC_AUTH_URL,
|
||||||
|
'--auth-type',
|
||||||
|
Plugin.auth_type,
|
||||||
|
'--auth',
|
||||||
|
USERNAME,
|
||||||
|
)
|
||||||
|
assert HTTP_OK in r
|
||||||
|
assert r.json == AUTH_OK
|
||||||
|
finally:
|
||||||
|
plugin_manager.unregister(Plugin)
|
@ -1,14 +1,16 @@
|
|||||||
"""Tests for dealing with binary request and response data."""
|
"""Tests for dealing with binary request and response data."""
|
||||||
from httpie.compat import urlopen
|
import requests
|
||||||
from httpie.output.streams import BINARY_SUPPRESSED_NOTICE
|
|
||||||
from utils import TestEnvironment, http
|
|
||||||
from fixtures import BIN_FILE_PATH, BIN_FILE_CONTENT, BIN_FILE_PATH_ARG
|
from fixtures import BIN_FILE_PATH, BIN_FILE_CONTENT, BIN_FILE_PATH_ARG
|
||||||
|
from httpie.output.streams import BINARY_SUPPRESSED_NOTICE
|
||||||
|
from utils import MockEnvironment, http
|
||||||
|
|
||||||
|
|
||||||
class TestBinaryRequestData:
|
class TestBinaryRequestData:
|
||||||
|
|
||||||
def test_binary_stdin(self, httpbin):
|
def test_binary_stdin(self, httpbin):
|
||||||
with open(BIN_FILE_PATH, 'rb') as stdin:
|
with open(BIN_FILE_PATH, 'rb') as stdin:
|
||||||
env = TestEnvironment(
|
env = MockEnvironment(
|
||||||
stdin=stdin,
|
stdin=stdin,
|
||||||
stdin_isatty=False,
|
stdin_isatty=False,
|
||||||
stdout_isatty=False
|
stdout_isatty=False
|
||||||
@ -17,38 +19,32 @@ class TestBinaryRequestData:
|
|||||||
assert r == BIN_FILE_CONTENT
|
assert r == BIN_FILE_CONTENT
|
||||||
|
|
||||||
def test_binary_file_path(self, httpbin):
|
def test_binary_file_path(self, httpbin):
|
||||||
env = TestEnvironment(stdin_isatty=True, stdout_isatty=False)
|
env = MockEnvironment(stdin_isatty=True, stdout_isatty=False)
|
||||||
r = http('--print=B', 'POST', httpbin.url + '/post',
|
r = http('--print=B', 'POST', httpbin.url + '/post',
|
||||||
'@' + BIN_FILE_PATH_ARG, env=env, )
|
'@' + BIN_FILE_PATH_ARG, env=env, )
|
||||||
assert r == BIN_FILE_CONTENT
|
assert r == BIN_FILE_CONTENT
|
||||||
|
|
||||||
def test_binary_file_form(self, httpbin):
|
def test_binary_file_form(self, httpbin):
|
||||||
env = TestEnvironment(stdin_isatty=True, stdout_isatty=False)
|
env = MockEnvironment(stdin_isatty=True, stdout_isatty=False)
|
||||||
r = http('--print=B', '--form', 'POST', httpbin.url + '/post',
|
r = http('--print=B', '--form', 'POST', httpbin.url + '/post',
|
||||||
'test@' + BIN_FILE_PATH_ARG, env=env)
|
'test@' + BIN_FILE_PATH_ARG, env=env)
|
||||||
assert bytes(BIN_FILE_CONTENT) in bytes(r)
|
assert bytes(BIN_FILE_CONTENT) in bytes(r)
|
||||||
|
|
||||||
|
|
||||||
class TestBinaryResponseData:
|
class TestBinaryResponseData:
|
||||||
url = 'http://www.google.com/favicon.ico'
|
|
||||||
|
|
||||||
@property
|
def test_binary_suppresses_when_terminal(self, httpbin):
|
||||||
def bindata(self):
|
r = http('GET', httpbin + '/bytes/1024')
|
||||||
if not hasattr(self, '_bindata'):
|
|
||||||
self._bindata = urlopen(self.url).read()
|
|
||||||
return self._bindata
|
|
||||||
|
|
||||||
def test_binary_suppresses_when_terminal(self):
|
|
||||||
r = http('GET', self.url)
|
|
||||||
assert BINARY_SUPPRESSED_NOTICE.decode() in r
|
assert BINARY_SUPPRESSED_NOTICE.decode() in r
|
||||||
|
|
||||||
def test_binary_suppresses_when_not_terminal_but_pretty(self):
|
def test_binary_suppresses_when_not_terminal_but_pretty(self, httpbin):
|
||||||
env = TestEnvironment(stdin_isatty=True, stdout_isatty=False)
|
env = MockEnvironment(stdin_isatty=True, stdout_isatty=False)
|
||||||
r = http('--pretty=all', 'GET', self.url,
|
r = http('--pretty=all', 'GET', httpbin + '/bytes/1024', env=env)
|
||||||
env=env)
|
|
||||||
assert BINARY_SUPPRESSED_NOTICE.decode() in r
|
assert BINARY_SUPPRESSED_NOTICE.decode() in r
|
||||||
|
|
||||||
def test_binary_included_and_correct_when_suitable(self):
|
def test_binary_included_and_correct_when_suitable(self, httpbin):
|
||||||
env = TestEnvironment(stdin_isatty=True, stdout_isatty=False)
|
env = MockEnvironment(stdin_isatty=True, stdout_isatty=False)
|
||||||
r = http('GET', self.url, env=env)
|
url = httpbin + '/bytes/1024?seed=1'
|
||||||
assert r == self.bindata
|
r = http('GET', url, env=env)
|
||||||
|
expected = requests.get(url).content
|
||||||
|
assert r == expected
|
||||||
|
@ -10,7 +10,7 @@ from httpie import input
|
|||||||
from httpie.input import KeyValue, KeyValueArgType, DataDict
|
from httpie.input import KeyValue, KeyValueArgType, DataDict
|
||||||
from httpie import ExitStatus
|
from httpie import ExitStatus
|
||||||
from httpie.cli import parser
|
from httpie.cli import parser
|
||||||
from utils import TestEnvironment, http, HTTP_OK
|
from utils import MockEnvironment, http, HTTP_OK
|
||||||
from fixtures import (
|
from fixtures import (
|
||||||
FILE_PATH_ARG, JSON_FILE_PATH_ARG,
|
FILE_PATH_ARG, JSON_FILE_PATH_ARG,
|
||||||
JSON_FILE_CONTENT, FILE_CONTENT, FILE_PATH
|
JSON_FILE_CONTENT, FILE_CONTENT, FILE_PATH
|
||||||
@ -49,9 +49,9 @@ class TestItemParsing:
|
|||||||
assert 'bar@baz' in items.files
|
assert 'bar@baz' in items.files
|
||||||
|
|
||||||
@pytest.mark.parametrize(('string', 'key', 'sep', 'value'), [
|
@pytest.mark.parametrize(('string', 'key', 'sep', 'value'), [
|
||||||
('path=c:\windows', 'path', '=', 'c:\windows'),
|
('path=c:\\windows', 'path', '=', 'c:\\windows'),
|
||||||
('path=c:\windows\\', 'path', '=', 'c:\windows\\'),
|
('path=c:\\windows\\', 'path', '=', 'c:\\windows\\'),
|
||||||
('path\==c:\windows', 'path=', '=', 'c:\windows'),
|
('path\\==c:\\windows', 'path=', '=', 'c:\\windows'),
|
||||||
])
|
])
|
||||||
def test_backslash_before_non_special_character_does_not_escape(
|
def test_backslash_before_non_special_character_does_not_escape(
|
||||||
self, string, key, sep, value):
|
self, string, key, sep, value):
|
||||||
@ -68,10 +68,11 @@ class TestItemParsing:
|
|||||||
def test_valid_items(self):
|
def test_valid_items(self):
|
||||||
items = input.parse_items([
|
items = input.parse_items([
|
||||||
self.key_value('string=value'),
|
self.key_value('string=value'),
|
||||||
self.key_value('header:value'),
|
self.key_value('Header:value'),
|
||||||
|
self.key_value('Unset-Header:'),
|
||||||
|
self.key_value('Empty-Header;'),
|
||||||
self.key_value('list:=["a", 1, {}, false]'),
|
self.key_value('list:=["a", 1, {}, false]'),
|
||||||
self.key_value('obj:={"a": "b"}'),
|
self.key_value('obj:={"a": "b"}'),
|
||||||
self.key_value('eh:'),
|
|
||||||
self.key_value('ed='),
|
self.key_value('ed='),
|
||||||
self.key_value('bool:=true'),
|
self.key_value('bool:=true'),
|
||||||
self.key_value('file@' + FILE_PATH_ARG),
|
self.key_value('file@' + FILE_PATH_ARG),
|
||||||
@ -83,7 +84,11 @@ class TestItemParsing:
|
|||||||
# Parsed headers
|
# Parsed headers
|
||||||
# `requests.structures.CaseInsensitiveDict` => `dict`
|
# `requests.structures.CaseInsensitiveDict` => `dict`
|
||||||
headers = dict(items.headers._store.values())
|
headers = dict(items.headers._store.values())
|
||||||
assert headers == {'header': 'value', 'eh': ''}
|
assert headers == {
|
||||||
|
'Header': 'value',
|
||||||
|
'Unset-Header': None,
|
||||||
|
'Empty-Header': ''
|
||||||
|
}
|
||||||
|
|
||||||
# Parsed data
|
# Parsed data
|
||||||
raw_json_embed = items.data.pop('raw-json-embed')
|
raw_json_embed = items.data.pop('raw-json-embed')
|
||||||
@ -103,8 +108,8 @@ class TestItemParsing:
|
|||||||
|
|
||||||
# Parsed file fields
|
# Parsed file fields
|
||||||
assert 'file' in items.files
|
assert 'file' in items.files
|
||||||
assert (items.files['file'][1].read().strip().decode('utf8')
|
assert (items.files['file'][1].read().strip().
|
||||||
== FILE_CONTENT)
|
decode('utf8') == FILE_CONTENT)
|
||||||
|
|
||||||
def test_multiple_file_fields_with_same_field_name(self):
|
def test_multiple_file_fields_with_same_field_name(self):
|
||||||
items = input.parse_items([
|
items = input.parse_items([
|
||||||
@ -154,46 +159,46 @@ class TestQuerystring:
|
|||||||
assert '"url": "%s"' % url in r
|
assert '"url": "%s"' % url in r
|
||||||
|
|
||||||
|
|
||||||
class TestURLshorthand:
|
class TestLocalhostShorthand:
|
||||||
def test_expand_localhost_shorthand(self):
|
def test_expand_localhost_shorthand(self):
|
||||||
args = parser.parse_args(args=[':'], env=TestEnvironment())
|
args = parser.parse_args(args=[':'], env=MockEnvironment())
|
||||||
assert args.url == 'http://localhost'
|
assert args.url == 'http://localhost'
|
||||||
|
|
||||||
def test_expand_localhost_shorthand_with_slash(self):
|
def test_expand_localhost_shorthand_with_slash(self):
|
||||||
args = parser.parse_args(args=[':/'], env=TestEnvironment())
|
args = parser.parse_args(args=[':/'], env=MockEnvironment())
|
||||||
assert args.url == 'http://localhost/'
|
assert args.url == 'http://localhost/'
|
||||||
|
|
||||||
def test_expand_localhost_shorthand_with_port(self):
|
def test_expand_localhost_shorthand_with_port(self):
|
||||||
args = parser.parse_args(args=[':3000'], env=TestEnvironment())
|
args = parser.parse_args(args=[':3000'], env=MockEnvironment())
|
||||||
assert args.url == 'http://localhost:3000'
|
assert args.url == 'http://localhost:3000'
|
||||||
|
|
||||||
def test_expand_localhost_shorthand_with_path(self):
|
def test_expand_localhost_shorthand_with_path(self):
|
||||||
args = parser.parse_args(args=[':/path'], env=TestEnvironment())
|
args = parser.parse_args(args=[':/path'], env=MockEnvironment())
|
||||||
assert args.url == 'http://localhost/path'
|
assert args.url == 'http://localhost/path'
|
||||||
|
|
||||||
def test_expand_localhost_shorthand_with_port_and_slash(self):
|
def test_expand_localhost_shorthand_with_port_and_slash(self):
|
||||||
args = parser.parse_args(args=[':3000/'], env=TestEnvironment())
|
args = parser.parse_args(args=[':3000/'], env=MockEnvironment())
|
||||||
assert args.url == 'http://localhost:3000/'
|
assert args.url == 'http://localhost:3000/'
|
||||||
|
|
||||||
def test_expand_localhost_shorthand_with_port_and_path(self):
|
def test_expand_localhost_shorthand_with_port_and_path(self):
|
||||||
args = parser.parse_args(args=[':3000/path'], env=TestEnvironment())
|
args = parser.parse_args(args=[':3000/path'], env=MockEnvironment())
|
||||||
assert args.url == 'http://localhost:3000/path'
|
assert args.url == 'http://localhost:3000/path'
|
||||||
|
|
||||||
def test_dont_expand_shorthand_ipv6_as_shorthand(self):
|
def test_dont_expand_shorthand_ipv6_as_shorthand(self):
|
||||||
args = parser.parse_args(args=['::1'], env=TestEnvironment())
|
args = parser.parse_args(args=['::1'], env=MockEnvironment())
|
||||||
assert args.url == 'http://::1'
|
assert args.url == 'http://::1'
|
||||||
|
|
||||||
def test_dont_expand_longer_ipv6_as_shorthand(self):
|
def test_dont_expand_longer_ipv6_as_shorthand(self):
|
||||||
args = parser.parse_args(
|
args = parser.parse_args(
|
||||||
args=['::ffff:c000:0280'],
|
args=['::ffff:c000:0280'],
|
||||||
env=TestEnvironment()
|
env=MockEnvironment()
|
||||||
)
|
)
|
||||||
assert args.url == 'http://::ffff:c000:0280'
|
assert args.url == 'http://::ffff:c000:0280'
|
||||||
|
|
||||||
def test_dont_expand_full_ipv6_as_shorthand(self):
|
def test_dont_expand_full_ipv6_as_shorthand(self):
|
||||||
args = parser.parse_args(
|
args = parser.parse_args(
|
||||||
args=['0000:0000:0000:0000:0000:0000:0000:0001'],
|
args=['0000:0000:0000:0000:0000:0000:0000:0001'],
|
||||||
env=TestEnvironment()
|
env=MockEnvironment()
|
||||||
)
|
)
|
||||||
assert args.url == 'http://0000:0000:0000:0000:0000:0000:0000:0001'
|
assert args.url == 'http://0000:0000:0000:0000:0000:0000:0000:0001'
|
||||||
|
|
||||||
@ -201,7 +206,7 @@ class TestURLshorthand:
|
|||||||
class TestArgumentParser:
|
class TestArgumentParser:
|
||||||
|
|
||||||
def setup_method(self, method):
|
def setup_method(self, method):
|
||||||
self.parser = input.Parser()
|
self.parser = input.HTTPieArgumentParser()
|
||||||
|
|
||||||
def test_guess_when_method_set_and_valid(self):
|
def test_guess_when_method_set_and_valid(self):
|
||||||
self.parser.args = argparse.Namespace()
|
self.parser.args = argparse.Namespace()
|
||||||
@ -210,7 +215,7 @@ class TestArgumentParser:
|
|||||||
self.parser.args.items = []
|
self.parser.args.items = []
|
||||||
self.parser.args.ignore_stdin = False
|
self.parser.args.ignore_stdin = False
|
||||||
|
|
||||||
self.parser.env = TestEnvironment()
|
self.parser.env = MockEnvironment()
|
||||||
|
|
||||||
self.parser._guess_method()
|
self.parser._guess_method()
|
||||||
|
|
||||||
@ -224,7 +229,7 @@ class TestArgumentParser:
|
|||||||
self.parser.args.url = 'http://example.com/'
|
self.parser.args.url = 'http://example.com/'
|
||||||
self.parser.args.items = []
|
self.parser.args.items = []
|
||||||
self.parser.args.ignore_stdin = False
|
self.parser.args.ignore_stdin = False
|
||||||
self.parser.env = TestEnvironment()
|
self.parser.env = MockEnvironment()
|
||||||
|
|
||||||
self.parser._guess_method()
|
self.parser._guess_method()
|
||||||
|
|
||||||
@ -238,7 +243,7 @@ class TestArgumentParser:
|
|||||||
self.parser.args.url = 'data=field'
|
self.parser.args.url = 'data=field'
|
||||||
self.parser.args.items = []
|
self.parser.args.items = []
|
||||||
self.parser.args.ignore_stdin = False
|
self.parser.args.ignore_stdin = False
|
||||||
self.parser.env = TestEnvironment()
|
self.parser.env = MockEnvironment()
|
||||||
self.parser._guess_method()
|
self.parser._guess_method()
|
||||||
|
|
||||||
assert self.parser.args.method == 'POST'
|
assert self.parser.args.method == 'POST'
|
||||||
@ -257,7 +262,7 @@ class TestArgumentParser:
|
|||||||
self.parser.args.items = []
|
self.parser.args.items = []
|
||||||
self.parser.args.ignore_stdin = False
|
self.parser.args.ignore_stdin = False
|
||||||
|
|
||||||
self.parser.env = TestEnvironment()
|
self.parser.env = MockEnvironment()
|
||||||
|
|
||||||
self.parser._guess_method()
|
self.parser._guess_method()
|
||||||
|
|
||||||
@ -280,7 +285,7 @@ class TestArgumentParser:
|
|||||||
]
|
]
|
||||||
self.parser.args.ignore_stdin = False
|
self.parser.args.ignore_stdin = False
|
||||||
|
|
||||||
self.parser.env = TestEnvironment()
|
self.parser.env = MockEnvironment()
|
||||||
|
|
||||||
self.parser._guess_method()
|
self.parser._guess_method()
|
||||||
|
|
||||||
@ -309,7 +314,7 @@ class TestIgnoreStdin:
|
|||||||
|
|
||||||
def test_ignore_stdin(self, httpbin):
|
def test_ignore_stdin(self, httpbin):
|
||||||
with open(FILE_PATH) as f:
|
with open(FILE_PATH) as f:
|
||||||
env = TestEnvironment(stdin=f, stdin_isatty=False)
|
env = MockEnvironment(stdin=f, stdin_isatty=False)
|
||||||
r = http('--ignore-stdin', '--verbose', httpbin.url + '/get',
|
r = http('--ignore-stdin', '--verbose', httpbin.url + '/get',
|
||||||
env=env)
|
env=env)
|
||||||
assert HTTP_OK in r
|
assert HTTP_OK in r
|
||||||
@ -325,8 +330,18 @@ class TestIgnoreStdin:
|
|||||||
|
|
||||||
class TestSchemes:
|
class TestSchemes:
|
||||||
|
|
||||||
def test_custom_scheme(self):
|
def test_invalid_custom_scheme(self):
|
||||||
# InvalidSchema is expected because HTTPie
|
# InvalidSchema is expected because HTTPie
|
||||||
# shouldn't touch a formally valid scheme.
|
# shouldn't touch a formally valid scheme.
|
||||||
with pytest.raises(InvalidSchema):
|
with pytest.raises(InvalidSchema):
|
||||||
http('foo+bar-BAZ.123://bah')
|
http('foo+bar-BAZ.123://bah')
|
||||||
|
|
||||||
|
def test_invalid_scheme_via_via_default_scheme(self):
|
||||||
|
# InvalidSchema is expected because HTTPie
|
||||||
|
# shouldn't touch a formally valid scheme.
|
||||||
|
with pytest.raises(InvalidSchema):
|
||||||
|
http('bah', '--default=scheme=foo+bar-BAZ.123')
|
||||||
|
|
||||||
|
def test_default_scheme(self, httpbin_secure):
|
||||||
|
url = '{0}:{1}'.format(httpbin_secure.host, httpbin_secure.port)
|
||||||
|
assert HTTP_OK in http(url, '--default-scheme=https')
|
||||||
|
40
tests/test_config.py
Normal file
40
tests/test_config.py
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
from httpie import __version__
|
||||||
|
from utils import MockEnvironment, http
|
||||||
|
from httpie.context import Environment
|
||||||
|
|
||||||
|
|
||||||
|
def test_default_options(httpbin):
|
||||||
|
env = MockEnvironment()
|
||||||
|
env.config['default_options'] = ['--form']
|
||||||
|
env.config.save()
|
||||||
|
r = http(httpbin.url + '/post', 'foo=bar', env=env)
|
||||||
|
assert r.json['form'] == {"foo": "bar"}
|
||||||
|
|
||||||
|
|
||||||
|
def test_default_options_overwrite(httpbin):
|
||||||
|
env = MockEnvironment()
|
||||||
|
env.config['default_options'] = ['--form']
|
||||||
|
env.config.save()
|
||||||
|
r = http('--json', httpbin.url + '/post', 'foo=bar', env=env)
|
||||||
|
assert r.json['json'] == {"foo": "bar"}
|
||||||
|
|
||||||
|
|
||||||
|
def test_migrate_implicit_content_type():
|
||||||
|
config = MockEnvironment().config
|
||||||
|
|
||||||
|
config['implicit_content_type'] = 'json'
|
||||||
|
config.save()
|
||||||
|
config.load()
|
||||||
|
assert 'implicit_content_type' not in config
|
||||||
|
assert not config['default_options']
|
||||||
|
|
||||||
|
config['implicit_content_type'] = 'form'
|
||||||
|
config.save()
|
||||||
|
config.load()
|
||||||
|
assert 'implicit_content_type' not in config
|
||||||
|
assert config['default_options'] == ['--form']
|
||||||
|
|
||||||
|
|
||||||
|
def test_current_version():
|
||||||
|
version = Environment().config['__meta__']['httpie']
|
||||||
|
assert version == __version__
|
@ -2,10 +2,26 @@
|
|||||||
Tests for the provided defaults regarding HTTP method, and --json vs. --form.
|
Tests for the provided defaults regarding HTTP method, and --json vs. --form.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
from utils import TestEnvironment, http, HTTP_OK, no_content_type
|
from httpie.client import JSON_ACCEPT
|
||||||
|
from utils import MockEnvironment, http, HTTP_OK
|
||||||
from fixtures import FILE_PATH
|
from fixtures import FILE_PATH
|
||||||
|
|
||||||
|
|
||||||
|
def test_default_headers_case_insensitive(httpbin):
|
||||||
|
"""
|
||||||
|
<https://github.com/jakubroztocil/httpie/issues/644>
|
||||||
|
"""
|
||||||
|
r = http(
|
||||||
|
'--debug',
|
||||||
|
'--print=H',
|
||||||
|
httpbin.url + '/post',
|
||||||
|
'CONTENT-TYPE:application/json-patch+json',
|
||||||
|
'a=b',
|
||||||
|
)
|
||||||
|
assert 'CONTENT-TYPE: application/json-patch+json' in r
|
||||||
|
assert 'Content-Type' not in r
|
||||||
|
|
||||||
|
|
||||||
class TestImplicitHTTPMethod:
|
class TestImplicitHTTPMethod:
|
||||||
def test_implicit_GET(self, httpbin):
|
def test_implicit_GET(self, httpbin):
|
||||||
r = http(httpbin.url + '/get')
|
r = http(httpbin.url + '/get')
|
||||||
@ -28,7 +44,7 @@ class TestImplicitHTTPMethod:
|
|||||||
|
|
||||||
def test_implicit_POST_stdin(self, httpbin):
|
def test_implicit_POST_stdin(self, httpbin):
|
||||||
with open(FILE_PATH) as f:
|
with open(FILE_PATH) as f:
|
||||||
env = TestEnvironment(stdin_isatty=False, stdin=f)
|
env = MockEnvironment(stdin_isatty=False, stdin=f)
|
||||||
r = http('--form', httpbin.url + '/post', env=env)
|
r = http('--form', httpbin.url + '/post', env=env)
|
||||||
assert HTTP_OK in r
|
assert HTTP_OK in r
|
||||||
|
|
||||||
@ -42,11 +58,11 @@ class TestAutoContentTypeAndAcceptHeaders:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
def test_GET_no_data_no_auto_headers(self, httpbin):
|
def test_GET_no_data_no_auto_headers(self, httpbin):
|
||||||
# https://github.com/jkbrzt/httpie/issues/62
|
# https://github.com/jakubroztocil/httpie/issues/62
|
||||||
r = http('GET', httpbin.url + '/headers')
|
r = http('GET', httpbin.url + '/headers')
|
||||||
assert HTTP_OK in r
|
assert HTTP_OK in r
|
||||||
assert r.json['headers']['Accept'] == '*/*'
|
assert r.json['headers']['Accept'] == '*/*'
|
||||||
assert no_content_type(r.json['headers'])
|
assert 'Content-Type' not in r.json['headers']
|
||||||
|
|
||||||
def test_POST_no_data_no_auto_headers(self, httpbin):
|
def test_POST_no_data_no_auto_headers(self, httpbin):
|
||||||
# JSON headers shouldn't be automatically set for POST with no data.
|
# JSON headers shouldn't be automatically set for POST with no data.
|
||||||
@ -58,22 +74,22 @@ class TestAutoContentTypeAndAcceptHeaders:
|
|||||||
def test_POST_with_data_auto_JSON_headers(self, httpbin):
|
def test_POST_with_data_auto_JSON_headers(self, httpbin):
|
||||||
r = http('POST', httpbin.url + '/post', 'a=b')
|
r = http('POST', httpbin.url + '/post', 'a=b')
|
||||||
assert HTTP_OK in r
|
assert HTTP_OK in r
|
||||||
assert '"Accept": "application/json"' in r
|
assert r.json['headers']['Accept'] == JSON_ACCEPT
|
||||||
assert '"Content-Type": "application/json' in r
|
assert r.json['headers']['Content-Type'] == 'application/json'
|
||||||
|
|
||||||
def test_GET_with_data_auto_JSON_headers(self, httpbin):
|
def test_GET_with_data_auto_JSON_headers(self, httpbin):
|
||||||
# JSON headers should automatically be set also for GET with data.
|
# JSON headers should automatically be set also for GET with data.
|
||||||
r = http('POST', httpbin.url + '/post', 'a=b')
|
r = http('POST', httpbin.url + '/post', 'a=b')
|
||||||
assert HTTP_OK in r
|
assert HTTP_OK in r
|
||||||
assert '"Accept": "application/json"' in r, r
|
assert r.json['headers']['Accept'] == JSON_ACCEPT
|
||||||
assert '"Content-Type": "application/json' in r
|
assert r.json['headers']['Content-Type'] == 'application/json'
|
||||||
|
|
||||||
def test_POST_explicit_JSON_auto_JSON_accept(self, httpbin):
|
def test_POST_explicit_JSON_auto_JSON_accept(self, httpbin):
|
||||||
r = http('--json', 'POST', httpbin.url + '/post')
|
r = http('--json', 'POST', httpbin.url + '/post')
|
||||||
assert HTTP_OK in r
|
assert HTTP_OK in r
|
||||||
assert r.json['headers']['Accept'] == 'application/json'
|
assert r.json['headers']['Accept'] == JSON_ACCEPT
|
||||||
# Make sure Content-Type gets set even with no data.
|
# Make sure Content-Type gets set even with no data.
|
||||||
# https://github.com/jkbrzt/httpie/issues/137
|
# https://github.com/jakubroztocil/httpie/issues/137
|
||||||
assert 'application/json' in r.json['headers']['Content-Type']
|
assert 'application/json' in r.json['headers']['Content-Type']
|
||||||
|
|
||||||
def test_GET_explicit_JSON_explicit_headers(self, httpbin):
|
def test_GET_explicit_JSON_explicit_headers(self, httpbin):
|
||||||
@ -96,11 +112,11 @@ class TestAutoContentTypeAndAcceptHeaders:
|
|||||||
assert '"Content-Type": "application/xml"' in r
|
assert '"Content-Type": "application/xml"' in r
|
||||||
|
|
||||||
def test_print_only_body_when_stdout_redirected_by_default(self, httpbin):
|
def test_print_only_body_when_stdout_redirected_by_default(self, httpbin):
|
||||||
env = TestEnvironment(stdin_isatty=True, stdout_isatty=False)
|
env = MockEnvironment(stdin_isatty=True, stdout_isatty=False)
|
||||||
r = http('GET', httpbin.url + '/get', env=env)
|
r = http('GET', httpbin.url + '/get', env=env)
|
||||||
assert 'HTTP/' not in r
|
assert 'HTTP/' not in r
|
||||||
|
|
||||||
def test_print_overridable_when_stdout_redirected(self, httpbin):
|
def test_print_overridable_when_stdout_redirected(self, httpbin):
|
||||||
env = TestEnvironment(stdin_isatty=True, stdout_isatty=False)
|
env = MockEnvironment(stdin_isatty=True, stdout_isatty=False)
|
||||||
r = http('--print=h', 'GET', httpbin.url + '/get', env=env)
|
r = http('--print=h', 'GET', httpbin.url + '/get', env=env)
|
||||||
assert HTTP_OK in r
|
assert HTTP_OK in r
|
||||||
|
@ -36,4 +36,4 @@ def test_rst_file_syntax(filename):
|
|||||||
stdout=subprocess.PIPE
|
stdout=subprocess.PIPE
|
||||||
)
|
)
|
||||||
err = p.communicate()[1]
|
err = p.communicate()[1]
|
||||||
assert p.returncode == 0, err
|
assert p.returncode == 0, err.decode('utf8')
|
||||||
|
@ -1,15 +1,17 @@
|
|||||||
import os
|
import os
|
||||||
|
import tempfile
|
||||||
import time
|
import time
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
import mock
|
||||||
from requests.structures import CaseInsensitiveDict
|
from requests.structures import CaseInsensitiveDict
|
||||||
|
|
||||||
from httpie.compat import urlopen
|
from httpie.compat import urlopen
|
||||||
from httpie.downloads import (
|
from httpie.downloads import (
|
||||||
parse_content_range, filename_from_content_disposition, filename_from_url,
|
parse_content_range, filename_from_content_disposition, filename_from_url,
|
||||||
get_unique_filename, ContentRangeError, Download,
|
get_unique_filename, ContentRangeError, Downloader,
|
||||||
)
|
)
|
||||||
from utils import http, TestEnvironment
|
from utils import http, MockEnvironment
|
||||||
|
|
||||||
|
|
||||||
class Response(object):
|
class Response(object):
|
||||||
@ -21,6 +23,7 @@ class Response(object):
|
|||||||
|
|
||||||
|
|
||||||
class TestDownloadUtils:
|
class TestDownloadUtils:
|
||||||
|
|
||||||
def test_Content_Range_parsing(self):
|
def test_Content_Range_parsing(self):
|
||||||
parse = parse_content_range
|
parse = parse_content_range
|
||||||
|
|
||||||
@ -74,7 +77,31 @@ class TestDownloadUtils:
|
|||||||
content_type='x-foo/bar'
|
content_type='x-foo/bar'
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_unique_filename(self):
|
@pytest.mark.parametrize(
|
||||||
|
'orig_name, unique_on_attempt, expected',
|
||||||
|
[
|
||||||
|
# Simple
|
||||||
|
('foo.bar', 0, 'foo.bar'),
|
||||||
|
('foo.bar', 1, 'foo.bar-1'),
|
||||||
|
('foo.bar', 10, 'foo.bar-10'),
|
||||||
|
# Trim
|
||||||
|
('A' * 20, 0, 'A' * 10),
|
||||||
|
('A' * 20, 1, 'A' * 8 + '-1'),
|
||||||
|
('A' * 20, 10, 'A' * 7 + '-10'),
|
||||||
|
# Trim before ext
|
||||||
|
('A' * 20 + '.txt', 0, 'A' * 6 + '.txt'),
|
||||||
|
('A' * 20 + '.txt', 1, 'A' * 4 + '.txt-1'),
|
||||||
|
# Trim at the end
|
||||||
|
('foo.' + 'A' * 20, 0, 'foo.' + 'A' * 6),
|
||||||
|
('foo.' + 'A' * 20, 1, 'foo.' + 'A' * 4 + '-1'),
|
||||||
|
('foo.' + 'A' * 20, 10, 'foo.' + 'A' * 3 + '-10'),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
@mock.patch('httpie.downloads.get_filename_max_length')
|
||||||
|
def test_unique_filename(self, get_filename_max_length,
|
||||||
|
orig_name, unique_on_attempt,
|
||||||
|
expected):
|
||||||
|
|
||||||
def attempts(unique_on_attempt=0):
|
def attempts(unique_on_attempt=0):
|
||||||
# noinspection PyUnresolvedReferences,PyUnusedLocal
|
# noinspection PyUnresolvedReferences,PyUnusedLocal
|
||||||
def exists(filename):
|
def exists(filename):
|
||||||
@ -86,54 +113,70 @@ class TestDownloadUtils:
|
|||||||
exists.attempt = 0
|
exists.attempt = 0
|
||||||
return exists
|
return exists
|
||||||
|
|
||||||
assert 'foo.bar' == get_unique_filename('foo.bar', attempts(0))
|
get_filename_max_length.return_value = 10
|
||||||
assert 'foo.bar-1' == get_unique_filename('foo.bar', attempts(1))
|
|
||||||
assert 'foo.bar-10' == get_unique_filename('foo.bar', attempts(10))
|
actual = get_unique_filename(orig_name, attempts(unique_on_attempt))
|
||||||
|
assert expected == actual
|
||||||
|
|
||||||
|
|
||||||
class TestDownloads:
|
class TestDownloads:
|
||||||
# TODO: more tests
|
# TODO: more tests
|
||||||
|
|
||||||
def test_actual_download(self, httpbin):
|
def test_actual_download(self, httpbin_both, httpbin):
|
||||||
url = httpbin.url + '/robots.txt'
|
robots_txt = '/robots.txt'
|
||||||
body = urlopen(url).read().decode()
|
body = urlopen(httpbin + robots_txt).read().decode()
|
||||||
env = TestEnvironment(stdin_isatty=True, stdout_isatty=False)
|
env = MockEnvironment(stdin_isatty=True, stdout_isatty=False)
|
||||||
r = http('--download', url, env=env)
|
r = http('--download', httpbin_both.url + robots_txt, env=env)
|
||||||
assert 'Downloading' in r.stderr
|
assert 'Downloading' in r.stderr
|
||||||
assert '[K' in r.stderr
|
assert '[K' in r.stderr
|
||||||
assert 'Done' in r.stderr
|
assert 'Done' in r.stderr
|
||||||
assert body == r
|
assert body == r
|
||||||
|
|
||||||
def test_download_with_Content_Length(self, httpbin):
|
def test_download_with_Content_Length(self, httpbin_both):
|
||||||
devnull = open(os.devnull, 'w')
|
with open(os.devnull, 'w') as devnull:
|
||||||
download = Download(output_file=devnull, progress_file=devnull)
|
downloader = Downloader(output_file=devnull, progress_file=devnull)
|
||||||
download.start(Response(
|
downloader.start(Response(
|
||||||
url=httpbin.url + '/',
|
url=httpbin_both.url + '/',
|
||||||
headers={'Content-Length': 10}
|
headers={'Content-Length': 10}
|
||||||
))
|
))
|
||||||
time.sleep(1.1)
|
time.sleep(1.1)
|
||||||
download.chunk_downloaded(b'12345')
|
downloader.chunk_downloaded(b'12345')
|
||||||
time.sleep(1.1)
|
time.sleep(1.1)
|
||||||
download.chunk_downloaded(b'12345')
|
downloader.chunk_downloaded(b'12345')
|
||||||
download.finish()
|
downloader.finish()
|
||||||
assert not download.interrupted
|
assert not downloader.interrupted
|
||||||
|
downloader._progress_reporter.join()
|
||||||
|
|
||||||
def test_download_no_Content_Length(self, httpbin):
|
def test_download_no_Content_Length(self, httpbin_both):
|
||||||
devnull = open(os.devnull, 'w')
|
with open(os.devnull, 'w') as devnull:
|
||||||
download = Download(output_file=devnull, progress_file=devnull)
|
downloader = Downloader(output_file=devnull, progress_file=devnull)
|
||||||
download.start(Response(url=httpbin.url + '/'))
|
downloader.start(Response(url=httpbin_both.url + '/'))
|
||||||
time.sleep(1.1)
|
time.sleep(1.1)
|
||||||
download.chunk_downloaded(b'12345')
|
downloader.chunk_downloaded(b'12345')
|
||||||
download.finish()
|
downloader.finish()
|
||||||
assert not download.interrupted
|
assert not downloader.interrupted
|
||||||
|
downloader._progress_reporter.join()
|
||||||
|
|
||||||
def test_download_interrupted(self, httpbin):
|
def test_download_interrupted(self, httpbin_both):
|
||||||
devnull = open(os.devnull, 'w')
|
with open(os.devnull, 'w') as devnull:
|
||||||
download = Download(output_file=devnull, progress_file=devnull)
|
downloader = Downloader(output_file=devnull, progress_file=devnull)
|
||||||
download.start(Response(
|
downloader.start(Response(
|
||||||
url=httpbin.url + '/',
|
url=httpbin_both.url + '/',
|
||||||
headers={'Content-Length': 5}
|
headers={'Content-Length': 5}
|
||||||
))
|
))
|
||||||
download.chunk_downloaded(b'1234')
|
downloader.chunk_downloaded(b'1234')
|
||||||
download.finish()
|
downloader.finish()
|
||||||
assert download.interrupted
|
assert downloader.interrupted
|
||||||
|
downloader._progress_reporter.join()
|
||||||
|
|
||||||
|
def test_download_with_redirect_original_url_used_for_filename(self, httpbin):
|
||||||
|
# Redirect from `/redirect/1` to `/get`.
|
||||||
|
expected_filename = '1.json'
|
||||||
|
orig_cwd = os.getcwd()
|
||||||
|
os.chdir(tempfile.mkdtemp(prefix='httpie_download_test_'))
|
||||||
|
try:
|
||||||
|
assert os.listdir('.') == []
|
||||||
|
http('--download', httpbin.url + '/redirect/1')
|
||||||
|
assert os.listdir('.') == [expected_filename]
|
||||||
|
finally:
|
||||||
|
os.chdir(orig_cwd)
|
||||||
|
@ -3,6 +3,7 @@ from pytest import raises
|
|||||||
from requests import Request, Timeout
|
from requests import Request, Timeout
|
||||||
from requests.exceptions import ConnectionError
|
from requests.exceptions import ConnectionError
|
||||||
|
|
||||||
|
from httpie import ExitStatus
|
||||||
from httpie.core import main
|
from httpie.core import main
|
||||||
|
|
||||||
error_msg = None
|
error_msg = None
|
||||||
@ -17,8 +18,8 @@ def test_error(get_response):
|
|||||||
exc = ConnectionError('Connection aborted')
|
exc = ConnectionError('Connection aborted')
|
||||||
exc.request = Request(method='GET', url='http://www.google.com')
|
exc.request = Request(method='GET', url='http://www.google.com')
|
||||||
get_response.side_effect = exc
|
get_response.side_effect = exc
|
||||||
ret = main(['--ignore-stdin', 'www.google.com'], error=error)
|
ret = main(['--ignore-stdin', 'www.google.com'], custom_log_error=error)
|
||||||
assert ret == 1
|
assert ret == ExitStatus.ERROR
|
||||||
assert error_msg == (
|
assert error_msg == (
|
||||||
'ConnectionError: '
|
'ConnectionError: '
|
||||||
'Connection aborted while doing GET request to URL: '
|
'Connection aborted while doing GET request to URL: '
|
||||||
@ -31,7 +32,7 @@ def test_error_traceback(get_response):
|
|||||||
exc.request = Request(method='GET', url='http://www.google.com')
|
exc.request = Request(method='GET', url='http://www.google.com')
|
||||||
get_response.side_effect = exc
|
get_response.side_effect = exc
|
||||||
with raises(ConnectionError):
|
with raises(ConnectionError):
|
||||||
ret = main(['--ignore-stdin', '--traceback', 'www.google.com'])
|
main(['--ignore-stdin', '--traceback', 'www.google.com'])
|
||||||
|
|
||||||
|
|
||||||
@mock.patch('httpie.core.get_response')
|
@mock.patch('httpie.core.get_response')
|
||||||
@ -43,6 +44,6 @@ def test_timeout(get_response):
|
|||||||
exc = Timeout('Request timed out')
|
exc = Timeout('Request timed out')
|
||||||
exc.request = Request(method='GET', url='http://www.google.com')
|
exc.request = Request(method='GET', url='http://www.google.com')
|
||||||
get_response.side_effect = exc
|
get_response.side_effect = exc
|
||||||
ret = main(['--ignore-stdin', 'www.google.com'], error=error)
|
ret = main(['--ignore-stdin', 'www.google.com'], custom_log_error=error)
|
||||||
assert ret == 2
|
assert ret == ExitStatus.ERROR_TIMEOUT
|
||||||
assert error_msg == 'Request timed out (30s).'
|
assert error_msg == 'Request timed out (30s).'
|
||||||
|
@ -1,63 +1,74 @@
|
|||||||
import requests
|
import mock
|
||||||
import pytest
|
|
||||||
|
|
||||||
from httpie import ExitStatus
|
from httpie import ExitStatus
|
||||||
from utils import TestEnvironment, http, HTTP_OK
|
from utils import MockEnvironment, http, HTTP_OK
|
||||||
|
|
||||||
|
|
||||||
class TestExitStatus:
|
def test_keyboard_interrupt_during_arg_parsing_exit_status(httpbin):
|
||||||
def test_ok_response_exits_0(self, httpbin):
|
with mock.patch('httpie.cli.parser.parse_args',
|
||||||
r = http('GET', httpbin.url + '/status/200')
|
side_effect=KeyboardInterrupt()):
|
||||||
assert HTTP_OK in r
|
r = http('GET', httpbin.url + '/get', error_exit_ok=True)
|
||||||
assert r.exit_status == ExitStatus.OK
|
assert r.exit_status == ExitStatus.ERROR_CTRL_C
|
||||||
|
|
||||||
def test_error_response_exits_0_without_check_status(self, httpbin):
|
|
||||||
r = http('GET', httpbin.url + '/status/500')
|
|
||||||
assert '500 INTERNAL SERVER ERRO' in r
|
|
||||||
assert r.exit_status == ExitStatus.OK
|
|
||||||
assert not r.stderr
|
|
||||||
|
|
||||||
@pytest.mark.skipif(
|
def test_keyboard_interrupt_in_program_exit_status(httpbin):
|
||||||
tuple(map(int, requests.__version__.split('.'))) < (2, 3, 0),
|
with mock.patch('httpie.core.program',
|
||||||
reason='timeout broken in requests prior v2.3.0 (#185)'
|
side_effect=KeyboardInterrupt()):
|
||||||
)
|
r = http('GET', httpbin.url + '/get', error_exit_ok=True)
|
||||||
def test_timeout_exit_status(self, httpbin):
|
assert r.exit_status == ExitStatus.ERROR_CTRL_C
|
||||||
|
|
||||||
r = http('--timeout=0.5', 'GET', httpbin.url + '/delay/1',
|
|
||||||
error_exit_ok=True)
|
|
||||||
assert r.exit_status == ExitStatus.ERROR_TIMEOUT
|
|
||||||
|
|
||||||
def test_3xx_check_status_exits_3_and_stderr_when_stdout_redirected(
|
def test_ok_response_exits_0(httpbin):
|
||||||
self, httpbin):
|
r = http('GET', httpbin.url + '/get')
|
||||||
env = TestEnvironment(stdout_isatty=False)
|
assert HTTP_OK in r
|
||||||
r = http('--check-status', '--headers',
|
assert r.exit_status == ExitStatus.SUCCESS
|
||||||
'GET', httpbin.url + '/status/301',
|
|
||||||
env=env, error_exit_ok=True)
|
|
||||||
assert '301 MOVED PERMANENTLY' in r
|
|
||||||
assert r.exit_status == ExitStatus.ERROR_HTTP_3XX
|
|
||||||
assert '301 moved permanently' in r.stderr.lower()
|
|
||||||
|
|
||||||
@pytest.mark.skipif(
|
|
||||||
requests.__version__ == '0.13.6',
|
|
||||||
reason='Redirects with prefetch=False are broken in Requests 0.13.6')
|
|
||||||
def test_3xx_check_status_redirects_allowed_exits_0(self, httpbin):
|
|
||||||
r = http('--check-status', '--follow',
|
|
||||||
'GET', httpbin.url + '/status/301',
|
|
||||||
error_exit_ok=True)
|
|
||||||
# The redirect will be followed so 200 is expected.
|
|
||||||
assert HTTP_OK in r
|
|
||||||
assert r.exit_status == ExitStatus.OK
|
|
||||||
|
|
||||||
def test_4xx_check_status_exits_4(self, httpbin):
|
def test_error_response_exits_0_without_check_status(httpbin):
|
||||||
r = http('--check-status', 'GET', httpbin.url + '/status/401',
|
r = http('GET', httpbin.url + '/status/500')
|
||||||
error_exit_ok=True)
|
assert '500 INTERNAL SERVER ERROR' in r
|
||||||
assert '401 UNAUTHORIZED' in r
|
assert r.exit_status == ExitStatus.SUCCESS
|
||||||
assert r.exit_status == ExitStatus.ERROR_HTTP_4XX
|
assert not r.stderr
|
||||||
# Also stderr should be empty since stdout isn't redirected.
|
|
||||||
assert not r.stderr
|
|
||||||
|
|
||||||
def test_5xx_check_status_exits_5(self, httpbin):
|
|
||||||
r = http('--check-status', 'GET', httpbin.url + '/status/500',
|
def test_timeout_exit_status(httpbin):
|
||||||
error_exit_ok=True)
|
|
||||||
assert '500 INTERNAL SERVER ERROR' in r
|
r = http('--timeout=0.01', 'GET', httpbin.url + '/delay/0.5',
|
||||||
assert r.exit_status == ExitStatus.ERROR_HTTP_5XX
|
error_exit_ok=True)
|
||||||
|
assert r.exit_status == ExitStatus.ERROR_TIMEOUT
|
||||||
|
|
||||||
|
|
||||||
|
def test_3xx_check_status_exits_3_and_stderr_when_stdout_redirected(
|
||||||
|
httpbin):
|
||||||
|
env = MockEnvironment(stdout_isatty=False)
|
||||||
|
r = http('--check-status', '--headers',
|
||||||
|
'GET', httpbin.url + '/status/301',
|
||||||
|
env=env, error_exit_ok=True)
|
||||||
|
assert '301 MOVED PERMANENTLY' in r
|
||||||
|
assert r.exit_status == ExitStatus.ERROR_HTTP_3XX
|
||||||
|
assert '301 moved permanently' in r.stderr.lower()
|
||||||
|
|
||||||
|
|
||||||
|
def test_3xx_check_status_redirects_allowed_exits_0(httpbin):
|
||||||
|
r = http('--check-status', '--follow',
|
||||||
|
'GET', httpbin.url + '/status/301',
|
||||||
|
error_exit_ok=True)
|
||||||
|
# The redirect will be followed so 200 is expected.
|
||||||
|
assert HTTP_OK in r
|
||||||
|
assert r.exit_status == ExitStatus.SUCCESS
|
||||||
|
|
||||||
|
|
||||||
|
def test_4xx_check_status_exits_4(httpbin):
|
||||||
|
r = http('--check-status', 'GET', httpbin.url + '/status/401',
|
||||||
|
error_exit_ok=True)
|
||||||
|
assert '401 UNAUTHORIZED' in r
|
||||||
|
assert r.exit_status == ExitStatus.ERROR_HTTP_4XX
|
||||||
|
# Also stderr should be empty since stdout isn't redirected.
|
||||||
|
assert not r.stderr
|
||||||
|
|
||||||
|
|
||||||
|
def test_5xx_check_status_exits_5(httpbin):
|
||||||
|
r = http('--check-status', 'GET', httpbin.url + '/status/500',
|
||||||
|
error_exit_ok=True)
|
||||||
|
assert '500 INTERNAL SERVER ERROR' in r
|
||||||
|
assert r.exit_status == ExitStatus.ERROR_HTTP_5XX
|
||||||
|
@ -1,79 +1,114 @@
|
|||||||
"""High-level tests."""
|
"""High-level tests."""
|
||||||
import pytest
|
import pytest
|
||||||
from utils import TestEnvironment, http, HTTP_OK
|
|
||||||
|
from httpie.input import ParseError
|
||||||
|
from utils import MockEnvironment, http, HTTP_OK
|
||||||
from fixtures import FILE_PATH, FILE_CONTENT
|
from fixtures import FILE_PATH, FILE_CONTENT
|
||||||
|
|
||||||
import httpie
|
import httpie
|
||||||
from httpie.compat import is_py26
|
|
||||||
|
|
||||||
|
|
||||||
class TestHTTPie:
|
def test_debug():
|
||||||
|
r = http('--debug')
|
||||||
|
assert r.exit_status == httpie.ExitStatus.SUCCESS
|
||||||
|
assert 'HTTPie %s' % httpie.__version__ in r.stderr
|
||||||
|
|
||||||
def test_debug(self):
|
|
||||||
r = http('--debug')
|
|
||||||
assert r.exit_status == httpie.ExitStatus.OK
|
|
||||||
assert 'HTTPie %s' % httpie.__version__ in r.stderr
|
|
||||||
assert 'HTTPie data:' in r.stderr
|
|
||||||
|
|
||||||
def test_help(self):
|
def test_help():
|
||||||
r = http('--help', error_exit_ok=True)
|
r = http('--help', error_exit_ok=True)
|
||||||
assert r.exit_status == httpie.ExitStatus.OK
|
assert r.exit_status == httpie.ExitStatus.SUCCESS
|
||||||
assert 'https://github.com/jkbrzt/httpie/issues' in r
|
assert 'https://github.com/jakubroztocil/httpie/issues' in r
|
||||||
|
|
||||||
def test_version(self):
|
|
||||||
r = http('--version', error_exit_ok=True)
|
|
||||||
assert r.exit_status == httpie.ExitStatus.OK
|
|
||||||
# FIXME: py3 has version in stdout, py2 in stderr
|
|
||||||
assert httpie.__version__ == r.stderr.strip() + r.strip()
|
|
||||||
|
|
||||||
def test_GET(self, httpbin):
|
def test_version():
|
||||||
r = http('GET', httpbin.url + '/get')
|
r = http('--version', error_exit_ok=True)
|
||||||
assert HTTP_OK in r
|
assert r.exit_status == httpie.ExitStatus.SUCCESS
|
||||||
|
# FIXME: py3 has version in stdout, py2 in stderr
|
||||||
|
assert httpie.__version__ == r.stderr.strip() + r.strip()
|
||||||
|
|
||||||
def test_DELETE(self, httpbin):
|
|
||||||
r = http('DELETE', httpbin.url + '/delete')
|
|
||||||
assert HTTP_OK in r
|
|
||||||
|
|
||||||
def test_PUT(self, httpbin):
|
def test_GET(httpbin_both):
|
||||||
r = http('PUT', httpbin.url + '/put', 'foo=bar')
|
r = http('GET', httpbin_both + '/get')
|
||||||
assert HTTP_OK in r
|
assert HTTP_OK in r
|
||||||
assert r.json['json']['foo'] == 'bar'
|
|
||||||
|
|
||||||
def test_POST_JSON_data(self, httpbin):
|
|
||||||
r = http('POST', httpbin.url + '/post', 'foo=bar')
|
|
||||||
assert HTTP_OK in r
|
|
||||||
assert r.json['json']['foo'] == 'bar'
|
|
||||||
|
|
||||||
def test_POST_form(self, httpbin):
|
def test_DELETE(httpbin_both):
|
||||||
r = http('--form', 'POST', httpbin.url + '/post', 'foo=bar')
|
r = http('DELETE', httpbin_both + '/delete')
|
||||||
assert HTTP_OK in r
|
assert HTTP_OK in r
|
||||||
assert '"foo": "bar"' in r
|
|
||||||
|
|
||||||
def test_POST_form_multiple_values(self, httpbin):
|
|
||||||
r = http('--form', 'POST', httpbin.url + '/post', 'foo=bar', 'foo=baz')
|
|
||||||
assert HTTP_OK in r
|
|
||||||
assert r.json['form'] == {'foo': ['bar', 'baz']}
|
|
||||||
|
|
||||||
def test_POST_stdin(self, httpbin):
|
def test_PUT(httpbin_both):
|
||||||
with open(FILE_PATH) as f:
|
r = http('PUT', httpbin_both + '/put', 'foo=bar')
|
||||||
env = TestEnvironment(stdin=f, stdin_isatty=False)
|
assert HTTP_OK in r
|
||||||
r = http('--form', 'POST', httpbin.url + '/post', env=env)
|
assert r.json['json']['foo'] == 'bar'
|
||||||
assert HTTP_OK in r
|
|
||||||
assert FILE_CONTENT in r
|
|
||||||
|
|
||||||
def test_headers(self, httpbin):
|
|
||||||
r = http('GET', httpbin.url + '/headers', 'Foo:bar')
|
|
||||||
assert HTTP_OK in r
|
|
||||||
assert '"User-Agent": "HTTPie' in r, r
|
|
||||||
assert '"Foo": "bar"' in r
|
|
||||||
|
|
||||||
@pytest.mark.skipif(
|
def test_POST_JSON_data(httpbin_both):
|
||||||
is_py26,
|
r = http('POST', httpbin_both + '/post', 'foo=bar')
|
||||||
reason='the `object_pairs_hook` arg for `json.loads()` is Py>2.6 only'
|
assert HTTP_OK in r
|
||||||
)
|
assert r.json['json']['foo'] == 'bar'
|
||||||
def test_json_input_preserve_order(self, httpbin):
|
|
||||||
r = http('PATCH', httpbin.url + '/patch',
|
|
||||||
'order:={"map":{"1":"first","2":"second"}}')
|
def test_POST_form(httpbin_both):
|
||||||
assert HTTP_OK in r
|
r = http('--form', 'POST', httpbin_both + '/post', 'foo=bar')
|
||||||
assert r.json['data'] == \
|
assert HTTP_OK in r
|
||||||
'{"order": {"map": {"1": "first", "2": "second"}}}'
|
assert '"foo": "bar"' in r
|
||||||
|
|
||||||
|
|
||||||
|
def test_POST_form_multiple_values(httpbin_both):
|
||||||
|
r = http('--form', 'POST', httpbin_both + '/post', 'foo=bar', 'foo=baz')
|
||||||
|
assert HTTP_OK in r
|
||||||
|
assert r.json['form'] == {'foo': ['bar', 'baz']}
|
||||||
|
|
||||||
|
|
||||||
|
def test_POST_stdin(httpbin_both):
|
||||||
|
with open(FILE_PATH) as f:
|
||||||
|
env = MockEnvironment(stdin=f, stdin_isatty=False)
|
||||||
|
r = http('--form', 'POST', httpbin_both + '/post', env=env)
|
||||||
|
assert HTTP_OK in r
|
||||||
|
assert FILE_CONTENT in r
|
||||||
|
|
||||||
|
|
||||||
|
def test_headers(httpbin_both):
|
||||||
|
r = http('GET', httpbin_both + '/headers', 'Foo:bar')
|
||||||
|
assert HTTP_OK in r
|
||||||
|
assert '"User-Agent": "HTTPie' in r, r
|
||||||
|
assert '"Foo": "bar"' in r
|
||||||
|
|
||||||
|
|
||||||
|
def test_headers_unset(httpbin_both):
|
||||||
|
r = http('GET', httpbin_both + '/headers')
|
||||||
|
assert 'Accept' in r.json['headers'] # default Accept present
|
||||||
|
|
||||||
|
r = http('GET', httpbin_both + '/headers', 'Accept:')
|
||||||
|
assert 'Accept' not in r.json['headers'] # default Accept unset
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skip('unimplemented')
|
||||||
|
def test_unset_host_header(httpbin_both):
|
||||||
|
r = http('GET', httpbin_both + '/headers')
|
||||||
|
assert 'Host' in r.json['headers'] # default Host present
|
||||||
|
|
||||||
|
r = http('GET', httpbin_both + '/headers', 'Host:')
|
||||||
|
assert 'Host' not in r.json['headers'] # default Host unset
|
||||||
|
|
||||||
|
|
||||||
|
def test_headers_empty_value(httpbin_both):
|
||||||
|
r = http('GET', httpbin_both + '/headers')
|
||||||
|
assert r.json['headers']['Accept'] # default Accept has value
|
||||||
|
|
||||||
|
r = http('GET', httpbin_both + '/headers', 'Accept;')
|
||||||
|
assert r.json['headers']['Accept'] == '' # Accept has no value
|
||||||
|
|
||||||
|
|
||||||
|
def test_headers_empty_value_with_value_gives_error(httpbin):
|
||||||
|
with pytest.raises(ParseError):
|
||||||
|
http('GET', httpbin + '/headers', 'Accept;SYNTAX_ERROR')
|
||||||
|
|
||||||
|
|
||||||
|
def test_json_input_preserve_order(httpbin_both):
|
||||||
|
r = http('PATCH', httpbin_both + '/patch',
|
||||||
|
'order:={"map":{"1":"first","2":"second"}}')
|
||||||
|
assert HTTP_OK in r
|
||||||
|
assert r.json['data'] == \
|
||||||
|
'{"order": {"map": {"1": "first", "2": "second"}}}'
|
||||||
|
@ -1,10 +1,30 @@
|
|||||||
|
import os
|
||||||
|
from tempfile import gettempdir
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from utils import TestEnvironment, http, HTTP_OK, COLOR, CRLF
|
from utils import MockEnvironment, http, HTTP_OK, COLOR, CRLF
|
||||||
from httpie import ExitStatus
|
from httpie import ExitStatus
|
||||||
|
from httpie.compat import urlopen
|
||||||
from httpie.output.formatters.colors import get_lexer
|
from httpie.output.formatters.colors import get_lexer
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('stdout_isatty', [True, False])
|
||||||
|
def test_output_option(httpbin, stdout_isatty):
|
||||||
|
output_filename = os.path.join(gettempdir(), test_output_option.__name__)
|
||||||
|
url = httpbin + '/robots.txt'
|
||||||
|
|
||||||
|
r = http('--output', output_filename, url,
|
||||||
|
env=MockEnvironment(stdout_isatty=stdout_isatty))
|
||||||
|
assert r == ''
|
||||||
|
|
||||||
|
expected_body = urlopen(url).read().decode()
|
||||||
|
with open(output_filename, 'r') as f:
|
||||||
|
actual_body = f.read()
|
||||||
|
|
||||||
|
assert actual_body == expected_body
|
||||||
|
|
||||||
|
|
||||||
class TestVerboseFlag:
|
class TestVerboseFlag:
|
||||||
def test_verbose(self, httpbin):
|
def test_verbose(self, httpbin):
|
||||||
r = http('--verbose',
|
r = http('--verbose',
|
||||||
@ -13,7 +33,7 @@ class TestVerboseFlag:
|
|||||||
assert r.count('__test__') == 2
|
assert r.count('__test__') == 2
|
||||||
|
|
||||||
def test_verbose_form(self, httpbin):
|
def test_verbose_form(self, httpbin):
|
||||||
# https://github.com/jkbrzt/httpie/issues/53
|
# https://github.com/jakubroztocil/httpie/issues/53
|
||||||
r = http('--verbose', '--form', 'POST', httpbin.url + '/post',
|
r = http('--verbose', '--form', 'POST', httpbin.url + '/post',
|
||||||
'A=B', 'C=D')
|
'A=B', 'C=D')
|
||||||
assert HTTP_OK in r
|
assert HTTP_OK in r
|
||||||
@ -25,25 +45,38 @@ class TestVerboseFlag:
|
|||||||
assert HTTP_OK in r
|
assert HTTP_OK in r
|
||||||
assert '"baz": "bar"' in r
|
assert '"baz": "bar"' in r
|
||||||
|
|
||||||
|
def test_verbose_implies_all(self, httpbin):
|
||||||
|
r = http('--verbose', '--follow', httpbin + '/redirect/1')
|
||||||
|
assert 'GET /redirect/1 HTTP/1.1' in r
|
||||||
|
assert 'HTTP/1.1 302 FOUND' in r
|
||||||
|
assert 'GET /get HTTP/1.1' in r
|
||||||
|
assert HTTP_OK in r
|
||||||
|
|
||||||
|
|
||||||
class TestColors:
|
class TestColors:
|
||||||
|
|
||||||
@pytest.mark.parametrize('mime', [
|
@pytest.mark.parametrize(
|
||||||
'application/json',
|
argnames=['mime', 'explicit_json', 'body', 'expected_lexer_name'],
|
||||||
'application/json+foo',
|
argvalues=[
|
||||||
'application/foo+json',
|
('application/json', False, None, 'JSON'),
|
||||||
'application/json-foo',
|
('application/json+foo', False, None, 'JSON'),
|
||||||
'application/x-json',
|
('application/foo+json', False, None, 'JSON'),
|
||||||
'foo/json',
|
('application/json-foo', False, None, 'JSON'),
|
||||||
'foo/json+bar',
|
('application/x-json', False, None, 'JSON'),
|
||||||
'foo/bar+json',
|
('foo/json', False, None, 'JSON'),
|
||||||
'foo/json-foo',
|
('foo/json+bar', False, None, 'JSON'),
|
||||||
'foo/x-json',
|
('foo/bar+json', False, None, 'JSON'),
|
||||||
])
|
('foo/json-foo', False, None, 'JSON'),
|
||||||
def test_get_lexer(self, mime):
|
('foo/x-json', False, None, 'JSON'),
|
||||||
lexer = get_lexer(mime)
|
('application/vnd.comverge.grid+hal+json', False, None, 'JSON'),
|
||||||
|
('text/plain', True, '{}', 'JSON'),
|
||||||
|
('text/plain', True, 'foo', 'Text only'),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
def test_get_lexer(self, mime, explicit_json, body, expected_lexer_name):
|
||||||
|
lexer = get_lexer(mime, body=body, explicit_json=explicit_json)
|
||||||
assert lexer is not None
|
assert lexer is not None
|
||||||
assert lexer.name == 'JSON'
|
assert lexer.name == expected_lexer_name
|
||||||
|
|
||||||
def test_get_lexer_not_found(self):
|
def test_get_lexer_not_found(self):
|
||||||
assert get_lexer('xxx/yyy') is None
|
assert get_lexer('xxx/yyy') is None
|
||||||
@ -53,7 +86,7 @@ class TestPrettyOptions:
|
|||||||
"""Test the --pretty flag handling."""
|
"""Test the --pretty flag handling."""
|
||||||
|
|
||||||
def test_pretty_enabled_by_default(self, httpbin):
|
def test_pretty_enabled_by_default(self, httpbin):
|
||||||
env = TestEnvironment(colors=256)
|
env = MockEnvironment(colors=256)
|
||||||
r = http('GET', httpbin.url + '/get', env=env)
|
r = http('GET', httpbin.url + '/get', env=env)
|
||||||
assert COLOR in r
|
assert COLOR in r
|
||||||
|
|
||||||
@ -62,7 +95,7 @@ class TestPrettyOptions:
|
|||||||
assert COLOR not in r
|
assert COLOR not in r
|
||||||
|
|
||||||
def test_force_pretty(self, httpbin):
|
def test_force_pretty(self, httpbin):
|
||||||
env = TestEnvironment(stdout_isatty=False, colors=256)
|
env = MockEnvironment(stdout_isatty=False, colors=256)
|
||||||
r = http('--pretty=all', 'GET', httpbin.url + '/get', env=env, )
|
r = http('--pretty=all', 'GET', httpbin.url + '/get', env=env, )
|
||||||
assert COLOR in r
|
assert COLOR in r
|
||||||
|
|
||||||
@ -75,13 +108,13 @@ class TestPrettyOptions:
|
|||||||
match any lexer.
|
match any lexer.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
env = TestEnvironment(colors=256)
|
env = MockEnvironment(colors=256)
|
||||||
r = http('--print=B', '--pretty=all', httpbin.url + '/post',
|
r = http('--print=B', '--pretty=all', httpbin.url + '/post',
|
||||||
'Content-Type:text/foo+json', 'a=b', env=env)
|
'Content-Type:text/foo+json', 'a=b', env=env)
|
||||||
assert COLOR in r
|
assert COLOR in r
|
||||||
|
|
||||||
def test_colors_option(self, httpbin):
|
def test_colors_option(self, httpbin):
|
||||||
env = TestEnvironment(colors=256)
|
env = MockEnvironment(colors=256)
|
||||||
r = http('--print=B', '--pretty=colors',
|
r = http('--print=B', '--pretty=colors',
|
||||||
'GET', httpbin.url + '/get', 'a=b',
|
'GET', httpbin.url + '/get', 'a=b',
|
||||||
env=env)
|
env=env)
|
||||||
@ -90,7 +123,7 @@ class TestPrettyOptions:
|
|||||||
assert COLOR in r
|
assert COLOR in r
|
||||||
|
|
||||||
def test_format_option(self, httpbin):
|
def test_format_option(self, httpbin):
|
||||||
env = TestEnvironment(colors=256)
|
env = MockEnvironment(colors=256)
|
||||||
r = http('--print=B', '--pretty=format',
|
r = http('--print=B', '--pretty=format',
|
||||||
'GET', httpbin.url + '/get', 'a=b',
|
'GET', httpbin.url + '/get', 'a=b',
|
||||||
env=env)
|
env=env)
|
||||||
@ -128,7 +161,7 @@ class TestLineEndings:
|
|||||||
|
|
||||||
def test_CRLF_formatted_response(self, httpbin):
|
def test_CRLF_formatted_response(self, httpbin):
|
||||||
r = http('--pretty=format', 'GET', httpbin.url + '/get')
|
r = http('--pretty=format', 'GET', httpbin.url + '/get')
|
||||||
assert r.exit_status == ExitStatus.OK
|
assert r.exit_status == ExitStatus.SUCCESS
|
||||||
self._validate_crlf(r)
|
self._validate_crlf(r)
|
||||||
|
|
||||||
def test_CRLF_ugly_request(self, httpbin):
|
def test_CRLF_ugly_request(self, httpbin):
|
||||||
|
47
tests/test_redirects.py
Normal file
47
tests/test_redirects.py
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
"""High-level tests."""
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from httpie import ExitStatus
|
||||||
|
from utils import http, HTTP_OK
|
||||||
|
|
||||||
|
|
||||||
|
def test_follow_all_redirects_shown(httpbin):
|
||||||
|
r = http('--follow', '--all', httpbin.url + '/redirect/2')
|
||||||
|
assert r.count('HTTP/1.1') == 3
|
||||||
|
assert r.count('HTTP/1.1 302 FOUND', 2)
|
||||||
|
assert HTTP_OK in r
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('follow_flag', ['--follow', '-F'])
|
||||||
|
def test_follow_without_all_redirects_hidden(httpbin, follow_flag):
|
||||||
|
r = http(follow_flag, httpbin.url + '/redirect/2')
|
||||||
|
assert r.count('HTTP/1.1') == 1
|
||||||
|
assert HTTP_OK in r
|
||||||
|
|
||||||
|
|
||||||
|
def test_follow_all_output_options_used_for_redirects(httpbin):
|
||||||
|
r = http('--check-status',
|
||||||
|
'--follow',
|
||||||
|
'--all',
|
||||||
|
'--print=H',
|
||||||
|
httpbin.url + '/redirect/2')
|
||||||
|
assert r.count('GET /') == 3
|
||||||
|
assert HTTP_OK not in r
|
||||||
|
|
||||||
|
|
||||||
|
def test_follow_redirect_output_options(httpbin):
|
||||||
|
r = http('--check-status',
|
||||||
|
'--follow',
|
||||||
|
'--all',
|
||||||
|
'--print=h',
|
||||||
|
'--history-print=H',
|
||||||
|
httpbin.url + '/redirect/2')
|
||||||
|
assert r.count('GET /') == 2
|
||||||
|
assert 'HTTP/1.1 302 FOUND' not in r
|
||||||
|
assert HTTP_OK in r
|
||||||
|
|
||||||
|
|
||||||
|
def test_max_redirects(httpbin):
|
||||||
|
r = http('--max-redirects=1', '--follow', httpbin.url + '/redirect/3',
|
||||||
|
error_exit_ok=True)
|
||||||
|
assert r.exit_status == ExitStatus.ERROR_TOO_MANY_REDIRECTS
|
@ -7,7 +7,7 @@ from httpie.compat import is_windows
|
|||||||
|
|
||||||
def test_Host_header_overwrite(httpbin):
|
def test_Host_header_overwrite(httpbin):
|
||||||
"""
|
"""
|
||||||
https://github.com/jkbrzt/httpie/issues/235
|
https://github.com/jakubroztocil/httpie/issues/235
|
||||||
|
|
||||||
"""
|
"""
|
||||||
host = 'httpbin.org'
|
host = 'httpbin.org'
|
||||||
@ -21,7 +21,7 @@ def test_Host_header_overwrite(httpbin):
|
|||||||
@pytest.mark.skipif(is_windows, reason='Unix-only')
|
@pytest.mark.skipif(is_windows, reason='Unix-only')
|
||||||
def test_output_devnull(httpbin):
|
def test_output_devnull(httpbin):
|
||||||
"""
|
"""
|
||||||
https://github.com/jkbrzt/httpie/issues/252
|
https://github.com/jakubroztocil/httpie/issues/252
|
||||||
|
|
||||||
"""
|
"""
|
||||||
http('--output=/dev/null', httpbin + '/get')
|
http('--output=/dev/null', httpbin + '/get')
|
||||||
|
@ -2,12 +2,12 @@
|
|||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
import sys
|
import sys
|
||||||
|
from tempfile import gettempdir
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from httpie.plugins.builtin import HTTPBasicAuth
|
from httpie.plugins.builtin import HTTPBasicAuth
|
||||||
from utils import TestEnvironment, mk_config_dir, http, HTTP_OK, \
|
from utils import MockEnvironment, mk_config_dir, http, HTTP_OK
|
||||||
no_content_type
|
|
||||||
from fixtures import UNICODE
|
from fixtures import UNICODE
|
||||||
|
|
||||||
|
|
||||||
@ -29,7 +29,7 @@ class SessionTestBase(object):
|
|||||||
for session files being reused.
|
for session files being reused.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
return TestEnvironment(config_dir=self.config_dir)
|
return MockEnvironment(config_dir=self.config_dir)
|
||||||
|
|
||||||
|
|
||||||
class TestSessionFlow(SessionTestBase):
|
class TestSessionFlow(SessionTestBase):
|
||||||
@ -81,8 +81,8 @@ class TestSessionFlow(SessionTestBase):
|
|||||||
assert HTTP_OK in r4
|
assert HTTP_OK in r4
|
||||||
assert r4.json['headers']['Hello'] == 'World2'
|
assert r4.json['headers']['Hello'] == 'World2'
|
||||||
assert r4.json['headers']['Cookie'] == 'hello=world2'
|
assert r4.json['headers']['Cookie'] == 'hello=world2'
|
||||||
assert (r2.json['headers']['Authorization'] !=
|
assert (r2.json['headers']['Authorization']
|
||||||
r4.json['headers']['Authorization'])
|
!= r4.json['headers']['Authorization'])
|
||||||
|
|
||||||
def test_session_read_only(self, httpbin):
|
def test_session_read_only(self, httpbin):
|
||||||
self.start_session(httpbin)
|
self.start_session(httpbin)
|
||||||
@ -125,7 +125,7 @@ class TestSession(SessionTestBase):
|
|||||||
r2 = http('--session=test', 'GET', httpbin.url + '/get',
|
r2 = http('--session=test', 'GET', httpbin.url + '/get',
|
||||||
env=self.env())
|
env=self.env())
|
||||||
assert HTTP_OK in r2
|
assert HTTP_OK in r2
|
||||||
assert no_content_type(r2.json['headers'])
|
assert 'Content-Type' not in r2.json['headers']
|
||||||
assert 'If-Unmodified-Since' not in r2.json['headers']
|
assert 'If-Unmodified-Since' not in r2.json['headers']
|
||||||
|
|
||||||
def test_session_by_path(self, httpbin):
|
def test_session_by_path(self, httpbin):
|
||||||
@ -143,7 +143,7 @@ class TestSession(SessionTestBase):
|
|||||||
@pytest.mark.skipif(
|
@pytest.mark.skipif(
|
||||||
sys.version_info >= (3,),
|
sys.version_info >= (3,),
|
||||||
reason="This test fails intermittently on Python 3 - "
|
reason="This test fails intermittently on Python 3 - "
|
||||||
"see https://github.com/jkbrzt/httpie/issues/282")
|
"see https://github.com/jakubroztocil/httpie/issues/282")
|
||||||
def test_session_unicode(self, httpbin):
|
def test_session_unicode(self, httpbin):
|
||||||
self.start_session(httpbin)
|
self.start_session(httpbin)
|
||||||
|
|
||||||
@ -164,7 +164,7 @@ class TestSession(SessionTestBase):
|
|||||||
|
|
||||||
def test_session_default_header_value_overwritten(self, httpbin):
|
def test_session_default_header_value_overwritten(self, httpbin):
|
||||||
self.start_session(httpbin)
|
self.start_session(httpbin)
|
||||||
# https://github.com/jkbrzt/httpie/issues/180
|
# https://github.com/jakubroztocil/httpie/issues/180
|
||||||
r1 = http('--session=test',
|
r1 = http('--session=test',
|
||||||
httpbin.url + '/headers', 'User-Agent:custom',
|
httpbin.url + '/headers', 'User-Agent:custom',
|
||||||
env=self.env())
|
env=self.env())
|
||||||
@ -174,3 +174,14 @@ class TestSession(SessionTestBase):
|
|||||||
r2 = http('--session=test', httpbin.url + '/headers', env=self.env())
|
r2 = http('--session=test', httpbin.url + '/headers', env=self.env())
|
||||||
assert HTTP_OK in r2
|
assert HTTP_OK in r2
|
||||||
assert r2.json['headers']['User-Agent'] == 'custom'
|
assert r2.json['headers']['User-Agent'] == 'custom'
|
||||||
|
|
||||||
|
def test_download_in_session(self, httpbin):
|
||||||
|
# https://github.com/jakubroztocil/httpie/issues/412
|
||||||
|
self.start_session(httpbin)
|
||||||
|
cwd = os.getcwd()
|
||||||
|
os.chdir(gettempdir())
|
||||||
|
try:
|
||||||
|
http('--session=test', '--download',
|
||||||
|
httpbin.url + '/get', env=self.env())
|
||||||
|
finally:
|
||||||
|
os.chdir(cwd)
|
||||||
|
@ -2,78 +2,114 @@ import os
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
import pytest_httpbin.certs
|
import pytest_httpbin.certs
|
||||||
from requests.exceptions import SSLError
|
import requests.exceptions
|
||||||
|
|
||||||
from httpie import ExitStatus
|
from httpie import ExitStatus
|
||||||
from utils import http, HTTP_OK, TESTS_ROOT
|
from httpie.input import SSL_VERSION_ARG_MAPPING
|
||||||
|
from utils import HTTP_OK, TESTS_ROOT, http
|
||||||
|
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Handle OpenSSL errors, if installed.
|
||||||
|
# See <https://github.com/jakubroztocil/httpie/issues/729>
|
||||||
|
# noinspection PyUnresolvedReferences
|
||||||
|
import OpenSSL.SSL
|
||||||
|
ssl_errors = (
|
||||||
|
requests.exceptions.SSLError,
|
||||||
|
OpenSSL.SSL.Error,
|
||||||
|
)
|
||||||
|
except ImportError:
|
||||||
|
ssl_errors = (
|
||||||
|
requests.exceptions.SSLError,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
CLIENT_CERT = os.path.join(TESTS_ROOT, 'client_certs', 'client.crt')
|
CLIENT_CERT = os.path.join(TESTS_ROOT, 'client_certs', 'client.crt')
|
||||||
CLIENT_KEY = os.path.join(TESTS_ROOT, 'client_certs', 'client.key')
|
CLIENT_KEY = os.path.join(TESTS_ROOT, 'client_certs', 'client.key')
|
||||||
CLIENT_PEM = os.path.join(TESTS_ROOT, 'client_certs', 'client.pem')
|
CLIENT_PEM = os.path.join(TESTS_ROOT, 'client_certs', 'client.pem')
|
||||||
|
# FIXME:
|
||||||
# We test against a local httpbin instance which uses a self-signed cert.
|
# We test against a local httpbin instance which uses a self-signed cert.
|
||||||
# Requests without --verify=<CA_BUNDLE> will fail with a verification error.
|
# Requests without --verify=<CA_BUNDLE> will fail with a verification error.
|
||||||
# See: https://github.com/kevin1024/pytest-httpbin#https-support
|
# See: https://github.com/kevin1024/pytest-httpbin#https-support
|
||||||
CA_BUNDLE = pytest_httpbin.certs.where()
|
CA_BUNDLE = pytest_httpbin.certs.where()
|
||||||
|
|
||||||
|
|
||||||
class TestClientSSLCertHandling(object):
|
@pytest.mark.parametrize('ssl_version', SSL_VERSION_ARG_MAPPING.keys())
|
||||||
|
def test_ssl_version(httpbin_secure, ssl_version):
|
||||||
|
try:
|
||||||
|
r = http(
|
||||||
|
'--ssl', ssl_version,
|
||||||
|
httpbin_secure + '/get'
|
||||||
|
)
|
||||||
|
assert HTTP_OK in r
|
||||||
|
except ssl_errors as e:
|
||||||
|
if ssl_version == 'ssl3':
|
||||||
|
# pytest-httpbin doesn't support ssl3
|
||||||
|
assert 'SSLV3_ALERT_HANDSHAKE_FAILURE' in str(e)
|
||||||
|
else:
|
||||||
|
raise
|
||||||
|
|
||||||
def test_cert_file_not_found(self, httpbin_secure):
|
|
||||||
r = http(httpbin_secure + '/get',
|
|
||||||
'--verify', CA_BUNDLE,
|
|
||||||
'--cert', '/__not_found__',
|
|
||||||
error_exit_ok=True)
|
|
||||||
assert r.exit_status == ExitStatus.ERROR
|
|
||||||
assert 'No such file or directory' in r.stderr
|
|
||||||
|
|
||||||
def test_cert_file_invalid(self, httpbin_secure):
|
class TestClientCert:
|
||||||
with pytest.raises(SSLError):
|
|
||||||
http(httpbin_secure + '/get',
|
|
||||||
'--verify', CA_BUNDLE,
|
|
||||||
'--cert', __file__)
|
|
||||||
|
|
||||||
def test_cert_ok_but_missing_key(self, httpbin_secure):
|
|
||||||
with pytest.raises(SSLError):
|
|
||||||
http(httpbin_secure + '/get',
|
|
||||||
'--verify', CA_BUNDLE,
|
|
||||||
'--cert', CLIENT_CERT)
|
|
||||||
|
|
||||||
def test_cert_and_key(self, httpbin_secure):
|
def test_cert_and_key(self, httpbin_secure):
|
||||||
r = http(httpbin_secure + '/get',
|
r = http(httpbin_secure + '/get',
|
||||||
'--verify', CA_BUNDLE,
|
|
||||||
'--cert', CLIENT_CERT,
|
'--cert', CLIENT_CERT,
|
||||||
'--cert-key', CLIENT_KEY)
|
'--cert-key', CLIENT_KEY)
|
||||||
assert HTTP_OK in r
|
assert HTTP_OK in r
|
||||||
|
|
||||||
def test_cert_pem(self, httpbin_secure):
|
def test_cert_pem(self, httpbin_secure):
|
||||||
r = http(httpbin_secure + '/get',
|
r = http(httpbin_secure + '/get',
|
||||||
'--verify', CA_BUNDLE,
|
|
||||||
'--cert', CLIENT_PEM)
|
'--cert', CLIENT_PEM)
|
||||||
assert HTTP_OK in r
|
assert HTTP_OK in r
|
||||||
|
|
||||||
|
def test_cert_file_not_found(self, httpbin_secure):
|
||||||
|
r = http(httpbin_secure + '/get',
|
||||||
|
'--cert', '/__not_found__',
|
||||||
|
error_exit_ok=True)
|
||||||
|
assert r.exit_status == ExitStatus.ERROR
|
||||||
|
assert 'No such file or directory' in r.stderr
|
||||||
|
|
||||||
class TestServerSSLCertHandling(object):
|
def test_cert_file_invalid(self, httpbin_secure):
|
||||||
|
with pytest.raises(ssl_errors):
|
||||||
|
http(httpbin_secure + '/get',
|
||||||
|
'--cert', __file__)
|
||||||
|
|
||||||
def test_self_signed_server_cert_by_default_raises_ssl_error(
|
def test_cert_ok_but_missing_key(self, httpbin_secure):
|
||||||
self, httpbin_secure):
|
with pytest.raises(ssl_errors):
|
||||||
with pytest.raises(SSLError):
|
http(httpbin_secure + '/get',
|
||||||
http(httpbin_secure.url + '/get')
|
'--cert', CLIENT_CERT)
|
||||||
|
|
||||||
|
|
||||||
|
class TestServerCert:
|
||||||
|
|
||||||
def test_verify_no_OK(self, httpbin_secure):
|
def test_verify_no_OK(self, httpbin_secure):
|
||||||
r = http(httpbin_secure.url + '/get', '--verify=no')
|
r = http(httpbin_secure.url + '/get', '--verify=no')
|
||||||
assert HTTP_OK in r
|
assert HTTP_OK in r
|
||||||
|
|
||||||
def test_verify_custom_ca_bundle_path(
|
@pytest.mark.parametrize('verify_value', ['false', 'fALse'])
|
||||||
self, httpbin_secure):
|
def test_verify_false_OK(self, httpbin_secure, verify_value):
|
||||||
r = http(httpbin_secure.url + '/get', '--verify', CA_BUNDLE)
|
r = http(httpbin_secure.url + '/get', '--verify', verify_value)
|
||||||
assert HTTP_OK in r
|
assert HTTP_OK in r
|
||||||
|
|
||||||
|
def test_verify_custom_ca_bundle_path(
|
||||||
|
self, httpbin_secure_untrusted
|
||||||
|
):
|
||||||
|
r = http(httpbin_secure_untrusted + '/get', '--verify', CA_BUNDLE)
|
||||||
|
assert HTTP_OK in r
|
||||||
|
|
||||||
|
def test_self_signed_server_cert_by_default_raises_ssl_error(
|
||||||
|
self,
|
||||||
|
httpbin_secure_untrusted
|
||||||
|
):
|
||||||
|
with pytest.raises(ssl_errors):
|
||||||
|
http(httpbin_secure_untrusted.url + '/get')
|
||||||
|
|
||||||
def test_verify_custom_ca_bundle_invalid_path(self, httpbin_secure):
|
def test_verify_custom_ca_bundle_invalid_path(self, httpbin_secure):
|
||||||
with pytest.raises(SSLError):
|
# since 2.14.0 requests raises IOError
|
||||||
|
with pytest.raises(ssl_errors + (IOError,)):
|
||||||
http(httpbin_secure.url + '/get', '--verify', '/__not_found__')
|
http(httpbin_secure.url + '/get', '--verify', '/__not_found__')
|
||||||
|
|
||||||
def test_verify_custom_ca_bundle_invalid_bundle(self, httpbin_secure):
|
def test_verify_custom_ca_bundle_invalid_bundle(self, httpbin_secure):
|
||||||
with pytest.raises(SSLError):
|
with pytest.raises(ssl_errors):
|
||||||
http(httpbin_secure.url + '/get', '--verify', __file__)
|
http(httpbin_secure.url + '/get', '--verify', __file__)
|
||||||
|
@ -2,41 +2,43 @@ import pytest
|
|||||||
|
|
||||||
from httpie.compat import is_windows
|
from httpie.compat import is_windows
|
||||||
from httpie.output.streams import BINARY_SUPPRESSED_NOTICE
|
from httpie.output.streams import BINARY_SUPPRESSED_NOTICE
|
||||||
from utils import http, TestEnvironment
|
from utils import http, MockEnvironment
|
||||||
from fixtures import BIN_FILE_CONTENT, BIN_FILE_PATH
|
from fixtures import BIN_FILE_CONTENT, BIN_FILE_PATH
|
||||||
|
|
||||||
|
|
||||||
class TestStream:
|
# GET because httpbin 500s with binary POST body.
|
||||||
# GET because httpbin 500s with binary POST body.
|
|
||||||
|
|
||||||
@pytest.mark.skipif(is_windows,
|
|
||||||
reason='Pretty redirect not supported under Windows')
|
|
||||||
def test_pretty_redirected_stream(self, httpbin):
|
|
||||||
"""Test that --stream works with prettified redirected output."""
|
|
||||||
with open(BIN_FILE_PATH, 'rb') as f:
|
|
||||||
env = TestEnvironment(colors=256, stdin=f,
|
|
||||||
stdin_isatty=False,
|
|
||||||
stdout_isatty=False)
|
|
||||||
r = http('--verbose', '--pretty=all', '--stream', 'GET',
|
|
||||||
httpbin.url + '/get', env=env)
|
|
||||||
assert BINARY_SUPPRESSED_NOTICE.decode() in r
|
|
||||||
|
|
||||||
def test_encoded_stream(self, httpbin):
|
@pytest.mark.skipif(is_windows,
|
||||||
"""Test that --stream works with non-prettified
|
reason='Pretty redirect not supported under Windows')
|
||||||
redirected terminal output."""
|
def test_pretty_redirected_stream(httpbin):
|
||||||
with open(BIN_FILE_PATH, 'rb') as f:
|
"""Test that --stream works with prettified redirected output."""
|
||||||
env = TestEnvironment(stdin=f, stdin_isatty=False)
|
with open(BIN_FILE_PATH, 'rb') as f:
|
||||||
r = http('--pretty=none', '--stream', '--verbose', 'GET',
|
env = MockEnvironment(colors=256, stdin=f,
|
||||||
httpbin.url + '/get', env=env)
|
stdin_isatty=False,
|
||||||
assert BINARY_SUPPRESSED_NOTICE.decode() in r
|
stdout_isatty=False)
|
||||||
|
r = http('--verbose', '--pretty=all', '--stream', 'GET',
|
||||||
|
httpbin.url + '/get', env=env)
|
||||||
|
assert BINARY_SUPPRESSED_NOTICE.decode() in r
|
||||||
|
|
||||||
def test_redirected_stream(self, httpbin):
|
|
||||||
"""Test that --stream works with non-prettified
|
def test_encoded_stream(httpbin):
|
||||||
redirected terminal output."""
|
"""Test that --stream works with non-prettified
|
||||||
with open(BIN_FILE_PATH, 'rb') as f:
|
redirected terminal output."""
|
||||||
env = TestEnvironment(stdout_isatty=False,
|
with open(BIN_FILE_PATH, 'rb') as f:
|
||||||
stdin_isatty=False,
|
env = MockEnvironment(stdin=f, stdin_isatty=False)
|
||||||
stdin=f)
|
r = http('--pretty=none', '--stream', '--verbose', 'GET',
|
||||||
r = http('--pretty=none', '--stream', '--verbose', 'GET',
|
httpbin.url + '/get', env=env)
|
||||||
httpbin.url + '/get', env=env)
|
assert BINARY_SUPPRESSED_NOTICE.decode() in r
|
||||||
assert BIN_FILE_CONTENT in r
|
|
||||||
|
|
||||||
|
def test_redirected_stream(httpbin):
|
||||||
|
"""Test that --stream works with non-prettified
|
||||||
|
redirected terminal output."""
|
||||||
|
with open(BIN_FILE_PATH, 'rb') as f:
|
||||||
|
env = MockEnvironment(stdout_isatty=False,
|
||||||
|
stdin_isatty=False,
|
||||||
|
stdin=f)
|
||||||
|
r = http('--pretty=none', '--stream', '--verbose', 'GET',
|
||||||
|
httpbin.url + '/get', env=env)
|
||||||
|
assert BIN_FILE_CONTENT in r
|
||||||
|
@ -7,81 +7,91 @@ from utils import http, HTTP_OK
|
|||||||
from fixtures import UNICODE
|
from fixtures import UNICODE
|
||||||
|
|
||||||
|
|
||||||
class TestUnicode:
|
def test_unicode_headers(httpbin):
|
||||||
|
# httpbin doesn't interpret utf8 headers
|
||||||
|
r = http(httpbin.url + '/headers', u'Test:%s' % UNICODE)
|
||||||
|
assert HTTP_OK in r
|
||||||
|
|
||||||
def test_unicode_headers(self, httpbin):
|
|
||||||
# httpbin doesn't interpret utf8 headers
|
|
||||||
r = http(httpbin.url + '/headers', u'Test:%s' % UNICODE)
|
|
||||||
assert HTTP_OK in r
|
|
||||||
|
|
||||||
def test_unicode_headers_verbose(self, httpbin):
|
def test_unicode_headers_verbose(httpbin):
|
||||||
# httpbin doesn't interpret utf8 headers
|
# httpbin doesn't interpret utf8 headers
|
||||||
r = http('--verbose', httpbin.url + '/headers', u'Test:%s' % UNICODE)
|
r = http('--verbose', httpbin.url + '/headers', u'Test:%s' % UNICODE)
|
||||||
assert HTTP_OK in r
|
assert HTTP_OK in r
|
||||||
assert UNICODE in r
|
assert UNICODE in r
|
||||||
|
|
||||||
def test_unicode_form_item(self, httpbin):
|
|
||||||
r = http('--form', 'POST', httpbin.url + '/post', u'test=%s' % UNICODE)
|
|
||||||
assert HTTP_OK in r
|
|
||||||
assert r.json['form'] == {'test': UNICODE}
|
|
||||||
|
|
||||||
def test_unicode_form_item_verbose(self, httpbin):
|
def test_unicode_form_item(httpbin):
|
||||||
r = http('--verbose', '--form',
|
r = http('--form', 'POST', httpbin.url + '/post', u'test=%s' % UNICODE)
|
||||||
'POST', httpbin.url + '/post', u'test=%s' % UNICODE)
|
assert HTTP_OK in r
|
||||||
assert HTTP_OK in r
|
assert r.json['form'] == {'test': UNICODE}
|
||||||
assert UNICODE in r
|
|
||||||
|
|
||||||
def test_unicode_json_item(self, httpbin):
|
|
||||||
r = http('--json', 'POST', httpbin.url + '/post', u'test=%s' % UNICODE)
|
|
||||||
assert HTTP_OK in r
|
|
||||||
assert r.json['json'] == {'test': UNICODE}
|
|
||||||
|
|
||||||
def test_unicode_json_item_verbose(self, httpbin):
|
def test_unicode_form_item_verbose(httpbin):
|
||||||
r = http('--verbose', '--json',
|
r = http('--verbose', '--form',
|
||||||
'POST', httpbin.url + '/post', u'test=%s' % UNICODE)
|
'POST', httpbin.url + '/post', u'test=%s' % UNICODE)
|
||||||
assert HTTP_OK in r
|
assert HTTP_OK in r
|
||||||
assert UNICODE in r
|
assert UNICODE in r
|
||||||
|
|
||||||
def test_unicode_raw_json_item(self, httpbin):
|
|
||||||
r = http('--json', 'POST', httpbin.url + '/post',
|
|
||||||
u'test:={ "%s" : [ "%s" ] }' % (UNICODE, UNICODE))
|
|
||||||
assert HTTP_OK in r
|
|
||||||
assert r.json['json'] == {'test': {UNICODE: [UNICODE]}}
|
|
||||||
|
|
||||||
def test_unicode_raw_json_item_verbose(self, httpbin):
|
def test_unicode_json_item(httpbin):
|
||||||
r = http('--json', 'POST', httpbin.url + '/post',
|
r = http('--json', 'POST', httpbin.url + '/post', u'test=%s' % UNICODE)
|
||||||
u'test:={ "%s" : [ "%s" ] }' % (UNICODE, UNICODE))
|
assert HTTP_OK in r
|
||||||
assert HTTP_OK in r
|
assert r.json['json'] == {'test': UNICODE}
|
||||||
assert r.json['json'] == {'test': {UNICODE: [UNICODE]}}
|
|
||||||
|
|
||||||
def test_unicode_url_query_arg_item(self, httpbin):
|
|
||||||
r = http(httpbin.url + '/get', u'test==%s' % UNICODE)
|
|
||||||
assert HTTP_OK in r
|
|
||||||
assert r.json['args'] == {'test': UNICODE}, r
|
|
||||||
|
|
||||||
def test_unicode_url_query_arg_item_verbose(self, httpbin):
|
def test_unicode_json_item_verbose(httpbin):
|
||||||
r = http('--verbose', httpbin.url + '/get', u'test==%s' % UNICODE)
|
r = http('--verbose', '--json',
|
||||||
assert HTTP_OK in r
|
'POST', httpbin.url + '/post', u'test=%s' % UNICODE)
|
||||||
assert UNICODE in r
|
assert HTTP_OK in r
|
||||||
|
assert UNICODE in r
|
||||||
|
|
||||||
def test_unicode_url(self, httpbin):
|
|
||||||
r = http(httpbin.url + u'/get?test=' + UNICODE)
|
|
||||||
assert HTTP_OK in r
|
|
||||||
assert r.json['args'] == {'test': UNICODE}
|
|
||||||
|
|
||||||
# def test_unicode_url_verbose(self):
|
def test_unicode_raw_json_item(httpbin):
|
||||||
# r = http(httpbin.url + '--verbose', u'/get?test=' + UNICODE)
|
r = http('--json', 'POST', httpbin.url + '/post',
|
||||||
# assert HTTP_OK in r
|
u'test:={ "%s" : [ "%s" ] }' % (UNICODE, UNICODE))
|
||||||
|
assert HTTP_OK in r
|
||||||
|
assert r.json['json'] == {'test': {UNICODE: [UNICODE]}}
|
||||||
|
|
||||||
def test_unicode_basic_auth(self, httpbin):
|
|
||||||
# it doesn't really authenticate us because httpbin
|
|
||||||
# doesn't interpret the utf8-encoded auth
|
|
||||||
http('--verbose', '--auth', u'test:%s' % UNICODE,
|
|
||||||
httpbin.url + u'/basic-auth/test/' + UNICODE)
|
|
||||||
|
|
||||||
def test_unicode_digest_auth(self, httpbin):
|
def test_unicode_raw_json_item_verbose(httpbin):
|
||||||
# it doesn't really authenticate us because httpbin
|
r = http('--json', 'POST', httpbin.url + '/post',
|
||||||
# doesn't interpret the utf8-encoded auth
|
u'test:={ "%s" : [ "%s" ] }' % (UNICODE, UNICODE))
|
||||||
http('--auth-type=digest',
|
assert HTTP_OK in r
|
||||||
'--auth', u'test:%s' % UNICODE,
|
assert r.json['json'] == {'test': {UNICODE: [UNICODE]}}
|
||||||
httpbin.url + u'/digest-auth/auth/test/' + UNICODE)
|
|
||||||
|
|
||||||
|
def test_unicode_url_query_arg_item(httpbin):
|
||||||
|
r = http(httpbin.url + '/get', u'test==%s' % UNICODE)
|
||||||
|
assert HTTP_OK in r
|
||||||
|
assert r.json['args'] == {'test': UNICODE}, r
|
||||||
|
|
||||||
|
|
||||||
|
def test_unicode_url_query_arg_item_verbose(httpbin):
|
||||||
|
r = http('--verbose', httpbin.url + '/get', u'test==%s' % UNICODE)
|
||||||
|
assert HTTP_OK in r
|
||||||
|
assert UNICODE in r
|
||||||
|
|
||||||
|
|
||||||
|
def test_unicode_url(httpbin):
|
||||||
|
r = http(httpbin.url + u'/get?test=' + UNICODE)
|
||||||
|
assert HTTP_OK in r
|
||||||
|
assert r.json['args'] == {'test': UNICODE}
|
||||||
|
|
||||||
|
# def test_unicode_url_verbose(self):
|
||||||
|
# r = http(httpbin.url + '--verbose', u'/get?test=' + UNICODE)
|
||||||
|
# assert HTTP_OK in r
|
||||||
|
|
||||||
|
|
||||||
|
def test_unicode_basic_auth(httpbin):
|
||||||
|
# it doesn't really authenticate us because httpbin
|
||||||
|
# doesn't interpret the utf8-encoded auth
|
||||||
|
http('--verbose', '--auth', u'test:%s' % UNICODE,
|
||||||
|
httpbin.url + u'/basic-auth/test/' + UNICODE)
|
||||||
|
|
||||||
|
|
||||||
|
def test_unicode_digest_auth(httpbin):
|
||||||
|
# it doesn't really authenticate us because httpbin
|
||||||
|
# doesn't interpret the utf8-encoded auth
|
||||||
|
http('--auth-type=digest',
|
||||||
|
'--auth', u'test:%s' % UNICODE,
|
||||||
|
httpbin.url + u'/digest-auth/auth/test/' + UNICODE)
|
||||||
|
@ -3,7 +3,7 @@ import os
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from httpie.input import ParseError
|
from httpie.input import ParseError
|
||||||
from utils import TestEnvironment, http, HTTP_OK
|
from utils import MockEnvironment, http, HTTP_OK
|
||||||
from fixtures import FILE_PATH_ARG, FILE_PATH, FILE_CONTENT
|
from fixtures import FILE_PATH_ARG, FILE_PATH, FILE_CONTENT
|
||||||
|
|
||||||
|
|
||||||
@ -23,6 +23,7 @@ class TestMultipartFormDataFileUpload:
|
|||||||
' filename="%s"' % os.path.basename(FILE_PATH) in r
|
' filename="%s"' % os.path.basename(FILE_PATH) in r
|
||||||
assert FILE_CONTENT in r
|
assert FILE_CONTENT in r
|
||||||
assert '"foo": "bar"' in r
|
assert '"foo": "bar"' in r
|
||||||
|
assert 'Content-Type: text/plain' in r
|
||||||
|
|
||||||
def test_upload_multiple_fields_with_the_same_name(self, httpbin):
|
def test_upload_multiple_fields_with_the_same_name(self, httpbin):
|
||||||
r = http('--form', '--verbose', 'POST', httpbin.url + '/post',
|
r = http('--form', '--verbose', 'POST', httpbin.url + '/post',
|
||||||
@ -34,6 +35,7 @@ class TestMultipartFormDataFileUpload:
|
|||||||
# Should be 4, but is 3 because httpbin
|
# Should be 4, but is 3 because httpbin
|
||||||
# doesn't seem to support filed field lists
|
# doesn't seem to support filed field lists
|
||||||
assert r.count(FILE_CONTENT) in [3, 4]
|
assert r.count(FILE_CONTENT) in [3, 4]
|
||||||
|
assert r.count('Content-Type: text/plain') == 2
|
||||||
|
|
||||||
|
|
||||||
class TestRequestBodyFromFilePath:
|
class TestRequestBodyFromFilePath:
|
||||||
@ -60,14 +62,14 @@ class TestRequestBodyFromFilePath:
|
|||||||
|
|
||||||
def test_request_body_from_file_by_path_no_field_name_allowed(
|
def test_request_body_from_file_by_path_no_field_name_allowed(
|
||||||
self, httpbin):
|
self, httpbin):
|
||||||
env = TestEnvironment(stdin_isatty=True)
|
env = MockEnvironment(stdin_isatty=True)
|
||||||
r = http('POST', httpbin.url + '/post', 'field-name@' + FILE_PATH_ARG,
|
r = http('POST', httpbin.url + '/post', 'field-name@' + FILE_PATH_ARG,
|
||||||
env=env, error_exit_ok=True)
|
env=env, error_exit_ok=True)
|
||||||
assert 'perhaps you meant --form?' in r.stderr
|
assert 'perhaps you meant --form?' in r.stderr
|
||||||
|
|
||||||
def test_request_body_from_file_by_path_no_data_items_allowed(
|
def test_request_body_from_file_by_path_no_data_items_allowed(
|
||||||
self, httpbin):
|
self, httpbin):
|
||||||
env = TestEnvironment(stdin_isatty=False)
|
env = MockEnvironment(stdin_isatty=False)
|
||||||
r = http('POST', httpbin.url + '/post', '@' + FILE_PATH_ARG, 'foo=bar',
|
r = http('POST', httpbin.url + '/post', '@' + FILE_PATH_ARG, 'foo=bar',
|
||||||
env=env, error_exit_ok=True)
|
env=env, error_exit_ok=True)
|
||||||
assert 'cannot be mixed' in r.stderr
|
assert 'cannot be mixed' in r.stderr
|
||||||
|
@ -4,7 +4,7 @@ import tempfile
|
|||||||
import pytest
|
import pytest
|
||||||
from httpie.context import Environment
|
from httpie.context import Environment
|
||||||
|
|
||||||
from utils import TestEnvironment, http
|
from utils import MockEnvironment, http
|
||||||
from httpie.compat import is_windows
|
from httpie.compat import is_windows
|
||||||
|
|
||||||
|
|
||||||
@ -20,9 +20,11 @@ class TestWindowsOnly:
|
|||||||
|
|
||||||
class TestFakeWindows:
|
class TestFakeWindows:
|
||||||
def test_output_file_pretty_not_allowed_on_windows(self, httpbin):
|
def test_output_file_pretty_not_allowed_on_windows(self, httpbin):
|
||||||
env = TestEnvironment(is_windows=True)
|
env = MockEnvironment(is_windows=True)
|
||||||
output_file = os.path.join(
|
output_file = os.path.join(
|
||||||
tempfile.gettempdir(), '__httpie_test_output__')
|
tempfile.gettempdir(),
|
||||||
|
self.test_output_file_pretty_not_allowed_on_windows.__name__
|
||||||
|
)
|
||||||
r = http('--output', output_file,
|
r = http('--output', output_file,
|
||||||
'--pretty=all', 'GET', httpbin.url + '/get',
|
'--pretty=all', 'GET', httpbin.url + '/get',
|
||||||
env=env, error_exit_ok=True)
|
env=env, error_exit_ok=True)
|
||||||
|
283
tests/utils.py
283
tests/utils.py
@ -1,23 +1,18 @@
|
|||||||
# coding=utf-8
|
# coding=utf-8
|
||||||
"""Utilities used by HTTPie tests.
|
"""Utilities for HTTPie test suite."""
|
||||||
|
|
||||||
"""
|
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
import json
|
import json
|
||||||
import shutil
|
|
||||||
import tempfile
|
import tempfile
|
||||||
|
|
||||||
import httpie
|
from httpie import ExitStatus, EXIT_STATUS_LABELS
|
||||||
from httpie.context import Environment
|
from httpie.context import Environment
|
||||||
from httpie.core import main
|
from httpie.core import main
|
||||||
from httpie.compat import bytes, str
|
from httpie.compat import bytes, str
|
||||||
|
|
||||||
|
|
||||||
TESTS_ROOT = os.path.abspath(os.path.dirname(__file__))
|
TESTS_ROOT = os.path.abspath(os.path.dirname(__file__))
|
||||||
|
|
||||||
|
|
||||||
CRLF = '\r\n'
|
CRLF = '\r\n'
|
||||||
COLOR = '\x1b['
|
COLOR = '\x1b['
|
||||||
HTTP_OK = '200 OK'
|
HTTP_OK = '200 OK'
|
||||||
@ -28,14 +23,9 @@ HTTP_OK_COLOR = (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def no_content_type(headers):
|
def mk_config_dir():
|
||||||
return (
|
dirname = tempfile.mkdtemp(prefix='httpie_config_')
|
||||||
'Content-Type' not in headers
|
return dirname
|
||||||
# We need to do also this because of this issue:
|
|
||||||
# <https://github.com/kevin1024/pytest-httpbin/issues/5>
|
|
||||||
# TODO: remove this function once the issue is if fixed
|
|
||||||
or headers['Content-Type'] == 'text/plain'
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def add_auth(url, auth):
|
def add_auth(url, auth):
|
||||||
@ -43,137 +33,47 @@ def add_auth(url, auth):
|
|||||||
return proto + '://' + auth + '@' + rest
|
return proto + '://' + auth + '@' + rest
|
||||||
|
|
||||||
|
|
||||||
class TestEnvironment(Environment):
|
class MockEnvironment(Environment):
|
||||||
"""
|
"""Environment subclass with reasonable defaults for testing."""
|
||||||
Environment subclass with reasonable defaults suitable for testing.
|
|
||||||
|
|
||||||
"""
|
|
||||||
colors = 0
|
colors = 0
|
||||||
stdin_isatty = True,
|
stdin_isatty = True,
|
||||||
stdout_isatty = True
|
stdout_isatty = True
|
||||||
is_windows = False
|
is_windows = False
|
||||||
|
|
||||||
_shutil = shutil # needed by __del__ (would get gc'd)
|
|
||||||
|
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
|
|
||||||
if 'stdout' not in kwargs:
|
if 'stdout' not in kwargs:
|
||||||
kwargs['stdout'] = tempfile.TemporaryFile('w+b')
|
kwargs['stdout'] = tempfile.TemporaryFile(
|
||||||
|
mode='w+b',
|
||||||
|
prefix='httpie_stdout'
|
||||||
|
)
|
||||||
if 'stderr' not in kwargs:
|
if 'stderr' not in kwargs:
|
||||||
kwargs['stderr'] = tempfile.TemporaryFile('w+t')
|
kwargs['stderr'] = tempfile.TemporaryFile(
|
||||||
|
mode='w+t',
|
||||||
|
prefix='httpie_stderr'
|
||||||
|
)
|
||||||
|
super(MockEnvironment, self).__init__(**kwargs)
|
||||||
|
self._delete_config_dir = False
|
||||||
|
|
||||||
self.delete_config_dir = False
|
@property
|
||||||
if 'config_dir' not in kwargs:
|
def config(self):
|
||||||
kwargs['config_dir'] = mk_config_dir()
|
if not self.config_dir.startswith(tempfile.gettempdir()):
|
||||||
self.delete_config_dir = True
|
self.config_dir = mk_config_dir()
|
||||||
|
self._delete_config_dir = True
|
||||||
|
return super(MockEnvironment, self).config
|
||||||
|
|
||||||
super(TestEnvironment, self).__init__(**kwargs)
|
def cleanup(self):
|
||||||
|
self.stdout.close()
|
||||||
|
self.stderr.close()
|
||||||
|
if self._delete_config_dir:
|
||||||
|
assert self.config_dir.startswith(tempfile.gettempdir())
|
||||||
|
from shutil import rmtree
|
||||||
|
rmtree(self.config_dir)
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
if self.delete_config_dir:
|
|
||||||
self._shutil.rmtree(self.config_dir)
|
|
||||||
|
|
||||||
|
|
||||||
def http(*args, **kwargs):
|
|
||||||
"""
|
|
||||||
Run HTTPie and capture stderr/out and exit status.
|
|
||||||
|
|
||||||
Invoke `httpie.core.main()` with `args` and `kwargs`,
|
|
||||||
and return a `CLIResponse` subclass instance.
|
|
||||||
|
|
||||||
The return value is either a `StrCLIResponse`, or `BytesCLIResponse`
|
|
||||||
if unable to decode the output.
|
|
||||||
|
|
||||||
The response has the following attributes:
|
|
||||||
|
|
||||||
`stdout` is represented by the instance itself (print r)
|
|
||||||
`stderr`: text written to stderr
|
|
||||||
`exit_status`: the exit status
|
|
||||||
`json`: decoded JSON (if possible) or `None`
|
|
||||||
|
|
||||||
Exceptions are propagated.
|
|
||||||
|
|
||||||
If you pass ``error_exit_ok=True``, then error exit statuses
|
|
||||||
won't result into an exception.
|
|
||||||
|
|
||||||
Example:
|
|
||||||
|
|
||||||
$ http --auth=user:password GET httpbin.org/basic-auth/user/password
|
|
||||||
|
|
||||||
>>> r = http('-a', 'user:pw', 'httpbin.org/basic-auth/user/pw')
|
|
||||||
>>> type(r) == StrCLIResponse
|
|
||||||
True
|
|
||||||
>>> r.exit_status
|
|
||||||
0
|
|
||||||
>>> r.stderr
|
|
||||||
''
|
|
||||||
>>> 'HTTP/1.1 200 OK' in r
|
|
||||||
True
|
|
||||||
>>> r.json == {'authenticated': True, 'user': 'user'}
|
|
||||||
True
|
|
||||||
|
|
||||||
|
|
||||||
"""
|
|
||||||
error_exit_ok = kwargs.pop('error_exit_ok', False)
|
|
||||||
env = kwargs.get('env')
|
|
||||||
if not env:
|
|
||||||
env = kwargs['env'] = TestEnvironment()
|
|
||||||
|
|
||||||
stdout = env.stdout
|
|
||||||
stderr = env.stderr
|
|
||||||
|
|
||||||
args = list(args)
|
|
||||||
if '--debug' not in args and '--traceback' not in args:
|
|
||||||
args = ['--traceback'] + args
|
|
||||||
|
|
||||||
def dump_stderr():
|
|
||||||
stderr.seek(0)
|
|
||||||
sys.stderr.write(stderr.read())
|
|
||||||
|
|
||||||
try:
|
|
||||||
try:
|
try:
|
||||||
exit_status = main(args=args, **kwargs)
|
self.cleanup()
|
||||||
if '--download' in args:
|
|
||||||
# Let the progress reporter thread finish.
|
|
||||||
time.sleep(.5)
|
|
||||||
except SystemExit:
|
|
||||||
if error_exit_ok:
|
|
||||||
exit_status = httpie.ExitStatus.ERROR
|
|
||||||
else:
|
|
||||||
dump_stderr()
|
|
||||||
raise
|
|
||||||
except Exception:
|
except Exception:
|
||||||
stderr.seek(0)
|
pass
|
||||||
sys.stderr.write(stderr.read())
|
|
||||||
raise
|
|
||||||
else:
|
|
||||||
if exit_status != httpie.ExitStatus.OK and not error_exit_ok:
|
|
||||||
dump_stderr()
|
|
||||||
raise Exception('Unexpected exit status: %s', exit_status)
|
|
||||||
|
|
||||||
stdout.seek(0)
|
|
||||||
stderr.seek(0)
|
|
||||||
output = stdout.read()
|
|
||||||
try:
|
|
||||||
output = output.decode('utf8')
|
|
||||||
except UnicodeDecodeError:
|
|
||||||
# noinspection PyArgumentList
|
|
||||||
r = BytesCLIResponse(output)
|
|
||||||
else:
|
|
||||||
# noinspection PyArgumentList
|
|
||||||
r = StrCLIResponse(output)
|
|
||||||
r.stderr = stderr.read()
|
|
||||||
r.exit_status = exit_status
|
|
||||||
|
|
||||||
if r.exit_status != httpie.ExitStatus.OK:
|
|
||||||
sys.stderr.write(r.stderr)
|
|
||||||
|
|
||||||
return r
|
|
||||||
|
|
||||||
finally:
|
|
||||||
stdout.close()
|
|
||||||
stderr.close()
|
|
||||||
|
|
||||||
|
|
||||||
class BaseCLIResponse(object):
|
class BaseCLIResponse(object):
|
||||||
@ -237,5 +137,120 @@ class StrCLIResponse(str, BaseCLIResponse):
|
|||||||
return self._json
|
return self._json
|
||||||
|
|
||||||
|
|
||||||
def mk_config_dir():
|
class ExitStatusError(Exception):
|
||||||
return tempfile.mkdtemp(prefix='httpie_test_config_dir_')
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def http(*args, **kwargs):
|
||||||
|
# noinspection PyUnresolvedReferences
|
||||||
|
"""
|
||||||
|
Run HTTPie and capture stderr/out and exit status.
|
||||||
|
|
||||||
|
Invoke `httpie.core.main()` with `args` and `kwargs`,
|
||||||
|
and return a `CLIResponse` subclass instance.
|
||||||
|
|
||||||
|
The return value is either a `StrCLIResponse`, or `BytesCLIResponse`
|
||||||
|
if unable to decode the output.
|
||||||
|
|
||||||
|
The response has the following attributes:
|
||||||
|
|
||||||
|
`stdout` is represented by the instance itself (print r)
|
||||||
|
`stderr`: text written to stderr
|
||||||
|
`exit_status`: the exit status
|
||||||
|
`json`: decoded JSON (if possible) or `None`
|
||||||
|
|
||||||
|
Exceptions are propagated.
|
||||||
|
|
||||||
|
If you pass ``error_exit_ok=True``, then error exit statuses
|
||||||
|
won't result into an exception.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
$ http --auth=user:password GET httpbin.org/basic-auth/user/password
|
||||||
|
|
||||||
|
>>> httpbin = getfixture('httpbin')
|
||||||
|
>>> r = http('-a', 'user:pw', httpbin.url + '/basic-auth/user/pw')
|
||||||
|
>>> type(r) == StrCLIResponse
|
||||||
|
True
|
||||||
|
>>> r.exit_status
|
||||||
|
0
|
||||||
|
>>> r.stderr
|
||||||
|
''
|
||||||
|
>>> 'HTTP/1.1 200 OK' in r
|
||||||
|
True
|
||||||
|
>>> r.json == {'authenticated': True, 'user': 'user'}
|
||||||
|
True
|
||||||
|
|
||||||
|
"""
|
||||||
|
error_exit_ok = kwargs.pop('error_exit_ok', False)
|
||||||
|
env = kwargs.get('env')
|
||||||
|
if not env:
|
||||||
|
env = kwargs['env'] = MockEnvironment()
|
||||||
|
|
||||||
|
stdout = env.stdout
|
||||||
|
stderr = env.stderr
|
||||||
|
|
||||||
|
args = list(args)
|
||||||
|
args_with_config_defaults = args + env.config.default_options
|
||||||
|
add_to_args = []
|
||||||
|
if '--debug' not in args_with_config_defaults:
|
||||||
|
if not error_exit_ok and '--traceback' not in args_with_config_defaults:
|
||||||
|
add_to_args.append('--traceback')
|
||||||
|
if not any('--timeout' in arg for arg in args_with_config_defaults):
|
||||||
|
add_to_args.append('--timeout=3')
|
||||||
|
args = add_to_args + args
|
||||||
|
|
||||||
|
def dump_stderr():
|
||||||
|
stderr.seek(0)
|
||||||
|
sys.stderr.write(stderr.read())
|
||||||
|
|
||||||
|
try:
|
||||||
|
try:
|
||||||
|
exit_status = main(args=args, **kwargs)
|
||||||
|
if '--download' in args:
|
||||||
|
# Let the progress reporter thread finish.
|
||||||
|
time.sleep(.5)
|
||||||
|
except SystemExit:
|
||||||
|
if error_exit_ok:
|
||||||
|
exit_status = ExitStatus.ERROR
|
||||||
|
else:
|
||||||
|
dump_stderr()
|
||||||
|
raise
|
||||||
|
except Exception:
|
||||||
|
stderr.seek(0)
|
||||||
|
sys.stderr.write(stderr.read())
|
||||||
|
raise
|
||||||
|
else:
|
||||||
|
if not error_exit_ok and exit_status != ExitStatus.SUCCESS:
|
||||||
|
dump_stderr()
|
||||||
|
raise ExitStatusError(
|
||||||
|
'httpie.core.main() unexpectedly returned'
|
||||||
|
' a non-zero exit status: {0} ({1})'.format(
|
||||||
|
exit_status,
|
||||||
|
EXIT_STATUS_LABELS[exit_status]
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
stdout.seek(0)
|
||||||
|
stderr.seek(0)
|
||||||
|
output = stdout.read()
|
||||||
|
try:
|
||||||
|
output = output.decode('utf8')
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
# noinspection PyArgumentList
|
||||||
|
r = BytesCLIResponse(output)
|
||||||
|
else:
|
||||||
|
# noinspection PyArgumentList
|
||||||
|
r = StrCLIResponse(output)
|
||||||
|
r.stderr = stderr.read()
|
||||||
|
r.exit_status = exit_status
|
||||||
|
|
||||||
|
if r.exit_status != ExitStatus.SUCCESS:
|
||||||
|
sys.stderr.write(r.stderr)
|
||||||
|
|
||||||
|
return r
|
||||||
|
|
||||||
|
finally:
|
||||||
|
stdout.close()
|
||||||
|
stderr.close()
|
||||||
|
env.cleanup()
|
||||||
|
20
tox.ini
20
tox.ini
@ -1,18 +1,26 @@
|
|||||||
# Tox (http://tox.testrun.org/) is a tool for running tests
|
# Tox (http://tox.testrun.org/) is a tool for running tests
|
||||||
# in multiple virtualenvs.
|
# in multiple virtualenvs. See ./CONTRIBUTING.rst
|
||||||
# Run:
|
|
||||||
# $ pip install -r requirements-dev.txt
|
|
||||||
# $ tox
|
|
||||||
|
|
||||||
|
|
||||||
[tox]
|
[tox]
|
||||||
envlist = py26, py27, py34, pypy
|
# pypy3 currently fails because of a Flask issue
|
||||||
|
envlist = py27, py37, pypy
|
||||||
|
|
||||||
|
|
||||||
[testenv]
|
[testenv]
|
||||||
deps =
|
deps =
|
||||||
|
mock
|
||||||
pytest
|
pytest
|
||||||
pytest-httpbin>=0.0.6
|
pytest-httpbin>=0.0.6
|
||||||
|
|
||||||
|
|
||||||
commands =
|
commands =
|
||||||
py.test --verbose --doctest-modules --basetemp={envtmpdir} {posargs:./tests ./httpie}
|
# NOTE: the order of the directories in posargs seems to matter.
|
||||||
|
# When changed, then many ImportMismatchError exceptions occurrs.
|
||||||
|
py.test \
|
||||||
|
--verbose \
|
||||||
|
--doctest-modules \
|
||||||
|
{posargs:./httpie ./tests}
|
||||||
|
|
||||||
|
[testenv:py27-osx-builtin]
|
||||||
|
basepython = /usr/bin/python2.7
|
||||||
|
Reference in New Issue
Block a user