Merge pull request #2772 from bblommers/bugfix/#2567

Bugfix - When mocking URLs, always return the first match
This commit is contained in:
Steve Pulec 2020-03-07 10:33:34 -06:00 committed by GitHub
commit 3dca5a3fe8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 2 deletions

View file

@ -7,6 +7,7 @@ import inspect
import os
import re
import six
import types
from io import BytesIO
from collections import defaultdict
from botocore.handlers import BUILTIN_HANDLERS
@ -217,12 +218,29 @@ botocore_mock = responses.RequestsMock(
assert_all_requests_are_fired=False,
target="botocore.vendored.requests.adapters.HTTPAdapter.send",
)
responses_mock = responses._default_mock
# Add passthrough to allow any other requests to work
# Since this uses .startswith, it applies to http and https requests.
responses_mock.add_passthru("http")
def _find_first_match(self, request):
for i, match in enumerate(self._matches):
if match.matches(request):
return match
return None
# 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
# - 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
# Fix: Always return the first match
responses_mock._find_match = types.MethodType(_find_first_match, responses_mock)
BOTOCORE_HTTP_METHODS = ["GET", "DELETE", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"]