test_main.py
This file
Path
tests/test_main.py
192 lines · 5.5 KiB · Python
Last changed by
Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c
History of this fileTake it
Add cli support (#1855)Tom Christiebefore adoptionee9250d
line 1why, one step back1import osTyping: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c
line 2why, one step back2import typingAdd cli support (#1855)Tom Christiebefore adoptionee9250d
lines 3–6why, one step back34from click.testing import CliRunner56import httpxTyping: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c
line 7why, one step back7from httpx import mainAdd cli support (#1855)Tom Christiebefore adoptionee9250d
line 8why, one step back8Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c
lines 9–11why, one step back9if typing.TYPE_CHECKING: # pragma: no cover10 # don't let mypy be misled by the fallback defined in httpx/__init__.py11 from httpx._main import main # noqa: F811Add cli support (#1855)Tom Christiebefore adoptionee9250d
line 12why, one step back12Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c
lines 13–14why, one step back1314def splitlines(output: str) -> typing.Iterable[str]:Add cli support (#1855)Tom Christiebefore adoptionee9250d
lines 15–17why, one step back15 return [line.strip() for line in output.splitlines()]1617Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c
line 18why, one step back18def remove_date_header(lines: typing.Iterable[str]) -> typing.Iterable[str]:Add cli support (#1855)Tom Christiebefore adoptionee9250d
lines 19–23why, one step back19 return [line for line in lines if not line.startswith("date:")]202122def test_help():23 runner = CliRunner()Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c
line 24why, one step back24 result = runner.invoke(main, ["--help"])Add cli support (#1855)Tom Christiebefore adoptionee9250d
lines 25–31why, one step back25 assert result.exit_code == 026 assert "A next generation HTTP client." in result.output272829def test_get(server):30 url = str(server.url)31 runner = CliRunner()Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c
line 32why, one step back32 result = runner.invoke(main, [url])Add cli support (#1855)Tom Christiebefore adoptionee9250d
lines 33–46why, one step back33 assert result.exit_code == 034 assert remove_date_header(splitlines(result.output)) == [35 "HTTP/1.1 200 OK",36 "server: uvicorn",37 "content-type: text/plain",38 "Transfer-Encoding: chunked",39 "",40 "Hello, world!",41 ]424344def test_json(server):45 url = str(server.url.copy_with(path="/json"))46 runner = CliRunner()Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c
line 47why, one step back47 result = runner.invoke(main, [url])Add cli support (#1855)Tom Christiebefore adoptionee9250d
lines 48–60why, one step back48 assert result.exit_code == 049 assert remove_date_header(splitlines(result.output)) == [50 "HTTP/1.1 200 OK",51 "server: uvicorn",52 "content-type: application/json",53 "Transfer-Encoding: chunked",54 "",55 "{",56 '"Hello": "world!"',57 "}",58 ]5960Suppress binary output on the command line (#2049) (#2076)waterfountain1996before adoption4c30748
lines 61–64why, one step back61def test_binary(server):62 url = str(server.url.copy_with(path="/echo_binary"))63 runner = CliRunner()64 content = "Hello, world!"Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c
line 65why, one step back65 result = runner.invoke(main, [url, "-c", content])Suppress binary output on the command line (#2049) (#2076)waterfountain1996before adoption4c30748
lines 66–76why, one step back66 assert result.exit_code == 067 assert remove_date_header(splitlines(result.output)) == [68 "HTTP/1.1 200 OK",69 "server: uvicorn",70 "content-type: application/octet-stream",71 "Transfer-Encoding: chunked",72 "",73 f"<{len(content)} bytes of binary data>",74 ]7576Add cli support (#1855)Tom Christiebefore adoptionee9250d
lines 77–79why, one step back77def test_redirects(server):78 url = str(server.url.copy_with(path="/redirect_301"))79 runner = CliRunner()Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c
line 80why, one step back80 result = runner.invoke(main, [url])Add cli support (#1855)Tom Christiebefore adoptionee9250d
lines 81–86why, one step back81 assert result.exit_code == 182 assert remove_date_header(splitlines(result.output)) == [83 "HTTP/1.1 301 Moved Permanently",84 "server: uvicorn",85 "location: /",86 "Transfer-Encoding: chunked",Version 0.21 (#1935)Tom Christiebefore adoption61188fe
line 87why, one step back87 "",Add cli support (#1855)Tom Christiebefore adoptionee9250d
lines 88–93why, one step back88 ]899091def test_follow_redirects(server):92 url = str(server.url.copy_with(path="/redirect_301"))93 runner = CliRunner()Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c
line 94why, one step back94 result = runner.invoke(main, [url, "--follow-redirects"])Add cli support (#1855)Tom Christiebefore adoptionee9250d
lines 95–113why, one step back95 assert result.exit_code == 096 assert remove_date_header(splitlines(result.output)) == [97 "HTTP/1.1 301 Moved Permanently",98 "server: uvicorn",99 "location: /",100 "Transfer-Encoding: chunked",101 "",102 "HTTP/1.1 200 OK",103 "server: uvicorn",104 "content-type: text/plain",105 "Transfer-Encoding: chunked",106 "",107 "Hello, world!",108 ]109110111def test_post(server):112 url = str(server.url.copy_with(path="/echo_body"))113 runner = CliRunner()Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c
line 114why, one step back114 result = runner.invoke(main, [url, "-m", "POST", "-j", '{"hello": "world"}'])Add cli support (#1855)Tom Christiebefore adoptionee9250d
lines 115–128why, one step back115 assert result.exit_code == 0116 assert remove_date_header(splitlines(result.output)) == [117 "HTTP/1.1 200 OK",118 "server: uvicorn",119 "content-type: text/plain",120 "Transfer-Encoding: chunked",121 "",122 '{"hello": "world"}',123 ]124125126def test_verbose(server):127 url = str(server.url)128 runner = CliRunner()Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c
line 129why, one step back129 result = runner.invoke(main, [url, "-v"])Add cli support (#1855)Tom Christiebefore adoptionee9250d
lines 130–131why, one step back130 assert result.exit_code == 0131 assert remove_date_header(splitlines(result.output)) == [Version 0.21 (#1935)Tom Christiebefore adoption61188fe
lines 132–133why, one step back132 "* Connecting to '127.0.0.1'",133 "* Connected to '127.0.0.1' on port 8000",Add cli support (#1855)Tom Christiebefore adoptionee9250d
lines 134–152why, one step back134 "GET / HTTP/1.1",135 f"Host: {server.url.netloc.decode('ascii')}",136 "Accept: */*",137 "Accept-Encoding: gzip, deflate, br",138 "Connection: keep-alive",139 f"User-Agent: python-httpx/{httpx.__version__}",140 "",141 "HTTP/1.1 200 OK",142 "server: uvicorn",143 "content-type: text/plain",144 "Transfer-Encoding: chunked",145 "",146 "Hello, world!",147 ]148149150def test_auth(server):151 url = str(server.url)152 runner = CliRunner()Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c
line 153why, one step back153 result = runner.invoke(main, [url, "-v", "--auth", "username", "password"])Add cli support (#1855)Tom Christiebefore adoptionee9250d
lines 154–156why, one step back154 print(result.output)155 assert result.exit_code == 0156 assert remove_date_header(splitlines(result.output)) == [Version 0.21 (#1935)Tom Christiebefore adoption61188fe
lines 157–158why, one step back157 "* Connecting to '127.0.0.1'",158 "* Connected to '127.0.0.1' on port 8000",Add cli support (#1855)Tom Christiebefore adoptionee9250d
lines 159–179why, one step back159 "GET / HTTP/1.1",160 f"Host: {server.url.netloc.decode('ascii')}",161 "Accept: */*",162 "Accept-Encoding: gzip, deflate, br",163 "Connection: keep-alive",164 f"User-Agent: python-httpx/{httpx.__version__}",165 "Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=",166 "",167 "HTTP/1.1 200 OK",168 "server: uvicorn",169 "content-type: text/plain",170 "Transfer-Encoding: chunked",171 "",172 "Hello, world!",173 ]174175176def test_download(server):177 url = str(server.url)178 runner = CliRunner()179 with runner.isolated_filesystem():Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c
line 180why, one step back180 runner.invoke(main, [url, "--download", "index.txt"])Add cli support (#1855)Tom Christiebefore adoptionee9250d
lines 181–187why, one step back181 assert os.path.exists("index.txt")182 with open("index.txt", "r") as input_file:183 assert input_file.read() == "Hello, world!"184185186def test_errors():187 runner = CliRunner()Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c
line 188why, one step back188 result = runner.invoke(main, ["invalid://example.org"])Add cli support (#1855)Tom Christiebefore adoptionee9250d
lines 189–192why, one step back189 assert result.exit_code == 1190 assert splitlines(result.output) == [191 "UnsupportedProtocol: Request URL has an unsupported protocol 'invalid://'.",192 ]