public async Task SendCompressedMessage_ServiceHasNoCompressionConfigured_ResponseIdentityEncoding()
        {
            // Arrange
            var requestMessage = new HelloRequest
            {
                Name = "World"
            };

            var requestStream = new MemoryStream();

            MessageHelpers.WriteMessage(requestStream, requestMessage, "gzip");

            var httpRequest = new HttpRequestMessage(HttpMethod.Post, "Greet.Greeter/SayHello");

            httpRequest.Headers.Add(GrpcProtocolConstants.MessageEncodingHeader, "gzip");
            httpRequest.Content = new GrpcStreamContent(requestStream);

            // Act
            var response = await Fixture.Client.SendAsync(httpRequest).DefaultTimeout();

            // Assert
            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
            Assert.AreEqual("identity", response.Headers.GetValues(GrpcProtocolConstants.MessageEncodingHeader).Single());

            var responseMessage = MessageHelpers.AssertReadMessage <HelloReply>(await response.Content.ReadAsByteArrayAsync().DefaultTimeout());

            Assert.AreEqual("Hello World", responseMessage.Message);
            response.AssertTrailerStatus();
        }
        public async Task SendIdentityGrpcAcceptEncodingToServiceWithCompression_ResponseUncompressed()
        {
            // Arrange
            var requestMessage = new HelloRequest
            {
                Name = "World"
            };

            var requestStream = new MemoryStream();

            MessageHelpers.WriteMessage(requestStream, requestMessage);

            var httpRequest = GrpcHttpHelper.Create("Compression.CompressionService/SayHello");

            httpRequest.Headers.Add(GrpcProtocolConstants.MessageEncodingHeader, "identity");
            httpRequest.Headers.Add(GrpcProtocolConstants.MessageAcceptEncodingHeader, "identity");
            httpRequest.Content = new GrpcStreamContent(requestStream);

            // Act
            var response = await Fixture.Client.SendAsync(httpRequest).DefaultTimeout();

            // Assert
            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
            Assert.AreEqual("identity", response.Headers.GetValues(GrpcProtocolConstants.MessageEncodingHeader).Single());

            var responseMessage = MessageHelpers.AssertReadMessage <HelloReply>(await response.Content.ReadAsByteArrayAsync().DefaultTimeout());

            Assert.AreEqual("Hello World", responseMessage.Message);
            response.AssertTrailerStatus();
        }
        public async Task SendCompressedMessageAndReturnResultWithNoCompressFlag_ResponseNotCompressed()
        {
            // Arrange
            var requestMessage = new HelloRequest
            {
                Name = "World"
            };

            var requestStream = new MemoryStream();

            MessageHelpers.WriteMessage(requestStream, requestMessage, "gzip");

            var httpRequest = new HttpRequestMessage(HttpMethod.Post, "Compression.CompressionService/WriteMessageWithoutCompression");

            httpRequest.Headers.Add(GrpcProtocolConstants.MessageEncodingHeader, "gzip");
            httpRequest.Headers.Add(GrpcProtocolConstants.MessageAcceptEncodingHeader, "gzip");
            httpRequest.Content = new GrpcStreamContent(requestStream);

            // Act
            var response = await Fixture.Client.SendAsync(httpRequest).DefaultTimeout();

            // Assert
            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);

            // The overall encoding is gzip but the actual response does not use compression
            Assert.AreEqual("gzip", response.Headers.GetValues(GrpcProtocolConstants.MessageEncodingHeader).Single());

            var responseMessage = MessageHelpers.AssertReadMessage <HelloReply>(await response.Content.ReadAsByteArrayAsync().DefaultTimeout());

            Assert.AreEqual("Hello World", responseMessage.Message);
            response.AssertTrailerStatus();
        }
        public async Task SendUncompressedMessageToServiceWithCompression_ResponseCompressed()
        {
            // Arrange
            var requestMessage = new HelloRequest
            {
                Name = "World"
            };

            var requestStream = new MemoryStream();

            MessageHelpers.WriteMessage(requestStream, requestMessage);

            var httpRequest = new HttpRequestMessage(HttpMethod.Post, "Compression.CompressionService/SayHello");

            httpRequest.Headers.Add(GrpcProtocolConstants.MessageAcceptEncodingHeader, "gzip");
            httpRequest.Content = new GrpcStreamContent(requestStream);

            // Act
            var response = await Fixture.Client.SendAsync(httpRequest).DefaultTimeout();

            // Assert
            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
            Assert.AreEqual("gzip", response.Headers.GetValues(GrpcProtocolConstants.MessageEncodingHeader).Single());

            var responseMessage = MessageHelpers.AssertReadMessage <HelloReply>(await response.Content.ReadAsByteArrayAsync().DefaultTimeout(), "gzip");

            Assert.AreEqual("Hello World", responseMessage.Message);

            Assert.AreEqual(StatusCode.OK.ToTrailerString(), Fixture.TrailersContainer.Trailers[GrpcProtocolConstants.StatusTrailer].ToString());
        }
        public async Task SendValidRequest_SuccessResponse()
        {
            // Arrange
            var requestMessage = new HelloRequest
            {
                Name = "World"
            };

            var ms = new MemoryStream();

            MessageHelpers.WriteMessage(ms, requestMessage);

            // Act
            var response = await Fixture.Client.PostAsync(
                "Greet.Greeter/SayHello",
                new StreamContent(ms)).DefaultTimeout();

            // Assert
            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
            Assert.AreEqual("identity", response.Headers.GetValues("grpc-encoding").Single());
            Assert.AreEqual("application/grpc", response.Content.Headers.ContentType.MediaType);

            var responseMessage = MessageHelpers.AssertReadMessage <HelloReply>(await response.Content.ReadAsByteArrayAsync().DefaultTimeout());

            Assert.AreEqual("Hello World", responseMessage.Message);

            Assert.AreEqual(StatusCode.OK.ToTrailerString(), Fixture.TrailersContainer.Trailers[GrpcProtocolConstants.StatusTrailer].Single());
        }
Beispiel #6
0
        public async Task CallAuthorizedServiceWithToken_Success()
        {
            // Arrange
            var tokenResponse = await Fixture.Client.GetAsync("generateJwtToken").DefaultTimeout();

            var token = await tokenResponse.Content.ReadAsStringAsync();

            var requestMessage = new HelloRequest
            {
                Name = "World"
            };

            var ms = new MemoryStream();

            MessageHelpers.WriteMessage(ms, requestMessage);

            var httpRequest = new HttpRequestMessage(HttpMethod.Post, "Authorize.AuthorizedGreeter/SayHello");

            httpRequest.Headers.Add("access_token", token);
            httpRequest.Content = new GrpcStreamContent(ms);

            // Act
            var response = await Fixture.Client.SendAsync(httpRequest);

            // Assert
            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
            Assert.AreEqual("identity", response.Headers.GetValues("grpc-encoding").Single());
            Assert.AreEqual("application/grpc", response.Content.Headers.ContentType.MediaType);

            var responseMessage = MessageHelpers.AssertReadMessage <HelloReply>(await response.Content.ReadAsByteArrayAsync().DefaultTimeout());

            Assert.AreEqual("Hello World", responseMessage.Message);

            Assert.AreEqual(StatusCode.OK.ToTrailerString(), Fixture.TrailersContainer.Trailers[GrpcProtocolConstants.StatusTrailer].Single());
        }
Beispiel #7
0
        public async Task CallNestedService_SuccessResponse()
        {
            // Arrange
            var requestMessage = new HelloRequest
            {
                Name = "World"
            };

            var ms = new MemoryStream();

            MessageHelpers.WriteMessage(ms, requestMessage);

            // Act
            var response = await Fixture.Client.PostAsync(
                "Nested.NestedService/SayHello",
                new GrpcStreamContent(ms)).DefaultTimeout();

            // Assert
            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);

            var responseMessage = MessageHelpers.AssertReadMessage <HelloReply>(await response.Content.ReadAsByteArrayAsync().DefaultTimeout());

            Assert.AreEqual("Hello Nested: World", responseMessage.Message);
            response.AssertTrailerStatus();
        }
        public async Task SendHeadersTwice_ThrowsException()
        {
            // Arrange
            var requestMessage = new HelloRequest
            {
                Name = "World"
            };

            var ms = new MemoryStream();

            MessageHelpers.WriteMessage(ms, requestMessage);

            // Act
            var response = await Fixture.Client.PostAsync(
                "Greet.Greeter/SayHelloSendHeadersTwice",
                new StreamContent(ms)).DefaultTimeout();

            // Assert
            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
            Assert.AreEqual("identity", response.Headers.GetValues("grpc-encoding").Single());
            Assert.AreEqual("application/grpc", response.Content.Headers.ContentType.MediaType);

            var responseMessage = MessageHelpers.AssertReadMessage <HelloReply>(await response.Content.ReadAsByteArrayAsync().DefaultTimeout());

            Assert.AreEqual("Exception validated", responseMessage.Message);
        }
Beispiel #9
0
        public async Task SayHello_ValidRequest_SuccessResponse()
        {
            // Arrange
            var requestMessage = new HelloRequest
            {
                Name = "World"
            };

            var stream = new MemoryStream();
            await MessageHelpers.WriteMessageAsync(stream, requestMessage).DefaultTimeout();

            // Act
            var response = await Fixture.Client.PostAsync(
                "Greet.Greeter/SayHello",
                new StreamContent(stream)).DefaultTimeout();

            // Assert
            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
            Assert.AreEqual("identity", response.Headers.GetValues("grpc-encoding").Single());
            Assert.AreEqual("application/grpc", response.Content.Headers.ContentType.MediaType);

            var responseMessage = MessageHelpers.AssertReadMessage <HelloReply>(await response.Content.ReadAsByteArrayAsync().DefaultTimeout());

            Assert.AreEqual("Hello World", responseMessage.Message);
        }
Beispiel #10
0
        private async Task <ValueResponse> CallLifetimeService(string path)
        {
            var requestStream = new MemoryStream();

            MessageHelpers.WriteMessage(requestStream, new Empty());

            var response = await Fixture.Client.PostAsync(path, new GrpcStreamContent(requestStream)).DefaultTimeout();

            return(MessageHelpers.AssertReadMessage <ValueResponse>(await response.Content.ReadAsByteArrayAsync().DefaultTimeout()));
        }
Beispiel #11
0
        public async Task SendCompressedMessage_UnaryEnabledInCallWithInvalidSetting_UncompressedMessageReturned()
        {
            async Task <HelloReply> UnaryEnableCompression(HelloRequest request, ServerCallContext context)
            {
                var headers = new Metadata {
                    new Metadata.Entry("grpc-internal-encoding-request", "PURPLE_MONKEY_DISHWASHER")
                };
                await context.WriteResponseHeadersAsync(headers);

                return(new HelloReply {
                    Message = "Hello " + request.Name
                });
            }

            // Arrange
            var method = Fixture.DynamicGrpc.AddUnaryMethod <HelloRequest, HelloReply>(UnaryEnableCompression);

            var ms = new MemoryStream();

            MessageHelpers.WriteMessage(ms, new HelloRequest
            {
                Name = "World"
            });

            var httpRequest = GrpcHttpHelper.Create(method.FullName);

            httpRequest.Content = new PushStreamContent(
                async s =>
            {
                await s.WriteAsync(ms.ToArray()).AsTask().DefaultTimeout();
                await s.FlushAsync().DefaultTimeout();
            });

            // Act
            var responseTask = Fixture.Client.SendAsync(httpRequest);

            // Assert
            var response = await responseTask.DefaultTimeout();

            response.AssertIsSuccessfulGrpcRequest();

            // Because the client didn't send this encoding in accept, the server has sent the message uncompressed.
            Assert.AreEqual("PURPLE_MONKEY_DISHWASHER", response.Headers.GetValues("grpc-encoding").Single());

            var returnedMessageData = await response.Content.ReadAsByteArrayAsync().DefaultTimeout();

            Assert.AreEqual(0, returnedMessageData[0]);

            var responseMessage = MessageHelpers.AssertReadMessage <HelloReply>(returnedMessageData);

            Assert.AreEqual("Hello World", responseMessage.Message);
            response.AssertTrailerStatus();
        }
Beispiel #12
0
        public async Task MessageSentInMultipleChunks_SuccessResponse()
        {
            // Arrange
            SetExpectedErrorsFilter(writeContext =>
            {
                return(writeContext.LoggerName == typeof(GreeterService).FullName &&
                       writeContext.EventId.Name == "RpcConnectionError" &&
                       writeContext.State.ToString() == "Error status code 'Internal' raised.");
            });

            var requestMessage = new HelloRequest
            {
                Name = "World"
            };

            var ms = new MemoryStream();

            MessageHelpers.WriteMessage(ms, requestMessage);

            var requestStream = new SyncPointMemoryStream();

            var httpRequest = new HttpRequestMessage(HttpMethod.Post, "Greet.Greeter/SayHello");

            httpRequest.Content = new GrpcStreamContent(requestStream);

            // Act
            var responseTask = Fixture.Client.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead);

            // Assert
            Assert.IsFalse(responseTask.IsCompleted, "Server should wait for client to finish streaming");

            // Send message one byte at a time then finish
            foreach (var b in ms.ToArray())
            {
                await requestStream.AddDataAndWait(new[] { b }).DefaultTimeout();
            }
            await requestStream.AddDataAndWait(Array.Empty <byte>()).DefaultTimeout();

            var response = await responseTask.DefaultTimeout();

            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);

            var responseMessage = MessageHelpers.AssertReadMessage <HelloReply>(await response.Content.ReadAsByteArrayAsync().DefaultTimeout());

            Assert.AreEqual("Hello World", responseMessage.Message);

            Assert.AreEqual(StatusCode.OK.ToTrailerString(), Fixture.TrailersContainer.Trailers[GrpcProtocolConstants.StatusTrailer].Single());
        }
Beispiel #13
0
        public async Task MessageSentInMultipleChunks_SuccessResponse()
        {
            // Arrange
            var requestMessage = new HelloRequest
            {
                Name = "World"
            };

            var ms = new MemoryStream();

            MessageHelpers.WriteMessage(ms, requestMessage);

            var requestStream = new SyncPointMemoryStream();

            var httpRequest = new HttpRequestMessage(HttpMethod.Post, "Greet.Greeter/SayHello");

            httpRequest.Content = new StreamContent(requestStream);

            // Act
            var responseTask = Fixture.Client.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead);

            // Assert
            Assert.IsFalse(responseTask.IsCompleted, "Server should wait for client to finish streaming");

            // Send message one byte at a time then finish
            foreach (var b in ms.ToArray())
            {
                await requestStream.AddDataAndWait(new[] { b }).DefaultTimeout();
            }
            await requestStream.AddDataAndWait(Array.Empty <byte>()).DefaultTimeout();

            var response = await responseTask.DefaultTimeout();

            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);

            var responseMessage = MessageHelpers.AssertReadMessage <HelloReply>(await response.Content.ReadAsByteArrayAsync().DefaultTimeout());

            Assert.AreEqual("Hello World", responseMessage.Message);
        }
        public async Task StreamedMessage_SuccessResponseAfterMessageReceived()
        {
            // Arrange
            var requestMessage = new HelloRequest
            {
                Name = "World"
            };

            var ms = new MemoryStream();

            MessageHelpers.WriteMessage(ms, requestMessage);

            var requestStream = new SyncPointMemoryStream();

            var httpRequest = new HttpRequestMessage(HttpMethod.Post, "Greet.Greeter/SayHello");

            httpRequest.Content = new StreamContent(requestStream);

            // Act
            var responseTask = Fixture.Client.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead);

            // Assert
            Assert.IsFalse(responseTask.IsCompleted, "Server should wait for client to finish streaming");

            await requestStream.AddDataAndWait(ms.ToArray()).DefaultTimeout();

            await requestStream.AddDataAndWait(Array.Empty <byte>()).DefaultTimeout();

            var response = await responseTask.DefaultTimeout();

            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
            Assert.AreEqual("identity", response.Headers.GetValues("grpc-encoding").Single());
            Assert.AreEqual("application/grpc", response.Content.Headers.ContentType.MediaType);

            var responseMessage = MessageHelpers.AssertReadMessage <HelloReply>(await response.Content.ReadAsByteArrayAsync().DefaultTimeout());

            Assert.AreEqual("Hello World", responseMessage.Message);
        }