PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Nancy.Tests/Unit/Diagnostics/CustomInteractiveDiagnosticsFixture.cs

http://github.com/NancyFx/Nancy
C# | 158 lines | 136 code | 19 blank | 3 comment | 0 complexity | 6d02bfde186af7f262d0233790bf153a MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-3.0, Apache-2.0, MPL-2.0-no-copyleft-exception, LGPL-2.0, BSD-3-Clause
  1. namespace Nancy.Tests.Unit.Diagnostics
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using Nancy.Bootstrapper;
  7. using Nancy.Cookies;
  8. using Nancy.Cryptography;
  9. using Nancy.Culture;
  10. using Nancy.Diagnostics;
  11. using Nancy.Localization;
  12. using Nancy.ModelBinding;
  13. using Nancy.Responses.Negotiation;
  14. using Nancy.Routing;
  15. using Nancy.Routing.Constraints;
  16. using Nancy.Testing;
  17. using Nancy.Tests; //While this directive is redundant, it's required to build on mono 2.x to allow it to resolve the Should* extension methods
  18. using Xunit;
  19. public class CustomInteractiveDiagnosticsHookFixture
  20. {
  21. private const string DiagsCookieName = "__ncd";
  22. private readonly CryptographyConfiguration cryptoConfig;
  23. private readonly IObjectSerializer objectSerializer;
  24. public CustomInteractiveDiagnosticsHookFixture()
  25. {
  26. this.cryptoConfig = CryptographyConfiguration.Default;
  27. this.objectSerializer = new DefaultObjectSerializer();
  28. }
  29. private class FakeDiagnostics : IDiagnostics
  30. {
  31. private readonly DiagnosticsConfiguration diagnosticsConfiguration;
  32. private readonly IEnumerable<IDiagnosticsProvider> diagnosticProviders;
  33. private readonly IRootPathProvider rootPathProvider;
  34. private readonly IRequestTracing requestTracing;
  35. private readonly NancyInternalConfiguration configuration;
  36. private readonly IModelBinderLocator modelBinderLocator;
  37. private readonly IEnumerable<IResponseProcessor> responseProcessors;
  38. private readonly IEnumerable<IRouteSegmentConstraint> routeSegmentConstraints;
  39. private readonly ICultureService cultureService;
  40. private readonly IRequestTraceFactory requestTraceFactory;
  41. private readonly IEnumerable<IRouteMetadataProvider> routeMetadataProviders;
  42. private readonly ITextResource textResource;
  43. public FakeDiagnostics(
  44. DiagnosticsConfiguration diagnosticsConfiguration,
  45. IRootPathProvider rootPathProvider,
  46. IRequestTracing requestTracing,
  47. NancyInternalConfiguration configuration,
  48. IModelBinderLocator modelBinderLocator,
  49. IEnumerable<IResponseProcessor> responseProcessors,
  50. IEnumerable<IRouteSegmentConstraint> routeSegmentConstraints,
  51. ICultureService cultureService,
  52. IRequestTraceFactory requestTraceFactory,
  53. IEnumerable<IRouteMetadataProvider> routeMetadataProviders,
  54. ITextResource textResource)
  55. {
  56. this.diagnosticsConfiguration = diagnosticsConfiguration;
  57. this.diagnosticProviders = (new IDiagnosticsProvider[] { new FakeDiagnosticsProvider() }).ToArray();
  58. this.rootPathProvider = rootPathProvider;
  59. this.requestTracing = requestTracing;
  60. this.configuration = configuration;
  61. this.modelBinderLocator = modelBinderLocator;
  62. this.responseProcessors = responseProcessors;
  63. this.routeSegmentConstraints = routeSegmentConstraints;
  64. this.cultureService = cultureService;
  65. this.requestTraceFactory = requestTraceFactory;
  66. this.routeMetadataProviders = routeMetadataProviders;
  67. this.textResource = textResource;
  68. }
  69. public void Initialize(IPipelines pipelines)
  70. {
  71. DiagnosticsHook.Enable(this.diagnosticsConfiguration,
  72. pipelines,
  73. this.diagnosticProviders,
  74. this.rootPathProvider,
  75. this.requestTracing,
  76. this.configuration,
  77. this.modelBinderLocator,
  78. this.responseProcessors,
  79. this.routeSegmentConstraints,
  80. this.cultureService,
  81. this.requestTraceFactory,
  82. this.routeMetadataProviders,
  83. this.textResource);
  84. }
  85. }
  86. private class FakeDiagnosticsProvider : IDiagnosticsProvider
  87. {
  88. public string Name
  89. {
  90. get { return "Fake testing provider"; }
  91. }
  92. public string Description
  93. {
  94. get { return "Fake testing provider"; }
  95. }
  96. public object DiagnosticObject
  97. {
  98. get { return this; }
  99. }
  100. }
  101. [Fact]
  102. public void Should_return_main_page_with_valid_auth_cookie()
  103. {
  104. // Given
  105. var diagsConfig = new DiagnosticsConfiguration { Password = "password", CryptographyConfiguration = this.cryptoConfig };
  106. var bootstrapper = new ConfigurableBootstrapper(with =>
  107. {
  108. with.EnableAutoRegistration();
  109. with.DiagnosticsConfiguration(diagsConfig);
  110. with.Diagnostics<FakeDiagnostics>();
  111. });
  112. var browser = new Browser(bootstrapper);
  113. // When
  114. var result = browser.Get(diagsConfig.Path + "/interactive/providers/", with =>
  115. {
  116. with.Cookie(DiagsCookieName, this.GetSessionCookieValue("password"));
  117. });
  118. // Then should see our fake provider and not the default testing provider
  119. result.Body.AsString().ShouldContain("Fake testing provider");
  120. result.Body.AsString().ShouldNotContain("Testing Diagnostic Provider");
  121. }
  122. private string GetSessionCookieValue(string password, DateTime? expiry = null)
  123. {
  124. var salt = DiagnosticsSession.GenerateRandomSalt();
  125. var hash = DiagnosticsSession.GenerateSaltedHash(password, salt);
  126. var session = new DiagnosticsSession
  127. {
  128. Hash = hash,
  129. Salt = salt,
  130. Expiry = expiry.HasValue ? expiry.Value : DateTime.Now.AddMinutes(15),
  131. };
  132. var serializedSession = this.objectSerializer.Serialize(session);
  133. var encryptedSession = this.cryptoConfig.EncryptionProvider.Encrypt(serializedSession);
  134. var hmacBytes = this.cryptoConfig.HmacProvider.GenerateHmac(encryptedSession);
  135. var hmacString = Convert.ToBase64String(hmacBytes);
  136. return String.Format("{1}{0}", encryptedSession, hmacString);
  137. }
  138. }
  139. }