test_main.py
This file
Path
tests/test_main.py
165 lines · 4.6 KiB · Python
Last changed by
Add cli support (#1855)Tom Christiebefore adoptionee9250d
History of this fileTake it
1import os23from click.testing import CliRunner45import httpx678def splitlines(output):9 return [line.strip() for line in output.splitlines()]101112def remove_date_header(lines):13 return [line for line in lines if not line.startswith("date:")]141516def test_help():17 runner = CliRunner()18 result = runner.invoke(httpx.main, ["--help"])19 assert result.exit_code == 020 assert "A next generation HTTP client." in result.output212223def test_get(server):24 url = str(server.url)25 runner = CliRunner()26 result = runner.invoke(httpx.main, [url])27 assert result.exit_code == 028 assert remove_date_header(splitlines(result.output)) == [29 "HTTP/1.1 200 OK",30 "server: uvicorn",31 "content-type: text/plain",32 "Transfer-Encoding: chunked",33 "",34 "Hello, world!",35 ]363738def test_json(server):39 url = str(server.url.copy_with(path="/json"))40 runner = CliRunner()41 result = runner.invoke(httpx.main, [url])42 assert result.exit_code == 043 assert remove_date_header(splitlines(result.output)) == [44 "HTTP/1.1 200 OK",45 "server: uvicorn",46 "content-type: application/json",47 "Transfer-Encoding: chunked",48 "",49 "{",50 '"Hello": "world!"',51 "}",52 ]535455def test_redirects(server):56 url = str(server.url.copy_with(path="/redirect_301"))57 runner = CliRunner()58 result = runner.invoke(httpx.main, [url])59 assert result.exit_code == 160 assert remove_date_header(splitlines(result.output)) == [61 "HTTP/1.1 301 Moved Permanently",62 "server: uvicorn",63 "location: /",64 "Transfer-Encoding: chunked",65 ]666768def test_follow_redirects(server):69 url = str(server.url.copy_with(path="/redirect_301"))70 runner = CliRunner()71 result = runner.invoke(httpx.main, [url, "--follow-redirects"])72 assert result.exit_code == 073 assert remove_date_header(splitlines(result.output)) == [74 "HTTP/1.1 301 Moved Permanently",75 "server: uvicorn",76 "location: /",77 "Transfer-Encoding: chunked",78 "",79 "HTTP/1.1 200 OK",80 "server: uvicorn",81 "content-type: text/plain",82 "Transfer-Encoding: chunked",83 "",84 "Hello, world!",85 ]868788def test_post(server):89 url = str(server.url.copy_with(path="/echo_body"))90 runner = CliRunner()91 result = runner.invoke(httpx.main, [url, "-m", "POST", "-j", '{"hello": "world"}'])92 assert result.exit_code == 093 assert remove_date_header(splitlines(result.output)) == [94 "HTTP/1.1 200 OK",95 "server: uvicorn",96 "content-type: text/plain",97 "Transfer-Encoding: chunked",98 "",99 '{"hello": "world"}',100 ]101102103def test_verbose(server):104 url = str(server.url)105 runner = CliRunner()106 result = runner.invoke(httpx.main, [url, "-v"])107 assert result.exit_code == 0108 assert remove_date_header(splitlines(result.output)) == [109 "GET / HTTP/1.1",110 f"Host: {server.url.netloc.decode('ascii')}",111 "Accept: */*",112 "Accept-Encoding: gzip, deflate, br",113 "Connection: keep-alive",114 f"User-Agent: python-httpx/{httpx.__version__}",115 "",116 "HTTP/1.1 200 OK",117 "server: uvicorn",118 "content-type: text/plain",119 "Transfer-Encoding: chunked",120 "",121 "Hello, world!",122 ]123124125def test_auth(server):126 url = str(server.url)127 runner = CliRunner()128 result = runner.invoke(httpx.main, [url, "-v", "--auth", "username", "password"])129 print(result.output)130 assert result.exit_code == 0131 assert remove_date_header(splitlines(result.output)) == [132 "GET / HTTP/1.1",133 f"Host: {server.url.netloc.decode('ascii')}",134 "Accept: */*",135 "Accept-Encoding: gzip, deflate, br",136 "Connection: keep-alive",137 f"User-Agent: python-httpx/{httpx.__version__}",138 "Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=",139 "",140 "HTTP/1.1 200 OK",141 "server: uvicorn",142 "content-type: text/plain",143 "Transfer-Encoding: chunked",144 "",145 "Hello, world!",146 ]147148149def test_download(server):150 url = str(server.url)151 runner = CliRunner()152 with runner.isolated_filesystem():153 runner.invoke(httpx.main, [url, "--download", "index.txt"])154 assert os.path.exists("index.txt")155 with open("index.txt", "r") as input_file:156 assert input_file.read() == "Hello, world!"157158159def test_errors():160 runner = CliRunner()161 result = runner.invoke(httpx.main, ["invalid://example.org"])162 assert result.exit_code == 1163 assert splitlines(result.output) == [164 "UnsupportedProtocol: Request URL has an unsupported protocol 'invalid://'.",165 ]