From 6e61ee4caa75efd7391cd6db4df75f8d61416a59 Mon Sep 17 00:00:00 2001 From: Dmytro Milinevskyy Date: Fri, 14 Apr 2017 13:32:52 +0200 Subject: [PATCH] s3: handle WebsiteRedirectLocation spulec/moto#821 --- moto/s3/models.py | 4 ++++ moto/s3/responses.py | 1 + tests/test_s3/test_s3.py | 13 +++++++++++++ 3 files changed, 18 insertions(+) diff --git a/moto/s3/models.py b/moto/s3/models.py index 04220c142..cdd96015e 100644 --- a/moto/s3/models.py +++ b/moto/s3/models.py @@ -25,6 +25,7 @@ class FakeKey(BaseModel): self.value = value self.last_modified = datetime.datetime.utcnow() self.acl = get_canned_acl('private') + self.website_redirect_location = None self._storage_class = storage if storage else "STANDARD" self._metadata = {} self._expiry = None @@ -103,6 +104,9 @@ class FakeKey(BaseModel): if self._is_versioned: res['x-amz-version-id'] = str(self._version_id) + if self.website_redirect_location: + res['x-amz-website-redirect-location'] = self.website_redirect_location + return res @property diff --git a/moto/s3/responses.py b/moto/s3/responses.py index 59cd9d322..5309e7ffc 100644 --- a/moto/s3/responses.py +++ b/moto/s3/responses.py @@ -570,6 +570,7 @@ class ResponseObject(_TemplateEnvironmentMixin): metadata = metadata_from_headers(request.headers) new_key.set_metadata(metadata) new_key.set_acl(acl) + new_key.website_redirect_location = request.headers.get('x-amz-website-redirect-location') template = self.response_template(S3_OBJECT_RESPONSE) response_headers.update(new_key.response_dict) diff --git a/tests/test_s3/test_s3.py b/tests/test_s3/test_s3.py index 36d4bdbc4..406494eba 100644 --- a/tests/test_s3/test_s3.py +++ b/tests/test_s3/test_s3.py @@ -1046,6 +1046,19 @@ def test_boto3_key_etag(): resp = s3.get_object(Bucket='mybucket', Key='steve') resp['ETag'].should.equal('"d32bda93738f7e03adb22e66c90fbc04"') +@mock_s3 +def test_website_redirect_location(): + s3 = boto3.client('s3', region_name='us-east-1') + s3.create_bucket(Bucket='mybucket') + + s3.put_object(Bucket='mybucket', Key='steve', Body=b'is awesome') + resp = s3.get_object(Bucket='mybucket', Key='steve') + resp.get('WebsiteRedirectLocation').should.be.none + + url = 'https://github.com/spulec/moto' + s3.put_object(Bucket='mybucket', Key='steve', Body=b'is awesome', WebsiteRedirectLocation=url) + resp = s3.get_object(Bucket='mybucket', Key='steve') + resp['WebsiteRedirectLocation'].should.equal(url) @mock_s3 def test_boto3_list_keys_xml_escaped():