test_main.py
This file
Path
tests/test_main.py
187 lines · 5.4 KiB · Python
Last changed by
Ensure JSON representation is compact. #3363 (#3367)BERRADA-Omarbefore adoption9fd6f0c
History of this fileTake it
The walk stopped after 200 commits: lines older than 1b8187c point at it.
Bump pytest from 7.2.2 to 7.3.1 (#2731)dependabot[bot]before adoption1b8187c
lines 1–116why, one step back1import os2import typing34from click.testing import CliRunner56import httpx789def splitlines(output: str) -> typing.Iterable[str]:10 return [line.strip() for line in output.splitlines()]111213def remove_date_header(lines: typing.Iterable[str]) -> typing.Iterable[str]:14 return [line for line in lines if not line.startswith("date:")]151617def test_help():18 runner = CliRunner()19 result = runner.invoke(httpx.main, ["--help"])20 assert result.exit_code == 021 assert "A next generation HTTP client." in result.output222324def test_get(server):25 url = str(server.url)26 runner = CliRunner()27 result = runner.invoke(httpx.main, [url])28 assert result.exit_code == 029 assert remove_date_header(splitlines(result.output)) == [30 "HTTP/1.1 200 OK",31 "server: uvicorn",32 "content-type: text/plain",33 "Transfer-Encoding: chunked",34 "",35 "Hello, world!",36 ]373839def test_json(server):40 url = str(server.url.copy_with(path="/json"))41 runner = CliRunner()42 result = runner.invoke(httpx.main, [url])43 assert result.exit_code == 044 assert remove_date_header(splitlines(result.output)) == [45 "HTTP/1.1 200 OK",46 "server: uvicorn",47 "content-type: application/json",48 "Transfer-Encoding: chunked",49 "",50 "{",51 '"Hello": "world!"',52 "}",53 ]545556def test_binary(server):57 url = str(server.url.copy_with(path="/echo_binary"))58 runner = CliRunner()59 content = "Hello, world!"60 result = runner.invoke(httpx.main, [url, "-c", content])61 assert result.exit_code == 062 assert remove_date_header(splitlines(result.output)) == [63 "HTTP/1.1 200 OK",64 "server: uvicorn",65 "content-type: application/octet-stream",66 "Transfer-Encoding: chunked",67 "",68 f"<{len(content)} bytes of binary data>",69 ]707172def test_redirects(server):73 url = str(server.url.copy_with(path="/redirect_301"))74 runner = CliRunner()75 result = runner.invoke(httpx.main, [url])76 assert result.exit_code == 177 assert remove_date_header(splitlines(result.output)) == [78 "HTTP/1.1 301 Moved Permanently",79 "server: uvicorn",80 "location: /",81 "Transfer-Encoding: chunked",82 "",83 ]848586def test_follow_redirects(server):87 url = str(server.url.copy_with(path="/redirect_301"))88 runner = CliRunner()89 result = runner.invoke(httpx.main, [url, "--follow-redirects"])90 assert result.exit_code == 091 assert remove_date_header(splitlines(result.output)) == [92 "HTTP/1.1 301 Moved Permanently",93 "server: uvicorn",94 "location: /",95 "Transfer-Encoding: chunked",96 "",97 "HTTP/1.1 200 OK",98 "server: uvicorn",99 "content-type: text/plain",100 "Transfer-Encoding: chunked",101 "",102 "Hello, world!",103 ]104105106def test_post(server):107 url = str(server.url.copy_with(path="/echo_body"))108 runner = CliRunner()109 result = runner.invoke(httpx.main, [url, "-m", "POST", "-j", '{"hello": "world"}'])110 assert result.exit_code == 0111 assert remove_date_header(splitlines(result.output)) == [112 "HTTP/1.1 200 OK",113 "server: uvicorn",114 "content-type: text/plain",115 "Transfer-Encoding: chunked",116 "",Ensure JSON representation is compact. #3363 (#3367)BERRADA-Omarbefore adoption9fd6f0c
line 117why, one step back117 '{"hello":"world"}',Bump pytest from 7.2.2 to 7.3.1 (#2731)dependabot[bot]before adoption1b8187c
lines 118–131why, one step back118 ]119120121def test_verbose(server):122 url = str(server.url)123 runner = CliRunner()124 result = runner.invoke(httpx.main, [url, "-v"])125 assert result.exit_code == 0126 assert remove_date_header(splitlines(result.output)) == [127 "* Connecting to '127.0.0.1'",128 "* Connected to '127.0.0.1' on port 8000",129 "GET / HTTP/1.1",130 f"Host: {server.url.netloc.decode('ascii')}",131 "Accept: */*",Add support for zstd decoding (#3139)Michiel W. Beijenbefore adoption392dbe4
line 132why, one step back132 "Accept-Encoding: gzip, deflate, br, zstd",Bump pytest from 7.2.2 to 7.3.1 (#2731)dependabot[bot]before adoption1b8187c
lines 133–156why, one step back133 "Connection: keep-alive",134 f"User-Agent: python-httpx/{httpx.__version__}",135 "",136 "HTTP/1.1 200 OK",137 "server: uvicorn",138 "content-type: text/plain",139 "Transfer-Encoding: chunked",140 "",141 "Hello, world!",142 ]143144145def test_auth(server):146 url = str(server.url)147 runner = CliRunner()148 result = runner.invoke(httpx.main, [url, "-v", "--auth", "username", "password"])149 print(result.output)150 assert result.exit_code == 0151 assert remove_date_header(splitlines(result.output)) == [152 "* Connecting to '127.0.0.1'",153 "* Connected to '127.0.0.1' on port 8000",154 "GET / HTTP/1.1",155 f"Host: {server.url.netloc.decode('ascii')}",156 "Accept: */*",Add support for zstd decoding (#3139)Michiel W. Beijenbefore adoption392dbe4
line 157why, one step back157 "Accept-Encoding: gzip, deflate, br, zstd",Bump pytest from 7.2.2 to 7.3.1 (#2731)dependabot[bot]before adoption1b8187c
lines 158–187why, one step back158 "Connection: keep-alive",159 f"User-Agent: python-httpx/{httpx.__version__}",160 "Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=",161 "",162 "HTTP/1.1 200 OK",163 "server: uvicorn",164 "content-type: text/plain",165 "Transfer-Encoding: chunked",166 "",167 "Hello, world!",168 ]169170171def test_download(server):172 url = str(server.url)173 runner = CliRunner()174 with runner.isolated_filesystem():175 runner.invoke(httpx.main, [url, "--download", "index.txt"])176 assert os.path.exists("index.txt")177 with open("index.txt", "r") as input_file:178 assert input_file.read() == "Hello, world!"179180181def test_errors():182 runner = CliRunner()183 result = runner.invoke(httpx.main, ["invalid://example.org"])184 assert result.exit_code == 1185 assert splitlines(result.output) == [186 "UnsupportedProtocol: Request URL has an unsupported protocol 'invalid://'.",187 ]