public void PassTheCorrectPostIdToThePostLocator()
        {
            string path = $"/{_dasBlogPostsFile}";
            Guid   id   = Guid.NewGuid();

            var query = (null as IQueryCollection).CreateMockQueryCollection();

            query.AddQueryParameter("id", id.ToString());

            var request  = (null as HttpRequest).CreateMockRequest(path, query.Object);
            var response = (null as HttpResponse).CreateMockResponse();

            var context     = (null as HttpContext).CreateMockContext(request, response);
            var postLocator = new Mock <IPostLocator>();

            var serviceCollection = (null as IServiceCollection).Create();

            serviceCollection.ReplaceDependency <IPostLocator>(postLocator.Object);

            var target = new DasBlogCompatibility((HttpContext c) => { throw new InvalidOperationException("The next delegate should not be called in this test"); },
                                                  serviceCollection.BuildServiceProvider());

            var t = target.Invoke(context.Object);

            // No need to wait for the task since it was never invoked --

            postLocator.Verify(l => l.GetUrlByPostId(id), Times.Once);
        }
        public void ReturnA404ResponseIfTheIdIsNotAValidGuid()
        {
            HttpContext actual = null;

            string path  = $"/{_dasBlogPostsFile}";
            var    query = (null as IQueryCollection).CreateMockQueryCollection();

            query.AddQueryParameter("id", "1234");

            var request  = (null as HttpRequest).CreateMockRequest(path, query.Object);
            var response = (null as HttpResponse).CreateMockResponse();

            var context         = (null as HttpContext).CreateMockContext(request, response);
            var serviceProvider = (null as IServiceProvider).Create();

            var target = new DasBlogCompatibility((HttpContext c) =>
            {
                actual = c;
                return(Task.CompletedTask);
            }, serviceProvider);

            var t = target.Invoke(context.Object);

            Task.WaitAll(t);

            Assert.Equal(404, context.Object.Response.StatusCode);
        }
        public void RedirectToThePostUrlBasedOnThePostId()
        {
            string path    = $"/{_dasBlogPostsFile}";
            Guid   id      = Guid.NewGuid();
            string postUrl = $"{string.Empty.GetRandom()}.html";

            var query = (null as IQueryCollection).CreateMockQueryCollection();

            query.AddQueryParameter("id", id.ToString());

            var request  = (null as HttpRequest).CreateMockRequest(path, query.Object);
            var response = (null as HttpResponse).CreateMockResponse();

            var context = (null as HttpContext).CreateMockContext(request, response);

            var postLocator = new Mock <IPostLocator>();

            postLocator.Setup(l => l.GetUrlByPostId(It.IsAny <Guid>()))
            .Returns(postUrl);

            var serviceCollection = (null as IServiceCollection).Create();

            serviceCollection.ReplaceDependency <IPostLocator>(postLocator.Object);

            var target = new DasBlogCompatibility((HttpContext c) => { throw new InvalidOperationException("The next delegate should not be called in this test"); },
                                                  serviceCollection.BuildServiceProvider());

            var t = target.Invoke(context.Object);

            // No need to wait for the task since it was never invoked --

            response.Verify(r => r.Redirect(postUrl, It.IsAny <bool>()), Times.Once);
        }
        public void ReturnA404ResponseIfThePostIdIsNotFound()
        {
            string path = $"/{_dasBlogPostsFile}";
            Guid   id   = Guid.NewGuid();

            var query = (null as IQueryCollection).CreateMockQueryCollection();

            query.AddQueryParameter("id", id.ToString());

            var request  = (null as HttpRequest).CreateMockRequest(path, query.Object);
            var response = (null as HttpResponse).CreateMockResponse();

            var headers = Mock.Of <IHeaderDictionary>();

            response.SetupGet(r => r.Headers).Returns(headers);

            var context     = (null as HttpContext).CreateMockContext(request, response);
            var postLocator = new Mock <IPostLocator>();

            postLocator.Setup(l => l.GetUrlByPostId(It.IsAny <Guid>()))
            .Throws(new PostNotFoundException(id));

            var serviceCollection = (null as IServiceCollection).Create();

            serviceCollection.ReplaceDependency <IPostLocator>(postLocator.Object);

            var target = new DasBlogCompatibility((HttpContext c) => { throw new InvalidOperationException("The next delegate should not be called in this test"); },
                                                  serviceCollection.BuildServiceProvider());

            var t = target.Invoke(context.Object);

            // No need to wait for the task since it was never invoked

            Assert.Equal(404, context.Object.Response.StatusCode);
        }
        public void ReturnTheProperArgumentNameIfTheServiceProviderIsNotSupplied()
        {
            string actual = string.Empty;

            try
            {
                var nextDelegate = Mock.Of <RequestDelegate>();
                var target       = new DasBlogCompatibility(nextDelegate, null);
            }
            catch (ArgumentNullException ex)
            {
                actual = ex.ParamName;
            }

            string expected = "serviceProvider";

            Assert.Equal(expected, actual);
        }
        public void PassTheHttpContextToTheInvokeCallOnTheNextDelegate()
        {
            HttpContext actual  = null;
            string      path    = $"/{string.Empty.GetRandom()}.html";
            var         context = (null as HttpContext).CreateMockContext(path);

            var target = new DasBlogCompatibility((HttpContext c) =>
            {
                actual = c;
                return(Task.CompletedTask);
            });

            var t = target.Invoke(context.Object);

            Task.WaitAll(t);

            Assert.Equal(context.Object, actual);
        }
        public void ReturnTheProperArgumentNameIfThePostLocatorIsNotSupplied()
        {
            string actual = string.Empty;

            try
            {
                var nextDelegate      = Mock.Of <RequestDelegate>();
                var serviceCollection = new ServiceCollection();
                var target            = new DasBlogCompatibility(nextDelegate, serviceCollection.BuildServiceProvider());
            }
            catch (DependencyNotFoundException ex)
            {
                actual = ex.InterfaceTypeName;
            }

            string expected = "IPostLocator";

            Assert.Equal(expected, actual);
        }
        public void RedirectIfCallIsToDasBlogSyndicationFile()
        {
            string path = $"/{_dasBlogSyndicationFile}";

            var request  = (null as HttpRequest).CreateMockRequest(path, null);
            var response = (null as HttpResponse).CreateMockResponse();
            var context  = (null as HttpContext).CreateMockContext(request, response);

            var target = new DasBlogCompatibility((HttpContext c) =>
            {
                return(Task.CompletedTask);
            });

            var t = target.Invoke(context.Object);

            Task.WaitAll(t);

            response.Verify(r => r.Redirect(It.IsAny <string>(), It.IsAny <bool>()), Times.Once);
        }
        public void CallInvokeOnTheNextDelegateIfNotACallToThePostFile()
        {
            bool   executed        = false;
            string path            = $"/{string.Empty.GetRandom()}.html";
            var    context         = (null as HttpContext).CreateMockContext(path);
            var    serviceProvider = (null as IServiceProvider).Create();

            var target = new DasBlogCompatibility((HttpContext c) =>
            {
                executed = true;
                return(Task.CompletedTask);
            }, serviceProvider);

            var t = target.Invoke(context.Object);

            Task.WaitAll(t);

            Assert.True(executed);
        }
        public void IssuesAPermanantRedirectIfCallIsToDasBlogSyndicationFile()
        {
            string folderName   = string.Empty.GetRandom();
            string expectedPath = $"/{folderName}/{_currentSyndicationFile}";
            string path         = $"/{folderName}/{_dasBlogSyndicationFile}";

            var request  = (null as HttpRequest).CreateMockRequest(path, null);
            var response = (null as HttpResponse).CreateMockResponse();
            var context  = (null as HttpContext).CreateMockContext(request, response);

            var target = new DasBlogCompatibility((HttpContext c) =>
            {
                return(Task.CompletedTask);
            });

            var t = target.Invoke(context.Object);

            Task.WaitAll(t);

            response.Verify(r => r.Redirect(It.IsAny <string>(), true), Times.Once);
        }
        public void CallInvokeOnTheNextDelegateIfNotACallToTheSyndicationFile()
        {
            bool executed = false;

            string path = $"/{string.Empty.GetRandom()}.html";

            System.Diagnostics.Debug.Assert(!path.EndsWith(_dasBlogSyndicationFile));

            var context = (null as HttpContext).CreateMockContext(path);

            var target = new DasBlogCompatibility((HttpContext c) =>
            {
                executed = true;
                return(Task.CompletedTask);
            });

            var t = target.Invoke(context.Object);

            Task.WaitAll(t);

            Assert.True(executed);
        }