2019-08-29 11:46:08 +02:00
|
|
|
"""
|
|
|
|
We test against httpbin which doesn't return the request data in a
|
|
|
|
consistent way:
|
|
|
|
|
|
|
|
1. Non-form requests: the `data` field contains base64 encoded version of
|
|
|
|
our zlib-encoded request data.
|
|
|
|
|
|
|
|
2. Form requests: `form` contains a messed up version of the data.
|
|
|
|
|
|
|
|
"""
|
|
|
|
import base64
|
|
|
|
import zlib
|
|
|
|
|
2021-05-05 14:13:39 +02:00
|
|
|
from .fixtures import FILE_PATH, FILE_CONTENT
|
2020-09-28 12:16:57 +02:00
|
|
|
from httpie.status import ExitStatus
|
2021-05-05 14:13:39 +02:00
|
|
|
from .utils import StdinBytesIO, http, HTTP_OK, MockEnvironment
|
2019-08-29 11:46:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
def assert_decompressed_equal(base64_compressed_data, expected_str):
|
|
|
|
compressed_data = base64.b64decode(
|
|
|
|
base64_compressed_data.split(',', 1)[1])
|
|
|
|
data = zlib.decompress(compressed_data)
|
|
|
|
actual_str = data.decode()
|
|
|
|
|
|
|
|
# FIXME: contains a trailing linebreak with an uploaded file
|
|
|
|
actual_str = actual_str.rstrip()
|
|
|
|
|
|
|
|
assert actual_str == expected_str
|
|
|
|
|
|
|
|
|
2020-09-28 12:16:57 +02:00
|
|
|
def test_cannot_combine_compress_with_chunked(httpbin):
|
2024-03-04 18:12:18 +01:00
|
|
|
r = http('--compress', '--chunked', httpbin + '/get',
|
2020-09-28 12:16:57 +02:00
|
|
|
tolerate_error_exit_status=True)
|
|
|
|
assert r.exit_status == ExitStatus.ERROR
|
|
|
|
assert 'cannot combine --compress and --chunked' in r.stderr
|
|
|
|
|
|
|
|
|
|
|
|
def test_cannot_combine_compress_with_multipart(httpbin):
|
2024-03-04 18:12:18 +01:00
|
|
|
r = http('--compress', '--multipart', httpbin + '/get',
|
2020-09-28 12:16:57 +02:00
|
|
|
tolerate_error_exit_status=True)
|
|
|
|
assert r.exit_status == ExitStatus.ERROR
|
|
|
|
assert 'cannot combine --compress and --multipart' in r.stderr
|
|
|
|
|
|
|
|
|
2019-08-29 11:46:08 +02:00
|
|
|
def test_compress_skip_negative_ratio(httpbin_both):
|
|
|
|
r = http(
|
|
|
|
'--compress',
|
|
|
|
httpbin_both + '/post',
|
|
|
|
'foo=bar',
|
|
|
|
)
|
|
|
|
assert HTTP_OK in r
|
|
|
|
assert 'Content-Encoding' not in r.json['headers']
|
|
|
|
assert r.json['json'] == {'foo': 'bar'}
|
|
|
|
|
|
|
|
|
|
|
|
def test_compress_force_with_negative_ratio(httpbin_both):
|
|
|
|
r = http(
|
|
|
|
'--compress',
|
|
|
|
'--compress',
|
|
|
|
httpbin_both + '/post',
|
|
|
|
'foo=bar',
|
|
|
|
)
|
|
|
|
assert HTTP_OK in r
|
|
|
|
assert r.json['headers']['Content-Encoding'] == 'deflate'
|
|
|
|
assert_decompressed_equal(r.json['data'], '{"foo": "bar"}')
|
|
|
|
|
|
|
|
|
|
|
|
def test_compress_json(httpbin_both):
|
|
|
|
r = http(
|
|
|
|
'--compress',
|
|
|
|
'--compress',
|
|
|
|
httpbin_both + '/post',
|
|
|
|
'foo=bar',
|
|
|
|
)
|
|
|
|
assert HTTP_OK in r
|
|
|
|
assert r.json['headers']['Content-Encoding'] == 'deflate'
|
|
|
|
assert_decompressed_equal(r.json['data'], '{"foo": "bar"}')
|
|
|
|
assert r.json['json'] is None
|
|
|
|
|
|
|
|
|
|
|
|
def test_compress_form(httpbin_both):
|
|
|
|
r = http(
|
|
|
|
'--form',
|
|
|
|
'--compress',
|
|
|
|
'--compress',
|
|
|
|
httpbin_both + '/post',
|
|
|
|
'foo=bar',
|
|
|
|
)
|
|
|
|
assert HTTP_OK in r
|
|
|
|
assert r.json['headers']['Content-Encoding'] == 'deflate'
|
|
|
|
assert r.json['data'] == ""
|
|
|
|
assert '"foo": "bar"' not in r
|
|
|
|
|
|
|
|
|
2021-05-24 14:29:54 +02:00
|
|
|
def test_compress_raw(httpbin_both):
|
|
|
|
r = http(
|
|
|
|
'--raw',
|
|
|
|
FILE_CONTENT,
|
|
|
|
'--compress',
|
|
|
|
'--compress',
|
|
|
|
httpbin_both + '/post',
|
|
|
|
)
|
|
|
|
assert HTTP_OK in r
|
|
|
|
assert r.json['headers']['Content-Encoding'] == 'deflate'
|
|
|
|
assert_decompressed_equal(r.json['data'], FILE_CONTENT.strip())
|
|
|
|
|
|
|
|
|
2019-08-29 11:46:08 +02:00
|
|
|
def test_compress_stdin(httpbin_both):
|
2020-09-28 12:16:57 +02:00
|
|
|
env = MockEnvironment(
|
|
|
|
stdin=StdinBytesIO(FILE_PATH.read_bytes()),
|
|
|
|
stdin_isatty=False,
|
|
|
|
)
|
|
|
|
r = http(
|
|
|
|
'--compress',
|
|
|
|
'--compress',
|
|
|
|
'PATCH',
|
|
|
|
httpbin_both + '/patch',
|
|
|
|
env=env,
|
|
|
|
)
|
2019-08-29 11:46:08 +02:00
|
|
|
assert HTTP_OK in r
|
|
|
|
assert r.json['headers']['Content-Encoding'] == 'deflate'
|
|
|
|
assert_decompressed_equal(r.json['data'], FILE_CONTENT.strip())
|
|
|
|
assert not r.json['json']
|
|
|
|
|
|
|
|
|
|
|
|
def test_compress_file(httpbin_both):
|
|
|
|
r = http(
|
|
|
|
'--form',
|
|
|
|
'--compress',
|
|
|
|
'--compress',
|
|
|
|
'PUT',
|
|
|
|
httpbin_both + '/put',
|
2020-09-28 12:16:57 +02:00
|
|
|
f'file@{FILE_PATH}',
|
2019-08-29 11:46:08 +02:00
|
|
|
)
|
|
|
|
assert HTTP_OK in r
|
|
|
|
assert r.json['headers']['Content-Encoding'] == 'deflate'
|
|
|
|
assert r.json['headers']['Content-Type'].startswith(
|
|
|
|
'multipart/form-data; boundary=')
|
|
|
|
assert r.json['files'] == {}
|
|
|
|
assert FILE_CONTENT not in r
|