Suppress binary output on the command line (#2049) (#2076)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by waterfountain1996 · committed by GitHub · parent a416106 · Browse files · 3 files·+40 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+40 added−2 removed
- tests:+37 added−0 removed
- other:+3 added−2 removed
Provenance
- Commit
4c30748- Tree
427c42d- Author
waterfountain1996 <71171892+waterfountain1996@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
a416106
3 files changed+40 added−2 removed
modified:httpx/_main.py+3 added-2 removed
170 unmodified lines171 except ValueError: # pragma: nocover172 text = response.text173 else:174 text = response.text175+176 syntax = rich.syntax.Syntax(text, lexer_name, theme="ansi_dark", word_wrap=True)177 console.print(syntax)177- else: # pragma: nocover178- console.print(rich.markup.escape(response.text))178+ else:179+ console.print(f"<{len(response.content)} bytes of binary data>")180 181 182 def format_certificate(cert: dict) -> str: # pragma: nocover183 lines = []314 unmodified linesmodified:tests/conftest.py+21 added
78 unmodified lines79 elif scope["path"].startswith("/status"):80 await status_code(scope, receive, send)81 elif scope["path"].startswith("/echo_body"):82 await echo_body(scope, receive, send)83+ elif scope["path"].startswith("/echo_binary"):84+ await echo_binary(scope, receive, send)85 elif scope["path"].startswith("/echo_headers"):86 await echo_headers(scope, receive, send)87 elif scope["path"].startswith("/redirect_301"):88 await redirect_301(scope, receive, send)67 unmodified lines156 )157 await send({"type": "http.response.body", "body": body})158 159 160+async def echo_binary(scope, receive, send):161+ body = b""162+ more_body = True163+164+ while more_body:165+ message = await receive()166+ body += message.get("body", b"")167+ more_body = message.get("more_body", False)168+169+ await send(170+ {171+ "type": "http.response.start",172+ "status": 200,173+ "headers": [[b"content-type", b"application/octet-stream"]],174+ }175+ )176+ await send({"type": "http.response.body", "body": body})177+178+179 async def echo_headers(scope, receive, send):180 body = {181 name.capitalize().decode(): value.decode()182 for name, value in scope.get("headers", [])141 unmodified linesmodified:tests/test_main.py+16 added
50 unmodified lines51 "}",52 ]53 54 55+def 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+ ]69+70+71 def test_redirects(server):72 url = str(server.url.copy_with(path="/redirect_301"))73 runner = CliRunner()74 result = runner.invoke(httpx.main, [url])112 unmodified lines