From 8befcb6a48e5e8040058391190c3d6a50ea40c69 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Fri, 15 Jan 2021 01:12:41 -0800 Subject: [PATCH] Leave the global responses configuration alone (#3529) Instead of modifying responses._default_mock, create our own responses.RequestsMock object that we can modify as needed without interfering with other users of the responses library. Fixes #3264. Signed-off-by: Anders Kaseorg --- moto/apigateway/models.py | 3 ++- moto/core/models.py | 2 +- tests/test_apigateway/test_apigateway.py | 6 +++--- tests/test_sns/test_publishing_boto3.py | 4 ++-- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/moto/apigateway/models.py b/moto/apigateway/models.py index 4a44404a2..c8d2ae5f2 100644 --- a/moto/apigateway/models.py +++ b/moto/apigateway/models.py @@ -41,6 +41,7 @@ from .exceptions import ( RestAPINotFound, ModelNotFound, ) +from ..core.models import responses_mock STAGE_URL = "https://{api_id}.execute-api.{region_name}.amazonaws.com/{stage_name}" @@ -565,7 +566,7 @@ class RestAPI(BaseModel): ) for url in [stage_url_lower, stage_url_upper]: - responses._default_mock._matches.insert( + responses_mock._matches.insert( 0, responses.CallbackResponse( url=url, diff --git a/moto/core/models.py b/moto/core/models.py index 2cd67188a..3e1b4f54e 100644 --- a/moto/core/models.py +++ b/moto/core/models.py @@ -248,7 +248,7 @@ botocore_mock = responses.RequestsMock( target="botocore.vendored.requests.adapters.HTTPAdapter.send", ) -responses_mock = responses._default_mock +responses_mock = responses.RequestsMock(assert_all_requests_are_fired=False) # 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") diff --git a/tests/test_apigateway/test_apigateway.py b/tests/test_apigateway/test_apigateway.py index f85fd4a02..3b4c8ca64 100644 --- a/tests/test_apigateway/test_apigateway.py +++ b/tests/test_apigateway/test_apigateway.py @@ -8,9 +8,9 @@ import requests import sure # noqa from botocore.exceptions import ClientError -import responses from moto import mock_apigateway, mock_cognitoidp, settings from moto.core import ACCOUNT_ID +from moto.core.models import responses_mock import pytest @@ -1786,8 +1786,8 @@ def test_get_model_with_invalid_name(): @mock_apigateway def test_http_proxying_integration(): - responses.add( - responses.GET, "http://httpbin.org/robots.txt", body="a fake response" + responses_mock.add( + responses_mock.GET, "http://httpbin.org/robots.txt", body="a fake response" ) region_name = "us-west-2" diff --git a/tests/test_sns/test_publishing_boto3.py b/tests/test_sns/test_publishing_boto3.py index 797ccdaba..6ee5ef1cb 100644 --- a/tests/test_sns/test_publishing_boto3.py +++ b/tests/test_sns/test_publishing_boto3.py @@ -8,11 +8,11 @@ import re from freezegun import freeze_time import sure # noqa -import responses from botocore.exceptions import ClientError import pytest from moto import mock_sns, mock_sqs, settings from moto.core import ACCOUNT_ID +from moto.core.models import responses_mock from moto.sns import sns_backend MESSAGE_FROM_SQS_TEMPLATE = ( @@ -332,7 +332,7 @@ def test_publish_to_http(): json.loads.when.called_with(request.body.decode()).should_not.throw(Exception) return 200, {}, "" - responses.add_callback( + responses_mock.add_callback( method="POST", url="http://example.com/foobar", callback=callback )