async def test_set_etag_method_not_allowed_warning(self, app, method, etag_disabled): app.config['ETAG_DISABLED'] = etag_disabled blp = Blueprint('test', __name__) with mock.patch.object(app.logger, 'warning') as mock_warning: async with app.test_request_context('/', method=method): blp.set_etag(None) if method in ['GET', 'HEAD', 'POST', 'PUT', 'PATCH']: assert not mock_warning.called else: assert mock_warning.called
async def test_etag_set_etag(self, app, schemas, etag_disabled): app.config['ETAG_DISABLED'] = etag_disabled blp = Blueprint('test', __name__) etag_schema = schemas.DocEtagSchema item = {'item_id': 1, 'db_field': 0} etag = blp._generate_etag(item) etag_with_schema = blp._generate_etag(item, etag_schema) async with app.test_request_context('/'): blp.set_etag(item) if not etag_disabled: assert _get_etag_ctx()['etag'] == etag del _get_etag_ctx()['etag'] else: assert 'etag' not in _get_etag_ctx() async with app.test_request_context('/', headers={'If-None-Match': etag}): if not etag_disabled: with pytest.raises(NotModified): blp.set_etag(item) else: blp.set_etag(item) assert 'etag' not in _get_etag_ctx() async with app.test_request_context( '/', headers={'If-None-Match': etag_with_schema}): if not etag_disabled: with pytest.raises(NotModified): blp.set_etag(item, etag_schema) else: blp.set_etag(item, etag_schema) assert 'etag' not in _get_etag_ctx() async with app.test_request_context('/', headers={'If-None-Match': 'dummy'}): if not etag_disabled: blp.set_etag(item) assert _get_etag_ctx()['etag'] == etag del _get_etag_ctx()['etag'] blp.set_etag(item, etag_schema) assert _get_etag_ctx()['etag'] == etag_with_schema del _get_etag_ctx()['etag'] else: blp.set_etag(item) assert 'etag' not in _get_etag_ctx() blp.set_etag(item, etag_schema) assert 'etag' not in _get_etag_ctx()