TestServerCallContext.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #region Copyright notice and license
  2. // Copyright 2015 gRPC authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #endregion
  16. using System;
  17. using System.Threading;
  18. using System.Threading.Tasks;
  19. using Grpc.Core;
  20. namespace Grpc.Core.Testing
  21. {
  22. /// <summary>
  23. /// Creates test doubles for <c>ServerCallContext</c>.
  24. /// </summary>
  25. public static class TestServerCallContext
  26. {
  27. /// <summary>
  28. /// Creates a test double for <c>ServerCallContext</c>. Only for testing.
  29. /// Note: experimental API that can change or be removed without any prior notice.
  30. /// </summary>
  31. public static ServerCallContext Create(string method, string host, DateTime deadline, Metadata requestHeaders, CancellationToken cancellationToken,
  32. string peer, AuthContext authContext, ContextPropagationToken contextPropagationToken,
  33. Func<Metadata, Task> writeHeadersFunc, Func<WriteOptions> writeOptionsGetter, Action<WriteOptions> writeOptionsSetter)
  34. {
  35. return new ServerCallContext(null, method, host, deadline, requestHeaders, cancellationToken,
  36. writeHeadersFunc, new WriteOptionsHolder(writeOptionsGetter, writeOptionsSetter),
  37. () => peer, () => authContext, () => contextPropagationToken);
  38. }
  39. private class WriteOptionsHolder : IHasWriteOptions
  40. {
  41. Func<WriteOptions> writeOptionsGetter;
  42. Action<WriteOptions> writeOptionsSetter;
  43. public WriteOptionsHolder(Func<WriteOptions> writeOptionsGetter, Action<WriteOptions> writeOptionsSetter)
  44. {
  45. this.writeOptionsGetter = writeOptionsGetter;
  46. this.writeOptionsSetter = writeOptionsSetter;
  47. }
  48. public WriteOptions WriteOptions { get => writeOptionsGetter(); set => writeOptionsSetter(value); }
  49. }
  50. }
  51. }