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: 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
def test_etag_set_etag_method_not_allowed_warning(self, app, method, etag_disabled): app.config["ETAG_DISABLED"] = etag_disabled blp = Blueprint("test", __name__) with pytest.warns(None) as record: with app.test_request_context("/", method=method): blp.set_etag(None) if method in HTTP_METHODS_ALLOWING_SET_ETAG: assert not record else: assert len(record) == 1 assert record[0].category == UserWarning assert str(record[0].message) == ( f"ETag cannot be set on {method} request.")
def test_etag_set_etag(self, app, schemas, method, 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) with app.test_request_context("/", method=method): 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() with app.test_request_context( "/", method=method, headers={"If-None-Match": etag}, ): if not etag_disabled: if method in ["GET", "HEAD"]: with pytest.raises(NotModified): blp.set_etag(item) else: blp.set_etag(item) assert "etag" not in _get_etag_ctx() with app.test_request_context( "/", method=method, headers={"If-None-Match": etag_with_schema}, ): if not etag_disabled: if method in ["GET", "HEAD"]: with pytest.raises(NotModified): blp.set_etag(item, etag_schema) else: blp.set_etag(item, etag_schema) assert "etag" not in _get_etag_ctx() with app.test_request_context( "/", method=method, 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()
def test_etag_set_etag(self, app, schemas, method, 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) with app.test_request_context('/', method=method): 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() with app.test_request_context( '/', method=method, headers={'If-None-Match': etag}, ): if not etag_disabled: if method in ['GET', 'HEAD']: with pytest.raises(NotModified): blp.set_etag(item) else: blp.set_etag(item) assert 'etag' not in _get_etag_ctx() with app.test_request_context( '/', method=method, headers={'If-None-Match': etag_with_schema}, ): if not etag_disabled: if method in ['GET', 'HEAD']: with pytest.raises(NotModified): blp.set_etag(item, etag_schema) else: blp.set_etag(item, etag_schema) assert 'etag' not in _get_etag_ctx() with app.test_request_context( '/', method=method, 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()