1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
from urllib.request import Request, urlopen
import pytest
import pook
from pook import Engine, MockEngine
from pook.interceptors import BaseInterceptor
class Interceptor(BaseInterceptor):
def activate(self):
self.active = True
def disable(self):
self.active = False
@pytest.fixture
def engine():
return MockEngine(Engine())
def test_mock_engine_instance(engine):
assert isinstance(engine.engine, Engine)
assert isinstance(engine.interceptors, list)
assert len(engine.interceptors) >= 2
def test_mock_engine_flush(engine):
assert len(engine.interceptors) >= 2
engine.flush_interceptors()
assert len(engine.interceptors) == 0
def test_mock_engine_interceptors(engine):
engine.flush_interceptors()
engine.add_interceptor(Interceptor)
assert len(engine.interceptors) == 1
assert isinstance(engine.interceptors[0], Interceptor)
engine.remove_interceptor("Interceptor")
assert len(engine.interceptors) == 0
def test_mock_engine_status(engine):
engine.flush_interceptors()
engine.add_interceptor(Interceptor)
assert len(engine.interceptors) == 1
interceptor = engine.interceptors[0]
engine.activate()
assert interceptor.active
engine.disable()
assert not interceptor.active
@pytest.mark.xfail(
reason="Pook cannot disambiguate the two mocks. Ideally it would try to find the most specific mock that matches, but that's not possible yet."
)
@pytest.mark.pook(allow_pending_mocks=True)
def test_mock_specificity(httpbin):
url404 = f"{httpbin.url}/status/404"
pook.get(url404).header_present("authorization").reply(201)
pook.get(url404).headers({"Authorization": "Bearer pook"}).reply(200)
res_with_headers = urlopen(
Request(url404, headers={"Authorization": "Bearer pook"})
)
assert res_with_headers.status == 200
|