|
| 1 | +#region Copyright notice and license |
| 2 | + |
| 3 | +// Copyright 2019 The gRPC Authors |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | + |
| 17 | +#endregion |
| 18 | + |
| 19 | +using System.Linq; |
| 20 | +using System.Threading.Tasks; |
| 21 | +using Greet; |
| 22 | +using Grpc.AspNetCore.FunctionalTests.Infrastructure; |
| 23 | +using Grpc.Core; |
| 24 | +using Grpc.Tests.Shared; |
| 25 | +using NUnit.Framework; |
| 26 | + |
| 27 | +namespace Grpc.AspNetCore.FunctionalTests.Server |
| 28 | +{ |
| 29 | + [TestFixture] |
| 30 | + public class TrailerMetadataTests : FunctionalTestBase |
| 31 | + { |
| 32 | + [Test] |
| 33 | + public async Task GetTrailers_UnaryMethodSetStatusWithTrailers_TrailersAvailableInClient() |
| 34 | + { |
| 35 | + Task<HelloReply> UnaryDeadlineExceeded(HelloRequest request, ServerCallContext context) |
| 36 | + { |
| 37 | + context.ResponseTrailers.Add(new Metadata.Entry("Name", "the value was empty")); |
| 38 | + context.Status = new Status(StatusCode.InvalidArgument, "Validation failed"); |
| 39 | + return Task.FromResult(new HelloReply()); |
| 40 | + } |
| 41 | + |
| 42 | + // Arrange |
| 43 | + SetExpectedErrorsFilter(writeContext => |
| 44 | + { |
| 45 | + if (writeContext.LoggerName == "Grpc.Net.Client.Internal.GrpcCall" && |
| 46 | + writeContext.EventId.Name == "ErrorReadingMessage" && |
| 47 | + writeContext.Message == "Error reading message.") |
| 48 | + { |
| 49 | + return true; |
| 50 | + } |
| 51 | + |
| 52 | + if (writeContext.LoggerName == "Grpc.Net.Client.Internal.GrpcCall" && |
| 53 | + writeContext.EventId.Name == "GrpcStatusError" && |
| 54 | + writeContext.Message == "Call failed with gRPC error status. Status code: 'InvalidArgument', Message: 'Validation failed'.") |
| 55 | + { |
| 56 | + return true; |
| 57 | + } |
| 58 | + |
| 59 | + return false; |
| 60 | + }); |
| 61 | + |
| 62 | + var method = Fixture.DynamicGrpc.AddUnaryMethod<HelloRequest, HelloReply>(UnaryDeadlineExceeded); |
| 63 | + |
| 64 | + var channel = CreateChannel(); |
| 65 | + |
| 66 | + var client = TestClientFactory.Create(channel, method); |
| 67 | + |
| 68 | + // Act |
| 69 | + var call = client.UnaryCall(new HelloRequest()); |
| 70 | + |
| 71 | + var ex = await ExceptionAssert.ThrowsAsync<RpcException>(() => call.ResponseAsync).DefaultTimeout(); |
| 72 | + |
| 73 | + // Assert |
| 74 | + var trailers = call.GetTrailers(); |
| 75 | + Assert.AreEqual(1, trailers.Count); |
| 76 | + Assert.AreEqual("the value was empty", trailers.Single(m => m.Key == "name").Value); |
| 77 | + |
| 78 | + Assert.AreEqual(StatusCode.InvalidArgument, ex.StatusCode); |
| 79 | + Assert.AreEqual("Validation failed", ex.Status.Detail); |
| 80 | + Assert.AreEqual(1, ex.Trailers.Count); |
| 81 | + Assert.AreEqual("the value was empty", ex.Trailers.Single(m => m.Key == "name").Value); |
| 82 | + } |
| 83 | + |
| 84 | + [Test] |
| 85 | + public async Task GetTrailers_UnaryMethodThrowsExceptionWithTrailers_TrailersAvailableInClient() |
| 86 | + { |
| 87 | + Task<HelloReply> UnaryDeadlineExceeded(HelloRequest request, ServerCallContext context) |
| 88 | + { |
| 89 | + var trailers = new Metadata(); |
| 90 | + trailers.Add(new Metadata.Entry("Name", "the value was empty")); |
| 91 | + return Task.FromException<HelloReply>(new RpcException(new Status(StatusCode.InvalidArgument, "Validation failed"), trailers)); |
| 92 | + } |
| 93 | + |
| 94 | + // Arrange |
| 95 | + SetExpectedErrorsFilter(writeContext => |
| 96 | + { |
| 97 | + if (writeContext.LoggerName == "Grpc.Net.Client.Internal.GrpcCall" && |
| 98 | + writeContext.EventId.Name == "ErrorReadingMessage" && |
| 99 | + writeContext.Message == "Error reading message.") |
| 100 | + { |
| 101 | + return true; |
| 102 | + } |
| 103 | + |
| 104 | + if (writeContext.LoggerName == "Grpc.Net.Client.Internal.GrpcCall" && |
| 105 | + writeContext.EventId.Name == "GrpcStatusError" && |
| 106 | + writeContext.Message == "Call failed with gRPC error status. Status code: 'InvalidArgument', Message: 'Validation failed'.") |
| 107 | + { |
| 108 | + return true; |
| 109 | + } |
| 110 | + |
| 111 | + if (writeContext.LoggerName == "SERVER Grpc.AspNetCore.Server.ServerCallHandler" && |
| 112 | + writeContext.EventId.Name == "RpcConnectionError" && |
| 113 | + writeContext.Message == "Error status code 'InvalidArgument' raised.") |
| 114 | + { |
| 115 | + return true; |
| 116 | + } |
| 117 | + |
| 118 | + return false; |
| 119 | + }); |
| 120 | + |
| 121 | + var method = Fixture.DynamicGrpc.AddUnaryMethod<HelloRequest, HelloReply>(UnaryDeadlineExceeded); |
| 122 | + |
| 123 | + var channel = CreateChannel(); |
| 124 | + |
| 125 | + var client = TestClientFactory.Create(channel, method); |
| 126 | + |
| 127 | + // Act |
| 128 | + var call = client.UnaryCall(new HelloRequest()); |
| 129 | + |
| 130 | + var ex = await ExceptionAssert.ThrowsAsync<RpcException>(() => call.ResponseAsync).DefaultTimeout(); |
| 131 | + |
| 132 | + // Assert |
| 133 | + var trailers = call.GetTrailers(); |
| 134 | + Assert.GreaterOrEqual(trailers.Count, 1); |
| 135 | + Assert.AreEqual("the value was empty", trailers.Single(m => m.Key == "name").Value); |
| 136 | + |
| 137 | + Assert.AreEqual(StatusCode.InvalidArgument, ex.StatusCode); |
| 138 | + Assert.AreEqual("Validation failed", ex.Status.Detail); |
| 139 | + Assert.GreaterOrEqual(ex.Trailers.Count, 1); |
| 140 | + Assert.AreEqual("the value was empty", ex.Trailers.Single(m => m.Key == "name").Value); |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments