コード例 #1
0
        public async Task AsyncUnaryCall_SuccessTrailersOnly_ThrowNoMessageError()
        {
            // Arrange
            HttpResponseMessage?responseMessage = null;
            var httpClient = ClientTestHelpers.CreateTestClient(request =>
            {
                responseMessage = ResponseUtils.CreateResponse(HttpStatusCode.OK, new ByteArrayContent(Array.Empty <byte>()), grpcStatusCode: null);
                responseMessage.Headers.Add(GrpcProtocolConstants.StatusTrailer, StatusCode.OK.ToString("D"));
                responseMessage.Headers.Add(GrpcProtocolConstants.MessageTrailer, "Detail!");
                return(Task.FromResult(responseMessage));
            });
            var invoker = HttpClientCallInvokerFactory.Create(httpClient);

            // Act
            var call    = invoker.AsyncUnaryCall <HelloRequest, HelloReply>(ClientTestHelpers.ServiceMethod, string.Empty, new CallOptions(), new HelloRequest());
            var headers = await call.ResponseHeadersAsync.DefaultTimeout();

            var response = await ExceptionAssert.ThrowsAsync <InvalidOperationException>(() => call.ResponseAsync).DefaultTimeout();

            // Assert
            Assert.NotNull(responseMessage);
            Assert.IsFalse(responseMessage !.TrailingHeaders.Any()); // sanity check that there are no trailers

            Assert.AreEqual(StatusCode.OK, call.GetStatus().StatusCode);
            Assert.AreEqual("Detail!", call.GetStatus().Detail);

            Assert.AreEqual(0, headers.Count);
            Assert.AreEqual(0, call.GetTrailers().Count);
        }
コード例 #2
0
        public async Task AsyncServerStreamingCall_StatusInFooterAndMessageInHeader_IgnoreMessage()
        {
            // Arrange
            HttpResponseMessage?responseMessage = null;
            var httpClient = ClientTestHelpers.CreateTestClient(request =>
            {
                responseMessage = ResponseUtils.CreateResponse(HttpStatusCode.OK, new ByteArrayContent(Array.Empty <byte>()));
                responseMessage.Headers.Add(GrpcProtocolConstants.MessageTrailer, "Ignored detail!");
                return(Task.FromResult(responseMessage));
            });
            var invoker = HttpClientCallInvokerFactory.Create(httpClient);

            // Act
            var call    = invoker.AsyncServerStreamingCall <HelloRequest, HelloReply>(ClientTestHelpers.ServiceMethod, string.Empty, new CallOptions(), new HelloRequest());
            var headers = await call.ResponseHeadersAsync;
            await call.ResponseStream.MoveNext(CancellationToken.None);

            // Assert
            Assert.IsTrue(responseMessage !.TrailingHeaders.TryGetValues(GrpcProtocolConstants.StatusTrailer, out _)); // sanity status is in trailers

            Assert.AreEqual(StatusCode.OK, call.GetStatus().StatusCode);
            Assert.AreEqual(null, call.GetStatus().Detail);

            Assert.AreEqual(0, headers.Count);
            Assert.AreEqual(0, call.GetTrailers().Count);
        }
コード例 #3
0
        public async Task AsyncServerStreamingCall_TrailersOnly_TrailersReturnedWithHeaders()
        {
            // Arrange
            HttpResponseMessage?responseMessage = null;
            var httpClient = ClientTestHelpers.CreateTestClient(request =>
            {
                responseMessage = ResponseUtils.CreateResponse(HttpStatusCode.OK, new ByteArrayContent(Array.Empty <byte>()), grpcStatusCode: null);
                responseMessage.Headers.Add(GrpcProtocolConstants.StatusTrailer, StatusCode.OK.ToString("D"));
                responseMessage.Headers.Add(GrpcProtocolConstants.MessageTrailer, "Detail!");
                return(Task.FromResult(responseMessage));
            });
            var invoker = HttpClientCallInvokerFactory.Create(httpClient);

            // Act
            var call    = invoker.AsyncServerStreamingCall <HelloRequest, HelloReply>(ClientTestHelpers.ServiceMethod, string.Empty, new CallOptions(), new HelloRequest());
            var headers = await call.ResponseHeadersAsync;
            await call.ResponseStream.MoveNext(CancellationToken.None);

            // Assert
            Assert.NotNull(responseMessage);
            Assert.IsFalse(responseMessage !.TrailingHeaders.Any()); // sanity check that there are no trailers

            Assert.AreEqual(StatusCode.OK, call.GetStatus().StatusCode);
            Assert.AreEqual("Detail!", call.GetStatus().Detail);

            Assert.AreEqual(0, headers.Count);
            Assert.AreEqual(0, call.GetTrailers().Count);
        }
コード例 #4
0
        public async Task AsyncUnaryCall_SuccessTrailersOnly_ThrowNoMessageError()
        {
            // Arrange
            HttpResponseMessage?responseMessage = null;
            var httpClient = ClientTestHelpers.CreateTestClient(request =>
            {
                responseMessage = ResponseUtils.CreateHeadersOnlyResponse(HttpStatusCode.OK, StatusCode.OK, customHeaders: new Dictionary <string, string> {
                    [GrpcProtocolConstants.MessageTrailer] = "Detail!"
                });
                return(Task.FromResult(responseMessage));
            });
            var invoker = HttpClientCallInvokerFactory.Create(httpClient);

            // Act
            var call    = invoker.AsyncUnaryCall <HelloRequest, HelloReply>(ClientTestHelpers.ServiceMethod, string.Empty, new CallOptions(), new HelloRequest());
            var headers = await call.ResponseHeadersAsync.DefaultTimeout();

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

            // Assert
            Assert.NotNull(responseMessage);
            Assert.IsFalse(responseMessage !.TrailingHeaders.Any()); // sanity check that there are no trailers

            Assert.AreEqual(StatusCode.Internal, ex.Status.StatusCode);
            Assert.AreEqual("Failed to deserialize response message.", ex.Status.Detail);

            Assert.AreEqual(StatusCode.Internal, call.GetStatus().StatusCode);
            Assert.AreEqual("Failed to deserialize response message.", call.GetStatus().Detail);

            Assert.AreEqual(0, headers.Count);
            Assert.AreEqual(0, call.GetTrailers().Count);
        }
コード例 #5
0
        public void OnHeader(object?state, ReadOnlySpan <byte> headerName, ReadOnlySpan <byte> headerValue)
        {
            string headerNameString  = Encoding.ASCII.GetString(headerName);
            string headerValueString = Encoding.ASCII.GetString(headerValue);

            if (state != TrailingHeaders)
            {
                if (!Headers.TryAddWithoutValidation(headerNameString, headerValueString))
                {
                    Content.Headers.TryAddWithoutValidation(headerNameString, headerValueString);
                }
            }
            else
            {
                TrailingHeaders.TryAddWithoutValidation(headerNameString, headerValueString);
            }
        }