Ejemplo n.º 1
0
        public void ReadMessageAsync_ExceedReceiveSize_ReturnData()
        {
            // Arrange
            var context = HttpContextServerCallContextHelper.CreateServerCallContext(serviceOptions: new GrpcServiceOptions {
                ReceiveMaxMessageSize = 1
            });
            var ms = new MemoryStream(new byte[]
            {
                0x00,     // compression = 0
                0x00,
                0x00,
                0x00,
                0x02,     // length = 1
                0x10,
                0x10
            });

            var pipeReader = new StreamPipeReader(ms);

            // Act
            var ex = Assert.ThrowsAsync <RpcException>(() => pipeReader.ReadSingleMessageAsync(context).AsTask());

            // Assert
            Assert.AreEqual("Received message exceeds the maximum configured message size.", ex.Status.Detail);
            Assert.AreEqual(StatusCode.ResourceExhausted, ex.StatusCode);
        }
Ejemplo n.º 2
0
        public async Task ReadMessageAsync_UnderReceiveSize_ReturnData()
        {
            // Arrange
            var context = HttpContextServerCallContextHelper.CreateServerCallContext(serviceOptions: new GrpcServiceOptions {
                SendMaxMessageSize = 1
            });
            var ms = new MemoryStream(new byte[]
            {
                0x00,     // compression = 0
                0x00,
                0x00,
                0x00,
                0x01,     // length = 1
                0x10
            });

            var pipeReader = new StreamPipeReader(ms);

            // Act
            var messageData = await pipeReader.ReadSingleMessageAsync(context);

            // Assert
            Assert.AreEqual(1, messageData.Length);
            Assert.AreEqual(0x10, messageData[0]);
        }
Ejemplo n.º 3
0
        public async Task ReadMessageAsync_LongMessage_ReturnData()
        {
            // Arrange
            var content = Encoding.UTF8.GetBytes("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam varius nibh a blandit mollis. "
                                                 + "In hac habitasse platea dictumst. Proin non quam nec neque convallis commodo. Orci varius natoque penatibus et magnis dis "
                                                 + "parturient montes, nascetur ridiculus mus. Mauris commodo est vehicula, semper arcu eu, ornare urna. Mauris malesuada nisl "
                                                 + "nisl, vitae tincidunt purus vestibulum sit amet. Interdum et malesuada fames ac ante ipsum primis in faucibus.");

            var ms = new MemoryStream(new byte[]
            {
                0x00,     // compression = 0
                0x00,
                0x00,
                0x01,
                0xC1     // length = 449
            }.Concat(content).ToArray());

            var pipeReader = new StreamPipeReader(ms);

            // Act
            var messageData = await pipeReader.ReadSingleMessageAsync(TestServerCallContext);

            // Assert
            Assert.AreEqual(449, messageData.Length);
            CollectionAssert.AreEqual(content, messageData);
        }
Ejemplo n.º 4
0
        public static async Task <T> AssertReadMessageAsync <T>(Stream stream, string compressionEncoding = null, List <ICompressionProvider> compressionProviders = null) where T : IMessage, new()
        {
            compressionProviders = compressionProviders ?? new List <ICompressionProvider>
            {
                new GzipCompressionProvider(CompressionLevel.Fastest)
            };

            var pipeReader = new StreamPipeReader(stream);

            var httpContext = new DefaultHttpContext();

            httpContext.Request.Headers[GrpcProtocolConstants.MessageEncodingHeader] = compressionEncoding;

            var serverCallContext = HttpContextServerCallContextHelper.CreateServerCallContext(
                httpContext: httpContext,
                serviceOptions: new GrpcServiceOptions
            {
                ResponseCompressionAlgorithm = compressionEncoding,
                CompressionProviders         = compressionProviders
            });

            var messageData = await pipeReader.ReadSingleMessageAsync(serverCallContext);

            var message = new T();

            message.MergeFrom(messageData);

            return(message);
        }
Ejemplo n.º 5
0
        public static async Task <T> AssertReadMessageAsync <T>(Stream stream) where T : IMessage, new()
        {
            var pipeReader = new StreamPipeReader(stream);

            var messageData = await pipeReader.ReadSingleMessageAsync(TestServerCallContext);

            var message = new T();

            message.MergeFrom(messageData);

            return(message);
        }
