def test_handle_deferred(self):
        @inlineCallbacks
        def get(**_):
            yield succeed(None)
            returnValue(("body", OK))

        request = DummyRequest()
        resource = Resource()
        resource.ALLOWED_CONTENT_TYPE = frozenset([""])
        resource.ALLOWED_ACCEPT = frozenset(["*/*"])
        resource.get = get

        with nested(
            patch.object(resource, "error"),
            patch.object(resource, "render_tuple"),
            patch.object(resource, "render_deferred"),
        ) as (error, render_tuple, render_deferred):
            response = resource.render(request)

        self.assertEqual(response, NOT_DONE_YET)
        self.assertFalse(error.called)
        self.assertFalse(render_tuple.called)
        self.assertEqual(render_deferred.call_count, 1)
        self.assertIs(render_deferred.call_args[0][0], request)
        self.assertIsInstance(render_deferred.call_args[0][1], Deferred)
    def test_unsupported_media_type(self):
        resource = Resource()
        resource.ALLOWED_CONTENT_TYPE = frozenset("")
        resource.ALLOWED_ACCEPT = frozenset("")
        resource.get = lambda **_: ""
        request = DummyRequest()
        request.requestHeaders.setRawHeaders("Accept", ["foobar"])
        request.set_content("hello")

        with patch.object(resource, "error") as error:
            response = resource.render(request)

        self.assertEqual(response, NOT_DONE_YET)
        error.assert_called_once_with(
            request, UNSUPPORTED_MEDIA_TYPE,
            "Can only support content type(s) frozenset([])"
        )
    def test_handle_tuple(self):
        request = DummyRequest()
        resource = Resource()
        resource.ALLOWED_CONTENT_TYPE = frozenset([""])
        resource.ALLOWED_ACCEPT = frozenset(["*/*"])
        resource.get = lambda **_: ("body", OK)

        with nested(
            patch.object(resource, "error"),
            patch.object(resource, "render_tuple"),
            patch.object(resource, "render_deferred"),
        ) as (error, render_tuple, render_deferred):
            response = resource.render(request)

        self.assertEqual(response, NOT_DONE_YET)
        self.assertFalse(error.called)
        render_tuple.assert_called_once_with(request, ("body", OK))
        self.assertFalse(render_deferred.called)
    def test_exception_in_handler_method(self):
        def get(**_):
            raise ValueError("error")

        request = DummyRequest()
        resource = Resource()
        resource.ALLOWED_CONTENT_TYPE = frozenset(["application/json"])
        resource.ALLOWED_ACCEPT = frozenset(["*/*"])
        resource.get = get

        with patch.object(resource, "error") as error:
            response = resource.render(request)

        self.assertEqual(response, NOT_DONE_YET)
        error.assert_called_once_with(
            request, INTERNAL_SERVER_ERROR,
            "Unhandled error while rendering response: error"
        )
    def test_default_content_type(self):
        """
        If the Content-Type response header is not already set, set it based
        on the defaults
        """
        request = DummyRequest()
        resource = Resource()
        request.requestHeaders.setRawHeaders("Accept", ["application/json"])
        resource.ALLOWED_ACCEPT = frozenset(["application/json"])
        resource.ALLOWED_CONTENT_TYPE = frozenset(["application/json"])
        resource.DEFAULT_CONTENT_TYPE = frozenset(["application/pyfarm"])
        resource.get = lambda *_, **__: ""
        resource.render(request)

        self.assertEqual(
            request.responseHeaders.getRawHeaders("Content-Type"),
            ["application/pyfarm"]
        )
    def test_handle_unknown_return_value(self):
        request = DummyRequest()
        resource = Resource()
        resource.ALLOWED_CONTENT_TYPE = frozenset([""])
        resource.ALLOWED_ACCEPT = frozenset(["*/*"])
        resource.get = lambda **_: None

        with nested(
            patch.object(resource, "error"),
            patch.object(resource, "render_tuple"),
            patch.object(resource, "render_deferred"),
        ) as (error, render_tuple, render_deferred):
            response = resource.render(request)

        self.assertEqual(response, NOT_DONE_YET)
        error.assert_called_once_with(
            request, INTERNAL_SERVER_ERROR,
            "Unhandled %r in response" % None
        )
        self.assertFalse(render_tuple.called)
        self.assertFalse(render_deferred.called)
 def test_non_callable_method(self):
     resource = Resource()
     resource.get = lambda self: None
     resource.put = lambda self: None
     resource.delete = None
     self.assertEqual(set(resource.methods()), set(["get", "put"]))
 def test_methods(self):
     resource = Resource()
     resource.get = lambda self: None
     resource.put = lambda self: None
     self.assertEqual(set(resource.methods()), set(["get", "put"]))