Merge pull request #2398 from RDProjekt/leading-double-slashes

Allows leading // for mocked s3 paths (fix for #1637)
This commit is contained in:
Steve Pulec 2019-09-11 21:49:53 -05:00 committed by GitHub
commit 12ad7d0ded
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 2 deletions

View File

@ -15,4 +15,6 @@ url_paths = {
'{0}/(?P<key_or_bucket_name>[^/]+)/?$': S3ResponseInstance.ambiguous_response, '{0}/(?P<key_or_bucket_name>[^/]+)/?$': S3ResponseInstance.ambiguous_response,
# path-based bucket + key # path-based bucket + key
'{0}/(?P<bucket_name_path>[^/]+)/(?P<key_name>.+)': S3ResponseInstance.key_response, '{0}/(?P<bucket_name_path>[^/]+)/(?P<key_name>.+)': S3ResponseInstance.key_response,
# subdomain bucket + key with empty first part of path
'{0}//(?P<key_name>.*)$': S3ResponseInstance.key_response,
} }

View File

@ -174,10 +174,11 @@ def create_backend_app(service):
backend_app.url_map.converters['regex'] = RegexConverter backend_app.url_map.converters['regex'] = RegexConverter
backend = list(BACKENDS[service].values())[0] backend = list(BACKENDS[service].values())[0]
for url_path, handler in backend.flask_paths.items(): for url_path, handler in backend.flask_paths.items():
view_func = convert_flask_to_httpretty_response(handler)
if handler.__name__ == 'dispatch': if handler.__name__ == 'dispatch':
endpoint = '{0}.dispatch'.format(handler.__self__.__name__) endpoint = '{0}.dispatch'.format(handler.__self__.__name__)
else: else:
endpoint = None endpoint = view_func.__name__
original_endpoint = endpoint original_endpoint = endpoint
index = 2 index = 2
@ -191,7 +192,7 @@ def create_backend_app(service):
url_path, url_path,
endpoint=endpoint, endpoint=endpoint,
methods=HTTP_METHODS, methods=HTTP_METHODS,
view_func=convert_flask_to_httpretty_response(handler), view_func=view_func,
strict_slashes=False, strict_slashes=False,
) )

View File

@ -2,6 +2,7 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import datetime import datetime
import os
from six.moves.urllib.request import urlopen from six.moves.urllib.request import urlopen
from six.moves.urllib.error import HTTPError from six.moves.urllib.error import HTTPError
from functools import wraps from functools import wraps
@ -23,6 +24,7 @@ from freezegun import freeze_time
import six import six
import requests import requests
import tests.backport_assert_raises # noqa import tests.backport_assert_raises # noqa
from nose import SkipTest
from nose.tools import assert_raises from nose.tools import assert_raises
import sure # noqa import sure # noqa
@ -2991,3 +2993,28 @@ def test_accelerate_configuration_is_not_supported_when_bucket_name_has_dots():
AccelerateConfiguration={'Status': 'Enabled'}, AccelerateConfiguration={'Status': 'Enabled'},
) )
exc.exception.response['Error']['Code'].should.equal('InvalidRequest') exc.exception.response['Error']['Code'].should.equal('InvalidRequest')
def store_and_read_back_a_key(key):
s3 = boto3.client('s3', region_name='us-east-1')
bucket_name = 'mybucket'
body = b'Some body'
s3.create_bucket(Bucket=bucket_name)
s3.put_object(
Bucket=bucket_name,
Key=key,
Body=body
)
response = s3.get_object(Bucket=bucket_name, Key=key)
response['Body'].read().should.equal(body)
@mock_s3
def test_paths_with_leading_slashes_work():
store_and_read_back_a_key('/a-key')
@mock_s3
def test_root_dir_with_empty_name_works():
if os.environ.get('TEST_SERVER_MODE', 'false').lower() == 'true':
raise SkipTest('Does not work in server mode due to error in Workzeug')
store_and_read_back_a_key('/')