1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
import os
import typing
from click.testing import CliRunner
import httpx
def splitlines(output: str) -> typing.Iterable[str]:
return [line.strip() for line in output.splitlines()]
def remove_date_header(lines: typing.Iterable[str]) -> typing.Iterable[str]:
return [line for line in lines if not line.startswith("date:")]
def test_help():
runner = CliRunner()
result = runner.invoke(httpx.main, ["--help"])
assert result.exit_code == 0
assert "A next generation HTTP client." in result.output
def test_get(server):
url = str(server.url)
runner = CliRunner()
result = runner.invoke(httpx.main, [url])
assert result.exit_code == 0
assert remove_date_header(splitlines(result.output)) == [
"HTTP/1.1 200 OK",
"server: uvicorn",
"content-type: text/plain",
"Transfer-Encoding: chunked",
"",
"Hello, world!",
]
def test_json(server):
url = str(server.url.copy_with(path="/json"))
runner = CliRunner()
result = runner.invoke(httpx.main, [url])
assert result.exit_code == 0
assert remove_date_header(splitlines(result.output)) == [
"HTTP/1.1 200 OK",
"server: uvicorn",
"content-type: application/json",
"Transfer-Encoding: chunked",
"",
"{",
'"Hello": "world!"',
"}",
]
def test_binary(server):
url = str(server.url.copy_with(path="/echo_binary"))
runner = CliRunner()
content = "Hello, world!"
result = runner.invoke(httpx.main, [url, "-c", content])
assert result.exit_code == 0
assert remove_date_header(splitlines(result.output)) == [
"HTTP/1.1 200 OK",
"server: uvicorn",
"content-type: application/octet-stream",
"Transfer-Encoding: chunked",
"",
f"<{len(content)} bytes of binary data>",
]
def test_redirects(server):
url = str(server.url.copy_with(path="/redirect_301"))
runner = CliRunner()
result = runner.invoke(httpx.main, [url])
assert result.exit_code == 1
assert remove_date_header(splitlines(result.output)) == [
"HTTP/1.1 301 Moved Permanently",
"server: uvicorn",
"location: /",
"Transfer-Encoding: chunked",
"",
]
def test_follow_redirects(server):
url = str(server.url.copy_with(path="/redirect_301"))
runner = CliRunner()
result = runner.invoke(httpx.main, [url, "--follow-redirects"])
assert result.exit_code == 0
assert remove_date_header(splitlines(result.output)) == [
"HTTP/1.1 301 Moved Permanently",
"server: uvicorn",
"location: /",
"Transfer-Encoding: chunked",
"",
"HTTP/1.1 200 OK",
"server: uvicorn",
"content-type: text/plain",
"Transfer-Encoding: chunked",
"",
"Hello, world!",
]
def test_post(server):
url = str(server.url.copy_with(path="/echo_body"))
runner = CliRunner()
result = runner.invoke(httpx.main, [url, "-m", "POST", "-j", '{"hello": "world"}'])
assert result.exit_code == 0
assert remove_date_header(splitlines(result.output)) == [
"HTTP/1.1 200 OK",
"server: uvicorn",
"content-type: text/plain",
"Transfer-Encoding: chunked",
"",
'{"hello":"world"}',
]
def test_verbose(server):
url = str(server.url)
runner = CliRunner()
result = runner.invoke(httpx.main, [url, "-v"])
assert result.exit_code == 0
assert remove_date_header(splitlines(result.output)) == [
"* Connecting to '127.0.0.1'",
"* Connected to '127.0.0.1' on port 8000",
"GET / HTTP/1.1",
f"Host: {server.url.netloc.decode('ascii')}",
"Accept: */*",
"Accept-Encoding: gzip, deflate, br, zstd",
"Connection: keep-alive",
f"User-Agent: python-httpx/{httpx.__version__}",
"",
"HTTP/1.1 200 OK",
"server: uvicorn",
"content-type: text/plain",
"Transfer-Encoding: chunked",
"",
"Hello, world!",
]
def test_auth(server):
url = str(server.url)
runner = CliRunner()
result = runner.invoke(httpx.main, [url, "-v", "--auth", "username", "password"])
print(result.output)
assert result.exit_code == 0
assert remove_date_header(splitlines(result.output)) == [
"* Connecting to '127.0.0.1'",
"* Connected to '127.0.0.1' on port 8000",
"GET / HTTP/1.1",
f"Host: {server.url.netloc.decode('ascii')}",
"Accept: */*",
"Accept-Encoding: gzip, deflate, br, zstd",
"Connection: keep-alive",
f"User-Agent: python-httpx/{httpx.__version__}",
"Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
"",
"HTTP/1.1 200 OK",
"server: uvicorn",
"content-type: text/plain",
"Transfer-Encoding: chunked",
"",
"Hello, world!",
]
def test_download(server):
url = str(server.url)
runner = CliRunner()
with runner.isolated_filesystem():
runner.invoke(httpx.main, [url, "--download", "index.txt"])
assert os.path.exists("index.txt")
with open("index.txt", "r") as input_file:
assert input_file.read() == "Hello, world!"
def test_errors():
runner = CliRunner()
result = runner.invoke(httpx.main, ["invalid://example.org"])
assert result.exit_code == 1
assert splitlines(result.output) == [
"UnsupportedProtocol: Request URL has an unsupported protocol 'invalid://'.",
]
|