public void WhenThereIsNoNext_DoesNotCrash()
            {
                var         sut = new OwinRequestScopeContextMiddleware(null, _options);
                Func <Task> act = () => sut.Invoke(_owinEnvironment);

                act.Should().NotThrow();
            }
 public virtual void SetUp()
 {
     _next    = owinEnvironment => Task.CompletedTask;
     _options = new OwinRequestScopeContextOptions {
         ItemKeyEqualityComparer = StringComparer.OrdinalIgnoreCase
     };
     _sut = new OwinRequestScopeContextMiddleware(_next, _options);
 }
            public void WhenThereIsAnOwinRequestScopeContextAlready_ThrowsInvalidOperationException()
            {
                _sut = new OwinRequestScopeContextMiddleware(
                    async owinEnvironment => { await new OwinRequestScopeContextMiddleware(null, _options).Invoke(owinEnvironment).ConfigureAwait(false); }, _options);
                Func <Task> act = () => _sut.Invoke(_owinEnvironment);

                act.Should().Throw <InvalidOperationException>();
            }
            public async Task FreesContextSlotAfterwards()
            {
                _sut = new OwinRequestScopeContextMiddleware(owinEnvironment => {
                    OwinRequestScopeContext.Current.Should().NotBeNull();
                    return(Task.CompletedTask);
                }, _options);
                await _sut.Invoke(_owinEnvironment).ConfigureAwait(false);

                OwinRequestScopeContext.Current.Should().BeNull();
            }
 private static MidFunc UseRequestScopeContext(OwinRequestScopeContextOptions options = null)
 {
     options = options ?? OwinRequestScopeContextOptions.Default;
     return
         (next =>
          async environment => {
         var middleware = new OwinRequestScopeContextMiddleware(next, options);
         await middleware.Invoke(environment);
     });
 }
            public async Task InvokesNext()
            {
                var nextHasBeenInvoked = false;

                _sut = new OwinRequestScopeContextMiddleware(owinEnvironment => {
                    nextHasBeenInvoked = true;
                    return(Task.CompletedTask);
                }, _options);
                await _sut.Invoke(_owinEnvironment).ConfigureAwait(false);

                nextHasBeenInvoked.Should().BeTrue();
            }
            public async Task DisposesContextAtEnd()
            {
                var disposable = A.Fake <IDisposable>();

                _sut = new OwinRequestScopeContextMiddleware(owinEnvironment => {
                    OwinRequestScopeContext.Current.RegisterForDisposal(disposable);
                    return(Task.CompletedTask);
                }, _options);
                await _sut.Invoke(_owinEnvironment).ConfigureAwait(false);

                A.CallTo(() => disposable.Dispose()).MustHaveHappened();
            }
            public async Task SetsNewOwinRequestScopeContext()
            {
                IReadOnlyDictionary <string, object> interceptedOwinEnvironmentInNext = null;

                var sut = new OwinRequestScopeContextMiddleware(owinEnvironment => {
                    interceptedOwinEnvironmentInNext = OwinRequestScopeContext.Current.OwinEnvironment;
                    OwinRequestScopeContext.Current.Should().NotBeNull();
                    return(Task.CompletedTask);
                }, _options);
                await sut.Invoke(_owinEnvironment).ConfigureAwait(false);

                interceptedOwinEnvironmentInNext.Should().BeEquivalentTo(_owinEnvironment);
            }
            public void DisposesContextAtEnd_EvenWhenPipelineFailed()
            {
                var disposable        = A.Fake <IDisposable>();
                var failureInPipeline = new InvalidOperationException("Failure for unit tests");

                _sut = new OwinRequestScopeContextMiddleware(owinEnvironment => {
                    OwinRequestScopeContext.Current.RegisterForDisposal(disposable);
                    throw failureInPipeline;
                }, _options);
                Func <Task> act = () => _sut.Invoke(_owinEnvironment);

                act.ShouldThrow <InvalidOperationException>().Where(_ => _.Equals(failureInPipeline));
                A.CallTo(() => disposable.Dispose()).MustHaveHappened();
            }
            public async Task GivenOptions_InitializesContextWithGivenOptions()
            {
                OwinRequestScopeContextOptions interceptedOptions     = null;
                IEqualityComparer <string>     interceptedKeyComparer = null;
                var sut = new OwinRequestScopeContextMiddleware(owinEnvironment => {
                    interceptedOptions     = ((OwinRequestScopeContext)OwinRequestScopeContext.Current).Options;
                    interceptedKeyComparer = ((OwinRequestScopeContextItems)OwinRequestScopeContext.Current.Items).KeyComparer;
                    return(Task.CompletedTask);
                }, _options);
                await sut.Invoke(_owinEnvironment).ConfigureAwait(false);

                interceptedOptions.Should().NotBeNull();
                interceptedOptions.ItemKeyEqualityComparer.Should().Be(_options.ItemKeyEqualityComparer);
                interceptedKeyComparer.Should().NotBeNull();
                interceptedKeyComparer.Should().Be(_options.ItemKeyEqualityComparer);
            }