File: test_redirects.py

package info (click to toggle)
httpie 3.2.4-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,904 kB
  • sloc: python: 13,760; xml: 162; makefile: 141; ruby: 79; sh: 32
file content (116 lines) | stat: -rw-r--r-- 3,537 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
"""High-level tests."""
import pytest

from httpie.compat import is_windows
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


# <https://developer.mozilla.org/en-US/docs/Web/HTTP/Redirections>
REDIRECTS_WITH_METHOD_BODY_PRESERVED = [307, 308]


def test_follow_all_redirects_shown(httpbin):
    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


@pytest.mark.parametrize('follow_flag', ['--follow', '-F'])
def test_follow_without_all_redirects_hidden(httpbin, follow_flag):
    r = http(follow_flag, httpbin + '/redirect/2')
    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):
    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',
             httpbin + '/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 + '/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 + '/redirect/3',
        tolerate_error_exit_status=True,
    )
    assert r.exit_status == ExitStatus.ERROR_TOO_MANY_REDIRECTS


@pytest.mark.skipif(is_windows, reason='occasionally fails w/ ConnectionError for no apparent reason')
@pytest.mark.parametrize('status_code', REDIRECTS_WITH_METHOD_BODY_PRESERVED)
def test_follow_redirect_with_repost(httpbin, status_code):
    r = http(
        '--follow',
        httpbin + '/redirect-to',
        'A:A',
        'A:B',
        'B:B',
        f'url=={httpbin}/post',
        f'status_code=={status_code}',
        '@' + FILE_PATH_ARG,
    )
    assert HTTP_OK in r
    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')
@pytest.mark.parametrize('status_code', REDIRECTS_WITH_METHOD_BODY_PRESERVED)
def test_verbose_follow_redirect_with_repost(httpbin, status_code):
    r = http(
        '--follow',
        '--verbose',
        httpbin + '/redirect-to',
        'A:A',
        'A:B',
        'B:B',
        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,
    ])