Last active
July 5, 2017 11:21
-
-
Save chbndrhnns/0bd5056a77687247bacf4cafa49700ad to your computer and use it in GitHub Desktop.
mock HTTPRequest in python
This file contains 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
# 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