Created
November 15, 2016 18:36
-
-
Save AaronTorgerson/30ca5cca4bf1a31667646cae3660cc8c to your computer and use it in GitHub Desktop.
Prevent un-mocked requests calls mixin (for use on a class that inherits from django.test.TestCase)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import requests | |
| def _fake_requests_send(self, request, **kwargs): | |
| raise Exception("Un-mocked request! request: {0}".format(request.__dict__)) | |
| _real_requests_send = requests.sessions.Session.send | |
| class PreventUnmockedRequestsMixin(object): | |
| """ | |
| Include this mixin into TestCase implementations to prevent HTTP calls via the `requests` library. | |
| """ | |
| def setUp(self): | |
| # hobble requests to prevent un-mocked HTTP calls | |
| requests.sessions.Session.send = _fake_requests_send | |
| def tearDown(self): | |
| # un-hobble requests | |
| requests.sessions.Session.send = _real_requests_send |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment