<commit_before>﻿namespace Nancy.Tests.Unit.Bootstrapper
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading;

    using FakeItEasy;

    using Nancy.Bootstrapper;
    using Nancy.Diagnostics;
    using Nancy.Tests.Fakes;

    using Xunit;

    public class NancyBootstrapperWithRequestContainerBaseFixture
    {
        private readonly FakeBootstrapper bootstrapper;

        public NancyBootstrapperWithRequestContainerBaseFixture()
        {
            this.bootstrapper = new FakeBootstrapper();
        }

        [Fact]
        public void Should_not_register_request_lifetime_types_into_application_container()
        {
            // Given
            var typeRegistrations = new[]
                                    {
                                        new TypeRegistration(typeof(object), typeof(object)),
                                        new TypeRegistration(typeof(FakeBootstrapperBaseImplementation), typeof(FakeBootstrapperBaseImplementation), Lifetime.Transient),
                                        new TypeRegistration(typeof(string), typeof(string), Lifetime.PerRequest),
                                    };
            var startupStub = A.Fake<IRegistrations>();
            A.CallTo(() => startupStub.TypeRegistrations).Returns(typeRegistrations);
            this.bootstrapper.OverriddenRegistrationTasks = new[] { startupStub };

            // When
            this.bootstrapper.Initialise();

            // Then
            this.bootstrapper.ApplicationTypeRegistrations.Any(tr => tr.RegistrationType == typeof(object)).ShouldBeTrue();
            this.bootstrapper.ApplicationTypeRegistrations.Any(tr => tr.RegistrationType == typeof(FakeBootstrapperBaseImplementation)).ShouldBeTrue();
            this.bootstrapper.ApplicationTypeRegistrations.Any(tr => tr.RegistrationType == typeof(string)).ShouldBeFalse();
        }

        [Fact]
        public void Should_register_request_lifetime_types_into_request_container_as_singletons()
        {
            // Given
            var typeRegistrations = new[]
                                    {
                                        new TypeRegistration(typeof(object), typeof(object)),
                                        new TypeRegistration(typeof(FakeBootstrapperBaseImplementation), typeof(FakeBootstrapperBaseImplementation), Lifetime.Transient),
                                        new TypeRegistration(typeof(string), typeof(string), Lifetime.PerRequest),
                                    };
            var startupStub = A.Fake<IRegistrations>();
            A.CallTo(() => startupStub.TypeRegistrations).Returns(typeRegistrations);
            this.bootstrapper.OverriddenRegistrationTasks = new[] { startupStub };

            // When
            this.bootstrapper.Initialise();

            // Then
            this.bootstrapper.RequestTypeRegistrations.Any(tr => tr.RegistrationType == typeof(string) && tr.Lifetime == Lifetime.Singleton).ShouldBeTrue();
        }

        [Fact]
        public void Should_not_register_request_lifetime_collectiontypes_into_application_container()
        {
            // Given
            var collectionRegistrations = new[]
                                    {
                                        new CollectionTypeRegistration(typeof(object), new[] { typeof(object) }),
                                        new CollectionTypeRegistration(typeof(FakeBootstrapperBaseImplementation), new[] { typeof(FakeBootstrapperBaseImplementation) }, Lifetime.Transient),
                                        new CollectionTypeRegistration(typeof(string), new[] { typeof(string) }, Lifetime.PerRequest),
                                    };
            var startupStub = A.Fake<IRegistrations>();
            A.CallTo(() => startupStub.CollectionTypeRegistrations).Returns(collectionRegistrations);
            this.bootstrapper.OverriddenRegistrationTasks = new[] { startupStub };

            // When
            this.bootstrapper.Initialise();

            // Then
            this.bootstrapper.ApplicationCollectionTypeRegistrations.Any(tr => tr.RegistrationType == typeof(object)).ShouldBeTrue();
            this.bootstrapper.ApplicationCollectionTypeRegistrations.Any(tr => tr.RegistrationType == typeof(FakeBootstrapperBaseImplementation)).ShouldBeTrue();
            this.bootstrapper.ApplicationCollectionTypeRegistrations.Any(tr => tr.RegistrationType == typeof(string)).ShouldBeFalse();
        }

        [Fact]
        public void Should_register_request_lifetime_collectiontypes_into_request_container_as_singletons()
        {
            // Given
            var collectionRegistrations = new[]
                                    {
                                        new CollectionTypeRegistration(typeof(object), new[] { typeof(object) }),
                                        new CollectionTypeRegistration(typeof(FakeBootstrapperBaseImplementation), new[] { typeof(FakeBootstrapperBaseImplementation) }, Lifetime.Transient),
                                        new CollectionTypeRegistration(typeof(string), new[] { typeof(string) }, Lifetime.PerRequest),
                                    };
            var startupStub = A.Fake<IRegistrations>();
            A.CallTo(() => startupStub.CollectionTypeRegistrations).Returns(collectionRegistrations);
            A.CallTo(() => this.bootstrapper.FakeNancyEngine.HandleRequest(A.Dummy<Request>(), A.Dummy<Func<NancyContext, NancyContext>>(), A.Dummy<CancellationToken>()))
                               .Invokes(foc => ((INancyEngine)foc.FakedObject).RequestPipelinesFactory.Invoke(new NancyContext()));
            this.bootstrapper.OverriddenRegistrationTasks = new[] { startupStub };
            this.bootstrapper.Initialise();
            var engine = this.bootstrapper.GetEngine();

            // When
            engine.HandleRequest(new FakeRequest("GET", "/"));

            // Then
            this.bootstrapper.RequestCollectionTypeRegistrations.Any(tr => tr.RegistrationType == typeof(string) && tr.Lifetime == Lifetime.Singleton).ShouldBeTrue();
        }

        internal class FakeBootstrapper : NancyBootstrapperWithRequestContainerBase<FakeContainer>
        {
            public IDiagnostics FakeDiagnostics { get; set; }
<commit_msg>Fixed tests<commit_after>﻿namespace Nancy.Tests.Unit.Bootstrapper
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading;
    using System.Threading.Tasks;

    using FakeItEasy;

    using Nancy.Bootstrapper;
    using Nancy.Diagnostics;
    using Nancy.Tests.Fakes;

    using Xunit;

    public class NancyBootstrapperWithRequestContainerBaseFixture
    {
        private readonly FakeBootstrapper bootstrapper;

        public NancyBootstrapperWithRequestContainerBaseFixture()
        {
            this.bootstrapper = new FakeBootstrapper();
        }

        [Fact]
        public void Should_not_register_request_lifetime_types_into_application_container()
        {
            // Given
            var typeRegistrations = new[]
                                    {
                                        new TypeRegistration(typeof(object), typeof(object)),
                                        new TypeRegistration(typeof(FakeBootstrapperBaseImplementation), typeof(FakeBootstrapperBaseImplementation), Lifetime.Transient),
                                        new TypeRegistration(typeof(string), typeof(string), Lifetime.PerRequest),
                                    };
            var startupStub = A.Fake<IRegistrations>();
            A.CallTo(() => startupStub.TypeRegistrations).Returns(typeRegistrations);
            this.bootstrapper.OverriddenRegistrationTasks = new[] { startupStub };

            // When
            this.bootstrapper.Initialise();

            // Then
            this.bootstrapper.ApplicationTypeRegistrations.Any(tr => tr.RegistrationType == typeof(object)).ShouldBeTrue();
            this.bootstrapper.ApplicationTypeRegistrations.Any(tr => tr.RegistrationType == typeof(FakeBootstrapperBaseImplementation)).ShouldBeTrue();
            this.bootstrapper.ApplicationTypeRegistrations.Any(tr => tr.RegistrationType == typeof(string)).ShouldBeFalse();
        }

        [Fact]
        public void Should_register_request_lifetime_types_into_request_container_as_singletons()
        {
            // Given
            var typeRegistrations = new[]
                                    {
                                        new TypeRegistration(typeof(object), typeof(object)),
                                        new TypeRegistration(typeof(FakeBootstrapperBaseImplementation), typeof(FakeBootstrapperBaseImplementation), Lifetime.Transient),
                                        new TypeRegistration(typeof(string), typeof(string), Lifetime.PerRequest),
                                    };
            var startupStub = A.Fake<IRegistrations>();
            A.CallTo(() => startupStub.TypeRegistrations).Returns(typeRegistrations);
            var engine = new FakeEngine();
            this.bootstrapper.FakeNancyEngine = engine;
            this.bootstrapper.OverriddenRegistrationTasks = new[] { startupStub };
            this.bootstrapper.Initialise();

            // When
            var builtEngine = this.bootstrapper.GetEngine();
            builtEngine.HandleRequest(new FakeRequest("GET", "/"));

            // Then
            this.bootstrapper.RequestTypeRegistrations.Any(tr => tr.RegistrationType == typeof(string) && tr.Lifetime == Lifetime.Singleton).ShouldBeTrue();
        }

        [Fact]
        public void Should_not_register_request_lifetime_collectiontypes_into_application_container()
        {
            // Given
            var collectionRegistrations = new[]
                                    {
                                        new CollectionTypeRegistration(typeof(object), new[] { typeof(object) }),
                                        new CollectionTypeRegistration(typeof(FakeBootstrapperBaseImplementation), new[] { typeof(FakeBootstrapperBaseImplementation) }, Lifetime.Transient),
                                        new CollectionTypeRegistration(typeof(string), new[] { typeof(string) }, Lifetime.PerRequest),
                                    };
            var startupStub = A.Fake<IRegistrations>();
            A.CallTo(() => startupStub.CollectionTypeRegistrations).Returns(collectionRegistrations);
            this.bootstrapper.OverriddenRegistrationTasks = new[] { startupStub };

            // When
            this.bootstrapper.Initialise();

            // Then
            this.bootstrapper.ApplicationCollectionTypeRegistrations.Any(tr => tr.RegistrationType == typeof(object)).ShouldBeTrue();
            this.bootstrapper.ApplicationCollectionTypeRegistrations.Any(tr => tr.RegistrationType == typeof(FakeBootstrapperBaseImplementation)).ShouldBeTrue();
            this.bootstrapper.ApplicationCollectionTypeRegistrations.Any(tr => tr.RegistrationType == typeof(string)).ShouldBeFalse();
        }

        [Fact]
        public void Should_register_request_lifetime_collectiontypes_into_request_container_as_singletons()
        {
            // Given
            var collectionRegistrations = new[]
                                    {
                                        new CollectionTypeRegistration(typeof(object), new[] { typeof(object) }),
                                        new CollectionTypeRegistration(typeof(FakeBootstrapperBaseImplementation), new[] { typeof(FakeBootstrapperBaseImplementation) }, Lifetime.Transient),
                                        new CollectionTypeRegistration(typeof(string), new[] { typeof(string) }, Lifetime.PerRequest),
                                    };
            var startupStub = A.Fake<IRegistrations>();
            A.CallTo(() => startupStub.CollectionTypeRegistrations).Returns(collectionRegistrations);
            var engine = new FakeEngine();
            this.bootstrapper.FakeNancyEngine = engine;
            this.bootstrapper.OverriddenRegistrationTasks = new[] { startupStub };
            this.bootstrapper.Initialise();

            // When
            var builtEngine = this.bootstrapper.GetEngine();
            builtEngine.HandleRequest(new FakeRequest("GET", "/"));

            // Then
            this.bootstrapper.RequestCollectionTypeRegistrations.Any(tr => tr.RegistrationType == typeof(string) && tr.Lifetime == Lifetime.Singleton).ShouldBeTrue();
        }

        internal class FakeEngine : INancyEngine
        {
            public Func<NancyContext, IPipelines> RequestPipelinesFactory { get; set; }

            public Task<NancyContext> HandleRequest(Request request, Func<NancyContext, NancyContext> preRequest, CancellationToken cancellationToken)
            {
                var tcs = new TaskCompletionSource<NancyContext>();

                var nancyContext = new NancyContext() { Request = request };

                this.RequestPipelinesFactory.Invoke(nancyContext);

                tcs.SetResult(nancyContext);
                
                return tcs.Task;
            }
        }

        internal class FakeBootstrapper : NancyBootstrapperWithRequestContainerBase<FakeContainer>
        {
            public IDiagnostics FakeDiagnostics { get; set; }

{'fraction_non_alphanumeric': 0.0898868036937742, 'fraction_numerical': 0.0, 'mean_word_length': 2.118671620993962, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 64, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2897121', 'n_tokens_mistral': 3055, 'n_tokens_neox': 3049, 'n_words': 461}