zulip/zerver/tests/test_internet.py

26 lines
947 B
Python
Raw Normal View History

import requests
import responses
from zerver.lib.test_classes import ZulipTestCase
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, "{}")