Skip to content
Snippets Groups Projects
Commit 3ac49002 authored by Jan Tattermusch's avatar Jan Tattermusch
Browse files

improve MetadataCredentials test

parent d63b1d62
No related branches found
No related tags found
No related merge requests found
...@@ -41,6 +41,9 @@ ...@@ -41,6 +41,9 @@
<Reference Include="CommandLine"> <Reference Include="CommandLine">
<HintPath>..\packages\CommandLineParser.1.9.71\lib\net45\CommandLine.dll</HintPath> <HintPath>..\packages\CommandLineParser.1.9.71\lib\net45\CommandLine.dll</HintPath>
</Reference> </Reference>
<Reference Include="Moq">
<HintPath>..\packages\Moq.4.2.1510.2205\lib\net40\Moq.dll</HintPath>
</Reference>
<Reference Include="nunit.framework"> <Reference Include="nunit.framework">
<HintPath>..\packages\NUnit.2.6.4\lib\nunit.framework.dll</HintPath> <HintPath>..\packages\NUnit.2.6.4\lib\nunit.framework.dll</HintPath>
</Reference> </Reference>
......
...@@ -40,6 +40,7 @@ using System.Threading.Tasks; ...@@ -40,6 +40,7 @@ using System.Threading.Tasks;
using Grpc.Core; using Grpc.Core;
using Grpc.Core.Utils; using Grpc.Core.Utils;
using Grpc.Testing; using Grpc.Testing;
using Moq;
using NUnit.Framework; using NUnit.Framework;
namespace Grpc.IntegrationTesting namespace Grpc.IntegrationTesting
...@@ -50,37 +51,37 @@ namespace Grpc.IntegrationTesting ...@@ -50,37 +51,37 @@ namespace Grpc.IntegrationTesting
Server server; Server server;
Channel channel; Channel channel;
TestService.ITestServiceClient client; TestService.ITestServiceClient client;
List<ChannelOption> options;
Mock<TestService.ITestService> serviceMock;
AsyncAuthInterceptor asyncAuthInterceptor;
[TestFixtureSetUp] [SetUp]
public void Init() public void Init()
{ {
var serverCredentials = new SslServerCredentials(new[] { new KeyCertificatePair(File.ReadAllText(TestCredentials.ServerCertChainPath), File.ReadAllText(TestCredentials.ServerPrivateKeyPath)) }); serviceMock = new Mock<TestService.ITestService>();
serviceMock.Setup(m => m.UnaryCall(It.IsAny<SimpleRequest>(), It.IsAny<ServerCallContext>()))
.Returns(new Func<SimpleRequest, ServerCallContext, Task<SimpleResponse>>(UnaryCallHandler));
server = new Server server = new Server
{ {
Services = { TestService.BindService(new TestServiceImpl()) }, Services = { TestService.BindService(serviceMock.Object) },
Ports = { { Host, ServerPort.PickUnused, serverCredentials } } Ports = { { Host, ServerPort.PickUnused, TestCredentials.CreateSslServerCredentials() } }
}; };
server.Start(); server.Start();
var options = new List<ChannelOption> options = new List<ChannelOption>
{ {
new ChannelOption(ChannelOptions.SslTargetNameOverride, TestCredentials.DefaultHostOverride) new ChannelOption(ChannelOptions.SslTargetNameOverride, TestCredentials.DefaultHostOverride)
}; };
var asyncAuthInterceptor = new AsyncAuthInterceptor(async (context, metadata) => asyncAuthInterceptor = new AsyncAuthInterceptor(async (context, metadata) =>
{ {
await Task.Delay(100); // make sure the operation is asynchronous. await Task.Delay(100).ConfigureAwait(false); // make sure the operation is asynchronous.
metadata.Add("authorization", "SECRET_TOKEN"); metadata.Add("authorization", "SECRET_TOKEN");
}); });
var clientCredentials = ChannelCredentials.Create(
new SslCredentials(File.ReadAllText(TestCredentials.ClientCertAuthorityPath)),
CallCredentials.FromInterceptor(asyncAuthInterceptor));
channel = new Channel(Host, server.Ports.Single().BoundPort, clientCredentials, options);
client = TestService.NewClient(channel);
} }
[TestFixtureTearDown] [TearDown]
public void Cleanup() public void Cleanup()
{ {
channel.ShutdownAsync().Wait(); channel.ShutdownAsync().Wait();
...@@ -90,8 +91,29 @@ namespace Grpc.IntegrationTesting ...@@ -90,8 +91,29 @@ namespace Grpc.IntegrationTesting
[Test] [Test]
public void MetadataCredentials() public void MetadataCredentials()
{ {
var response = client.UnaryCall(new SimpleRequest { ResponseSize = 10 }); var channelCredentials = ChannelCredentials.Create(TestCredentials.CreateSslCredentials(),
Assert.AreEqual(10, response.Payload.Body.Length); CallCredentials.FromInterceptor(asyncAuthInterceptor));
channel = new Channel(Host, server.Ports.Single().BoundPort, channelCredentials, options);
client = TestService.NewClient(channel);
client.UnaryCall(new SimpleRequest {});
}
[Test]
public void MetadataCredentials_PerCall()
{
channel = new Channel(Host, server.Ports.Single().BoundPort, TestCredentials.CreateSslCredentials(), options);
client = TestService.NewClient(channel);
var callCredentials = CallCredentials.FromInterceptor(asyncAuthInterceptor);
client.UnaryCall(new SimpleRequest { }, new CallOptions(credentials: callCredentials));
}
private Task<SimpleResponse> UnaryCallHandler(SimpleRequest request, ServerCallContext context)
{
var authToken = context.RequestHeaders.First((entry) => entry.Key == "authorization").Value;
Assert.AreEqual("SECRET_TOKEN", authToken);
return Task.FromResult(new SimpleResponse());
} }
} }
} }
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
<package id="Microsoft.Bcl.Async" version="1.0.168" targetFramework="net45" /> <package id="Microsoft.Bcl.Async" version="1.0.168" targetFramework="net45" />
<package id="Microsoft.Bcl.Build" version="1.0.21" targetFramework="net45" /> <package id="Microsoft.Bcl.Build" version="1.0.21" targetFramework="net45" />
<package id="Microsoft.Net.Http" version="2.2.29" targetFramework="net45" /> <package id="Microsoft.Net.Http" version="2.2.29" targetFramework="net45" />
<package id="Moq" version="4.2.1510.2205" targetFramework="net45" />
<package id="Newtonsoft.Json" version="7.0.1" targetFramework="net45" /> <package id="Newtonsoft.Json" version="7.0.1" targetFramework="net45" />
<package id="NUnit" version="2.6.4" targetFramework="net45" /> <package id="NUnit" version="2.6.4" targetFramework="net45" />
</packages> </packages>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment