mirror of
https://github.com/zulip/zulip.git
synced 2025-11-02 04:53:36 +00:00
responses is an module analogous to httpretty for mocking external URLs, with a very similar interface (potentially cleaner in that it makes use of context managers). The most important (in the moment) problem with httpretty is that it breaks the ability to use redis in parts of code where httpretty is enabled. From more research, the module in general has tendency to have various troublesome bugs with breaking URLs that it shouldn't be affecting, caused by it working at the socket interface layer. While those issues could be fixed, responses seems to be less buggy (based on both third-party reports like ckan/ckan#4755 and our own experience in removing workarounds for bugs in httpretty) and is more actively maintained.
21 lines
911 B
Python
21 lines
911 B
Python
from zerver.lib.test_classes import ZulipTestCase
|
|
|
|
import responses
|
|
import requests
|
|
|
|
class ResponsesTest(ZulipTestCase):
|
|
def test_responses(self) -> None:
|
|
# With our test setup, accessing the internet should be blocked.
|
|
with self.assertRaises(Exception):
|
|
result = requests.request('GET', 'https://www.google.com')
|
|
|
|
# A test can invoke its own responses.RequestsMock context manager
|
|
# and register URLs to mock, accessible from within the context.
|
|
with responses.RequestsMock() as requests_mock:
|
|
requests_mock.add(responses.GET, 'https://www.google.com',
|
|
body='{}', status=200,
|
|
content_type='application/json')
|
|
result = requests.request('GET', 'https://www.google.com')
|
|
self.assertEqual(result.status_code, 200)
|
|
self.assertEqual(result.text, '{}')
|