1
0
mirror of https://github.com/httpie/cli.git synced 2026-05-04 20:44:57 +02:00

Automatically enable --stream on server sent events (#1226)

* Automatically enable --stream when used chunked encoding

* try fix 3.6 mock issue

* Only enable on text/event-stream

Co-authored-by: Jakub Roztocil <jakub@roztocil.co>
This commit is contained in:
Batuhan Taskaya
2021-12-08 18:49:12 +03:00
committed by GitHub
parent 62e43abc86
commit 207b970d94
4 changed files with 55 additions and 2 deletions
+26
View File
@@ -2,6 +2,7 @@ import json
import pytest
import responses
from unittest.mock import Mock
from httpie.compat import is_windows
from httpie.cli.constants import PRETTY_MAP
@@ -107,3 +108,28 @@ def test_redirected_stream(httpbin):
r = http('--pretty=none', '--stream', '--verbose', 'GET',
httpbin.url + '/get', env=env)
assert BIN_FILE_CONTENT in r
# /drip endpoint produces 3 individual lines,
# if we set text/event-stream HTTPie should stream
# it by default. Otherwise, it will buffer and then
# print.
@pytest.mark.parametrize('extras, expected', [
(
['Accept:text/event-stream'],
3
),
(
['Accept:text/plain'],
1
)
])
def test_auto_streaming(http_server, extras, expected):
env = MockEnvironment()
env.stdout.write = Mock()
http(http_server + '/drip', *extras, env=env)
assert len([
call_arg
for call_arg in env.stdout.write.call_args_list
if b'test' in call_arg[0][0]
]) == expected
+16
View File
@@ -36,6 +36,22 @@ def get_headers(handler):
handler.end_headers()
@TestHandler.handler('GET', '/drip')
def chunked_drip(handler):
handler.send_response(200)
accept = handler.headers.get('Accept')
if accept is not None:
handler.send_header('Content-Type', accept)
handler.send_header('Transfer-Encoding', 'chunked')
handler.end_headers()
for _ in range(3):
body = 'test\n'
handler.wfile.write(f'{len(body):X}\r\n{body}\r\n'.encode('utf-8'))
handler.wfile.write('0\r\n\r\n'.encode('utf-8'))
@pytest.fixture(scope="function")
def http_server():
"""A custom HTTP server implementation for our tests, that is