Skip to content
Myr

test_main.py

This file

Path

tests/test_main.py

170 lines · 4.8 KiB · Python

Last changed by

Version 0.21 (#1935)Tom Christiebefore adoption61188fe

History of this file

Take it

1import os23from click.testing import CliRunner45import httpx678def splitlines(output):9    return [line.strip() for line in output.splitlines()]101112def remove_date_header(lines):13    return [line for line in lines if not line.startswith("date:")]141516def test_help():17    runner = CliRunner()18    result = runner.invoke(httpx.main, ["--help"])19    assert result.exit_code == 020    assert "A next generation HTTP client." in result.output212223def test_get(server):24    url = str(server.url)25    runner = CliRunner()26    result = runner.invoke(httpx.main, [url])27    assert result.exit_code == 028    assert remove_date_header(splitlines(result.output)) == [29        "HTTP/1.1 200 OK",30        "server: uvicorn",31        "content-type: text/plain",32        "Transfer-Encoding: chunked",33        "",34        "Hello, world!",35    ]363738def test_json(server):39    url = str(server.url.copy_with(path="/json"))40    runner = CliRunner()41    result = runner.invoke(httpx.main, [url])42    assert result.exit_code == 043    assert remove_date_header(splitlines(result.output)) == [44        "HTTP/1.1 200 OK",45        "server: uvicorn",46        "content-type: application/json",47        "Transfer-Encoding: chunked",48        "",49        "{",50        '"Hello": "world!"',51        "}",52    ]535455def test_redirects(server):56    url = str(server.url.copy_with(path="/redirect_301"))57    runner = CliRunner()58    result = runner.invoke(httpx.main, [url])59    assert result.exit_code == 160    assert remove_date_header(splitlines(result.output)) == [61        "HTTP/1.1 301 Moved Permanently",62        "server: uvicorn",63        "location: /",64        "Transfer-Encoding: chunked",65        "",66    ]676869def test_follow_redirects(server):70    url = str(server.url.copy_with(path="/redirect_301"))71    runner = CliRunner()72    result = runner.invoke(httpx.main, [url, "--follow-redirects"])73    assert result.exit_code == 074    assert remove_date_header(splitlines(result.output)) == [75        "HTTP/1.1 301 Moved Permanently",76        "server: uvicorn",77        "location: /",78        "Transfer-Encoding: chunked",79        "",80        "HTTP/1.1 200 OK",81        "server: uvicorn",82        "content-type: text/plain",83        "Transfer-Encoding: chunked",84        "",85        "Hello, world!",86    ]878889def test_post(server):90    url = str(server.url.copy_with(path="/echo_body"))91    runner = CliRunner()92    result = runner.invoke(httpx.main, [url, "-m", "POST", "-j", '{"hello": "world"}'])93    assert result.exit_code == 094    assert remove_date_header(splitlines(result.output)) == [95        "HTTP/1.1 200 OK",96        "server: uvicorn",97        "content-type: text/plain",98        "Transfer-Encoding: chunked",99        "",100        '{"hello": "world"}',101    ]102103104def test_verbose(server):105    url = str(server.url)106    runner = CliRunner()107    result = runner.invoke(httpx.main, [url, "-v"])108    assert result.exit_code == 0109    assert remove_date_header(splitlines(result.output)) == [110        "* Connecting to '127.0.0.1'",111        "* Connected to '127.0.0.1' on port 8000",112        "GET / HTTP/1.1",113        f"Host: {server.url.netloc.decode('ascii')}",114        "Accept: */*",115        "Accept-Encoding: gzip, deflate, br",116        "Connection: keep-alive",117        f"User-Agent: python-httpx/{httpx.__version__}",118        "",119        "HTTP/1.1 200 OK",120        "server: uvicorn",121        "content-type: text/plain",122        "Transfer-Encoding: chunked",123        "",124        "Hello, world!",125    ]126127128def test_auth(server):129    url = str(server.url)130    runner = CliRunner()131    result = runner.invoke(httpx.main, [url, "-v", "--auth", "username", "password"])132    print(result.output)133    assert result.exit_code == 0134    assert remove_date_header(splitlines(result.output)) == [135        "* Connecting to '127.0.0.1'",136        "* Connected to '127.0.0.1' on port 8000",137        "GET / HTTP/1.1",138        f"Host: {server.url.netloc.decode('ascii')}",139        "Accept: */*",140        "Accept-Encoding: gzip, deflate, br",141        "Connection: keep-alive",142        f"User-Agent: python-httpx/{httpx.__version__}",143        "Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=",144        "",145        "HTTP/1.1 200 OK",146        "server: uvicorn",147        "content-type: text/plain",148        "Transfer-Encoding: chunked",149        "",150        "Hello, world!",151    ]152153154def test_download(server):155    url = str(server.url)156    runner = CliRunner()157    with runner.isolated_filesystem():158        runner.invoke(httpx.main, [url, "--download", "index.txt"])159        assert os.path.exists("index.txt")160        with open("index.txt", "r") as input_file:161            assert input_file.read() == "Hello, world!"162163164def test_errors():165    runner = CliRunner()166    result = runner.invoke(httpx.main, ["invalid://example.org"])167    assert result.exit_code == 1168    assert splitlines(result.output) == [169        "UnsupportedProtocol: Request URL has an unsupported protocol 'invalid://'.",170    ]