Exemplo n.º 1
0
 def put(self, new_item, item_id):
     item = self._get_item(item_id)
     # Check ETag is a manual action, no shema used
     check_etag(item['db_field'])
     new_item = collection.put(item_id, new_item)
     # Compute ETag using arbitrary data and no schema
     set_etag(new_item['db_field'])
     return new_item
Exemplo n.º 2
0
    def test_set_etag_method_not_allowed_warning(self, app, method):

        with mock.patch.object(app.logger, 'warning') as mock_warning:
            with app.test_request_context('/', method=method):
                set_etag(None)
            if method in ['GET', 'HEAD', 'POST', 'PUT', 'PATCH']:
                assert not mock_warning.called
            else:
                assert mock_warning.called
Exemplo n.º 3
0
    def test_etag_set_etag(self, app, schemas):

        etag_schema = schemas.DocEtagSchema
        item = {'item_id': 1, 'db_field': 0}

        etag = _generate_etag(item)
        etag_with_schema = _generate_etag(item, etag_schema)

        with app.test_request_context('/'):
            set_etag(item)
            if is_etag_enabled(app):
                assert _get_etag_ctx()['etag'] == etag
                del _get_etag_ctx()['etag']
            else:
                assert 'etag' not in _get_etag_ctx()
            disable_etag_for_request()
            set_etag(item)
            assert 'etag' not in _get_etag_ctx()
        with app.test_request_context('/', headers={'If-None-Match': etag}):
            if is_etag_enabled(app):
                with pytest.raises(NotModified):
                    set_etag(item)
            else:
                set_etag(item)
                assert 'etag' not in _get_etag_ctx()
            disable_etag_for_request()
            set_etag(item)
            assert 'etag' not in _get_etag_ctx()
        with app.test_request_context(
                '/', headers={'If-None-Match': etag_with_schema}):
            if is_etag_enabled(app):
                with pytest.raises(NotModified):
                    set_etag(item, etag_schema)
            else:
                set_etag(item, etag_schema)
                assert 'etag' not in _get_etag_ctx()
            disable_etag_for_request()
            set_etag(item, etag_schema)
            assert 'etag' not in _get_etag_ctx()
        with app.test_request_context('/', headers={'If-None-Match': 'dummy'}):
            if is_etag_enabled(app):
                set_etag(item)
                assert _get_etag_ctx()['etag'] == etag
                del _get_etag_ctx()['etag']
                set_etag(item, etag_schema)
                assert _get_etag_ctx()['etag'] == etag_with_schema
                del _get_etag_ctx()['etag']
            else:
                set_etag(item)
                assert 'etag' not in _get_etag_ctx()
                set_etag(item, etag_schema)
                assert 'etag' not in _get_etag_ctx()
            disable_etag_for_request()
            set_etag(item)
            assert 'etag' not in _get_etag_ctx()
            set_etag(item, etag_schema)
            assert 'etag' not in _get_etag_ctx()
Exemplo n.º 4
0
 def get(self, item_id):
     item = self._get_item(item_id)
     # Compute ETag using arbitrary data and no schema
     set_etag(item['db_field'])
     return item
Exemplo n.º 5
0
 def post(self, new_item):
     # Compute ETag using arbitrary data and no schema
     set_etag(new_item['db_field'])
     return collection.post(new_item)