예제 #1
0
    def test_exposed_preprocessor(self):
        d = defer.Deferred()

        def preprocessor(http_request, service_request):
            return reactor.callLater(0, lambda: True)

        preprocessor = gateway.expose_request(preprocessor)
        gw = twisted.TwistedGateway(
            {'echo': lambda x: x},
            expose_request=False,
            preprocessor=preprocessor
        )
        proc = twisted.AMF3RequestProcessor(gw)

        request = remoting.Request(
            'null',
            body=[
                messaging.RemotingMessage(body=['spam.eggs'], operation='echo')
            ]
        )

        def cb(result):
            try:
                self.assertTrue(result)
            except:
                d.errback()
            else:
                d.callback(None)

        proc(request).addCallback(cb).addErrback(lambda failure: d.errback())

        return d
예제 #2
0
    def test_async(self):
        d = defer.Deferred()

        gw = twisted.TwistedGateway(
            {'spam.eggs': lambda x: x}, expose_request=False
        )
        proc = twisted.AMF3RequestProcessor(gw)

        request = remoting.Request('null', body=[
            messaging.AsyncMessage(
                body=[None],
                destination='spam',
                operation='eggs')
        ])

        def cb(result):
            msg = result.body

            try:
                self.assertTrue(isinstance(msg, messaging.AcknowledgeMessage))
            except:
                d.errback()
            else:
                d.callback(None)

        proc(request).addCallback(cb).addErrback(lambda failure: d.errback())

        return d
예제 #3
0
    def test_error_preprocessor(self):
        def preprocessor(service_request, *args):
            raise IndexError

        gw = twisted.TwistedGateway(
            {'echo': lambda x: x},
            expose_request=False,
            preprocessor=preprocessor
        )
        proc = twisted.AMF3RequestProcessor(gw)

        request = remoting.Request(
            'null',
            body=[
                messaging.RemotingMessage(body=['spam.eggs'], operation='echo')
            ]
        )

        d = proc(request)

        self.assertTrue(isinstance(d, defer.Deferred))
        response = d.result
        self.assertTrue(isinstance(response, remoting.Response))
        self.assertTrue(response.status, remoting.STATUS_ERROR)
        self.assertTrue(isinstance(response.body, messaging.ErrorMessage))
        self.assertEqual(response.body.faultCode, 'IndexError')
예제 #4
0
    def test_deferred_preprocessor(self):
        d = defer.Deferred()

        def preprocessor(u, *args):
            d2 = defer.Deferred()
            reactor.callLater(0, lambda: d2.callback(None))

            return d2

        gw = twisted.TwistedGateway(
            {'echo': lambda x: x},
            expose_request=False, preprocessor=preprocessor
        )
        proc = twisted.AMF3RequestProcessor(gw)

        request = remoting.Request(
            'null',
            body=[
                messaging.RemotingMessage(body=['spam.eggs'], operation='echo')
            ]
        )

        def cb(result):
            self.assertTrue(result)
            d.callback(None)

        proc(request).addCallback(cb).addErrback(lambda failure: d.errback())

        return d
예제 #5
0
    def test_error_deferred_body(self):
        d = defer.Deferred()

        def echo(x):
            d2 = defer.Deferred()

            def cb(result):
                raise IndexError

            reactor.callLater(0, lambda: d2.callback(None))

            d2.addCallback(cb)
            return d2

        gw = twisted.TwistedGateway({'echo': echo}, expose_request=False)
        proc = twisted.AMF3RequestProcessor(gw)

        request = remoting.Request('null', body=[messaging.RemotingMessage(body=['spam.eggs'], operation='echo')])

        def cb(result):
            try:
                self.assertTrue(isinstance(result, remoting.Response))
                self.assertTrue(result.status, remoting.STATUS_ERROR)
                self.assertTrue(isinstance(result.body, messaging.ErrorMessage))
                self.assertEqual(result.body.faultCode, 'IndexError')
            except:
                d.errback()
            else:
                d.callback(None)

        proc(request).addCallback(cb).addErrback(lambda x: d.errback())

        return d
예제 #6
0
    def test_destination(self):
        d = defer.Deferred()

        gw = _twisted.TwistedGateway({'spam.eggs': lambda x: x},
                                     expose_request=False)
        proc = _twisted.AMF3RequestProcessor(gw)

        request = remoting.Request('null',
                                   body=[
                                       messaging.RemotingMessage(
                                           body=[None],
                                           destination='spam',
                                           operation='eggs')
                                   ])

        def cb(result):
            try:
                self.assertTrue(result)
            except:
                d.errback()
            else:
                d.callback(None)

        proc(request).addCallback(cb).addErrback(lambda failure: d.errback())

        return d
예제 #7
0
    def test_unknown_service_request(self):
        gw = twisted.TwistedGateway({'echo': lambda x: x}, expose_request=False)
        proc = twisted.AMF3RequestProcessor(gw)

        request = remoting.Request('null', body=[messaging.RemotingMessage(body=['spam.eggs'], operation='ss')])

        d = proc(request)

        self.assertTrue(isinstance(d, defer.Deferred))
        response = d.result
        self.assertTrue(isinstance(response, remoting.Response))
        self.assertTrue(response.status, remoting.STATUS_ERROR)
        self.assertTrue(isinstance(response.body, messaging.ErrorMessage))
예제 #8
0
    def test_preprocessor(self):
        d = defer.Deferred()

        def preprocessor(service_request, *args):
            d.callback(None)

        gw = twisted.TwistedGateway({'echo': lambda x: x}, expose_request=False, preprocessor=preprocessor)
        proc = twisted.AMF3RequestProcessor(gw)

        request = remoting.Request('null', body=[messaging.RemotingMessage(body=['spam.eggs'], operation='echo')])

        proc(request).addErrback(lambda failure: d.errback())

        return d
예제 #9
0
    def test_error_body(self):
        def echo(x):
            raise KeyError

        gw = _twisted.TwistedGateway({'echo': echo}, expose_request=False)
        proc = _twisted.AMF3RequestProcessor(gw)

        request = remoting.Request('null',
                                   body=[
                                       messaging.RemotingMessage(
                                           body=['spam.eggs'],
                                           operation='echo')
                                   ])

        d = proc(request)

        self.assertTrue(isinstance(d, defer.Deferred))
        response = d.result
        self.assertTrue(isinstance(response, remoting.Response))
        self.assertTrue(response.status, remoting.STATUS_ERROR)
        self.assertTrue(isinstance(response.body, messaging.ErrorMessage))
        self.assertEquals(response.body.faultCode, 'KeyError')