Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

responses_mock.remove() causes infinite loop #8449

Open
ralphie0112358 opened this issue Dec 30, 2024 · 1 comment
Open

responses_mock.remove() causes infinite loop #8449

ralphie0112358 opened this issue Dec 30, 2024 · 1 comment

Comments

@ralphie0112358
Copy link

moto.core.responses_custom_registry.CustomRegistry does not implement remove() method, code will fallback to inherited responses.registries.FirstMatchRegistry

test env is current moto master (release 5.0.25) using make init

here is a test case which fails due to infinite loop

import moto
import requests
import responses
from unittest import TestCase


class TestBad(TestCase):

    @responses.activate
    @moto.mock_aws
    def test_bad(self):
        from moto.core.models import responses_mock

        responses_mock.get(url="https://example.com/", body="test")
        resp = requests.get("https://example.com/")
        assert resp.text == "test"

        # causes infinite loop
        responses_mock.remove("GET", "https://example.com/")


if __name__ == "__main__":
    import unittest
    unittest.main()

suggested fix

    def remove(self, response: responses.BaseResponse) -> list[responses.BaseResponse]:
        removed_responses = []
        registered = self._registered[response.method]
        while response in registered:
            registered.remove(response)
            removed_responses.append(response)
        return removed_responses
@ralphie0112358
Copy link
Author

also, .replace is not implemented. suggested implementation

def replace(self, response: responses.BaseResponse) -> responses.BaseResponse:
    registered = self._registered[response.method]
    try:
        index = registered.index(response)
    except ValueError:
        raise ValueError(f"Response is not registered for URL {response.url}")
    registered[index] = response
    return response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant