Fix failures with latest responses library (0.12.1) (#3466)
* Fix failures with latest responses library (0.12.1) * Detect version of responses library and supply a compatible monkey patch depending on version * Seperate responses_mock._find_match monkey patchs depending on reponses lib version to improve readability Co-authored-by: Guillermo Arribas <garribas@atlassian.com>
This commit is contained in:
parent
6107561717
commit
7749c1f757
@ -5,6 +5,7 @@ from __future__ import absolute_import
|
|||||||
import functools
|
import functools
|
||||||
import inspect
|
import inspect
|
||||||
import os
|
import os
|
||||||
|
import pkg_resources
|
||||||
import re
|
import re
|
||||||
import six
|
import six
|
||||||
import types
|
import types
|
||||||
@ -14,6 +15,7 @@ from collections import defaultdict
|
|||||||
from botocore.config import Config
|
from botocore.config import Config
|
||||||
from botocore.handlers import BUILTIN_HANDLERS
|
from botocore.handlers import BUILTIN_HANDLERS
|
||||||
from botocore.awsrequest import AWSResponse
|
from botocore.awsrequest import AWSResponse
|
||||||
|
from distutils.version import LooseVersion
|
||||||
from six.moves.urllib.parse import urlparse
|
from six.moves.urllib.parse import urlparse
|
||||||
from werkzeug.wrappers import Request
|
from werkzeug.wrappers import Request
|
||||||
|
|
||||||
@ -28,6 +30,7 @@ from .utils import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
ACCOUNT_ID = os.environ.get("MOTO_ACCOUNT_ID", "123456789012")
|
ACCOUNT_ID = os.environ.get("MOTO_ACCOUNT_ID", "123456789012")
|
||||||
|
RESPONSES_VERSION = pkg_resources.get_distribution("responses").version
|
||||||
|
|
||||||
|
|
||||||
class BaseMockAWS(object):
|
class BaseMockAWS(object):
|
||||||
@ -251,7 +254,7 @@ responses_mock = responses._default_mock
|
|||||||
responses_mock.add_passthru("http")
|
responses_mock.add_passthru("http")
|
||||||
|
|
||||||
|
|
||||||
def _find_first_match(self, request):
|
def _find_first_match_legacy(self, request):
|
||||||
for i, match in enumerate(self._matches):
|
for i, match in enumerate(self._matches):
|
||||||
if match.matches(request):
|
if match.matches(request):
|
||||||
return match
|
return match
|
||||||
@ -259,11 +262,28 @@ def _find_first_match(self, request):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def _find_first_match(self, request):
|
||||||
|
match_failed_reasons = []
|
||||||
|
for i, match in enumerate(self._matches):
|
||||||
|
match_result, reason = match.matches(request)
|
||||||
|
if match_result:
|
||||||
|
return match, match_failed_reasons
|
||||||
|
else:
|
||||||
|
match_failed_reasons.append(reason)
|
||||||
|
|
||||||
|
return None, match_failed_reasons
|
||||||
|
|
||||||
|
|
||||||
# Modify behaviour of the matcher to only/always return the first match
|
# Modify behaviour of the matcher to only/always return the first match
|
||||||
# Default behaviour is to return subsequent matches for subsequent requests, which leads to https://github.com/spulec/moto/issues/2567
|
# Default behaviour is to return subsequent matches for subsequent requests, which leads to https://github.com/spulec/moto/issues/2567
|
||||||
# - First request matches on the appropriate S3 URL
|
# - First request matches on the appropriate S3 URL
|
||||||
# - Same request, executed again, will be matched on the subsequent match, which happens to be the catch-all, not-yet-implemented, callback
|
# - Same request, executed again, will be matched on the subsequent match, which happens to be the catch-all, not-yet-implemented, callback
|
||||||
# Fix: Always return the first match
|
# Fix: Always return the first match
|
||||||
|
if LooseVersion(RESPONSES_VERSION) < LooseVersion("0.12.1"):
|
||||||
|
responses_mock._find_match = types.MethodType(
|
||||||
|
_find_first_match_legacy, responses_mock
|
||||||
|
)
|
||||||
|
else:
|
||||||
responses_mock._find_match = types.MethodType(_find_first_match, responses_mock)
|
responses_mock._find_match = types.MethodType(_find_first_match, responses_mock)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user