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
1import os2import typing34from click.testing import CliRunner56import httpx7from httpx import main89if 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: F811121314def splitlines(output: str) -> typing.Iterable[str]:15 return [line.strip() for line in output.splitlines()]161718def remove_date_header(lines: typing.Iterable[str]) -> typing.Iterable[str]:19 return [line for line in lines if not line.startswith("date:")]202122def test_help():23 runner = CliRunner()24 result = runner.invoke(main, ["--help"])25 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()32 result = runner.invoke(main, [url])33 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()47 result = runner.invoke(main, [url])48 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 ]596061def test_binary(server):62 url = str(server.url.copy_with(path="/echo_binary"))63 runner = CliRunner()64 content = "Hello, world!"65 result = runner.invoke(main, [url, "-c", content])66 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 ]757677def test_redirects(server):78 url = str(server.url.copy_with(path="/redirect_301"))79 runner = CliRunner()80 result = runner.invoke(main, [url])81 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",87 "",88 ]899091def test_follow_redirects(server):92 url = str(server.url.copy_with(path="/redirect_301"))93 runner = CliRunner()94 result = runner.invoke(main, [url, "--follow-redirects"])95 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()114 result = runner.invoke(main, [url, "-m", "POST", "-j", '{"hello": "world"}'])115 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()129 result = runner.invoke(main, [url, "-v"])130 assert result.exit_code == 0131 assert remove_date_header(splitlines(result.output)) == [132 "* Connecting to '127.0.0.1'",133 "* Connected to '127.0.0.1' on port 8000",134 "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()153 result = runner.invoke(main, [url, "-v", "--auth", "username", "password"])154 print(result.output)155 assert result.exit_code == 0156 assert remove_date_header(splitlines(result.output)) == [157 "* Connecting to '127.0.0.1'",158 "* Connected to '127.0.0.1' on port 8000",159 "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():180 runner.invoke(main, [url, "--download", "index.txt"])181 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()188 result = runner.invoke(main, ["invalid://example.org"])189 assert result.exit_code == 1190 assert splitlines(result.output) == [191 "UnsupportedProtocol: Request URL has an unsupported protocol 'invalid://'.",192 ]