Ejemplo n.º 6
0
        public void ReadMessageStreamAsync_HeaderIncomplete_ThrowError()
        {
            // Arrange
            var ms = new MemoryStream(new byte[]
            {
                0x00,     // compression = 0
                0x00,
                0x00
            });

            var pipeReader = new StreamPipeReader(ms);

            // Act
            var ex = Assert.ThrowsAsync <InvalidDataException>(
                () => pipeReader.ReadSingleMessageAsync().AsTask());

            // Assert
            Assert.AreEqual("Incomplete message.", ex.Message);
        }
Ejemplo n.º 7
0
        public async Task ReadMessageAsync_EmptyMessage_ReturnNoData()
        {
            // Arrange
            var ms = new MemoryStream(new byte[]
            {
                0x00,     // compression = 0
                0x00,
                0x00,
                0x00,
                0x00     // length = 0
            });

            var pipeReader = new StreamPipeReader(ms);

            // Act
            var messageData = await pipeReader.ReadSingleMessageAsync(TestServerCallContext);

            // Assert
            Assert.AreEqual(0, messageData.Length);
        }
Ejemplo n.º 8
0
        public void ReadMessageStreamAsync_HeaderIncomplete_ThrowError()
        {
            // Arrange
            var ms = new MemoryStream(new byte[]
            {
                0x00,     // compression = 0
                0x00,
                0x00
            });

            var pipeReader = new StreamPipeReader(ms);

            // Act
            var ex = Assert.ThrowsAsync <RpcException>(
                () => pipeReader.ReadSingleMessageAsync(TestServerCallContext).AsTask());

            // Assert
            Assert.AreEqual("Incomplete message.", ex.Status.Detail);
            Assert.AreEqual(StatusCode.Internal, ex.StatusCode);
        }
Ejemplo n.º 9
0
        public async Task ReadMessageAsync_OneByteMessage_ReturnData()
        {
            // Arrange
            var ms = new MemoryStream(new byte[]
            {
                0x00,     // compression = 0
                0x00,
                0x00,
                0x00,
                0x01,     // length = 1
                0x10
            });

            var pipeReader = new StreamPipeReader(ms);

            // Act
            var messageData = await pipeReader.ReadSingleMessageAsync();

            // Assert
            Assert.AreEqual(1, messageData.Length);
            Assert.AreEqual(0x10, messageData[0]);
        }
        public void ReadMessageAsync_ExceedReceiveSize_ReturnData()
        {
            // Arrange
            var ms = new MemoryStream(new byte[]
                {
                    0x00, // compression = 0
                    0x00,
                    0x00,
                    0x00,
                    0x02, // length = 1
                    0x10,
                    0x10
                });

            var pipeReader = new StreamPipeReader(ms);

            // Act
            var ex = Assert.ThrowsAsync<InvalidDataException>(() => pipeReader.ReadSingleMessageAsync(new GrpcServiceOptions { ReceiveMaxMessageSize = 1 }).AsTask());

            // Assert
            Assert.AreEqual("Received message exceeds the maximum configured message size.", ex.Message);
        }
Ejemplo n.º 11
0
        public void ReadMessageAsync_AdditionalData_ThrowError()
        {
            // Arrange
            var ms = new MemoryStream(new byte[]
            {
                0x00,     // compression = 0
                0x00,
                0x00,
                0x00,
                0x01,    // length = 2
                0x10,
                0x10     // additional data
            });

            var pipeReader = new StreamPipeReader(ms);

            // Act
            var ex = Assert.ThrowsAsync <InvalidDataException>(
                () => pipeReader.ReadSingleMessageAsync().AsTask());

            // Assert
            Assert.AreEqual("Additional data after the message received.", ex.Message);
        }
Ejemplo n.º 12
0
        public void ReadMessageAsync_AdditionalData_ThrowError()
        {
            // Arrange
            var ms = new MemoryStream(new byte[]
            {
                0x00,     // compression = 0
                0x00,
                0x00,
                0x00,
                0x01,    // length = 2
                0x10,
                0x10     // additional data
            });

            var pipeReader = new StreamPipeReader(ms);

            // Act
            var ex = Assert.ThrowsAsync <RpcException>(
                () => pipeReader.ReadSingleMessageAsync(TestServerCallContext).AsTask());

            // Assert
            Assert.AreEqual("Additional data after the message received.", ex.Status.Detail);
            Assert.AreEqual(StatusCode.Internal, ex.StatusCode);
        }