@@ -649,6 +649,95 @@ func TestServer_Publish(t *testing.T) {
649649
650650}
651651
652+ func TestServer_PublishToClientWithMatch (t * testing.T ) {
653+ a := assert .New (t )
654+ srv , conn := connectedServer (nil )
655+ defer srv .Stop (context .Background ())
656+
657+ c := conn .(* rwTestConn )
658+ tt := []packets.Topic {
659+ {Qos : packets .QOS_0 , Name : "t0" },
660+ {Qos : packets .QOS_1 , Name : "t1" },
661+ {Qos : packets .QOS_2 , Name : "t2" },
662+ }
663+ srv .subscriptionsDB .Subscribe ("MQTT" , tt ... )
664+
665+ // create a package for an unsubscribed channel
666+ pubU := & packets.Publish {
667+ Dup : false ,
668+ Qos : packets .QOS_0 ,
669+ Retain : false ,
670+ TopicName : []byte ("tu" ),
671+ Payload : []byte ("payload" ),
672+ }
673+ srv .publishService .PublishToClient ("MQTT" ,
674+ NewMessage (string (pubU .TopicName ), pubU .Payload , pubU .Qos , Retained (pubU .Retain )),
675+ true ) // publish with match=true, i.e. the pubU package should not be delivered
676+
677+ pub := & packets.Publish {
678+ Dup : false ,
679+ Qos : packets .QOS_0 ,
680+ Retain : false ,
681+ TopicName : []byte ("t0" ),
682+ Payload : []byte ("payload" ),
683+ }
684+ srv .publishService .PublishToClient ("MQTT" ,
685+ NewMessage (string (pub .TopicName ), pub .Payload , pub .Qos , Retained (pub .Retain )),
686+ true )
687+ packet , err := readPacket (c )
688+ a .Nil (err )
689+
690+ if p , ok := packet .(* packets.Publish ); ok {
691+ a .NotEqual (string (p .TopicName ), string (pubU .TopicName ), "Match error, received message for unsubscribed topic" )
692+ } else {
693+ t .Fatalf ("unexpected Packet Type, want %v, got %v" , reflect .TypeOf (& packets.Publish {}), reflect .TypeOf (packet ))
694+ }
695+ }
696+
697+ func TestServer_PublishToClientWithoutMatch (t * testing.T ) {
698+ a := assert .New (t )
699+ srv , conn := connectedServer (nil )
700+ defer srv .Stop (context .Background ())
701+ c := conn .(* rwTestConn )
702+ tt := []packets.Topic {
703+ {Qos : packets .QOS_0 , Name : "t0" },
704+ {Qos : packets .QOS_1 , Name : "t1" },
705+ {Qos : packets .QOS_2 , Name : "t2" },
706+ }
707+ srv .subscriptionsDB .Subscribe ("MQTT" , tt ... )
708+
709+ // create a package for an unsubscribed channel
710+ pubU := & packets.Publish {
711+ Dup : false ,
712+ Qos : packets .QOS_0 ,
713+ Retain : false ,
714+ TopicName : []byte ("tu" ),
715+ Payload : []byte ("payload" ),
716+ }
717+ srv .publishService .PublishToClient ("MQTT" ,
718+ NewMessage (string (pubU .TopicName ), pubU .Payload , pubU .Qos , Retained (pubU .Retain )),
719+ false ) // publish with match=false, i.e. the pubU package should be delivered
720+
721+ pub := & packets.Publish {
722+ Dup : false ,
723+ Qos : packets .QOS_0 ,
724+ Retain : false ,
725+ TopicName : []byte ("t0" ),
726+ Payload : []byte ("payload" ),
727+ }
728+ srv .publishService .PublishToClient ("MQTT" ,
729+ NewMessage (string (pub .TopicName ), pub .Payload , pub .Qos , Retained (pub .Retain )),
730+ true )
731+ packet , err := readPacket (c )
732+ a .Nil (err )
733+
734+ if p , ok := packet .(* packets.Publish ); ok {
735+ a .Equal (string (pubU .TopicName ), string (p .TopicName ), "Match error, did not receive message for unsubscribed topic" )
736+ } else {
737+ t .Fatalf ("unexpected Packet Type, want %v, got %v" , reflect .TypeOf (& packets.Publish {}), reflect .TypeOf (packet ))
738+ }
739+ }
740+
652741func TestUnsubscribe (t * testing.T ) {
653742 srv , conn := connectedServer (nil )
654743 defer srv .Stop (context .Background ())
0 commit comments