public async Task MetadataCredentials_ComposedPerCall() { serviceImpl.StreamingOutputCallHandler = async(req, responseStream, context) => { var firstAuth = context.RequestHeaders.Last((entry) => entry.Key == "first_authorization").Value; Assert.AreEqual("FIRST_SECRET_TOKEN", firstAuth); var secondAuth = context.RequestHeaders.First((entry) => entry.Key == "second_authorization").Value; Assert.AreEqual("SECOND_SECRET_TOKEN", secondAuth); await responseStream.WriteAsync(new StreamingOutputCallResponse()); }; channel = new Channel(Host, server.Ports.Single().BoundPort, TestCredentials.CreateSslCredentials(), options); var client = new TestService.TestServiceClient(channel); var first = CallCredentials.FromInterceptor(new AsyncAuthInterceptor((context, metadata) => { metadata.Add("first_authorization", "FIRST_SECRET_TOKEN"); return(TaskUtils.CompletedTask); })); var second = CallCredentials.FromInterceptor(new AsyncAuthInterceptor((context, metadata) => { metadata.Add("second_authorization", "SECOND_SECRET_TOKEN"); return(TaskUtils.CompletedTask); })); var call = client.StreamingOutputCall(new StreamingOutputCallRequest { }, new CallOptions(credentials: CallCredentials.Compose(first, second))); Assert.IsTrue(await call.ResponseStream.MoveNext()); Assert.IsFalse(await call.ResponseStream.MoveNext()); }
public async Task MetadataCredentials_Composed() { var first = CallCredentials.FromInterceptor(new AsyncAuthInterceptor((context, metadata) => { // Attempt to exercise the case where async callback is inlineable/synchronously-runnable. metadata.Add("first_authorization", "FIRST_SECRET_TOKEN"); return(TaskUtils.CompletedTask); })); var second = CallCredentials.FromInterceptor(new AsyncAuthInterceptor((context, metadata) => { metadata.Add("second_authorization", "SECOND_SECRET_TOKEN"); return(TaskUtils.CompletedTask); })); var third = CallCredentials.FromInterceptor(new AsyncAuthInterceptor((context, metadata) => { metadata.Add("third_authorization", "THIRD_SECRET_TOKEN"); return(TaskUtils.CompletedTask); })); var channelCredentials = ChannelCredentials.Create(TestCredentials.CreateSslCredentials(), CallCredentials.Compose(first, second, third)); channel = new Channel(Host, server.Ports.Single().BoundPort, channelCredentials, options); var client = new TestService.TestServiceClient(channel); var call = client.StreamingOutputCall(new StreamingOutputCallRequest { }); Assert.IsTrue(await call.ResponseStream.MoveNext()); Assert.IsFalse(await call.ResponseStream.MoveNext()); }
private static CallOptions ResolveCallOptionsCredentials(CallOptions callOptions, CallCredentials credentials) { if (callOptions.Credentials != null) { credentials = CallCredentials.Compose(callOptions.Credentials, credentials); } return(callOptions.WithCredentials(credentials)); }
public void CallCredentials_ToNativeCredentials() { var composite = CallCredentials.Compose( CallCredentials.FromInterceptor(async(uri, m) => { await Task.Delay(1); }), CallCredentials.FromInterceptor(async(uri, m) => { await Task.Delay(2); })); using (var nativeComposite = composite.ToNativeCredentials()) { } }
public void CallCredentials_ToNativeCredentials() { var composite = CallCredentials.Compose( new MetadataCredentials(async(uri, m) => { await Task.Delay(1); }), new MetadataCredentials(async(uri, m) => { await Task.Delay(2); })); using (var nativeComposite = composite.ToNativeCredentials()) { } }
public async Task CompositeCallCredentialsWithHttps_MetadataOnRequest() { // Arrange HttpRequestHeaders?requestHeaders = null; var httpClient = ClientTestHelpers.CreateTestClient(async request => { requestHeaders = request.Headers; var reply = new HelloReply { Message = "Hello world" }; var streamContent = await ClientTestHelpers.CreateResponseContent(reply).DefaultTimeout(); return(ResponseUtils.CreateResponse(HttpStatusCode.OK, streamContent)); }); var invoker = HttpClientCallInvokerFactory.Create(httpClient); var first = CallCredentials.FromInterceptor(new AsyncAuthInterceptor((context, metadata) => { metadata.Add("first_authorization", "FIRST_SECRET_TOKEN"); return(Task.CompletedTask); })); var second = CallCredentials.FromInterceptor(new AsyncAuthInterceptor((context, metadata) => { metadata.Add("second_authorization", "SECOND_SECRET_TOKEN"); return(Task.CompletedTask); })); var third = CallCredentials.FromInterceptor(new AsyncAuthInterceptor((context, metadata) => { metadata.Add("third_authorization", "THIRD_SECRET_TOKEN"); return(Task.CompletedTask); })); // Act var callCredentials = CallCredentials.Compose(first, second, third); var call = invoker.AsyncUnaryCall <HelloRequest, HelloReply>(ClientTestHelpers.ServiceMethod, string.Empty, new CallOptions(credentials: callCredentials), new HelloRequest()); await call.ResponseAsync.DefaultTimeout(); // Assert Assert.AreEqual("FIRST_SECRET_TOKEN", requestHeaders !.GetValues("first_authorization").Single()); Assert.AreEqual("SECOND_SECRET_TOKEN", requestHeaders !.GetValues("second_authorization").Single()); Assert.AreEqual("THIRD_SECRET_TOKEN", requestHeaders !.GetValues("third_authorization").Single()); }
public async Task MetadataCredentials_ComposedPerCall() { channel = new Channel(Host, server.Ports.Single().BoundPort, TestCredentials.CreateSslCredentials(), options); var client = new TestService.TestServiceClient(channel); var first = CallCredentials.FromInterceptor(new AsyncAuthInterceptor((context, metadata) => { metadata.Add("first_authorization", "FIRST_SECRET_TOKEN"); return(TaskUtils.CompletedTask); })); var second = CallCredentials.FromInterceptor(new AsyncAuthInterceptor((context, metadata) => { metadata.Add("second_authorization", "SECOND_SECRET_TOKEN"); return(TaskUtils.CompletedTask); })); var third = CallCredentials.FromInterceptor(new AsyncAuthInterceptor((context, metadata) => { metadata.Add("third_authorization", "THIRD_SECRET_TOKEN"); return(TaskUtils.CompletedTask); })); var call = client.StreamingOutputCall(new StreamingOutputCallRequest { }, new CallOptions(credentials: CallCredentials.Compose(first, second, third))); Assert.IsTrue(await call.ResponseStream.MoveNext()); Assert.IsFalse(await call.ResponseStream.MoveNext()); }
public void CallCredentials_ComposeAtLeastTwo() { Assert.Throws(typeof(ArgumentException), () => CallCredentials.Compose(new FakeCallCredentials())); }
/// <summary> /// 创建gRPC信道 /// </summary> /// <param name="endpoint">终结点配置</param> /// <returns>gRPC信道</returns> private static GrpcChannel CreateGrpcChannel(EndpointElement endpoint) { //读取配置文件 EndpointConfigurationElement endpointConfiguration = null; IList <AuthInterceptorElement> authInterceptorElements = new List <AuthInterceptorElement>(); if (!string.IsNullOrWhiteSpace(endpoint.EndpointConfiguration)) { endpointConfiguration = GrpcSetting.EndpointConfigurations[endpoint.EndpointConfiguration]; } if (!string.IsNullOrWhiteSpace(endpoint.AuthInterceptors)) { string[] authInterceptorNames = endpoint.AuthInterceptors.Split(','); authInterceptorElements = GrpcSetting.AuthInterceptors.Where <KeyValuePair <string, AuthInterceptorElement> >(x => authInterceptorNames.Contains(x.Key)).Select(x => x.Value).ToList(); } //构造身份凭据 IList <CallCredentials> callCredentials = new List <CallCredentials>(); foreach (AuthInterceptorElement authInterceptorElement in authInterceptorElements) { Assembly assembly = Assembly.Load(authInterceptorElement.Assembly); Type type = assembly.GetType(authInterceptorElement.Type); IAuthInterceptor authInterceptor = (IAuthInterceptor)Activator.CreateInstance(type); CallCredentials callCredential = CallCredentials.FromInterceptor(authInterceptor.AuthIntercept); callCredentials.Add(callCredential); } CallCredentials credentials = null; if (callCredentials.Count == 1) { credentials = callCredentials.Single(); } if (callCredentials.Count > 1) { credentials = CallCredentials.Compose(callCredentials.ToArray()); } //构造gRPC信道选项 GrpcChannelOptions channelOptions = new GrpcChannelOptions { MaxSendMessageSize = endpointConfiguration?.MaxSendMessageSize, MaxReceiveMessageSize = endpointConfiguration?.MaxReceiveMessageSize, MaxRetryAttempts = endpointConfiguration?.MaxRetryAttempts, MaxRetryBufferSize = endpointConfiguration?.MaxRetryBufferSize, MaxRetryBufferPerCallSize = endpointConfiguration?.MaxRetryBufferPerCallSize, DisposeHttpClient = endpointConfiguration?.DisposeHttpClient ?? false, ThrowOperationCanceledOnCancellation = endpointConfiguration?.ThrowOperationCanceledOnCancellation ?? false }; if (credentials != null) { channelOptions.Credentials = ChannelCredentials.Create(new SslCredentials(), credentials); } //创建gRPC信道 GrpcChannel channel = GrpcChannel.ForAddress(endpoint.Address, channelOptions); return(channel); }