def test_accept_headers_get_best_match(self): value = ('text/*;q=0.3, text/html;q=0.7, text/html;level=1,' 'text/html;level=2;q=0.4, */*;q=0.5') accept_header = AcceptHeader(value) best = accept_header.get_best_match(['text/json', 'blah/blah']) self.assertEqual(best[0].mimetype, 'text/*') self.assertEqual(best[1], 'text/json')
def test_accept_headers_get_match(self): value = ('text/*;q=0.3, text/html;q=0.7, text/html;level=1,' 'text/html;level=2;q=0.4, */*;q=0.5') accept_header = AcceptHeader(value) best = accept_header.get_match('text/something') self.assertEqual(best.mimetype, 'text/*') best = accept_header.get_match('text/html') self.assertEqual(best.mimetype, 'text/html') best = accept_header.get_match('*/*') self.assertEqual(best.mimetype, '*/*') best = accept_header.get_match('blah/*') self.assertEqual(best.mimetype, '*/*')
def get_best_mimetype(request, mimetypes): # Get the best matching mimetype from the given set, or raise an http # error if a good match can't be provided if 'accept' in request.headers: accept = AcceptHeader(request.headers.get('accept', '*/*')) match = accept.get_best_match(mimetypes) if match is None: raise web.HTTPNotAcceptable else: return match[1] else: return mimetypes[0]
def test_accept_headers_get_best_match_none(self): value = ('text/*;q=0.3, text/html;q=0.7, text/html;level=1,' 'text/html;level=2;q=0.4') accept_header = AcceptHeader(value) self.assertIsNone(accept_header.get_best_match('application/json'))