Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than 2146c75 point at it.

Allow rich-11 (#2057)Michał Górnybefore adoption2146c75

line 1why, one step back
1import os

Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c

line 2why, one step back
2import typing

Allow rich-11 (#2057)Michał Górnybefore adoption2146c75

lines 3–8why, one step back
3
4from click.testing import CliRunner56import httpx78

Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c

line 9why, one step back
9def splitlines(output: str) -> typing.Iterable[str]:

Allow rich-11 (#2057)Michał Górnybefore adoption2146c75

lines 10–12why, one step back
10 return [line.strip() for line in output.splitlines()]
1112

Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c

line 13why, one step back
13def remove_date_header(lines: typing.Iterable[str]) -> typing.Iterable[str]:

Allow rich-11 (#2057)Michał Górnybefore adoption2146c75

lines 14–18why, one step back
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 back
19 result = runner.invoke(httpx.main, ["--help"])

Allow rich-11 (#2057)Michał Górnybefore adoption2146c75

lines 20–26why, one step back
20 assert result.exit_code == 0
21 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 back
27 result = runner.invoke(httpx.main, [url])

Allow rich-11 (#2057)Michał Górnybefore adoption2146c75

lines 28–41why, one step back
28 assert result.exit_code == 0
29 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 back
42 result = runner.invoke(httpx.main, [url])

Allow rich-11 (#2057)Michał Górnybefore adoption2146c75

lines 43–55why, one step back
43 assert result.exit_code == 0
44 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 ]5455

Suppress binary output on the command line (#2049) (#2076)waterfountain1996before adoption4c30748

lines 56–59why, one step back
56def 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 back
60 result = runner.invoke(httpx.main, [url, "-c", content])

Suppress binary output on the command line (#2049) (#2076)waterfountain1996before adoption4c30748

lines 61–71why, one step back
61 assert result.exit_code == 0
62 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 ]7071

Allow rich-11 (#2057)Michał Górnybefore adoption2146c75

lines 72–74why, one step back
72def 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 back
75 result = runner.invoke(httpx.main, [url])

Allow rich-11 (#2057)Michał Górnybefore adoption2146c75

lines 76–88why, one step back
76 assert result.exit_code == 1
77 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 back
89 result = runner.invoke(httpx.main, [url, "--follow-redirects"])

Allow rich-11 (#2057)Michał Górnybefore adoption2146c75

lines 90–108why, one step back
90 assert result.exit_code == 0
91 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 back
109 result = runner.invoke(httpx.main, [url, "-m", "POST", "-j", '{"hello": "world"}'])

Allow rich-11 (#2057)Michał Górnybefore adoption2146c75

lines 110–123why, one step back
110 assert result.exit_code == 0
111 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 back
124 result = runner.invoke(httpx.main, [url, "-v"])

Allow rich-11 (#2057)Michał Górnybefore adoption2146c75

lines 125–147why, one step back
125 assert result.exit_code == 0
126 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 back
148 result = runner.invoke(httpx.main, [url, "-v", "--auth", "username", "password"])

Allow rich-11 (#2057)Michał Górnybefore adoption2146c75

lines 149–174why, one step back
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: */*",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 back
175 runner.invoke(httpx.main, [url, "--download", "index.txt"])

Allow rich-11 (#2057)Michał Górnybefore adoption2146c75

lines 176–182why, one step back
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()

Drop private imports from test_main.py (#2574)Florimond Mancabefore adoption78d381f

line 183why, one step back
183 result = runner.invoke(httpx.main, ["invalid://example.org"])

Allow rich-11 (#2057)Michał Górnybefore adoption2146c75

lines 184–187why, one step back
184 assert result.exit_code == 1
185 assert splitlines(result.output) == [186 "UnsupportedProtocol: Request URL has an unsupported protocol 'invalid://'.",187 ]