OLD | NEW |
(Empty) | |
| 1 #region Copyright notice and license |
| 2 |
| 3 // Copyright 2015, Google Inc. |
| 4 // All rights reserved. |
| 5 // |
| 6 // Redistribution and use in source and binary forms, with or without |
| 7 // modification, are permitted provided that the following conditions are |
| 8 // met: |
| 9 // |
| 10 // * Redistributions of source code must retain the above copyright |
| 11 // notice, this list of conditions and the following disclaimer. |
| 12 // * Redistributions in binary form must reproduce the above |
| 13 // copyright notice, this list of conditions and the following disclaimer |
| 14 // in the documentation and/or other materials provided with the |
| 15 // distribution. |
| 16 // * Neither the name of Google Inc. nor the names of its |
| 17 // contributors may be used to endorse or promote products derived from |
| 18 // this software without specific prior written permission. |
| 19 // |
| 20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 |
| 32 #endregion |
| 33 |
| 34 using System; |
| 35 using System.Collections.Generic; |
| 36 using System.IO; |
| 37 using System.Linq; |
| 38 using System.Threading; |
| 39 using System.Threading.Tasks; |
| 40 using Grpc.Core; |
| 41 using Grpc.Core.Utils; |
| 42 using Grpc.Testing; |
| 43 using Moq; |
| 44 using NUnit.Framework; |
| 45 |
| 46 namespace Grpc.IntegrationTesting |
| 47 { |
| 48 public class MetadataCredentialsTest |
| 49 { |
| 50 const string Host = "localhost"; |
| 51 Server server; |
| 52 Channel channel; |
| 53 TestService.ITestServiceClient client; |
| 54 List<ChannelOption> options; |
| 55 Mock<TestService.ITestService> serviceMock; |
| 56 AsyncAuthInterceptor asyncAuthInterceptor; |
| 57 |
| 58 [SetUp] |
| 59 public void Init() |
| 60 { |
| 61 serviceMock = new Mock<TestService.ITestService>(); |
| 62 serviceMock.Setup(m => m.UnaryCall(It.IsAny<SimpleRequest>(), It.IsA
ny<ServerCallContext>())) |
| 63 .Returns(new Func<SimpleRequest, ServerCallContext, Task<SimpleR
esponse>>(UnaryCallHandler)); |
| 64 |
| 65 server = new Server |
| 66 { |
| 67 Services = { TestService.BindService(serviceMock.Object) }, |
| 68 Ports = { { Host, ServerPort.PickUnused, TestCredentials.CreateS
slServerCredentials() } } |
| 69 }; |
| 70 server.Start(); |
| 71 |
| 72 options = new List<ChannelOption> |
| 73 { |
| 74 new ChannelOption(ChannelOptions.SslTargetNameOverride, TestCred
entials.DefaultHostOverride) |
| 75 }; |
| 76 |
| 77 asyncAuthInterceptor = new AsyncAuthInterceptor(async (context, meta
data) => |
| 78 { |
| 79 await Task.Delay(100).ConfigureAwait(false); // make sure the o
peration is asynchronous. |
| 80 metadata.Add("authorization", "SECRET_TOKEN"); |
| 81 }); |
| 82 } |
| 83 |
| 84 [TearDown] |
| 85 public void Cleanup() |
| 86 { |
| 87 channel.ShutdownAsync().Wait(); |
| 88 server.ShutdownAsync().Wait(); |
| 89 } |
| 90 |
| 91 [Test] |
| 92 public void MetadataCredentials() |
| 93 { |
| 94 var channelCredentials = ChannelCredentials.Create(TestCredentials.C
reateSslCredentials(), |
| 95 CallCredentials.FromInterceptor(asyncAuthInterceptor)); |
| 96 channel = new Channel(Host, server.Ports.Single().BoundPort, channel
Credentials, options); |
| 97 client = TestService.NewClient(channel); |
| 98 |
| 99 client.UnaryCall(new SimpleRequest {}); |
| 100 } |
| 101 |
| 102 [Test] |
| 103 public void MetadataCredentials_PerCall() |
| 104 { |
| 105 channel = new Channel(Host, server.Ports.Single().BoundPort, TestCre
dentials.CreateSslCredentials(), options); |
| 106 client = TestService.NewClient(channel); |
| 107 |
| 108 var callCredentials = CallCredentials.FromInterceptor(asyncAuthInter
ceptor); |
| 109 client.UnaryCall(new SimpleRequest { }, new CallOptions(credentials:
callCredentials)); |
| 110 } |
| 111 |
| 112 private Task<SimpleResponse> UnaryCallHandler(SimpleRequest request, Ser
verCallContext context) |
| 113 { |
| 114 var authToken = context.RequestHeaders.First((entry) => entry.Key ==
"authorization").Value; |
| 115 Assert.AreEqual("SECRET_TOKEN", authToken); |
| 116 return Task.FromResult(new SimpleResponse()); |
| 117 } |
| 118 } |
| 119 } |
OLD | NEW |