def test_should_roundtrip_headers(inbound, outbound): rpc = RPC( service='test-service', inbounds=[inbound], outbounds={ 'test-service': outbound, }, ) def hi(request): return Response( body=request.body, headers=request.headers, ) rpc.register(Procedure('hi', hi)) rpc.start() headers = { 'hello': 'world', 'mynameis': 'bob', } req = Request( encoding='raw', procedure='hi', headers=headers, ttl=10000, ) resp = yield rpc.channel('test-service').call(req) assert headers == resp.headers
def test_should_call_procedure(inbound, outbound): rpc = RPC( service='test-service', inbounds=[inbound], outbounds={ 'test-service': outbound, }, ) def hi(request): assert isinstance(request, Request) assert request.encoding == 'raw' assert request.procedure == 'hi' assert request.body == 'hello' return Response(body="%s" % request.body) rpc.register(Procedure('hi', hi)) rpc.start() req = Request( encoding='raw', procedure='hi', body='hello', ttl=10000, ) resp = hi(req) assert isinstance(resp, Response) assert resp.body == 'hello' resp = yield rpc.channel('test-service').call(req) assert isinstance(resp, Response) assert resp.body == 'hello'
def json(respw, server, transport, **kwargs): rpc = RPC(service="client", outbounds={"yarpc-test": tr.factory(server, transport)}) expected = {"token": rand.string(7)} request = Request(procedure="echo", body=expected, ttl=10000) client = JSONClient(rpc.channel("yarpc-test")) response = yield client.call(request) if response.body == expected: respw.success("Server said: %s" % response.body["token"]) else: respw.fail("expected %s, got %s" % (expected, response.body))
def raw(respw, server, transport, **kwargs): rpc = RPC( service='client', outbounds={ 'yarpc-test': tr.factory(server, transport), }, ) expected = rand.string(7) request = Request(procedure='echo/raw', body=expected, ttl=10000) client = RawClient(rpc.channel('yarpc-test')) response = yield client.call(request) if response.body == expected: respw.success("Server said: %s" % response.body) else: respw.fail("expected %s, got %s" % (expected, response.body))
def thrift(respw, server, transport, **kwargs): rpc = RPC( service='client', outbounds={ 'yarpc-test': tr.factory(server, transport), }, ) expected = rand.string(7) request = Request( body=service.Echo.echo(service.Ping(beep=expected)), ttl=10000, ) client = ThriftClient(rpc.channel('yarpc-test')) response = yield client.call(request) if response.body.boop == expected: respw.success("Server said: %s" % response.body.boop) else: respw.fail("expected %s, got %s" % (expected, response.body))
def headers(respw, server, transport, encoding, **kwargs): rpc = RPC( service='client', outbounds={ 'yarpc-test': tr.factory(server, transport), }, ) caller = get_caller(encoding, rpc.channel('yarpc-test')) for test in tests: desc, give, want = test try: resp = yield caller.call(give) except Exception as e: respw.fail('%s: call failed, %s' % (desc, str(e))) else: got = resp.headers if want != got: respw.fail( '%s: call failed, got %s, want %s' % (desc, got, want) ) else: respw.success('%s: returns valid headers' % desc)
def test_no_outbound_for_service_error(): rpc = RPC('') with pytest.raises(NoOutboundForServiceError): rpc.channel('doesntexist')