1 #region Copyright notice and license 2 3 // Copyright 2015 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; 20 using System.Threading; 21 using System.Threading.Tasks; 22 23 namespace Grpc.Core.Testing 24 { 25 /// <summary> 26 /// Creates test doubles for <c>ServerCallContext</c>. 27 /// </summary> 28 public static class TestServerCallContext 29 { 30 /// <summary> 31 /// Creates a test double for <c>ServerCallContext</c>. Only for testing. 32 /// Note: experimental API that can change or be removed without any prior notice. 33 /// </summary> Create(string method, string host, DateTime deadline, Metadata requestHeaders, CancellationToken cancellationToken, string peer, AuthContext authContext, ContextPropagationToken contextPropagationToken, Func<Metadata, Task> writeHeadersFunc, Func<WriteOptions> writeOptionsGetter, Action<WriteOptions> writeOptionsSetter)34 public static ServerCallContext Create(string method, string host, DateTime deadline, Metadata requestHeaders, CancellationToken cancellationToken, 35 string peer, AuthContext authContext, ContextPropagationToken contextPropagationToken, 36 Func<Metadata, Task> writeHeadersFunc, Func<WriteOptions> writeOptionsGetter, Action<WriteOptions> writeOptionsSetter) 37 { 38 return new TestingServerCallContext(method, host, deadline, requestHeaders, cancellationToken, peer, 39 authContext, contextPropagationToken, writeHeadersFunc, writeOptionsGetter, writeOptionsSetter); 40 } 41 42 private class TestingServerCallContext : ServerCallContext 43 { 44 private readonly string method; 45 private readonly string host; 46 private readonly DateTime deadline; 47 private readonly Metadata requestHeaders; 48 private readonly CancellationToken cancellationToken; 49 private readonly Metadata responseTrailers = new Metadata(); 50 private Status status; 51 private readonly string peer; 52 private readonly AuthContext authContext; 53 private readonly ContextPropagationToken contextPropagationToken; 54 private readonly Func<Metadata, Task> writeHeadersFunc; 55 private readonly Func<WriteOptions> writeOptionsGetter; 56 private readonly Action<WriteOptions> writeOptionsSetter; 57 TestingServerCallContext(string method, string host, DateTime deadline, Metadata requestHeaders, CancellationToken cancellationToken, string peer, AuthContext authContext, ContextPropagationToken contextPropagationToken, Func<Metadata, Task> writeHeadersFunc, Func<WriteOptions> writeOptionsGetter, Action<WriteOptions> writeOptionsSetter)58 public TestingServerCallContext(string method, string host, DateTime deadline, Metadata requestHeaders, CancellationToken cancellationToken, 59 string peer, AuthContext authContext, ContextPropagationToken contextPropagationToken, 60 Func<Metadata, Task> writeHeadersFunc, Func<WriteOptions> writeOptionsGetter, Action<WriteOptions> writeOptionsSetter) 61 { 62 this.method = method; 63 this.host = host; 64 this.deadline = deadline; 65 this.requestHeaders = requestHeaders; 66 this.cancellationToken = cancellationToken; 67 this.responseTrailers = new Metadata(); 68 this.status = Status.DefaultSuccess; 69 this.peer = peer; 70 this.authContext = authContext; 71 this.contextPropagationToken = contextPropagationToken; 72 this.writeHeadersFunc = writeHeadersFunc; 73 this.writeOptionsGetter = writeOptionsGetter; 74 this.writeOptionsSetter = writeOptionsSetter; 75 } 76 77 protected override string MethodCore => method; 78 79 protected override string HostCore => host; 80 81 protected override string PeerCore => peer; 82 83 protected override DateTime DeadlineCore => deadline; 84 85 protected override Metadata RequestHeadersCore => requestHeaders; 86 87 protected override CancellationToken CancellationTokenCore => cancellationToken; 88 89 protected override Metadata ResponseTrailersCore => responseTrailers; 90 91 protected override Status StatusCore { get => status; set => status = value; } 92 protected override WriteOptions WriteOptionsCore { get => writeOptionsGetter(); set => writeOptionsSetter(value); } 93 94 protected override AuthContext AuthContextCore => authContext; 95 CreatePropagationTokenCore(ContextPropagationOptions options)96 protected override ContextPropagationToken CreatePropagationTokenCore(ContextPropagationOptions options) 97 { 98 return contextPropagationToken; 99 } 100 WriteResponseHeadersAsyncCore(Metadata responseHeaders)101 protected override Task WriteResponseHeadersAsyncCore(Metadata responseHeaders) 102 { 103 return writeHeadersFunc(responseHeaders); 104 } 105 } 106 } 107 } 108