def test_single(self): """ A single ``Accept`` value is parsed into its content type with no parameters. """ self.assertThat( _parseAccept(['text/plain']).items(), Equals([('text/plain', {})]))
def test_multiple(self): """ Multiple ``Accept`` values are split on comma each with no parameters. """ self.assertThat( _parseAccept(['text/plain', 'text/html,text/csv']).items(), Equals([('text/plain', {}), ('text/html', {}), ('text/csv', {})]))
def test_q(self): """ Content types are sorted in descending order by their ``q`` parameter, the absence of a ``q`` parameter indicates a value of ``1.0``. """ self.assertThat( _parseAccept(['text/plain;q=0.2', 'text/html,text/csv;q=0.4']).items(), Equals([('text/html', {}), ('text/csv', {'q': '0.4'}), ('text/plain', {'q': '0.2'})]))
def _negotiateHandler(self, request): """ Negotiate a handler based on the content types acceptable to the client. :rtype: 2-`tuple` of `twisted.web.iweb.IResource` and `bytes` :return: Pair of a resource and the content type. """ accept = _parseAccept(request.requestHeaders.getRawHeaders('Accept')) for contentType in accept.keys(): handler = self._acceptHandlers.get(contentType.lower()) if handler is not None: return handler, handler.contentType if self._fallback: handler = self._handlers[0] return handler, handler.contentType return NotAcceptable(), None