OptionsSnapshotMock.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. using System.Security.Claims;
  2. using Hotline.Configurations;
  3. using Microsoft.AspNetCore.Http;
  4. using Microsoft.Extensions.Configuration;
  5. using Microsoft.Extensions.Options;
  6. namespace Hotline.Tests.Mock;
  7. public class OptionsSnapshotMock : IOptionsSnapshot<AppConfiguration>
  8. {
  9. private readonly IHttpContextAccessor _contextAccessor;
  10. private readonly IConfiguration _configuration;
  11. public OptionsSnapshotMock(IHttpContextAccessor contextAccessor, IConfiguration configuration)
  12. {
  13. _contextAccessor = contextAccessor;
  14. _configuration = configuration;
  15. }
  16. public AppConfiguration Value
  17. {
  18. get
  19. {
  20. var appConfiguration = _configuration.GetRequiredSection(nameof(AppConfiguration)).Get<AppConfiguration>();
  21. if (_contextAccessor != null && _contextAccessor.HttpContext != null)
  22. appConfiguration.AppScope = _contextAccessor.HttpContext.User.FindFirstValue("AppScope");
  23. return appConfiguration;
  24. }
  25. }
  26. public AppConfiguration Get(string? name)
  27. {
  28. throw new NotImplementedException();
  29. }
  30. }