diff --git a/publisher/test/LeanPipe.IntegrationTests/BasicTopicTests.cs b/publisher/test/LeanPipe.IntegrationTests/BasicTopicTests.cs index b0bab46..dabb3d8 100644 --- a/publisher/test/LeanPipe.IntegrationTests/BasicTopicTests.cs +++ b/publisher/test/LeanPipe.IntegrationTests/BasicTopicTests.cs @@ -1,4 +1,3 @@ -using System.Net.Http.Json; using FluentAssertions; using LeanPipe.IntegrationTests.App; using LeanPipe.TestClient; @@ -25,7 +24,7 @@ public BasicTopicTests() } [Fact] - public async Task Basic_topic_subscriber_receives_both_kinds_of_messages() + public async Task Subscriber_receives_both_kinds_of_notifications_when_subscribed() { var topic = new UnauthorizedTopic { TopicId = Guid.NewGuid() }; @@ -33,29 +32,85 @@ public async Task Basic_topic_subscriber_receives_both_kinds_of_messages() leanPipeClient.NotificationsOn(topic).Should().BeEmpty(); var notificationTask = leanPipeClient.GetNextNotificationTaskOn(topic); - await httpClient.PostAsJsonAsync( - "/publish_unauthorized", - new NotificationDataDTO - { + await httpClient.PublishToUnauthorizedTopicAsync(new() { TopicId = topic.TopicId, Kind = NotificationKindDTO.Greeting, Name = "Tester", - } - ); + }); (await notificationTask).Should() .BeEquivalentTo(new GreetingNotificationDTO { Greeting = "Hello Tester" }); notificationTask = leanPipeClient.GetNextNotificationTaskOn(topic); - await httpClient.PostAsJsonAsync( - "/publish_unauthorized", - new NotificationDataDTO - { - TopicId = topic.TopicId, - Kind = NotificationKindDTO.Farewell, - Name = "Tester", - } - ); + await httpClient.PublishToUnauthorizedTopicAsync(new() { + TopicId = topic.TopicId, + Kind = NotificationKindDTO.Farewell, + Name = "Tester", + }); + + (await notificationTask).Should() + .BeEquivalentTo(new FarewellNotificationDTO { Farewell = "Goodbye Tester" }); + + await leanPipeClient.UnsubscribeSuccessAsync(topic); + + await httpClient.PublishToUnauthorizedTopicAsync(new() { + TopicId = topic.TopicId, + Kind = NotificationKindDTO.Farewell, + Name = "other Tester", + }); + + await Task.Delay(1000); + leanPipeClient.NotificationsOn(topic).Should().HaveCount(2); + } + + [Fact] + public async Task Subscriber_does_not_receive_messages_from_other_topic_instances() + { + var topic = new UnauthorizedTopic { TopicId = Guid.NewGuid() }; + var otherTopic = new UnauthorizedTopic { TopicId = Guid.NewGuid() }; + + await leanPipeClient.SubscribeSuccessAsync(topic); + + await httpClient.PublishToUnauthorizedTopicAsync(new() { + TopicId = otherTopic.TopicId, + Kind = NotificationKindDTO.Greeting, + Name = "Tester", + }); + + await Task.Delay(1000); + leanPipeClient.NotificationsOn(topic).Should().BeEmpty(); + leanPipeClient.NotificationsOn(otherTopic).Should().BeEmpty(); + + await leanPipeClient.UnsubscribeSuccessAsync(topic); + } + + [Fact] + public async Task Resubscribing_after_disconnect_works() + { + var topic = new UnauthorizedTopic { TopicId = Guid.NewGuid() }; + + await leanPipeClient.SubscribeSuccessAsync(topic); + leanPipeClient.NotificationsOn(topic).Should().BeEmpty(); + + var notificationTask = leanPipeClient.GetNextNotificationTaskOn(topic); + await httpClient.PublishToUnauthorizedTopicAsync(new() { + TopicId = topic.TopicId, + Kind = NotificationKindDTO.Greeting, + Name = "Tester", + }); + + (await notificationTask).Should() + .BeEquivalentTo(new GreetingNotificationDTO { Greeting = "Hello Tester" }); + + await leanPipeClient.DisconnectAsync(); + await leanPipeClient.SubscribeSuccessAsync(topic); + + notificationTask = leanPipeClient.GetNextNotificationTaskOn(topic); + await httpClient.PublishToUnauthorizedTopicAsync(new() { + TopicId = topic.TopicId, + Kind = NotificationKindDTO.Farewell, + Name = "Tester", + }); (await notificationTask).Should() .BeEquivalentTo(new FarewellNotificationDTO { Farewell = "Goodbye Tester" }); diff --git a/publisher/test/LeanPipe.IntegrationTests/PublishingExtensions.cs b/publisher/test/LeanPipe.IntegrationTests/PublishingExtensions.cs new file mode 100644 index 0000000..f9f01b8 --- /dev/null +++ b/publisher/test/LeanPipe.IntegrationTests/PublishingExtensions.cs @@ -0,0 +1,27 @@ +using System.Net.Http.Json; +using LeanPipe.IntegrationTests.App; + +namespace LeanPipe.IntegrationTests; + +public static class PublishingExtensions +{ + public static Task PublishToUnauthorizedTopicAsync(this HttpClient client, NotificationDataDTO notificationData) + { + return PostAsync(client, "/publish_unauthorized", notificationData); + } + + public static Task PublishToAuthorizedTopicAsync(this HttpClient client, NotificationDataDTO notificationData) + { + return PostAsync(client, "/publish_authorized", notificationData); + } + + private static async Task PostAsync(HttpClient client, string uri, TPayload payload) + { + using var response = await client.PostAsJsonAsync( + uri, + payload + ); + + response.EnsureSuccessStatusCode(); + } +}