def test_purge_key_unsuccessful(self, monkeypatch, result): cacher = fastly.FastlyCache(api_key="an api key", service_id="the-service-id", purger=None) response = pretend.stub( raise_for_status=pretend.call_recorder(lambda: None), json=lambda: result) requests_post = pretend.call_recorder(lambda *a, **kw: response) monkeypatch.setattr(requests, "post", requests_post) with pytest.raises(fastly.UnsuccessfulPurgeError): cacher.purge_key("one") assert requests_post.calls == [ pretend.call( "https://api.fastly.com/service/the-service-id/purge/one", headers={ "Accept": "application/json", "Fastly-Key": "an api key", "Fastly-Soft-Purge": "1", }, ) ] assert response.raise_for_status.calls == [pretend.call()]
def test_adds_surrogate_key(self): request = pretend.stub() response = pretend.stub(headers={}) cacher = fastly.FastlyCache(api_key=None, service_id=None, purger=None) cacher.cache(["abc", "defg"], request, response) assert response.headers == {"Surrogate-Key": "abc defg"}
def test_purge(self, monkeypatch): purge_delay = pretend.call_recorder(lambda *a, **kw: None) cacher = fastly.FastlyCache(api_key="an api key", service_id="the-service-id", purger=purge_delay) cacher.purge(["one", "two"]) assert purge_delay.calls == [pretend.call("one"), pretend.call("two")]
def test_multiple_calls_to_cache_dont_overwrite_surrogate_keys(self): request = pretend.stub() response = pretend.stub(headers={}) cacher = fastly.FastlyCache(api_key=None, service_id=None, purger=None) cacher.cache(["abc"], request, response) cacher.cache(["defg"], request, response) assert response.headers == {"Surrogate-Key": "abc defg"}
def test_adds_surrogate_control(self): request = pretend.stub() response = pretend.stub(headers={}) cacher = fastly.FastlyCache() cacher.cache(["abc", "defg"], request, response, seconds=9123) assert response.headers == { "Surrogate-Key": "abc defg", "Surrogate-Control": "max-age=9123", }
def test_multiple_calls_with_different_requests(self): request_a = pretend.stub() request_b = pretend.stub() response_a = pretend.stub(headers={}) response_b = pretend.stub(headers={}) cacher = fastly.FastlyCache(api_key=None, service_id=None, purger=None) cacher.cache(["abc"], request_a, response_a) cacher.cache(["defg"], request_b, response_b) assert response_a.headers == {"Surrogate-Key": "abc"} assert response_b.headers == {"Surrogate-Key": "defg"}
def test_adds_surrogate_control(self): request = pretend.stub() response = pretend.stub(headers={}) cacher = fastly.FastlyCache(api_key=None, service_id=None, purger=None) cacher.cache( ["abc", "defg"], request, response, seconds=9123, stale_while_revalidate=4567, stale_if_error=2276, ) assert response.headers == { "Surrogate-Key": "abc defg", "Surrogate-Control": ("max-age=9123, stale-while-revalidate=4567, stale-if-error=2276"), }
def test_esi_include(self, path, cookies, expected): cacher = fastly.FastlyCache( api_key="an api key", service_id="the-service-id", ) request = pretend.stub( add_response_callback=pretend.call_recorder(lambda cb: None), ) assert cacher.esi_include(request, path, cookies=cookies) == \ '<esi:include src="{}" />'.format(expected) assert request.add_response_callback.calls == [pretend.call(mock.ANY)] cb_request, cb_response = pretend.stub(), pretend.stub(headers={}) cb = request.add_response_callback.calls[0].args[0] cb(cb_request, cb_response) if cookies: assert cb_response.headers == { "Warehouse-ESI-Vary": "Cookie", "Warehouse-ESI-Enable": "1", } else: assert cb_response.headers == {"Warehouse-ESI-Enable": "1"}
def test_purge_not_implemented(self): cacher = fastly.FastlyCache() with pytest.raises(NotImplementedError): cacher.purge(["one", "two"])