def test_14_1_audio(self): # RFC2616 14.1, audio example self.assertEqual(http.parse_accept( 'audio/*; q=0.2, audio/basic' ), [ ('audio/basic', '1', {}), ('audio/*', '0.2', {}), ])
def test_14_1_text_media_range(self): # RFC2616 14.1, media range example. self.assertEqual(http.parse_accept( 'text/*, text/html, text/html;level=1, */*' ), [ ('text/html', '1', {'level': '1'}), ('text/html', '1', {}), ('text/*', '1', {}), ('*/*', '1', {}), ])
def test_14_1_text_elaborate(self): # RFC2616 14.1, elaborate text example self.assertEqual(http.parse_accept( 'text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c' ), [ ('text/html', '1', {}), ('text/x-c', '1', {}), ('text/x-dvi', '0.8', {}), ('text/plain', '0.5', {}), ])
def test_14_1_mix_final(self): # RFC2616 14.1, final mixture example self.assertEqual(http.parse_accept( 'text/*;q=0.3, text/html;q=0.7, text/html;level=1,' 'text/html;level=2;q=0.4, */*;q=0.5' ), [ ('text/html', '1', {'level': '1'}), ('text/html', '0.7', {}), ('*/*', '0.5', {}), ('text/html', '0.4', {'level': '2'}), ('text/*', '0.3', {}), ])
def __call__(self, request): accept = request['HTTP_ACCEPT'] if 'json' not in accept: # If json wasn't even mention don't waste time. return media_types = parse_accept(accept) results = [] response_mime_types = [] for media_type in media_types: if not media_type.type in layer_functions: continue type_, q, params = media_type try: result = layer_functions[type_](params) if result: # set content-type header # XXX I am not sure whether setting that directly # here will result in other side-effects (such as # unintentionally overriding other processes/methods # of doing so in other libraries). So just in case, # while it is possible we only just simply assign it # to a "private" attribute which views will have to # handle the actual setting sepearate. # XXX this variable should only be used in this # module. response_mime_types.append( response_mime_type(type_, params)) results.append(result) if result is False: # We have a guaranteed ignore. if not results: # Really ignoring as the request has nothing # we can handle with a higher precedence. return except NotAcceptableError: if len(media_types) == 1: # XXX if there are _any_ other media types this # cannot assert that there are NO other possible # handlers for content-type. So only send this iff # there is only one media_type specified. request.response.setStatus(406, 'Not Acceptable') continue if response_mime_types: setattr(request, REQUEST_MARKER_KEY, (response_mime_types, len(media_types))) return results
def test_basic(self): self.assertEqual(http.parse_accept( 'text/plain' ), [ ('text/plain', '1', {}), ])