Skip to content
Myr

test_main.py

This file

Path

tests/test_main.py

187 lines · 5.4 KiB · Python

Last changed by

Add support for zstd decoding (#3139)Michiel W. Beijenbefore adoption392dbe4

History of this file

Take 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 back
1import os
2import 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 back
19 result = runner.invoke(httpx.main, ["--help"])

Raise TypeError if content is passed a 'dict' instance. (#2495)Tom Christiebefore adoptiona8dd079

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])

Raise TypeError if content is passed a 'dict' instance. (#2495)Tom Christiebefore adoptiona8dd079

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])

Raise TypeError if content is passed a 'dict' instance. (#2495)Tom Christiebefore adoptiona8dd079

lines 43–59why, 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 ]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 back
60 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 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 ]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 back
75 result = runner.invoke(httpx.main, [url])

Raise TypeError if content is passed a 'dict' instance. (#2495)Tom Christiebefore adoptiona8dd079

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"])

Raise TypeError if content is passed a 'dict' instance. (#2495)Tom Christiebefore adoptiona8dd079

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"}'])

Raise TypeError if content is passed a 'dict' instance. (#2495)Tom Christiebefore adoptiona8dd079

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"])

Raise TypeError if content is passed a 'dict' instance. (#2495)Tom Christiebefore adoptiona8dd079

lines 125–131why, 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: */*",

Add support for zstd decoding (#3139)Michiel W. Beijenbefore adoption392dbe4

line 132why, one step back
132 "Accept-Encoding: gzip, deflate, br, zstd",

Raise TypeError if content is passed a 'dict' instance. (#2495)Tom Christiebefore adoptiona8dd079

lines 133–147why, one step back
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"])

Raise TypeError if content is passed a 'dict' instance. (#2495)Tom Christiebefore adoptiona8dd079

lines 149–156why, 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: */*",

Add support for zstd decoding (#3139)Michiel W. Beijenbefore adoption392dbe4

line 157why, one step back
157 "Accept-Encoding: gzip, deflate, br, zstd",

Raise TypeError if content is passed a 'dict' instance. (#2495)Tom Christiebefore adoptiona8dd079

lines 158–174why, one step back
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"])

Raise TypeError if content is passed a 'dict' instance. (#2495)Tom Christiebefore adoptiona8dd079

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"])

Raise TypeError if content is passed a 'dict' instance. (#2495)Tom Christiebefore adoptiona8dd079

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 ]