本文整理汇总了Python中werkzeug.http.quote_etag函数的典型用法代码示例。如果您正苦于以下问题:Python quote_etag函数的具体用法?Python quote_etag怎么用?Python quote_etag使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了quote_etag函数的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_etags
def test_etags(self):
assert http.quote_etag("foo") == '"foo"'
assert http.quote_etag("foo", True) == 'w/"foo"'
assert http.unquote_etag('"foo"') == ("foo", False)
assert http.unquote_etag('w/"foo"') == ("foo", True)
es = http.parse_etags('"foo", "bar", w/"baz", blar')
assert sorted(es) == ["bar", "blar", "foo"]
assert "foo" in es
assert "baz" not in es
assert es.contains_weak("baz")
assert "blar" in es
assert es.contains_raw('w/"baz"')
assert es.contains_raw('"foo"')
assert sorted(es.to_header().split(", ")) == ['"bar"', '"blar"', '"foo"', 'w/"baz"']
示例2: test_exception_header_forwarded
def test_exception_header_forwarded(self):
'''Ensure that HTTPException's headers are extended properly'''
self.app.config['DEBUG'] = True
api = restplus.Api(self.app)
class NotModified(HTTPException):
code = 304
def __init__(self, etag, *args, **kwargs):
super(NotModified, self).__init__(*args, **kwargs)
self.etag = quote_etag(etag)
def get_headers(self, *args, **kwargs):
return [('ETag', self.etag)]
@api.route('/foo')
class Foo1(restplus.Resource):
def get(self):
abort(304, etag='myETag')
abort.mapping.update({304: NotModified})
with self.app.test_client() as client:
foo = client.get('/foo')
self.assertEquals(foo.get_etag(),
unquote_etag(quote_etag('myETag')))
示例3: test_exception_header_forwarded
def test_exception_header_forwarded(self):
"""Test that HTTPException's headers are extended properly"""
app = Flask(__name__)
app.config['DEBUG'] = True
api = flask_restful.Api(app)
class NotModified(HTTPException):
code = 304
def __init__(self, etag, *args, **kwargs):
super(NotModified, self).__init__(*args, **kwargs)
self.etag = quote_etag(etag)
def get_headers(self, *args, **kwargs):
"""Get a list of headers."""
return [('ETag', self.etag)]
class Foo1(flask_restful.Resource):
def get(self):
flask_abort(304, etag='myETag')
api.add_resource(Foo1, '/foo')
_aborter.mapping.update({304: NotModified})
with app.test_client() as client:
foo = client.get('/foo')
self.assertEquals(foo.get_etag(),
unquote_etag(quote_etag('myETag')))
示例4: build_etag
def build_etag(self, response, include_etag=True, **kwargs):
"""
Add an etag to the response body.
Uses spooky where possible because it is empirically fast and well-regarded.
See: http://blog.reverberate.org/2012/01/state-of-hash-functions-2012.html
"""
if not include_etag:
return
if not spooky:
# use built-in md5
response.add_etag()
return
# use spooky
response.headers["ETag"] = quote_etag(
hexlify(
spooky.hash128(
response.get_data(),
).to_bytes(16, "little"),
).decode("utf-8"),
)
示例5: response
def response(self, request, data, etag=None, cache_policy=None):
"""Renders `data` to a JSON response.
An ETag may be specified. When it is not specified one will be generated
based on the data.
The caching policy can be optionally configured. By default it takes the
policy from the controller object: `cache_policy`.
"""
if etag is None and data is not None:
etag = self.etag(data)
# FIXME: Check content-type headers
if data is None:
if etag is None:
raise TypeError('response requires an etag when '
'the response body is None')
resp = Response(status=304, content_type='application/json')
else:
# Avoid sending the resource when an ETag matches
request_etags = parse_etags(
request.environ.get('HTTP_IF_NONE_MATCH'))
if request_etags.contains(etag):
resp = Response(status=304, content_type='application/json')
# Render the given data to a response object
else:
resp = Response(self.data_encoder.encode(data), content_type='application/json')
resp.headers['ETag'] = quote_etag(etag)
if cache_policy is None:
cache_policy = self.cache_policy
return cache_policy(resp)
示例6: check_normal_response
def check_normal_response(res, method):
if method != client.head:
parsed = json.loads(res.get_data(as_text=True))
expected = {'id': 1, 'method': parsed['method']}
assert parsed == expected
# check that the right method was called
assert method_names[parsed['method']] == method
assert res.content_type == 'application/json'
assert res.status_code == 200
# check that the ETag is correct
assert unquote_etag(res.headers['ETag']) == \
unquote_etag(quote_etag('abc'))
示例7: serve_file
def serve_file(path):
headers = {}
st = os.stat(path)
etag = 'clay-{0}-{1}-{2}'.format(
os.path.getmtime(path),
os.path.getsize(path),
adler32(path.encode('utf-8')) & 0xffffffff
)
headers['ETag'] = quote_etag(etag)
# Set the Last-Modified response header, so that
# modified-since validation code can work.
headers['Last-Modified'] = httputil.HTTPDate(st.st_mtime)
_, ext = os.path.splitext(path)
content_type = mimetypes.types_map.get(ext, None)
headers['Content-Type'] = content_type or 'text/plain'
fileobj = open(path, 'rb')
return serve_fileobj(fileobj, headers, st.st_size)
示例8: test_exception_header_forwarded
def test_exception_header_forwarded(self, app, client):
'''Ensure that HTTPException's headers are extended properly'''
api = restplus.Api(app)
class NotModified(HTTPException):
code = 304
def __init__(self, etag, *args, **kwargs):
super(NotModified, self).__init__(*args, **kwargs)
self.etag = quote_etag(etag)
def get_headers(self, *args, **kwargs):
return [('ETag', self.etag)]
custom_abort = Aborter(mapping={304: NotModified})
@api.route('/foo')
class Foo1(restplus.Resource):
def get(self):
custom_abort(304, etag='myETag')
foo = client.get('/foo')
assert foo.get_etag() == unquote_etag(quote_etag('myETag'))
示例9: __init__
def __init__(self, etag, *args, **kwargs):
super(NotModified, self).__init__(*args, **kwargs)
self.etag = quote_etag(etag)
示例10: set_etag
def set_etag(self, etag, weak=False):
"""Set the etag, and override the old one if there was one."""
self.headers['ETag'] = quote_etag(etag, weak)
示例11: check_304_response
def check_304_response(res):
assert res.status_code == 304
# check that the ETag is correct
assert unquote_etag(res.headers['ETag']) == \
unquote_etag(quote_etag('abc'))
示例12: set_etag
def set_etag(self, etag, weak = False):
self.headers['ETag'] = quote_etag(etag, weak)