highlevel tests import pytest from httpie compat import iswindows from httpie status import exitstatus from fixtures import filepatharg filecontent from utils import http httpok from utils matching import assertoutputmatches expect expectsequence https developer mozilla orgenusdocswebhttpredirections redirectswithmethodbodypreserved 307 308 def testfollowallredirectsshownhttpbin r http follow all httpbin url redirect2 assert r count http1 1 3 assert r count http1 1 302 found 2 assert httpok in r pytest mark parametrize followflag follow f def testfollowwithoutallredirectshiddenhttpbin followflag r httpfollowflag httpbin url redirect2 assert r count http1 1 1 assert httpok in r pytest mark xfailtrue reasonhttps github comhttpiecliissues1082 def testfollowoutputoptionsusedforredirectshttpbin r http follow printh httpbin url redirect2 assert r count get 1 assert httpok not in r def testfollowalloutputoptionsusedforredirectshttpbin r http checkstatus follow all printh httpbin url redirect2 assert r count get 3 assert httpok not in r def testfollowredirectoutputoptionshttpbin r http checkstatus follow all printh historyprinth httpbin url redirect2 assert r count get 2 assert http1 1 302 found not in r assert httpok in r def testmaxredirectshttpbin r http maxredirects1 follow httpbin url redirect3 tolerateerrorexitstatustrue assert r exitstatus exitstatus errortoomanyredirects pytest mark skipifiswindows reason occasionally fails w connectionerror for no apparent reason pytest mark parametrize statuscode redirectswithmethodbodypreserved def testfollowredirectwithreposthttpbin statuscode r http follow httpbin url redirectto a a a b b b f urlhttpbin urlpost f statuscodestatuscode filepatharg assert httpok in r assert filecontent in r assert r json headers a a b assert r json headers b b pytest mark skipifiswindows reason occasionally fails w connectionerror for no apparent reason pytest mark parametrize statuscode redirectswithmethodbodypreserved def testverbosefollowredirectwithreposthttpbin statuscode r http follow verbose httpbin url redirectto a a a b b b f urlhttpbin urlpost f statuscodestatuscode filepatharg assert f http1 1 statuscode in r assert a a in r assert a b in r assert b b in r assert r count post redirectto 1 assert r count post post 1 assert r countfilecontent 3 two requests final response contain it assert httpok in r assertoutputmatchesr expectsequence terminalrequest expect responseheaders expect separator expectsequence terminalexchange https developer mozilla org en us docs web http redirections 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 two requests final response contain it
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



REDIRECTS_WITH_METHOD_BODY_PRESERVED = [307, 308]


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


@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.url + '/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.url + '/redirect/2')
    assert r.count('GET /') == 3
    assert HTTP_OK not in r
















def test_max_redirects(httpbin):
    r = http(
        '--max-redirects=1',
        '--follow',
        httpbin.url + '/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.url + '/redirect-to',
        'A:A',
        'A:B',
        'B:B',
        f'url=={httpbin.url}/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.url + '/redirect-to',
        'A:A',
        'A:B',
        'B:B',
        f'url=={httpbin.url}/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  
    assert HTTP_OK in r
    assert_output_matches(r, [
        *ExpectSequence.TERMINAL_REQUEST,
        Expect.RESPONSE_HEADERS,
        Expect.SEPARATOR,
        *ExpectSequence.TERMINAL_EXCHANGE,
    ])