python
pip install httpretty
python
import httpretty
import requests
def get_data():
response = requests.get('http://api.example.com/data')
return response.json()
with httpretty.enabled():
httpretty.register_uri(httpretty.GET, 'http://api.example.com/data',
body='{"message": "success"}',
content_type='application/json')
data = get_data()
python
with httpretty.enabled():
httpretty.register_uri(httpretty.GET, 'http://api.example.com/data',
body='{"message": "GET success"}',
content_type='application/json')
httpretty.register_uri(httpretty.POST, 'http://api.example.com/data',
body='{"message": "POST success"}',
content_type='application/json')
data_get = requests.get('http://api.example.com/data').json()
data_post = requests.post('http://api.example.com/data').json()
python
with httpretty.enabled():
httpretty.register_uri(httpretty.GET, 'http://api.example.com',
status=302,
location='http://new.example.com')
response = requests.get('http://api.example.com')
python
with httpretty.enabled():
httpretty.register_uri(httpretty.GET, 'http://api.example.com',
status=503)
response = requests.get('http://api.example.com')