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'))
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"']
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"']
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"']
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') flask_abort.mapping.update({304: NotModified}) with app.test_client() as client: foo = client.get('/foo') self.assertEquals(foo.get_etag(), unquote_etag(quote_etag('myETag')))
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')))
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')))
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"', ]
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'))
def get_etag(self): return unquote_etag(self.headers.get('ETag'))
def get_etag(self) -> t.Union[t.Tuple[str, bool], t.Tuple[None, None]]: """Return a tuple in the form ``(etag, is_weak)``. If there is no ETag the return value is ``(None, None)``. """ return unquote_etag(self.headers.get("ETag"))
def get_etag(self): """Return a tuple in the form ``(etag, is_weak)``. If there is no ETag the return value is ``(None, None)``. """ return unquote_etag(self.headers.get('ETag'))
def contains_raw(self, etag): etag, weak = unquote_etag(etag) if weak: return self.contains_weak(etag) return self.contains(etag)
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'))
def get_etag(self): return unquote_etag(self.headers.get("ETag"))