def test_content_type_negotiation(self): """ View should pick a serializer to match the Accept header when possible """ view = View() view.serializers = ( ('application/x-foo', FooSerializer()), ('application/x-bar', BarSerializer()) ) # No accept header means the first serializer in the list content_type, result = view.serialize('', {}) self.assertEquals(content_type, 'application/x-foo') self.assertEquals(result, 'Foo') # A matching accept header means the matching serializer content_type, result = view.serialize('application/x-bar', {}) self.assertEquals(content_type, 'application/x-bar') self.assertEquals(result, 'Bar') # An unmatched accept header means the first serializer in the list content_type, result = view.serialize('text/xml', {}) self.assertEquals(content_type, 'application/x-foo') self.assertEquals(result, 'Foo')
def test_abstract_methods(self): """ View has abstract methods that should raise an error if used """ view = View() with self.assertRaises(NotImplementedError): view.get_list_response(None, None) with self.assertRaises(NotImplementedError): view.get_individual_response(None, None)
def count(self, req, resp): kwargs = self.parse_params(req) kwargs['count'] = True result = self.interface.list(**kwargs) resp.content_type, _ = View.choose(req.get_header('accept'), self.views) resp.set_header('X-Count', str(result))
def duplicate_field_error(views, exc, req, resp, params): error = {} error[exc.message] = 'A duplicate value already exists.' _, view = View.choose(req.get_header('accept'), views) resp.content_type, resp.body = view.get_individual_response( req.get_header('accept'), error) resp.status = falcon.HTTP_400
def count_link_or_reference(self, req, resp, id, link_name): kwargs = self.parse_params(req) kwargs['count'] = True result = self.interface.link(id, link_name, **kwargs) resp.content_type, _ = View.choose(req.get_header('accept'), self.views) if isinstance(result, int): resp.set_header('X-Count', str(result))
def response(self, content, status_code=200): _, view = View.choose(request.headers.get('accept'), self.views) serialize_fn = view.get_list_response if isinstance(content, collections.Sequence) else view.get_individual_response content_type, body = serialize_fn(request.headers.get('accept'), content) res = make_response(body) res.status_code = status_code res.headers['content-type'] = content_type return res
def head(self): kwargs = self.parse_params() kwargs['count'] = True count = self.interface.list(**kwargs) res = make_response('') res.headers['Content-Type'], _ = View.choose(request.headers.get('accept'), self.views) res.headers['X-Count'] = str(count) return res
def head(self): kwargs = self.parse_params() kwargs['count'] = True count = self.interface.list(**kwargs) res = make_response('') res.headers['Content-Type'], _ = View.choose( request.headers.get('accept'), self.views) res.headers['X-Count'] = str(count) return res
def response(self, content, status_code=200): _, view = View.choose(request.headers.get('accept'), self.views) serialize_fn = view.get_list_response if isinstance( content, collections.Sequence) else view.get_individual_response content_type, body = serialize_fn(request.headers.get('accept'), content) res = make_response(body) res.status_code = status_code res.headers['content-type'] = content_type return res
def error_response(code, item=None, views=[]): if not item: abort(code) else: _, view = View.choose(request.headers.get('accept'), views) content_type, body = view.get_individual_response(request.headers.get('accept'), item) res = make_response(body) res.status_code = code res.headers['content-type'] = content_type return res
def error_response(code, item=None, views=[]): if not item: abort(code) else: _, view = View.choose(request.headers.get('accept'), views) content_type, body = view.get_individual_response( request.headers.get('accept'), item) res = make_response(body) res.status_code = code res.headers['content-type'] = content_type return res
def test_content_type_negotiation(self): """ View should pick a serializer to match the Accept header when possible """ view = View() view.serializers = (('application/x-foo', FooSerializer()), ('application/x-bar', BarSerializer())) # No accept header means the first serializer in the list req = create_fake_request() content_type, result = view.serialize(req, {}) self.assertEquals(content_type, 'application/x-foo') self.assertEquals(result, 'Foo') # A matching accept header means the matching serializer req = create_fake_request(headers={'accept': 'application/x-bar'}) content_type, result = view.serialize(req, {}) self.assertEquals(content_type, 'application/x-bar') self.assertEquals(result, 'Bar') # An unmatched accept header means the first serializer in the list req = create_fake_request(headers={'accept': 'text/xml'}) content_type, result = view.serialize(req, {}) self.assertEquals(content_type, 'application/x-foo') self.assertEquals(result, 'Foo')
def get_view(self, req): """Get the correct view based on the Accept header""" _, view = View.choose(req.get_header('accept'), self.views) return view
def duplicate_field_error(views, exc, req, resp, params): error = {} error[exc.message] = 'A duplicate value already exists.' _, view = View.choose(req.get_header('accept'), views) resp.content_type, resp.body = view.get_individual_response(req.get_header('accept'), error) resp.status = falcon.HTTP_400
def validation_error_handler(views, exc, req, resp, params): _, view = View.choose(req.get_header('accept'), views) resp.content_type, resp.body = view.get_individual_response(req.get_header('accept'), exc.errors) resp.status = falcon.HTTP_400
def validation_error_handler(views, exc, req, resp, params): _, view = View.choose(req.get_header('accept'), views) resp.content_type, resp.body = view.get_individual_response( req.get_header('accept'), exc.errors) resp.status = falcon.HTTP_400