Skip to content
Myr

test_main.py

This file

Path

tests/test_main.py

186 lines · 5.3 KiB · Python

Last changed by

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

History of this file

Take it

Add cli support (#1855)Tom Christiebefore adoptionee9250d

lines 1–54why, one step back
1import os
23from 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 ]5354

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

lines 55–70why, one step back
55def test_binary(server):
56 url = str(server.url.copy_with(path="/echo_binary"))57 runner = CliRunner()58 content = "Hello, world!"59 result = runner.invoke(httpx.main, [url, "-c", content])60 assert result.exit_code == 061 assert remove_date_header(splitlines(result.output)) == [62 "HTTP/1.1 200 OK",63 "server: uvicorn",64 "content-type: application/octet-stream",65 "Transfer-Encoding: chunked",66 "",67 f"<{len(content)} bytes of binary data>",68 ]6970

Add cli support (#1855)Tom Christiebefore adoptionee9250d

lines 71–80why, one step back
71def test_redirects(server):
72 url = str(server.url.copy_with(path="/redirect_301"))73 runner = CliRunner()74 result = runner.invoke(httpx.main, [url])75 assert result.exit_code == 176 assert remove_date_header(splitlines(result.output)) == [77 "HTTP/1.1 301 Moved Permanently",78 "server: uvicorn",79 "location: /",80 "Transfer-Encoding: chunked",

Version 0.21 (#1935)Tom Christiebefore adoption61188fe

line 81why, one step back
81 "",

Add cli support (#1855)Tom Christiebefore adoptionee9250d

lines 82–125why, one step back
82 ]
838485def test_follow_redirects(server):86 url = str(server.url.copy_with(path="/redirect_301"))87 runner = CliRunner()88 result = runner.invoke(httpx.main, [url, "--follow-redirects"])89 assert result.exit_code == 090 assert remove_date_header(splitlines(result.output)) == [91 "HTTP/1.1 301 Moved Permanently",92 "server: uvicorn",93 "location: /",94 "Transfer-Encoding: chunked",95 "",96 "HTTP/1.1 200 OK",97 "server: uvicorn",98 "content-type: text/plain",99 "Transfer-Encoding: chunked",100 "",101 "Hello, world!",102 ]103104105def test_post(server):106 url = str(server.url.copy_with(path="/echo_body"))107 runner = CliRunner()108 result = runner.invoke(httpx.main, [url, "-m", "POST", "-j", '{"hello": "world"}'])109 assert result.exit_code == 0110 assert remove_date_header(splitlines(result.output)) == [111 "HTTP/1.1 200 OK",112 "server: uvicorn",113 "content-type: text/plain",114 "Transfer-Encoding: chunked",115 "",116 '{"hello": "world"}',117 ]118119120def test_verbose(server):121 url = str(server.url)122 runner = CliRunner()123 result = runner.invoke(httpx.main, [url, "-v"])124 assert result.exit_code == 0125 assert remove_date_header(splitlines(result.output)) == [

Version 0.21 (#1935)Tom Christiebefore adoption61188fe

lines 126–127why, one step back
126 "* Connecting to '127.0.0.1'",
127 "* Connected to '127.0.0.1' on port 8000",

Add cli support (#1855)Tom Christiebefore adoptionee9250d

lines 128–150why, one step back
128 "GET / HTTP/1.1",
129 f"Host: {server.url.netloc.decode('ascii')}",130 "Accept: */*",131 "Accept-Encoding: gzip, deflate, br",132 "Connection: keep-alive",133 f"User-Agent: python-httpx/{httpx.__version__}",134 "",135 "HTTP/1.1 200 OK",136 "server: uvicorn",137 "content-type: text/plain",138 "Transfer-Encoding: chunked",139 "",140 "Hello, world!",141 ]142143144def test_auth(server):145 url = str(server.url)146 runner = CliRunner()147 result = runner.invoke(httpx.main, [url, "-v", "--auth", "username", "password"])148 print(result.output)149 assert result.exit_code == 0150 assert remove_date_header(splitlines(result.output)) == [

Version 0.21 (#1935)Tom Christiebefore adoption61188fe

lines 151–152why, one step back
151 "* Connecting to '127.0.0.1'",
152 "* Connected to '127.0.0.1' on port 8000",

Add cli support (#1855)Tom Christiebefore adoptionee9250d

lines 153–186why, one step back
153 "GET / HTTP/1.1",
154 f"Host: {server.url.netloc.decode('ascii')}",155 "Accept: */*",156 "Accept-Encoding: gzip, deflate, br",157 "Connection: keep-alive",158 f"User-Agent: python-httpx/{httpx.__version__}",159 "Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=",160 "",161 "HTTP/1.1 200 OK",162 "server: uvicorn",163 "content-type: text/plain",164 "Transfer-Encoding: chunked",165 "",166 "Hello, world!",167 ]168169170def test_download(server):171 url = str(server.url)172 runner = CliRunner()173 with runner.isolated_filesystem():174 runner.invoke(httpx.main, [url, "--download", "index.txt"])175 assert os.path.exists("index.txt")176 with open("index.txt", "r") as input_file:177 assert input_file.read() == "Hello, world!"178179180def test_errors():181 runner = CliRunner()182 result = runner.invoke(httpx.main, ["invalid://example.org"])183 assert result.exit_code == 1184 assert splitlines(result.output) == [185 "UnsupportedProtocol: Request URL has an unsupported protocol 'invalid://'.",186 ]