Skip to content

Commit bf26e03

Browse files
authored
Add client streaming interceptor test (#1337)
1 parent 6920e2a commit bf26e03

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

test/FunctionalTests/Client/AuthorizationTests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ Task<HelloReply> UnaryTelemetryHeader(HelloRequest request, ServerCallContext co
8484
[Test]
8585
public async Task ClientFactory_CallCredentials_RoundtripToken()
8686
{
87+
// Arrange
8788
string? authorization = null;
8889
Task<HelloReply> UnaryTelemetryHeader(HelloRequest request, ServerCallContext context)
8990
{
@@ -129,10 +130,12 @@ Task<HelloReply> UnaryTelemetryHeader(HelloRequest request, ServerCallContext co
129130

130131
var client = services.GetRequiredService<TestClient<HelloRequest, HelloReply>>();
131132

133+
// Act
132134
var call = client.UnaryCall(new HelloRequest { Name = "world" });
133135

134136
await call.ResponseAsync.DefaultTimeout();
135137

138+
// Assert
136139
Assert.AreEqual("Bearer token!", authorization);
137140
}
138141
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
}

test/FunctionalTests/Grpc.AspNetCore.FunctionalTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<Compile Include="..\Shared\TestHelpers.cs" Link="Infrastructure\TestHelpers.cs" />
2222
<Compile Include="..\Shared\TestResolver.cs" Link="Infrastructure\Balancer\TestResolver.cs" />
2323
<Compile Include="..\Shared\TestResolverFactory.cs" Link="Infrastructure\Balancer\TestResolverFactory.cs" />
24+
<Compile Include="..\Shared\CallbackInterceptor.cs" Link="Infrastructure\CallbackInterceptor.cs" />
2425
</ItemGroup>
2526

2627
<ItemGroup>

0 commit comments

Comments
 (0)