Пример #1
0
        public async Task AsyncUnaryCall_CompressedResponseWithUnknownEncoding_ErrorThrown()
        {
            // Arrange
            HttpRequestMessage?httpRequestMessage = null;
            HelloRequest?      helloRequest       = null;

            var httpClient = ClientTestHelpers.CreateTestClient(async request =>
            {
                httpRequestMessage = request;

                var requestStream = await request.Content !.ReadAsStreamAsync().DefaultTimeout();

                helloRequest = await StreamSerializationHelper.ReadMessageAsync(
                    requestStream,
                    ClientTestHelpers.ServiceMethod.RequestMarshaller.ContextualDeserializer,
                    "gzip",
                    maximumMessageSize: null,
                    GrpcProtocolConstants.DefaultCompressionProviders,
                    singleMessage: true,
                    CancellationToken.None);

                HelloReply reply = new HelloReply
                {
                    Message = "Hello world"
                };

                var compressionProvider = new GzipCompressionProvider(System.IO.Compression.CompressionLevel.Fastest);
                var streamContent       = await ClientTestHelpers.CreateResponseContent(reply, compressionProvider).DefaultTimeout();

                return(ResponseUtils.CreateResponse(HttpStatusCode.OK, streamContent, grpcEncoding: "not-supported"));
            });
            var invoker = HttpClientCallInvokerFactory.Create(httpClient);

            // Act
            var call = invoker.AsyncUnaryCall <HelloRequest, HelloReply>(ClientTestHelpers.ServiceMethod, string.Empty, new CallOptions(), new HelloRequest
            {
                Name = "Hello"
            });

            // Assert
            var ex = await ExceptionAssert.ThrowsAsync <RpcException>(() => call.ResponseAsync).DefaultTimeout();

            Assert.AreEqual(StatusCode.Unimplemented, ex.StatusCode);
#if NET6_0_OR_GREATER
            Assert.AreEqual("Unsupported grpc-encoding value 'not-supported'. Supported encodings: identity, gzip, deflate", ex.Status.Detail);
#else
            Assert.AreEqual("Unsupported grpc-encoding value 'not-supported'. Supported encodings: identity, gzip", ex.Status.Detail);
#endif
        }
        public async Task AsyncUnaryCall_CompressedResponse_ResponseMessageDecompressed()
        {
            // Arrange
            HttpRequestMessage?httpRequestMessage = null;
            HelloRequest?      helloRequest       = null;

            var httpClient = ClientTestHelpers.CreateTestClient(async request =>
            {
                httpRequestMessage = request;

                var requestStream = await request.Content !.ReadAsStreamAsync().DefaultTimeout();

                helloRequest = await StreamExtensions.ReadMessageAsync(
                    requestStream,
                    new DefaultDeserializationContext(),
                    NullLogger.Instance,
                    ClientTestHelpers.ServiceMethod.RequestMarshaller.ContextualDeserializer,
                    "gzip",
                    maximumMessageSize: null,
                    GrpcProtocolConstants.DefaultCompressionProviders,
                    singleMessage: true,
                    CancellationToken.None);

                HelloReply reply = new HelloReply
                {
                    Message = "Hello world"
                };

                var compressionProvider = new GzipCompressionProvider(System.IO.Compression.CompressionLevel.Fastest);
                var streamContent       = await ClientTestHelpers.CreateResponseContent(reply, compressionProvider).DefaultTimeout();

                return(ResponseUtils.CreateResponse(HttpStatusCode.OK, streamContent, grpcEncoding: "gzip"));
            });
            var invoker = HttpClientCallInvokerFactory.Create(httpClient);

            // Act
            var call = invoker.AsyncUnaryCall <HelloRequest, HelloReply>(ClientTestHelpers.ServiceMethod, string.Empty, new CallOptions(), new HelloRequest
            {
                Name = "Hello"
            });

            // Assert
            var response = await call.ResponseAsync;

            Assert.IsNotNull(response);
            Assert.AreEqual("Hello world", response.Message);
        }
Пример #3
0
        public void AsyncUnaryCall_CompressedResponseWithUnknownEncoding_ErrorThrown()
        {
            // Arrange
            HttpRequestMessage?httpRequestMessage = null;
            HelloRequest?      helloRequest       = null;

            var httpClient = ClientTestHelpers.CreateTestClient(async request =>
            {
                httpRequestMessage = request;

                var requestStream = await request.Content.ReadAsStreamAsync();

                helloRequest = await StreamExtensions.ReadSingleMessageAsync(
                    requestStream,
                    NullLogger.Instance,
                    ClientTestHelpers.ServiceMethod.RequestMarshaller.Deserializer,
                    "gzip",
                    CancellationToken.None);

                HelloReply reply = new HelloReply
                {
                    Message = "Hello world"
                };

                var compressionProvider = new GzipCompressionProvider(System.IO.Compression.CompressionLevel.Fastest);
                var streamContent       = await ClientTestHelpers.CreateResponseContent(reply, compressionProvider).DefaultTimeout();

                return(ResponseUtils.CreateResponse(HttpStatusCode.OK, streamContent, grpcEncoding: "not-supported"));
            });
            var invoker = HttpClientCallInvokerFactory.Create(httpClient);

            // Act
            var call = invoker.AsyncUnaryCall <HelloRequest, HelloReply>(ClientTestHelpers.ServiceMethod, string.Empty, new CallOptions(), new HelloRequest
            {
                Name = "Hello"
            });

            // Assert
            var ex = Assert.ThrowsAsync <RpcException>(async() => await call.ResponseAsync.DefaultTimeout());

            Assert.AreEqual(StatusCode.Unimplemented, ex.StatusCode);
            Assert.AreEqual("Unsupported grpc-encoding value 'not-supported'. Supported encodings: gzip", ex.Status.Detail);
        }