From 2f8d330b5753d53b2627d5f01b929bac5647bc13 Mon Sep 17 00:00:00 2001 From: Jakub Roztocil Date: Mon, 28 Sep 2020 16:40:16 +0200 Subject: [PATCH] Fix `--offline --chunked`, add more tests --- httpie/client.py | 4 +++ tests/test_httpie.py | 60 ---------------------------------- tests/test_offline.py | 75 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 60 deletions(-) create mode 100644 tests/test_offline.py diff --git a/httpie/client.py b/httpie/client.py index e7c8267d..34ce06ce 100644 --- a/httpie/client.py +++ b/httpie/client.py @@ -266,6 +266,10 @@ def make_request_kwargs( if base_headers: headers.update(base_headers) headers.update(args.headers) + if args.offline and args.chunked and 'Transfer-Encoding' not in headers: + # When online, we let requests set the header instead to be able more + # easily verify chunking is taking place. + headers['Transfer-Encoding'] = 'chunked' headers = finalize_headers(headers) if (args.form and files) or args.multipart: diff --git a/tests/test_httpie.py b/tests/test_httpie.py index 02208793..baa0ffb4 100644 --- a/tests/test_httpie.py +++ b/tests/test_httpie.py @@ -183,63 +183,3 @@ def test_json_input_preserve_order(httpbin_both): assert HTTP_OK in r assert r.json['data'] == \ '{"order": {"map": {"1": "first", "2": "second"}}}' - - -def test_offline(): - r = http( - '--offline', - 'https://this-should.never-resolve/foo', - ) - assert 'GET /foo' in r - - -def test_offline_form(): - r = http( - '--offline', - '--form', - 'https://this-should.never-resolve/foo', - 'foo=bar' - ) - assert 'POST /foo' in r - assert 'foo=bar' in r - - -def test_offline_json(): - r = http( - '--offline', - 'https://this-should.never-resolve/foo', - 'foo=bar' - ) - assert 'POST /foo' in r - assert r.json == {'foo': 'bar'} - - -def test_offline_multipart(): - r = http( - '--offline', - '--multipart', - 'https://this-should.never-resolve/foo', - 'foo=bar' - ) - assert 'POST /foo' in r - assert 'name="foo"' in r - - -def test_offline_from_file(): - r = http( - '--offline', - 'https://this-should.never-resolve/foo', - f'@{FILE_PATH_ARG}' - ) - assert 'POST /foo' in r - assert FILE_CONTENT in r - - -def test_offline_download(): - """Absence of response should be handled gracefully with --download""" - r = http( - '--offline', - '--download', - 'https://this-should.never-resolve/foo', - ) - assert 'GET /foo' in r diff --git a/tests/test_offline.py b/tests/test_offline.py new file mode 100644 index 00000000..4abc96bb --- /dev/null +++ b/tests/test_offline.py @@ -0,0 +1,75 @@ +from fixtures import FILE_CONTENT, FILE_PATH_ARG +from utils import http + + +def test_offline(): + r = http( + '--offline', + 'https://this-should.never-resolve/foo', + ) + assert 'GET /foo' in r + + +def test_offline_form(): + r = http( + '--offline', + '--form', + 'https://this-should.never-resolve/foo', + 'foo=bar' + ) + assert 'POST /foo' in r + assert 'foo=bar' in r + + +def test_offline_json(): + r = http( + '--offline', + 'https://this-should.never-resolve/foo', + 'foo=bar' + ) + assert 'POST /foo' in r + assert r.json == {'foo': 'bar'} + + +def test_offline_multipart(): + r = http( + '--offline', + '--multipart', + 'https://this-should.never-resolve/foo', + 'foo=bar' + ) + assert 'POST /foo' in r + assert 'name="foo"' in r + + +def test_offline_from_file(): + r = http( + '--offline', + 'https://this-should.never-resolve/foo', + f'@{FILE_PATH_ARG}' + ) + assert 'POST /foo' in r + assert FILE_CONTENT in r + + +def test_offline_chunked(): + r = http( + '--offline', + '--chunked', + '--form', + 'https://this-should.never-resolve/foo', + 'hello=world' + ) + assert 'POST /foo' in r + assert 'Transfer-Encoding: chunked' in r, r + assert 'hello=world' in r + + +def test_offline_download(): + """Absence of response should be handled gracefully with --download""" + r = http( + '--offline', + '--download', + 'https://this-should.never-resolve/foo', + ) + assert 'GET /foo' in r