Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than 61188fe point at it.

Version 0.21 (#1935)Tom Christiebefore adoption61188fe

line 1why, one step back
1import os

Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c

line 2why, one step back
2import typing

Version 0.21 (#1935)Tom Christiebefore adoption61188fe

lines 3–6why, one step back
3
4from click.testing import CliRunner56import httpx

Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c

line 7why, one step back
7from httpx import main

Version 0.21 (#1935)Tom Christiebefore adoption61188fe

line 8why, one step back
8

Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c

lines 9–11why, one step back
9if typing.TYPE_CHECKING: # pragma: no cover
10 # don't let mypy be misled by the fallback defined in httpx/__init__.py11 from httpx._main import main # noqa: F811

Version 0.21 (#1935)Tom Christiebefore adoption61188fe

line 12why, one step back
12

Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c

lines 13–14why, one step back
13
14def splitlines(output: str) -> typing.Iterable[str]:

Version 0.21 (#1935)Tom Christiebefore adoption61188fe

lines 15–17why, one step back
15 return [line.strip() for line in output.splitlines()]
1617

Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c

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

Version 0.21 (#1935)Tom Christiebefore adoption61188fe

lines 19–23why, one step back
19 return [line for line in lines if not line.startswith("date:")]
202122def test_help():23 runner = CliRunner()

Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c

line 24why, one step back
24 result = runner.invoke(main, ["--help"])

Version 0.21 (#1935)Tom Christiebefore adoption61188fe

lines 25–31why, one step back
25 assert result.exit_code == 0
26 assert "A next generation HTTP client." in result.output272829def test_get(server):30 url = str(server.url)31 runner = CliRunner()

Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c

line 32why, one step back
32 result = runner.invoke(main, [url])

Version 0.21 (#1935)Tom Christiebefore adoption61188fe

lines 33–46why, one step back
33 assert result.exit_code == 0
34 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()

Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c

line 47why, one step back
47 result = runner.invoke(main, [url])

Version 0.21 (#1935)Tom Christiebefore adoption61188fe

lines 48–60why, one step back
48 assert result.exit_code == 0
49 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 ]5960

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

lines 61–64why, one step back
61def test_binary(server):
62 url = str(server.url.copy_with(path="/echo_binary"))63 runner = CliRunner()64 content = "Hello, world!"

Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c

line 65why, one step back
65 result = runner.invoke(main, [url, "-c", content])

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

lines 66–76why, one step back
66 assert result.exit_code == 0
67 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 ]7576

Version 0.21 (#1935)Tom Christiebefore adoption61188fe

lines 77–79why, one step back
77def test_redirects(server):
78 url = str(server.url.copy_with(path="/redirect_301"))79 runner = CliRunner()

Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c

line 80why, one step back
80 result = runner.invoke(main, [url])

Version 0.21 (#1935)Tom Christiebefore adoption61188fe

lines 81–93why, one step back
81 assert result.exit_code == 1
82 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()

Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c

line 94why, one step back
94 result = runner.invoke(main, [url, "--follow-redirects"])

Version 0.21 (#1935)Tom Christiebefore adoption61188fe

lines 95–113why, one step back
95 assert result.exit_code == 0
96 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()

Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c

line 114why, one step back
114 result = runner.invoke(main, [url, "-m", "POST", "-j", '{"hello": "world"}'])

Version 0.21 (#1935)Tom Christiebefore adoption61188fe

lines 115–128why, one step back
115 assert result.exit_code == 0
116 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()

Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c

line 129why, one step back
129 result = runner.invoke(main, [url, "-v"])

Version 0.21 (#1935)Tom Christiebefore adoption61188fe

lines 130–152why, one step back
130 assert result.exit_code == 0
131 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()

Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c

line 153why, one step back
153 result = runner.invoke(main, [url, "-v", "--auth", "username", "password"])

Version 0.21 (#1935)Tom Christiebefore adoption61188fe

lines 154–179why, one step back
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():

Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c

line 180why, one step back
180 runner.invoke(main, [url, "--download", "index.txt"])

Version 0.21 (#1935)Tom Christiebefore adoption61188fe

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

Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c

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

Version 0.21 (#1935)Tom Christiebefore adoption61188fe

lines 189–192why, one step back
189 assert result.exit_code == 1
190 assert splitlines(result.output) == [191 "UnsupportedProtocol: Request URL has an unsupported protocol 'invalid://'.",192 ]