httpie-cli/tests/test_redirects.py

117 lines
3.5 KiB
Python
Raw Permalink Normal View History

"""High-level tests."""
2016-03-09 14:58:34 +01:00
import pytest
from httpie.compat import is_windows
2019-09-16 13:26:18 +02:00
from httpie.status import ExitStatus
from .fixtures import FILE_PATH_ARG, FILE_CONTENT
from .utils import http, HTTP_OK
from .utils.matching import assert_output_matches, Expect, ExpectSequence
2021-06-15 17:31:56 +02:00
# <https://developer.mozilla.org/en-US/docs/Web/HTTP/Redirections>
REDIRECTS_WITH_METHOD_BODY_PRESERVED = [307, 308]
def test_follow_all_redirects_shown(httpbin):
2024-03-04 18:12:18 +01:00
r = http('--follow', '--all', httpbin + '/redirect/2')
assert r.count('HTTP/1.1') == 3
assert r.count('HTTP/1.1 302 FOUND', 2)
assert HTTP_OK in r
2016-03-09 14:58:34 +01:00
@pytest.mark.parametrize('follow_flag', ['--follow', '-F'])
def test_follow_without_all_redirects_hidden(httpbin, follow_flag):
2024-03-04 18:12:18 +01:00
r = http(follow_flag, httpbin + '/redirect/2')
2016-02-29 07:31:27 +01:00
assert r.count('HTTP/1.1') == 1
assert HTTP_OK in r
@pytest.mark.xfail(True, reason="https://github.com/httpie/cli/issues/1082")
def test_follow_output_options_used_for_redirects(httpbin):
2024-03-04 18:12:18 +01:00
r = http('--follow', '--print=H', httpbin + '/redirect/2')
assert r.count('GET /') == 1
assert HTTP_OK not in r
def test_follow_all_output_options_used_for_redirects(httpbin):
r = http('--check-status',
'--follow',
'--all',
'--print=H',
2024-03-04 18:12:18 +01:00
httpbin + '/redirect/2')
assert r.count('GET /') == 3
assert HTTP_OK not in r
2021-06-15 17:31:56 +02:00
#
# def test_follow_redirect_output_options(httpbin):
# r = http('--check-status',
# '--follow',
# '--all',
# '--print=h',
# '--history-print=H',
2024-03-04 18:12:18 +01:00
# httpbin + '/redirect/2')
# assert r.count('GET /') == 2
# assert 'HTTP/1.1 302 FOUND' not in r
# assert HTTP_OK in r
#
2016-02-29 07:31:27 +01:00
def test_max_redirects(httpbin):
r = http(
'--max-redirects=1',
'--follow',
2024-03-04 18:12:18 +01:00
httpbin + '/redirect/3',
tolerate_error_exit_status=True,
)
2016-02-29 07:31:27 +01:00
assert r.exit_status == ExitStatus.ERROR_TOO_MANY_REDIRECTS
@pytest.mark.skipif(is_windows, reason='occasionally fails w/ ConnectionError for no apparent reason')
2021-06-15 17:31:56 +02:00
@pytest.mark.parametrize('status_code', REDIRECTS_WITH_METHOD_BODY_PRESERVED)
def test_follow_redirect_with_repost(httpbin, status_code):
r = http(
'--follow',
2024-03-04 18:12:18 +01:00
httpbin + '/redirect-to',
'A:A',
'A:B',
'B:B',
2024-03-04 18:12:18 +01:00
f'url=={httpbin}/post',
2021-06-15 17:31:56 +02:00
f'status_code=={status_code}',
'@' + FILE_PATH_ARG,
)
assert HTTP_OK in r
2021-06-15 17:31:56 +02:00
assert FILE_CONTENT in r
assert r.json['headers']['A'] == 'A,B'
assert r.json['headers']['B'] == 'B'
@pytest.mark.skipif(is_windows, reason='occasionally fails w/ ConnectionError for no apparent reason')
2021-06-15 17:31:56 +02:00
@pytest.mark.parametrize('status_code', REDIRECTS_WITH_METHOD_BODY_PRESERVED)
def test_verbose_follow_redirect_with_repost(httpbin, status_code):
r = http(
'--follow',
'--verbose',
2024-03-04 18:12:18 +01:00
httpbin + '/redirect-to',
'A:A',
'A:B',
'B:B',
2024-03-04 18:12:18 +01:00
f'url=={httpbin}/post',
f'status_code=={status_code}',
'@' + FILE_PATH_ARG,
)
assert f'HTTP/1.1 {status_code}' in r
assert 'A: A' in r
assert 'A: B' in r
assert 'B: B' in r
assert r.count('POST /redirect-to') == 1
assert r.count('POST /post') == 1
assert r.count(FILE_CONTENT) == 3 # two requests + final response contain it
assert HTTP_OK in r
assert_output_matches(r, [
*ExpectSequence.TERMINAL_REQUEST,
Expect.RESPONSE_HEADERS,
Expect.SEPARATOR,
*ExpectSequence.TERMINAL_EXCHANGE,
])