Exemplo n.º 1
0
    def test_get_acceptable(self):
        # defining a service with different "accept" headers, we should be able
        # to retrieve this information easily
        service = Service("color", "/favorite-color")
        service.add_view("GET", lambda x: "blue", accept="text/plain")
        self.assertEquals(service.get_acceptable("GET"), ['text/plain'])

        service.add_view("GET", lambda x: "blue", accept="application/json")
        self.assertEquals(service.get_acceptable("GET"),
                          ['text/plain', 'application/json'])

        # adding a view for the POST method should not break everything :-)
        service.add_view("POST", lambda x: "ok", accept=('foo/bar'))
        self.assertEquals(service.get_acceptable("GET"),
                          ['text/plain', 'application/json'])
        # and of course the list of accepted content-types  should be available
        # for the "POST" as well.
        self.assertEquals(service.get_acceptable("POST"),
                          ['foo/bar'])

        # it is possible to give acceptable content-types dynamically at
        # run-time. You don't always want to have the callables when retrieving
        # all the acceptable content-types
        service.add_view("POST", lambda x: "ok", accept=lambda r: "text/json")
        self.assertEquals(len(service.get_acceptable("POST")), 2)
        self.assertEquals(len(service.get_acceptable("POST", True)), 1)
Exemplo n.º 2
0
    def test_get_acceptable(self):
        # defining a service with different "accept" headers, we should be able
        # to retrieve this information easily
        service = Service("color", "/favorite-color")
        service.add_view("GET", lambda x: "blue", accept="text/plain")
        self.assertEquals(service.get_acceptable("GET"), ['text/plain'])

        service.add_view("GET", lambda x: "blue", accept="application/json")
        self.assertEquals(service.get_acceptable("GET"),
                          ['text/plain', 'application/json'])

        # adding a view for the POST method should not break everything :-)
        service.add_view("POST", lambda x: "ok", accept=('foo/bar'))
        self.assertEquals(service.get_acceptable("GET"),
                          ['text/plain', 'application/json'])
        # and of course the list of accepted content-types  should be available
        # for the "POST" as well.
        self.assertEquals(service.get_acceptable("POST"), ['foo/bar'])

        # it is possible to give acceptable content-types dynamically at
        # run-time. You don't always want to have the callables when retrieving
        # all the acceptable content-types
        service.add_view("POST", lambda x: "ok", accept=lambda r: "text/json")
        self.assertEquals(len(service.get_acceptable("POST")), 2)
        self.assertEquals(len(service.get_acceptable("POST", True)), 1)