async def bot_fixture(event_loop): """ Bot fixture """ pook.on() _bot = Bot(API_TOKEN) yield _bot await _bot.close() pook.off()
def test_persistent(): pook.on() pook.get("https://www.example.com").persist().reply(200).json( {"data": True}) for counter in range(5): res = requests.get("https://www.example.com") assert res.json().get('data') pook.off()
def test_pook_whitelist(): pook.on() # pook.enable_network("www.example.com") # Broken, at least on Python 3.7 pook.enable_network() res = requests.get( "https://www.example.com") # Unmatched, so it is redirected with pytest.raises(Exception): assert res.json() pook.off()
def client(): import pook # Alternatively, one could use pook decorators, etc. pook.on() # Still need to write this for every API call in the server or at least this test suite (pook.get("https://www.behance.net/v2/projects?api_key=u_n_m_o_c_k_200") .persist() .reply(200) .json({"projects": "lazy"})) yield app.test_client() pook.off()
def test_pook_content_matching(): pook.on() pook.get("https://www.example.com").times(2).body("abc").reply(200).json( {"data": True}) res = requests.get("https://www.example.com", data="abc") assert res.json().get('data') with pytest.raises(Exception): requests.get("https://www.example.com", data="def") # Will throw once again pook.off()
def test_pook_pending(): pook.on() pook.get("https://www.example.com").times(2).reply(200).json( {"data": True}) for counter in [1, 0]: res = requests.get("https://www.example.com") assert res.json().get('data') assert pook.pending() == counter with pytest.raises(Exception): # Will throw as there's no mocks res = requests.get("https://www.example.com") pook.off()
}).json({'body': 'FIRST'})) # Register with same URL but diff BODY (pook.post('httpbin.org/post').json({ 'foo': 'ha' }).type('json').header('Client', 'requests').reply(204).headers({ 'server': 'pook' }).json({'body': 'SECOND'})) res = requests.post('http://httpbin.org/post', data=json.dumps({'foo': 'bar'}), headers={ 'Client': 'requests', 'Content-Type': 'application/json' }) print(res.status_code) print(res.headers) print(res.content) res = requests.post('http://httpbin.org/post', data=json.dumps({'foo': 'ha'}), headers={ 'Client': 'requests', 'Content-Type': 'application/json' }) print(res.status_code) print(res.headers) print(res.content) pook.off()
def test_pook_status(): pook.on() assert pook.isactive() pook.off() assert not pook.isactive()
def test_pook_off(): pook.off() # Called off. with pytest.raises(pook.exceptions.PookNoMatches): # Still raises even though called off! res = requests.get("http://www.i-forgot-to-set-my-env-variable.com/")