test_main.py
This file
Path
tests/test_main.py
187 lines · 5.4 KiB · Python
Last changed by
Drop private imports from test_main.py (#2574)Florimond Mancabefore adoption78d381f
History of this fileTake it
The walk stopped after 200 commits: lines older than a8dd079 point at it.
Raise TypeError if content is passed a 'dict' instance. (#2495)Tom Christiebefore adoptiona8dd079
lines 1–18why, 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()Drop private imports from test_main.py (#2574)Florimond Mancabefore adoption78d381f
line 19why, one step back19 result = runner.invoke(httpx.main, ["--help"])Raise TypeError if content is passed a 'dict' instance. (#2495)Tom Christiebefore adoptiona8dd079
lines 20–26why, one step back20 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()Drop private imports from test_main.py (#2574)Florimond Mancabefore adoption78d381f
line 27why, one step back27 result = runner.invoke(httpx.main, [url])Raise TypeError if content is passed a 'dict' instance. (#2495)Tom Christiebefore adoptiona8dd079
lines 28–41why, one step back28 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()Drop private imports from test_main.py (#2574)Florimond Mancabefore adoption78d381f
line 42why, one step back42 result = runner.invoke(httpx.main, [url])Raise TypeError if content is passed a 'dict' instance. (#2495)Tom Christiebefore adoptiona8dd079
lines 43–59why, one step back43 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!"Drop private imports from test_main.py (#2574)Florimond Mancabefore adoption78d381f
line 60why, one step back60 result = runner.invoke(httpx.main, [url, "-c", content])Raise TypeError if content is passed a 'dict' instance. (#2495)Tom Christiebefore adoptiona8dd079
lines 61–74why, one step back61 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()Drop private imports from test_main.py (#2574)Florimond Mancabefore adoption78d381f
line 75why, one step back75 result = runner.invoke(httpx.main, [url])Raise TypeError if content is passed a 'dict' instance. (#2495)Tom Christiebefore adoptiona8dd079
lines 76–88why, one step back76 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()Drop private imports from test_main.py (#2574)Florimond Mancabefore adoption78d381f
line 89why, one step back89 result = runner.invoke(httpx.main, [url, "--follow-redirects"])Raise TypeError if content is passed a 'dict' instance. (#2495)Tom Christiebefore adoptiona8dd079
lines 90–108why, one step back90 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()Drop private imports from test_main.py (#2574)Florimond Mancabefore adoption78d381f
line 109why, one step back109 result = runner.invoke(httpx.main, [url, "-m", "POST", "-j", '{"hello": "world"}'])Raise TypeError if content is passed a 'dict' instance. (#2495)Tom Christiebefore adoptiona8dd079
lines 110–123why, one step back110 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 "",117 '{"hello": "world"}',118 ]119120121def test_verbose(server):122 url = str(server.url)123 runner = CliRunner()Drop private imports from test_main.py (#2574)Florimond Mancabefore adoption78d381f
line 124why, one step back124 result = runner.invoke(httpx.main, [url, "-v"])Raise TypeError if content is passed a 'dict' instance. (#2495)Tom Christiebefore adoptiona8dd079
lines 125–147why, one step back125 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: */*",132 "Accept-Encoding: gzip, deflate, br",133 "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()Drop private imports from test_main.py (#2574)Florimond Mancabefore adoption78d381f
line 148why, one step back148 result = runner.invoke(httpx.main, [url, "-v", "--auth", "username", "password"])Raise TypeError if content is passed a 'dict' instance. (#2495)Tom Christiebefore adoptiona8dd079
lines 149–174why, one step back149 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: */*",157 "Accept-Encoding: gzip, deflate, br",158 "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():Drop private imports from test_main.py (#2574)Florimond Mancabefore adoption78d381f
line 175why, one step back175 runner.invoke(httpx.main, [url, "--download", "index.txt"])Raise TypeError if content is passed a 'dict' instance. (#2495)Tom Christiebefore adoptiona8dd079
lines 176–182why, one step back176 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()Drop private imports from test_main.py (#2574)Florimond Mancabefore adoption78d381f
line 183why, one step back183 result = runner.invoke(httpx.main, ["invalid://example.org"])Raise TypeError if content is passed a 'dict' instance. (#2495)Tom Christiebefore adoptiona8dd079
lines 184–187why, one step back184 assert result.exit_code == 1185 assert splitlines(result.output) == [186 "UnsupportedProtocol: Request URL has an unsupported protocol 'invalid://'.",187 ]