Skip to content

Instantly share code, notes, and snippets.

@chbndrhnns
Last active July 5, 2017 11:21
Show Gist options
  • Save chbndrhnns/0bd5056a77687247bacf4cafa49700ad to your computer and use it in GitHub Desktop.
Save chbndrhnns/0bd5056a77687247bacf4cafa49700ad to your computer and use it in GitHub Desktop.
mock HTTPRequest in python
# source: https://stackoverflow.com/a/28507806
def mocked_requests_get(*args, **kwargs):
class MockResponse:
def __init__(self, json_data, status_code):
self.json_data = json_data
self.status_code = status_code
def json(self):
return self.json_data
if 'http://localhost:8082' in args[0]:
return MockResponse({"href": "http://localhost:8082", "links": []}, 200)
elif args[0] == 'http://someotherurl.com/anothertest.json':
return MockResponse({"key2": "value2"}, 200)
return MockResponse(None, 404)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment