Skip to content

Commit

Permalink
Add test for failing 400 s3 response
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesls committed Mar 26, 2018
1 parent 3ae0718 commit 68b3997
Showing 1 changed file with 50 additions and 25 deletions.
75 changes: 50 additions & 25 deletions tests/functional/test_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import botocore.session
from botocore.config import Config
from botocore.compat import urlsplit
from botocore.exceptions import ParamValidationError
from botocore.exceptions import ParamValidationError, ClientError
from botocore import UNSIGNED


Expand Down Expand Up @@ -277,6 +277,16 @@ def setUp(self):
b' <IsTruncated>false</IsTruncated>'
b'</ListBucketResult>')

def create_response(self, content=b'',
status_code=200, headers=None):
response = mock.Mock()
if headers is None:
headers = {}
response.headers = headers
response.content = content
response.status_code = status_code
return response

def test_region_redirect(self):
self.http_session_send_mock.side_effect = [
self.redirect_response, self.success_response]
Expand Down Expand Up @@ -340,30 +350,18 @@ def test_resign_request_with_region_when_needed(self):
self.assertEqual(calls[1].url, fixed_url)

def test_resign_request_in_us_east_1(self):
bad_request_response = mock.Mock()
bad_request_response.headers = {}
bad_request_response.content = b''
bad_request_response.status_code = 400

bad_head_bucket_response = mock.Mock()
bad_head_bucket_response.headers = {
'x-amz-bucket-region': 'eu-central-1'
}
bad_head_bucket_response.content = b''
bad_head_bucket_response.status_code = 400

head_bucket_response = mock.Mock()
head_bucket_response.headers = {
'x-amz-bucket-region': 'eu-central-1'
}
head_bucket_response.content = b''
head_bucket_response.status_code = 200

request_response = mock.Mock()
request_response.headers = {}
request_response.content = b''
request_response.status_code = 200

bad_request_response = self.create_response(status_code=400)
bad_head_bucket_response = self.create_response(
status_code=400,
headers={'x-amz-bucket-region': 'eu-central-1'}
)
head_bucket_response = self.create_response(
headers={
'x-amz-bucket-region': 'eu-central-1'
},
status_code=200,
)
request_response = self.create_response(status_code=200)
self.http_session_send_mock.side_effect = [
bad_request_response,
bad_head_bucket_response,
Expand All @@ -384,6 +382,33 @@ def test_resign_request_in_us_east_1(self):
fixed_url = ('https://foo.s3.eu-central-1.amazonaws.com/bar')
self.assertEqual(calls[-1].url, fixed_url)

def test_resign_request_in_us_east_1_fails(self):
bad_request_response = self.create_response(status_code=400)
bad_head_bucket_response = self.create_response(
status_code=400,
headers={'x-amz-bucket-region': 'eu-central-1'}
)
head_bucket_response = self.create_response(
headers={
'x-amz-bucket-region': 'eu-central-1'
}
)
# The final request still fails with a 400.
request_response = self.create_response(status_code=400)

self.http_session_send_mock.side_effect = [
bad_request_response,
bad_head_bucket_response,
head_bucket_response,
request_response,
]

# Verify that the final 400 response is propagated
# back to the user.
client = self.session.create_client('s3', 'us-east-1')
with self.assertRaises(ClientError) as e:
client.head_object(Bucket='foo', Key='bar')


class TestGeneratePresigned(BaseS3OperationTest):
def test_generate_unauthed_url(self):
Expand Down

0 comments on commit 68b3997

Please sign in to comment.