示例#1
0
        public void ParseMessage_TypeIsNull_ThrowException()
        {
            // arrange
            ReadOnlySequence <byte> message = GetBytes($@"{{""type"": null, ""Foo"":1}}");

            // act
            Exception?ex = Record.Exception(() => GraphQLWebSocketMessageParser.Parse(message));

            // assert
            Assert.IsType <SerializationException>(ex).Message.MatchSnapshot();
        }
示例#2
0
        public void ParseMessage_OnlyId_ThrowException()
        {
            // arrange
            ReadOnlySequence <byte> message = GetBytes(@"{""id"": ""123""}");

            // act
            Exception?ex = Record.Exception(() => GraphQLWebSocketMessageParser.Parse(message));

            // assert
            Assert.IsType <SerializationException>(ex).Message.MatchSnapshot();
        }
示例#3
0
        public void ParseMessage_KeepAlive_ParseMessage()
        {
            // arrange
            ReadOnlySequence <byte> message = GetBytes(@"{""type"": ""ka""}");

            // act
            GraphQLWebSocketMessage parsed = GraphQLWebSocketMessageParser.Parse(message);

            // assert
            Assert.Equal(GraphQLWebSocketMessageType.KeepAlive, parsed.Type);
        }
示例#4
0
        public void ParseMessage_UnknownType_ThrowException(string type)
        {
            // arrange
            ReadOnlySequence <byte> message = GetBytes($@"{{""type"": ""{type}"", ""Foo"":1}}");

            // act
            Exception?ex = Record.Exception(() => GraphQLWebSocketMessageParser.Parse(message));

            // assert
            Assert.IsType <SerializationException>(ex)
            .Message.MatchSnapshot(new SnapshotNameExtension(type));
        }
示例#5
0
        public void ParseMessage_IncompleteDocument_ThrowException()
        {
            // arrange
            ReadOnlySequence <byte> message = GetBytes(@"{""id"": ""123""");

            // act
            Exception?ex = Record.Exception(() => GraphQLWebSocketMessageParser.Parse(message));

            // assert
            Assert.NotNull(ex);
            ex.Message.MatchSnapshot();
        }
示例#6
0
        public void ParseMessage_ConnectionTerminate_ParseMessage()
        {
            // arrange
            ReadOnlySequence <byte> message =
                GetBytes(@"{""type"": ""connection_terminate"" }");

            // act
            GraphQLWebSocketMessage parsed = GraphQLWebSocketMessageParser.Parse(message);

            // assert
            Assert.Equal(GraphQLWebSocketMessageType.ConnectionTerminate, parsed.Type);
        }
示例#7
0
        public void ParseMessage_ConnectionError_ParseMessage()
        {
            // arrange
            ReadOnlySequence <byte> message =
                GetBytes(@"{""type"": ""connection_error"", ""payload"": ""payload""}");

            // act
            GraphQLWebSocketMessage parsed = GraphQLWebSocketMessageParser.Parse(message);

            // assert
            Assert.Equal(GraphQLWebSocketMessageType.ConnectionError, parsed.Type);
            Assert.Equal("payload", parsed.Payload.RootElement.ToString());
        }
示例#8
0
        public void ParseMessage_Complete_ParseMessage()
        {
            // arrange
            ReadOnlySequence <byte> message =
                GetBytes(@"{""type"": ""complete"", ""id"":""123""}");

            // act
            GraphQLWebSocketMessage parsed = GraphQLWebSocketMessageParser.Parse(message);

            // assert
            Assert.Equal(GraphQLWebSocketMessageType.Complete, parsed.Type);
            Assert.Equal("123", parsed.Id);
        }
示例#9
0
        public void ParseMessage_Start_ParseMessage()
        {
            // arrange
            ReadOnlySequence <byte> message =
                GetBytes(@"{""type"": ""start"", ""id"":""123"", ""payload"": ""payload""}");

            // act
            GraphQLWebSocketMessage parsed = GraphQLWebSocketMessageParser.Parse(message);

            // assert
            Assert.Equal(GraphQLWebSocketMessageType.Start, parsed.Type);
            Assert.Equal("123", parsed.Id);
            Assert.Equal("payload", parsed.Payload.RootElement.ToString());
        }
示例#10
0
        private ValueTask ProcessAsync(
            ReadOnlySequence <byte> slice,
            CancellationToken cancellationToken)
        {
            try
            {
                GraphQLWebSocketMessage message = GraphQLWebSocketMessageParser.Parse(slice);

                if (message.Id is { } id)
                {
                    return(message.Type switch
                    {
                        GraphQLWebSocketMessageType.Data =>
                        Notify(
                            id,
                            new DataDocumentOperationMessage <JsonDocument>(message.Payload),
                            cancellationToken),

                        GraphQLWebSocketMessageType.Complete =>
                        Notify(
                            id,
                            CompleteOperationMessage.Default,
                            cancellationToken),

                        GraphQLWebSocketMessageType.Error =>
                        Notify(
                            id,
                            ErrorOperationMessage.UnexpectedServerError,
                            cancellationToken),

                        GraphQLWebSocketMessageType.ConnectionError =>
                        Notify(
                            id,
                            ErrorOperationMessage.ConnectionInitializationError,
                            cancellationToken),

                        _ => CloseSocketOnProtocolError(
                            "Invalid message type received: " + message.Type,
                            cancellationToken)
                    });
                }