def test_accept(): request = wrappers.Request( { "HTTP_ACCEPT": "text/xml,application/xml,application/xhtml+xml," "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5", "HTTP_ACCEPT_CHARSET": "ISO-8859-1,utf-8;q=0.7,*;q=0.7", "HTTP_ACCEPT_ENCODING": "gzip,deflate", "HTTP_ACCEPT_LANGUAGE": "en-us,en;q=0.5", "SERVER_NAME": "eggs", "SERVER_PORT": "80", } ) assert request.accept_mimetypes == MIMEAccept( [ ("text/xml", 1), ("application/xml", 1), ("application/xhtml+xml", 1), ("image/png", 1), ("text/html", 0.9), ("text/plain", 0.8), ("*/*", 0.5), ] ) assert request.accept_charsets == CharsetAccept( [("ISO-8859-1", 1), ("utf-8", 0.7), ("*", 0.7)] ) assert request.accept_encodings == Accept([("gzip", 1), ("deflate", 1)]) assert request.accept_languages == LanguageAccept([("en-us", 1), ("en", 0.5)]) request = wrappers.Request( {"HTTP_ACCEPT": "", "SERVER_NAME": "example.org", "SERVER_PORT": "80"} ) assert request.accept_mimetypes == MIMEAccept()
def test_accept_mixin(): request = wrappers.Request( { "HTTP_ACCEPT": "text/xml,application/xml,application/xhtml+xml," "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5", "HTTP_ACCEPT_CHARSET": "ISO-8859-1,utf-8;q=0.7,*;q=0.7", "HTTP_ACCEPT_ENCODING": "gzip,deflate", "HTTP_ACCEPT_LANGUAGE": "en-us,en;q=0.5", } ) assert request.accept_mimetypes == MIMEAccept( [ ("text/xml", 1), ("image/png", 1), ("application/xml", 1), ("application/xhtml+xml", 1), ("text/html", 0.9), ("text/plain", 0.8), ("*/*", 0.5), ] ) strict_eq( request.accept_charsets, CharsetAccept([("ISO-8859-1", 1), ("utf-8", 0.7), ("*", 0.7)]), ) strict_eq(request.accept_encodings, Accept([("gzip", 1), ("deflate", 1)])) strict_eq(request.accept_languages, LanguageAccept([("en-us", 1), ("en", 0.5)])) request = wrappers.Request({"HTTP_ACCEPT": ""}) strict_eq(request.accept_mimetypes, MIMEAccept())
def test_accept_mixin(): request = wrappers.Request({ 'HTTP_ACCEPT': 'text/xml,application/xml,application/xhtml+xml,' 'text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5', 'HTTP_ACCEPT_CHARSET': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7', 'HTTP_ACCEPT_ENCODING': 'gzip,deflate', 'HTTP_ACCEPT_LANGUAGE': 'en-us,en;q=0.5' }) assert request.accept_mimetypes == MIMEAccept([('text/xml', 1), ('image/png', 1), ('application/xml', 1), ('application/xhtml+xml', 1), ('text/html', 0.9), ('text/plain', 0.8), ('*/*', 0.5)]) strict_eq(request.accept_charsets, CharsetAccept([('ISO-8859-1', 1), ('utf-8', 0.7), ('*', 0.7)])) strict_eq(request.accept_encodings, Accept([('gzip', 1), ('deflate', 1)])) strict_eq(request.accept_languages, LanguageAccept([('en-us', 1), ('en', 0.5)])) request = wrappers.Request({'HTTP_ACCEPT': ''}) strict_eq(request.accept_mimetypes, MIMEAccept())
def validate_accept(produced_content_types: Iterable, accept_headers: MIMEAccept): if not len(accept_headers): accept_headers = MIMEAccept([('*/*', 1)]) # Flask does not like empty content types, but we use them. content_type = accept_headers.best_match( filter(lambda x: x != '', produced_content_types)) if content_type is None: if '*/*' in accept_headers and '' in produced_content_types: return '' raise NotAcceptableError( description= 'This endpoint only returns one of the following content types: %s' % ', '.join(produced_content_types)) return content_type
def set_accept_mimetype(): """Set the accepted mimetype if a `format` args exists. This is necessary because accepted formats are not yet implemented in flask_resources: https://github.com/inveniosoftware/flask-resources/blob/master/flask_resources/content_negotiation.py#L105 """ if request.args.get('format'): request.accept_mimetypes = MIMEAccept([(request.args['format'], 1)])
def preferring(mimetype, accept): """ Rebuild a MIMEAccept to prefer a given mimetype. The given type will always appear at the front of the new header, causing it to be preferred. The preference is built by doubling the quality of the match with the given type. """ types = [] q = 2 for name, value in accept: if name == mimetype: q = 2 * value else: types.append((name, value)) return MIMEAccept([(mimetype, q)] + types)
def test_preferring_ahead(self): t = "text/plain" a = MIMEAccept([("text/plain", 1), ("text/xml", 1)]) a = preferring(t, a) self.assertEqual(a.best, t)
def test_preferring_new(self): t = "text/plain" a = MIMEAccept([("text/xml", 1), ("image/png", 0.9)]) a = preferring(t, a) self.assertEqual(a.best, t)
def test_preferring_quality_behind(self): t = "text/plain" a = MIMEAccept([("text/xml", 1), ("text/plain", 0.9)]) a = preferring(t, a) self.assertEqual(a.best, t)
def testAcceptsOneProducesOne(self): accept_headers = MIMEAccept([('application/json', 1)]) produces = ['application/json'] self.assertEquals(validate_accept(produces, accept_headers), 'application/json')
def testAcceptsAnyProducesMany(self): accept_headers = MIMEAccept([('*/*', 1)]) produces = ['application/json', 'text/html', 'text/xml'] self.assertEquals(validate_accept(produces, accept_headers), 'application/json')
def testAcceptsAnyProducesEmpty(self): accept_headers = MIMEAccept([('*/*', 1)]) produces = [''] self.assertEquals(validate_accept(produces, accept_headers), '')
def try_accept(*mime_types): accept = MIMEAccept(mime_types) rv = self.client.get(self.url, headers = Headers([('Accept', accept.to_header())])) return rv.json
def testAcceptsAnyProducesNone(self): accept_headers = MIMEAccept([('*/*', 1)]) produces = [] with self.assertRaises(NotAcceptableError): validate_accept(produces, accept_headers)
def test_mime_accept(self, values, matches, default, expect): accept = MIMEAccept(values) match = accept.best_match(matches, default=default) assert match == expect
def testAcceptsNoneProducesEmpty(self): accept_headers = MIMEAccept([]) produces = [''] self.assertEquals(validate_accept(produces, accept_headers), '')
def try_reject(*mime_types): accept = MIMEAccept(mime_types) rv = self.client.get(self.url, headers = Headers([('Accept', accept.to_header())])) return reject_msg in rv.data and rv.status_code >= 400 and rv.mimetype != "application/json"