diff --git a/test/router_test.dart b/test/router_test.dart new file mode 100644 index 0000000..5a80459 --- /dev/null +++ b/test/router_test.dart @@ -0,0 +1,90 @@ +import "package:test/test.dart"; +import "package:wampproto/messages.dart" as msg; +import "package:wampproto/serializers.dart"; + +import "package:xconn/exports.dart"; + +void main() { + const testProcedure = "io.xconn.test_procedure"; + const testTopic = "io.xconn.test_topic"; + + var callee = LocalBaseSession(1, "realm1", "john", "anonymous", JSONSerializer()); + var caller = LocalBaseSession(2, "realm1", "wick", "anonymous", JSONSerializer()); + + var router = Router() + ..addRealm("realm1") + ..attachClient(callee) + ..attachClient(caller); + + late int registrationID; + + test("register", () async { + var registerMsg = msg.Register(3, testProcedure); + await router.receiveMessage(callee, registerMsg); + + var registered = await callee.receiveMessage(); + expect(registered, isA()); + + registrationID = (registered as msg.Registered).registrationID; + }); + + test("call", () async { + var callMsg = msg.Call(4, testProcedure); + await router.receiveMessage(caller, callMsg); + + var invocation = await callee.receiveMessage(); + expect(invocation, isA()); + + var requestID = (invocation as msg.Invocation).requestID; + var yieldMsg = msg.Yield(requestID); + await router.receiveMessage(callee, yieldMsg); + + var result = await caller.receiveMessage(); + expect(result, isA()); + }); + + test("unregister", () async { + var unregisterMsg = msg.UnRegister(5, registrationID); + await router.receiveMessage(callee, unregisterMsg); + + var unregistered = await callee.receiveMessage(); + expect(unregistered, isA()); + }); + + late int subscriptionID; + + test("subscribe", () async { + var subscribeMsg = msg.Subscribe(6, testTopic); + await router.receiveMessage(callee, subscribeMsg); + + var subscribed = await callee.receiveMessage(); + expect(subscribed, isA()); + + subscriptionID = (subscribed as msg.Subscribed).subscriptionID; + }); + + test("publish", () async { + var publish = msg.Publish(7, testTopic); + await router.receiveMessage(caller, publish); + + var event = await callee.receiveMessage(); + expect(event, isA()); + + var publishAck = msg.Publish(8, testTopic, options: {"acknowledge": true}); + await router.receiveMessage(caller, publishAck); + + var eventAck = await callee.receiveMessage(); + expect(eventAck, isA()); + + var published = await caller.receiveMessage(); + expect(published, isA()); + }); + + test("unsubscribe", () async { + var unsubscribeMsg = msg.UnSubscribe(9, subscriptionID); + await router.receiveMessage(callee, unsubscribeMsg); + + var unsubscribed = await callee.receiveMessage(); + expect(unsubscribed, isA()); + }); +}