Skip to content

Instantly share code, notes, and snippets.

@AaronTorgerson
Created November 15, 2016 18:36
Show Gist options
  • Select an option

  • Save AaronTorgerson/30ca5cca4bf1a31667646cae3660cc8c to your computer and use it in GitHub Desktop.

Select an option

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)
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