public async Task SessionIsWrittenToStorageAndCookie()
        {
            var session = CreateSession(new Existing(SessionId));

            var lifecycleHandlerBuilder = new SessionLifecycleHandlerBuilder()
                                          .ConfigureSessionLoader(session)
                                          .ConfigureExpirationRetriever(session)
                                          .ConfigureSessionWriter(session)
                                          .ConfigureCookieWriter(session);
            var lifecycleHandler = lifecycleHandlerBuilder.Build();

            var request  = CreateMockRequest();
            var response = CreateMockResponse();

            await lifecycleHandler.OnRequest(request);

            await lifecycleHandler.OnResponse(request, response);

            lifecycleHandlerBuilder
            .SessionWriter
            .Verify(w => w.WriteSession(
                        It.IsAny <RawSession>(),
                        It.IsAny <DateTime>()));
            lifecycleHandlerBuilder
            .CookieWriter
            .Verify(w => w.WriteCookie(
                        It.IsAny <IRequest>(),
                        It.IsAny <IResponse>(),
                        It.IsAny <RawSession>(),
                        It.IsAny <DateTime>()));
        }
        public void SessionCannotBeAccessedBeforeCallingOnRequest()
        {
            var builder          = new SessionLifecycleHandlerBuilder();
            var lifecycleHandler = builder.Build();

            Assert.Throws <InvalidOperationException>(() =>
            {
                var unusedResult = lifecycleHandler.Session;
            });
        }
        public async Task UsesLoadedSession()
        {
            var session = CreateSession(new Existing(SessionId));
            var builder = new SessionLifecycleHandlerBuilder()
                          .ConfigureSessionLoader(session);

            var lifecycleHandler = builder.Build();
            await lifecycleHandler.OnRequest(CreateMockRequest());

            Assert.Equal(
                session,
                ExtractRawSession(lifecycleHandler.Session));
        }
        public async Task CreatesNewSessionIfNoneCanBeLoaded()
        {
            var session = CreateSession(new New(SessionId));
            var builder = new SessionLifecycleHandlerBuilder()
                          .ConfigureSessionLoader(null)
                          .ConfigureSessionCreator(session);

            var lifecycleHandler = builder.Build();
            await lifecycleHandler.OnRequest(CreateMockRequest());

            Assert.Equal(
                session,
                ExtractRawSession(lifecycleHandler.Session));
        }
        public void SessionIsReadOnlyAfterResponseHasBeenSent()
        {
            var session = CreateSession(new Existing(SessionId));

            var lifecycleHandler = new SessionLifecycleHandlerBuilder()
                                   .ConfigureSessionLoader(session)
                                   .ConfigureExpirationRetriever(session)
                                   .ConfigureSessionWriter(session)
                                   .ConfigureCookieWriter(session)
                                   .Build();

            var request  = CreateMockRequest();
            var response = CreateMockResponse();

            lifecycleHandler.OnRequest(request);
            lifecycleHandler.OnResponse(request, response);

            Assert.True(session.ReadOnly);
        }