From cbd7a336159f883c01c8935c1ef6dd82d576a573 Mon Sep 17 00:00:00 2001 From: Shane Krueger Date: Sun, 27 Oct 2024 09:41:52 -0400 Subject: [PATCH] Reflect ping payload in pong (#77) --- .../WebSockets/GraphQLWs/SubscriptionServer.cs | 4 +++- src/Tests/WebSockets/NewSubscriptionServerTests.cs | 10 +++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/GraphQL.AspNetCore3/WebSockets/GraphQLWs/SubscriptionServer.cs b/src/GraphQL.AspNetCore3/WebSockets/GraphQLWs/SubscriptionServer.cs index 9b57782..47e6924 100644 --- a/src/GraphQL.AspNetCore3/WebSockets/GraphQLWs/SubscriptionServer.cs +++ b/src/GraphQL.AspNetCore3/WebSockets/GraphQLWs/SubscriptionServer.cs @@ -115,7 +115,9 @@ public override async Task OnMessageReceivedAsync(OperationMessage message) /// Executes when a ping message is received. /// protected virtual Task OnPingAsync(OperationMessage message) - => Connection.SendMessageAsync(_pongMessage); + => message.Payload == null + ? Connection.SendMessageAsync(_pongMessage) + : Connection.SendMessageAsync(new OperationMessage { Type = MessageType.Pong, Payload = message.Payload }); /// /// Executes when a pong message is received. diff --git a/src/Tests/WebSockets/NewSubscriptionServerTests.cs b/src/Tests/WebSockets/NewSubscriptionServerTests.cs index d703f31..86d749b 100644 --- a/src/Tests/WebSockets/NewSubscriptionServerTests.cs +++ b/src/Tests/WebSockets/NewSubscriptionServerTests.cs @@ -94,11 +94,15 @@ public async Task Message_ThrowsWhenNotInitialized(string? messageType) } [Theory] - [InlineData(false)] - [InlineData(true)] - public async Task Message_Ping(bool initialized) + [InlineData(false, false)] + [InlineData(false, true)] + [InlineData(true, false)] + [InlineData(true, true)] + public async Task Message_Ping(bool initialized, bool withPayload) { var message = new OperationMessage { Type = "ping" }; + if (withPayload) + message.Payload = new { id = Guid.NewGuid().ToString("N") }; _mockServer.Protected().Setup("OnPingAsync", message) .Returns(Task.CompletedTask).Verifiable(); if (initialized) {