|
| 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.Net.Http; |
| 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 Microsoft.Extensions.DependencyInjection; |
| 26 | +using Microsoft.Extensions.Logging; |
| 27 | +using NUnit.Framework; |
| 28 | + |
| 29 | +namespace Grpc.AspNetCore.FunctionalTests.Client |
| 30 | +{ |
| 31 | + [TestFixture] |
| 32 | + public class InterceptorTests : FunctionalTestBase |
| 33 | + { |
| 34 | + [Test] |
| 35 | + public async Task ClientStreams_CreateWithClientFactory_InterceptorCalled() |
| 36 | + { |
| 37 | + async Task<HelloReply> ClientStreamingMethod(IAsyncStreamReader<HelloRequest> request, ServerCallContext context) |
| 38 | + { |
| 39 | + while (await request.MoveNext()) |
| 40 | + { |
| 41 | + // Nom |
| 42 | + } |
| 43 | + return new HelloReply(); |
| 44 | + } |
| 45 | + var method = Fixture.DynamicGrpc.AddClientStreamingMethod<HelloRequest, HelloReply>(ClientStreamingMethod); |
| 46 | + |
| 47 | + var invokeCount = 0; |
| 48 | + var serviceCollection = new ServiceCollection(); |
| 49 | + serviceCollection.AddSingleton(new CallbackInterceptor(o => invokeCount++)); |
| 50 | + serviceCollection.AddSingleton<ILoggerFactory>(LoggerFactory); |
| 51 | + serviceCollection |
| 52 | + .AddGrpcClient<TestClient<HelloRequest, HelloReply>>(options => |
| 53 | + { |
| 54 | + options.Address = Fixture.GetUrl(TestServerEndpointName.Http2WithTls); |
| 55 | + }) |
| 56 | + .AddInterceptor<CallbackInterceptor>() |
| 57 | + .ConfigureGrpcClientCreator(invoker => |
| 58 | + { |
| 59 | + return TestClientFactory.Create(invoker, method); |
| 60 | + }) |
| 61 | + .ConfigurePrimaryHttpMessageHandler(() => |
| 62 | + { |
| 63 | + return new HttpClientHandler |
| 64 | + { |
| 65 | + ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator |
| 66 | + }; |
| 67 | + }); |
| 68 | + var services = serviceCollection.BuildServiceProvider(); |
| 69 | + |
| 70 | + var client = services.GetRequiredService<TestClient<HelloRequest, HelloReply>>(); |
| 71 | + |
| 72 | + // Act |
| 73 | + var call = client.ClientStreamingCall(); |
| 74 | + |
| 75 | + // Assert |
| 76 | + Assert.AreEqual(1, invokeCount); |
| 77 | + |
| 78 | + await call.RequestStream.CompleteAsync().DefaultTimeout(); |
| 79 | + await call.ResponseAsync.DefaultTimeout(); |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments