Skip to content

Commit

Permalink
Request matching - return known requests first (#3793)
Browse files Browse the repository at this point in the history
  • Loading branch information
bblommers committed Aug 28, 2021
1 parent 9f47c0c commit 8f79edb
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 6 deletions.
44 changes: 43 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,48 @@ jobs:
fail_ci_if_error: false
flags: servertests

test_responses:
name: Test Responses==0.12.0
runs-on: ubuntu-latest
needs: lint
strategy:
matrix:
python-version: [ 3.7, 3.8 ]

steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Get pip cache dir
id: pip-cache
run: |
echo "::set-output name=dir::$(pip cache dir)"
- name: pip cache
uses: actions/cache@v2
with:
path: ${{ steps.pip-cache.outputs.dir }}
key: pip-${{ matrix.python-version }}-${{ hashFiles('**/setup.py') }}-4
- name: Install project dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements-dev.txt
pip install pytest-cov
pip install responses==0.12.0
pip install "coverage<=4.5.4"
- name: Test core-logic with responses==0.12.0
run: |
pytest -sv --cov=moto --cov-report xml ./tests/test_core
- name: "Upload coverage to Codecov"
if: ${{ github.repository == 'spulec/moto'}}
uses: codecov/codecov-action@v1
with:
fail_ci_if_error: true
flags: test_responses

terraform:
name: Terraform Tests
runs-on: ubuntu-latest
Expand Down Expand Up @@ -263,7 +305,7 @@ jobs:
deploy:
name: Deploy
runs-on: ubuntu-latest
needs: [test, testserver, terraform]
needs: [test, testserver, terraform, test_responses]
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/master' && github.repository == 'spulec/moto' }}
strategy:
matrix:
Expand Down
2 changes: 1 addition & 1 deletion codecov.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
codecov:
notify:
# Leave a GitHub comment after all builds have passed
after_n_builds: 8
after_n_builds: 10
coverage:
status:
project:
Expand Down
31 changes: 28 additions & 3 deletions moto/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,22 +286,47 @@ def _url_matches(self, url, other, match_querystring=False):


def _find_first_match_legacy(self, request):
matches = []
for i, match in enumerate(self._matches):
if match.matches(request):
return match

matches.append(match)

# Look for implemented callbacks first
implemented_matches = [
m
for m in matches
if type(m) is not CallbackResponse or m.callback != not_implemented_callback
]
if implemented_matches:
return implemented_matches[0]
elif matches:
# We had matches, but all were of type not_implemented_callback
return matches[0]
return None


def _find_first_match(self, request):
matches = []
match_failed_reasons = []
for i, match in enumerate(self._matches):
match_result, reason = match.matches(request)
if match_result:
return match, match_failed_reasons
matches.append(match)
else:
match_failed_reasons.append(reason)

# Look for implemented callbacks first
implemented_matches = [
m
for m in matches
if type(m) is not CallbackResponse or m.callback != not_implemented_callback
]
if implemented_matches:
return implemented_matches[0], []
elif matches:
# We had matches, but all were of type not_implemented_callback
return matches[0], match_failed_reasons

return None, match_failed_reasons


Expand Down
29 changes: 28 additions & 1 deletion tests/test_core/test_request_mocking.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import sure # noqa

import boto3
from moto import mock_sqs, settings
from moto import mock_s3, mock_sts, mock_sqs, settings


@mock_sqs
Expand All @@ -23,3 +23,30 @@ def test_requests_to_amazon_subdomains_dont_work():
res = requests.get("https://fakeservice.amazonaws.com/foo/bar")
assert res.content == b"The method is not implemented"
assert res.status_code == 400


@mock_sts
@mock_s3
def test_decorator_ordering():
"""
https://github.com/spulec/moto/issues/3790#issuecomment-803979809
"""
bucket_name = "banana-slugs"
key = "trash-file"
region = "us-east-1"
client = boto3.client("s3", region_name=region)
s3 = boto3.resource("s3", region_name=region)
bucket = s3.Bucket(bucket_name)
bucket.create()
bucket.put_object(Body=b"ABCD", Key=key)
presigned_url = client.generate_presigned_url(
ClientMethod=client.get_object.__name__,
Params={
"Bucket": bucket_name,
"Key": key,
"ResponseContentDisposition": "attachment;filename=bar",
},
)

resp = requests.get(presigned_url)
resp.status_code.should.equal(200)

0 comments on commit 8f79edb

Please sign in to comment.