public Task Should_InjectServicesIntoInterceptor()
        {
            return(TryTest(async() =>
            {
                // arrange
                using TestServer testServer = CreateStarWarsServer(configureServices: sp =>
                {
                    sp.AddSingleton <ExampleService>();
                    sp
                    .AddGraphQLServer()
                    .AddSocketSessionInterceptor <SocketInterceptor>();
                });
                WebSocketClient client = CreateWebSocketClient(testServer);
                WebSocket webSocket = await ConnectToServerAsync(client);
                TestSocketSessionInterceptor testInterceptor =
                    testServer.Services.GetRequiredService <TestSocketSessionInterceptor>();

                DocumentNode document = Utf8GraphQLParser.Parse(
                    "subscription { onReview(episode: NEW_HOPE) { stars } }");

                var request = new GraphQLRequest(document);

                const string subscriptionId = "abc";

                await webSocket.SendSubscriptionStartAsync(subscriptionId, request);

                // act
                await webSocket.SendSubscriptionStopAsync(subscriptionId);

                // assert
                Assert.NotNull(SocketInterceptor.Instance.Service);
            }));
        }
        public Task Send_ShouldTrigger_OnCloseAsync()
        {
            return(TryTest(async() =>
            {
                // arrange
                using TestServer testServer = CreateStarWarsServer();
                WebSocketClient client = CreateWebSocketClient(testServer);
                WebSocket webSocket = await ConnectToServerAsync(client);
                TestSocketSessionInterceptor testInterceptor =
                    testServer.Services.GetRequiredService <TestSocketSessionInterceptor>();

                DocumentNode document = Utf8GraphQLParser.Parse(
                    "subscription { onReview(episode: NEW_HOPE) { stars } }");

                var request = new GraphQLRequest(document);

                const string subscriptionId = "abc";

                await webSocket.SendSubscriptionStartAsync(subscriptionId, request);

                // act
                await webSocket.SendSubscriptionStopAsync(subscriptionId);
                await webSocket.SendTerminateConnectionAsync();

                await WaitForConditions(
                    () => testInterceptor.CloseWasCalled,
                    TimeSpan.FromMilliseconds(500));

                // assert
                Assert.True(testInterceptor.CloseWasCalled);
            }));
        }
        public Task Send_ShouldTrigger_OnCloseAsync_UserClose()
        {
            return(TryTest(async() =>
            {
                // arrange
                using TestServer testServer = CreateStarWarsServer();
                WebSocketClient client = CreateWebSocketClient(testServer);
                WebSocket webSocket = await ConnectToServerAsync(client);
                TestSocketSessionInterceptor testInterceptor =
                    testServer.Services.GetRequiredService <TestSocketSessionInterceptor>();

                DocumentNode document =
                    Utf8GraphQLParser.Parse("subscription { onNext }");

                var request = new GraphQLRequest(document);

                const string subscriptionId = "abc";

                await webSocket.SendSubscriptionStartAsync(subscriptionId, request);

                var cts = new CancellationTokenSource();
                webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "", cts.Token);

                // act
                await WaitForConditions(
                    () => testInterceptor.CloseWasCalled,
                    TimeSpan.FromMilliseconds(500));

                cts.Cancel();

                // assert
                Assert.True(testInterceptor.CloseWasCalled);
            }));
        }