Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlm committed Jul 11, 2023
1 parent d90db29 commit 92043ea
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
6 changes: 2 additions & 4 deletions botocore/compress.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@


class RequestCompressor:
"""A class that can compress the body of an ``AWSRequest``."""
"""A class that can compress a request body."""

@classmethod
def compress(cls, config, request_dict, operation_model):
Expand All @@ -34,7 +34,7 @@ def compress(cls, config, request_dict, operation_model):
body = request_dict['body']
if cls._should_compress_request(config, body, operation_model):
encodings = operation_model.request_compression['encodings']
headers = request_dict.get('headers', {})
headers = request_dict['headers']
for encoding in encodings:
encoder = getattr(cls, f'_{encoding}_compress_body', None)
if encoder is not None:
Expand All @@ -47,8 +47,6 @@ def compress(cls, config, request_dict, operation_model):
'Compressing request with %s encoding', encoding
)
request_dict['body'] = encoder(body)
if 'headers' not in request_dict:
request_dict['headers'] = headers
else:
logger.debug(
'Unsupported compression encoding: %s' % encoding
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/test_compress.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
Expand Down
19 changes: 12 additions & 7 deletions tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1704,9 +1704,7 @@ def test_request_compression_client_config(self):
self.assertEqual(
service_client.meta.config.request_min_compression_size_bytes, 100
)
self.assertEqual(
service_client.meta.config.disable_request_compression, True
)
self.assertTrue(service_client.meta.config.disable_request_compression)

def test_request_compression_config_store(self):
self.config_store.set_config_variable(
Expand All @@ -1724,9 +1722,7 @@ def test_request_compression_config_store(self):
self.assertEqual(
service_client.meta.config.request_min_compression_size_bytes, 100
)
self.assertEqual(
service_client.meta.config.disable_request_compression, True
)
self.assertTrue(service_client.meta.config.disable_request_compression)

def test_request_compression_client_config_overrides_config_store(self):
self.config_store.set_config_variable(
Expand All @@ -1753,10 +1749,19 @@ def test_request_compression_client_config_overrides_config_store(self):
)

def test_bad_request_min_compression_size_bytes(self):
creator = self.create_client_creator()
with self.assertRaises(InvalidConfigError):
creator.create_client(
'myservice',
'us-west-2',
credentials=self.credentials,
client_config=botocore.config.Config(
request_min_compression_size_bytes='foo',
),
)
self.config_store.set_config_variable(
'request_min_compression_size_bytes', 'foo'
)
creator = self.create_client_creator()
with self.assertRaises(InvalidConfigError):
creator.create_client(
'myservice',
Expand Down
16 changes: 9 additions & 7 deletions tests/unit/test_compress.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def _assert_compression(is_compressed, body, encoding):
disable_request_compression=True,
request_min_compression_size_bytes=1000,
),
{'body': b'foo'},
{'body': b'foo', 'headers': {}},
OP_WITH_COMPRESSION,
False,
None,
Expand Down Expand Up @@ -172,28 +172,28 @@ def _assert_compression(is_compressed, body, encoding):
),
(
COMPRESSION_CONFIG_128_BYTES,
{'body': REQUEST_BODY.decode()},
{'body': REQUEST_BODY.decode(), 'headers': {}},
OP_WITH_COMPRESSION,
True,
'gzip',
),
(
COMPRESSION_CONFIG_128_BYTES,
{'body': bytearray(REQUEST_BODY)},
{'body': bytearray(REQUEST_BODY), 'headers': {}},
OP_WITH_COMPRESSION,
True,
'gzip',
),
(
COMPRESSION_CONFIG_128_BYTES,
{'body': io.BytesIO(REQUEST_BODY)},
{'body': io.BytesIO(REQUEST_BODY), 'headers': {}},
OP_WITH_COMPRESSION,
True,
'gzip',
),
(
COMPRESSION_CONFIG_128_BYTES,
{'body': io.StringIO(REQUEST_BODY.decode())},
{'body': io.StringIO(REQUEST_BODY.decode()), 'headers': {}},
OP_WITH_COMPRESSION,
True,
'gzip',
Expand Down Expand Up @@ -232,7 +232,7 @@ def test_compress(

@pytest.mark.parametrize('body', [1, object(), None, True, 1.0])
def test_compress_bad_types(body):
request_dict = {'body': body}
request_dict = {'body': body, 'headers': {}}
RequestCompressor.compress(
COMPRESSION_CONFIG_0_BYTES, request_dict, OP_WITH_COMPRESSION
)
Expand All @@ -245,6 +245,8 @@ def test_compress_bad_types(body):
)
def test_body_streams_position_reset(body):
RequestCompressor.compress(
COMPRESSION_CONFIG_0_BYTES, {'body': body}, OP_WITH_COMPRESSION
COMPRESSION_CONFIG_0_BYTES,
{'body': body, 'headers': {}},
OP_WITH_COMPRESSION,
)
assert body.tell() == 0

0 comments on commit 92043ea

Please sign in to comment.