2020-06-11 00:54:34 +02:00
|
|
|
import requests
|
|
|
|
import responses
|
|
|
|
|
2020-01-22 17:41:49 +01:00
|
|
|
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):
|
2021-02-12 08:20:45 +01:00
|
|
|
result = requests.request("GET", "https://www.google.com")
|
2020-01-22 17:41:49 +01:00
|
|
|
|
|
|
|
# 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:
|
2021-02-12 08:19:30 +01:00
|
|
|
requests_mock.add(
|
|
|
|
responses.GET,
|
2021-02-12 08:20:45 +01:00
|
|
|
"https://www.google.com",
|
|
|
|
body="{}",
|
2021-02-12 08:19:30 +01:00
|
|
|
status=200,
|
2021-02-12 08:20:45 +01:00
|
|
|
content_type="application/json",
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
result = requests.request("GET", "https://www.google.com")
|
2020-01-22 17:41:49 +01:00
|
|
|
self.assertEqual(result.status_code, 200)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(result.text, "{}")
|