1 #region Copyright notice and license
3 // Copyright 2015 gRPC authors.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
20 using System.Threading;
21 using System.Threading.Tasks;
23 namespace Grpc.Core.Testing
26 /// Creates test doubles for <c>ServerCallContext</c>.
28 public static class TestServerCallContext
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.
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)
38 return new TestingServerCallContext(method, host, deadline, requestHeaders, cancellationToken, peer,
39 authContext, contextPropagationToken, writeHeadersFunc, writeOptionsGetter, writeOptionsSetter);
42 private class TestingServerCallContext : ServerCallContext
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;
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)
64 this.deadline = deadline;
65 this.requestHeaders = requestHeaders;
66 this.cancellationToken = cancellationToken;
67 this.responseTrailers = new Metadata();
68 this.status = Status.DefaultSuccess;
70 this.authContext = authContext;
71 this.contextPropagationToken = contextPropagationToken;
72 this.writeHeadersFunc = writeHeadersFunc;
73 this.writeOptionsGetter = writeOptionsGetter;
74 this.writeOptionsSetter = writeOptionsSetter;
77 protected override string MethodCore => method;
79 protected override string HostCore => host;
81 protected override string PeerCore => peer;
83 protected override DateTime DeadlineCore => deadline;
85 protected override Metadata RequestHeadersCore => requestHeaders;
87 protected override CancellationToken CancellationTokenCore => cancellationToken;
89 protected override Metadata ResponseTrailersCore => responseTrailers;
91 protected override Status StatusCore { get => status; set => status = value; }
92 protected override WriteOptions WriteOptionsCore { get => writeOptionsGetter(); set => writeOptionsSetter(value); }
94 protected override AuthContext AuthContextCore => authContext;
96 protected override ContextPropagationToken CreatePropagationTokenCore(ContextPropagationOptions options)
98 return contextPropagationToken;
101 protected override Task WriteResponseHeadersAsyncCore(Metadata responseHeaders)
103 return writeHeadersFunc(responseHeaders);