You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestServerCallContext.cs 3.1 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #pragma warning disable IDE0073
  2. // Copyright 2019 The 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. using Grpc.Core;
  16. namespace Microsoft.AutoGen.RuntimeGateway.Grpc.Tests.Helpers.Grpc;
  17. public class TestServerCallContext : ServerCallContext
  18. {
  19. private readonly Metadata _requestHeaders;
  20. private readonly CancellationToken _cancellationToken;
  21. private readonly Metadata _responseTrailers;
  22. private readonly AuthContext _authContext;
  23. private readonly Dictionary<object, object> _userState;
  24. private WriteOptions? _writeOptions;
  25. public Metadata? ResponseHeaders { get; private set; }
  26. private TestServerCallContext(Metadata requestHeaders, CancellationToken cancellationToken)
  27. {
  28. _requestHeaders = requestHeaders;
  29. _cancellationToken = cancellationToken;
  30. _responseTrailers = new Metadata();
  31. _authContext = new AuthContext(string.Empty, new Dictionary<string, List<AuthProperty>>());
  32. _userState = new Dictionary<object, object>();
  33. }
  34. protected override string MethodCore => "MethodName";
  35. protected override string HostCore => "HostName";
  36. protected override string PeerCore => "PeerName";
  37. protected override DateTime DeadlineCore { get; }
  38. protected override Metadata RequestHeadersCore => _requestHeaders;
  39. protected override CancellationToken CancellationTokenCore => _cancellationToken;
  40. protected override Metadata ResponseTrailersCore => _responseTrailers;
  41. protected override Status StatusCore { get; set; }
  42. protected override WriteOptions? WriteOptionsCore { get => _writeOptions; set { _writeOptions = value; } }
  43. protected override AuthContext AuthContextCore => _authContext;
  44. protected override ContextPropagationToken CreatePropagationTokenCore(ContextPropagationOptions? options)
  45. {
  46. throw new NotImplementedException();
  47. }
  48. protected override Task WriteResponseHeadersAsyncCore(Metadata responseHeaders)
  49. {
  50. if (ResponseHeaders != null)
  51. {
  52. throw new InvalidOperationException("Response headers have already been written.");
  53. }
  54. ResponseHeaders = responseHeaders;
  55. return Task.CompletedTask;
  56. }
  57. protected override IDictionary<object, object> UserStateCore => _userState;
  58. public static TestServerCallContext Create(Metadata? requestHeaders = null, CancellationToken cancellationToken = default)
  59. {
  60. requestHeaders ??= new Metadata() { { "client-id", Guid.NewGuid().ToString() } };
  61. return new TestServerCallContext(requestHeaders ?? new Metadata(), cancellationToken);
  62. }
  63. }