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 test_http_tutorial(): # Activate the HTTP mock engine pook.on() # Register a sample mock pook.get('server.org/foo?bar=baz', reply=200, response_headers={'Server': 'nginx'}, response_json={'foo': 'bar'}) # Perform HTTP request res = requests.get('http://server.org/foo?bar=baz') # Test response status to be OK res | should.be.ok # Or alternatively using the status code res | should.have.status(200) # Test request URL res | should.have.url.hostname('server.org') res | should.have.url.port(80) res | should.have.url.path('/foo') res | should.have.url.query.params({'bar': 'baz'}) # Test response body MIME content type res | should.have.content('json') # Test response headers (res | (should.have.header('Content-Type') .that.should.be.equal('application/json'))) res | should.have.header('Server').that.should.contain('nginx') # Test response body res | should.have.body.equal.to('{\n "foo": "bar"\n}') res | should.have.body.that.contains('foo') # Test response body length res | should.have.body.length.of(20) res | should.have.body.length.higher.than(10) # Test response JSON body res | should.have.json.equal.to({'foo': 'bar'}) res | should.have.json.have.key('foo') > should.be.equal.to('bar') # Validate response JSON bodies using JSONSchema res | should.implement.jsonschema({ '$schema': 'http://json-schema.org/draft-04/schema#', 'title': 'Response JSON', 'type': 'object', 'required': ['foo'], 'properties': { 'foo': { 'description': 'foo always means foo', 'type': 'string' } } })
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()
import json import pook import requests # Enable mock engine pook.on() (pook.post('httpbin.org/post') .json({'foo': 'bar'}) .type('json') .header('Client', 'requests') .reply(204) .headers({'server': 'pook'}) .json({'error': 'simulated'})) res = requests.post('http://httpbin.org/post', data=json.dumps({'foo': 'bar'}), headers={'Client': 'requests', 'Content-Type': 'application/json'}) print('Status:', res.status_code) print('Body:', res.json()) print('Is done:', pook.isdone()) print('Pending mocks:', pook.pending_mocks())
def test_pook_status(): pook.on() assert pook.isactive() pook.off() assert not pook.isactive()