Esempio n. 1
0
def test_non_void_with_headers_call_directly(
    server, service, ThriftTest, server_ttypes, client_ttypes
):

    # Given this test server:

    @server.thrift.register(ThriftTest)
    def testStruct(request):
        assert request.headers == {'req': 'header'}
        assert request.body.thing.string_thing == 'req string'

        return Response(
            server_ttypes.Xtruct(string_thing="resp string"),
            headers={'resp': 'header'},
        )

    resp = testStruct(
        Request(
            headers={'req': 'header'},
            body=body(thing=client_ttypes.Xtruct("req string")),
        )
    )

    assert isinstance(resp, Response)
    assert resp.headers == {'resp': 'header'}
    assert resp.body == server_ttypes.Xtruct("resp string")
def test_non_void_call_directly(server, ThriftTest):
    @server.thrift.register(ThriftTest)
    def testString(request):
        return request.body.thing

    resp = testString(Request(body=body(thing='howdy')))
    assert resp == 'howdy'
def test_void_call_directly(server, ThriftTest):
    @server.thrift.register(ThriftTest)
    def testVoid(request):
        pass

    resp = testVoid(Request())
    assert resp is None
def test_void_with_headers_call_directly(server, ThriftTest):
    @server.thrift.register(ThriftTest)
    def testVoid(request):
        assert request.headers == {'req': 'header'}
        return Response(headers={'resp': 'header'})

    resp = testVoid(Request(headers={'req': 'header'}))
    assert resp.headers == {'resp': 'header'}
    assert resp.body is None
Esempio n. 5
0
def test_endpoint_can_be_called_as_a_pure_func():

    # Given this test server:

    server = TChannel(name='server')

    @server.register(scheme=schemes.RAW)
    def endpoint(request):

        assert isinstance(request, Request)
        assert request.body == 'req body'
        assert request.headers == 'req headers'

        return Response('resp body', headers='resp headers')

    server.listen()

    # Able to call over TChannel

    tchannel = TChannel(name='client')

    resp = yield tchannel.call(
        scheme=schemes.RAW,
        service='server',
        arg1='endpoint',
        arg2='req headers',
        arg3='req body',
        hostport=server.hostport,
    )

    assert isinstance(resp, Response)
    assert resp.headers == 'resp headers'
    assert resp.body == 'resp body'

    # Able to call as function

    resp = endpoint(Request('req body', headers='req headers'))

    assert isinstance(resp, Response)
    assert resp.headers == 'resp headers'
    assert resp.body == 'resp body'