コード例 #1
0
        RequestNotMatchingAnyBlog_ButWithASingleBlogInSystemWithLocalHostButNotMatchingSubfolder_ReturnsUpdatesItsHostThenRedirectsToSubfolder
            ()
        {
            //arrange
            var onlyBlog = new Blog {
                Host = "localhost", Subfolder = "sub"
            };
            var pagedCollection = new PagedCollection <Blog> {
                onlyBlog
            };

            pagedCollection.MaxItems = 1;

            var repository = new Mock <ObjectRepository>();

            repository.Setup(r => r.GetBlog("example.com", It.IsAny <string>())).Returns((Blog)null);
            repository.Setup(r => r.GetPagedBlogs(null, 0, It.IsAny <int>(), ConfigurationFlags.None)).Returns(
                pagedCollection);
            var appSettings = new NameValueCollection();

            appSettings.Add("AggregateEnabled", "false");
            var hostInfo    = new HostInfo(appSettings);
            var service     = new BlogLookupService(repository.Object, new LazyNotNull <HostInfo>(() => hostInfo));
            var blogRequest = new BlogRequest("example.com", string.Empty, new Uri("http://example.com/foo/bar"), false);

            //act
            BlogLookupResult result = service.Lookup(blogRequest);

            //assert
            Assert.IsNull(result.Blog);
            Assert.IsNotNull(result.AlternateUrl);
            Assert.AreEqual("http://example.com/sub/foo/bar", result.AlternateUrl.ToString());
            Assert.AreEqual("example.com", onlyBlog.Host);
            repository.Verify(r => r.UpdateBlog(It.IsAny <Blog>()));
        }
コード例 #2
0
        public void RequestWithSubfolderNotMatchingAnyBlog_ButWithAggregateBlogsEnabledAndMoreThanOneActiveBlogsInTheSystem_ReturnsNull()
        {
            //arrange
            var repository = new Mock <ObjectRepository>();

            repository.Setup(r => r.GetBlog("example.com", It.IsAny <string>())).Returns((Blog)null);
            var blog1 = new Blog {
                Host = "example.com", Subfolder = "not-sub"
            };
            var blog2 = new Blog {
                Host = "example.com", Subfolder = "not-sub-2"
            };
            var pagedCollection = new PagedCollection <Blog> {
                blog1, blog2
            };

            pagedCollection.MaxItems = 2;
            repository.Setup(r => r.GetPagedBlogs(null, 0, It.IsAny <int>(), ConfigurationFlags.None)).Returns(
                pagedCollection);
            var appSettings = new NameValueCollection();

            appSettings.Add("AggregateEnabled", "true");
            var hostInfo = new HostInfo(appSettings);
            var service  = new BlogLookupService(repository.Object,
                                                 new LazyNotNull <HostInfo>(() => hostInfo));
            var blogRequest = new BlogRequest("example.com", "blog1234", new Uri("http://example.com/foo/bar"), false);

            //act
            BlogLookupResult result = service.Lookup(blogRequest);

            //assert
            Assert.IsNull(result);
        }
コード例 #3
0
        RequestNotMatchingAnyBlog_ButWithASingleBlogInSystemWithMatchingHostButDifferentSubfolder_RedirectsToOnlyBlog
            ()
        {
            //arrange
            var onlyBlog = new Blog {
                Host = "example.com", Subfolder = "not-sub"
            };
            var pagedCollection = new PagedCollection <Blog> {
                onlyBlog
            };

            pagedCollection.MaxItems = 1;

            var repository = new Mock <ObjectRepository>();

            repository.Setup(r => r.GetBlog("example.com", "sub")).Returns((Blog)null);
            repository.Setup(r => r.GetBlog("example.com", "not-sub")).Returns(onlyBlog);
            repository.Setup(r => r.GetPagedBlogs(null, 0, It.IsAny <int>(), ConfigurationFlags.None)).Returns(
                pagedCollection);
            var appSettings = new NameValueCollection();

            appSettings.Add("AggregateEnabled", "false");
            var hostInfo    = new HostInfo(appSettings);
            var service     = new BlogLookupService(repository.Object, new LazyNotNull <HostInfo>(() => hostInfo));
            var blogRequest = new BlogRequest("example.com", "sub", new Uri("http://example.com/Subtext.Web/sub/bar"),
                                              false, RequestLocation.Blog, "/Subtext.Web");

            //act
            BlogLookupResult result = service.Lookup(blogRequest);

            //assert
            Assert.IsNull(result.Blog);
            Assert.AreEqual("http://example.com/Subtext.Web/not-sub/bar", result.AlternateUrl.ToString());
        }
コード例 #4
0
        public void RequestNotMatchingAnyBlog_ButWithAggregateBlogsEnabledAndActiveBlogsInTheSystem_ReturnsAggregateBlog
            ()
        {
            //arrange
            var repository = new Mock <ObjectProvider>();

            repository.Setup(r => r.GetBlog("example.com", It.IsAny <string>())).Returns((Blog)null);
            var onlyBlog = new Blog {
                Host = "example.com", Subfolder = "not-sub"
            };
            var pagedCollection = new PagedCollection <Blog> {
                onlyBlog
            };

            pagedCollection.MaxItems = 1;
            repository.Setup(r => r.GetPagedBlogs(null, 0, It.IsAny <int>(), ConfigurationFlags.None)).Returns(
                pagedCollection);
            var aggregateBlog = new Blog();
            var service       = new BlogLookupService(repository.Object,
                                                      new HostInfo
            {
                BlogAggregationEnabled = true, AggregateBlog = aggregateBlog
            });
            var blogRequest = new BlogRequest("example.com", string.Empty, new Uri("http://example.com/foo/bar"), false);

            //act
            BlogLookupResult result = service.Lookup(blogRequest);

            //assert
            Assert.AreSame(aggregateBlog, result.Blog);
        }
コード例 #5
0
        public void RequestNotMatchingAnyBlog_ButWithASingleBlogInSystemWithLocalHost_ReturnsThatBlogAndUpdatesItsHost()
        {
            //arrange
            var onlyBlog = new Blog {
                Host = "localhost", Subfolder = ""
            };
            var pagedCollection = new PagedCollection <Blog> {
                onlyBlog
            };

            pagedCollection.MaxItems = 1;

            var repository = new Mock <ObjectProvider>();

            repository.Setup(r => r.GetBlog("example.com", It.IsAny <string>())).Returns((Blog)null);
            repository.Setup(r => r.GetPagedBlogs(null, 0, It.IsAny <int>(), ConfigurationFlags.None)).Returns(
                pagedCollection);
            var service = new BlogLookupService(repository.Object, new HostInfo {
                BlogAggregationEnabled = false
            });
            var blogRequest = new BlogRequest("example.com", string.Empty, new Uri("http://example.com/foo/bar"), false);

            //act
            BlogLookupResult result = service.Lookup(blogRequest);

            //assert
            Assert.IsNotNull(result.Blog);
            Assert.IsNull(result.AlternateUrl);
            Assert.AreEqual("example.com", result.Blog.Host);
            Assert.AreEqual("example.com", onlyBlog.Host);
            repository.Verify(r => r.UpdateBlog(It.IsAny <Blog>()));
        }
コード例 #6
0
        public void ConvertRequestToBlogRequestForStaticImage_WithNoMatchingBlog_DoesNotRedirect()
        {
            //arrange
            var service = new Mock <IBlogLookupService>();
            var result  = new BlogLookupResult(null, new Uri("http://localhost/images/blog/services/identiconhandler.ashx"));

            service.Setup(s => s.Lookup(It.IsAny <BlogRequest>())).Returns(result);
            var httpResponse = new Mock <HttpResponseBase>();

            httpResponse.Setup(r => r.Redirect(It.IsAny <string>(), true)).Throws(new InvalidOperationException("Should not redirect"));
            Mock <HttpRequestBase> httpRequest = CreateRequest("example.com", "/", "/images/services/identiconhandler.ashx", true);
            var httpContext = new Mock <HttpContextBase>();

            httpContext.Setup(c => c.Request).Returns(httpRequest.Object);
            httpContext.Setup(c => c.Response).Returns(httpResponse.Object);
            var module = new BlogRequestModule(service.Object);

            //act
            BlogRequest request = module.ConvertRequestToBlogRequest(httpContext.Object);

            //assert
            Assert.IsNotNull(request);
            Assert.IsNull(request.Blog);
            Assert.AreEqual(RequestLocation.StaticFile, request.RequestLocation);
        }
コード例 #7
0
        public void Request_WithNonMatchingHostButAlternativeHostMatches_ReturnsAlternativeHost()
        {
            //arrange
            var repository = new Mock <ObjectRepository>();

            repository.Setup(r => r.GetBlog("example.com", It.IsAny <string>())).Returns(new Blog {
                Host = "www.example.com"
            });
            var service = new BlogLookupService(repository.Object, new LazyNotNull <HostInfo>(CreateHostInfo));

            //act
            BlogLookupResult result =
                service.Lookup(new BlogRequest("example.com", string.Empty, new Uri("http://example.com/foo/bar"), false));

            //assert
            Assert.IsNull(result.Blog);
            Assert.AreEqual("http://www.example.com/foo/bar", result.AlternateUrl.ToString());
        }
コード例 #8
0
        public void Request_WithMatchingHost_ReturnsCorrespondingBlog()
        {
            //arrange
            var repository = new Mock <ObjectProvider>();

            repository.Setup(r => r.GetBlog("example.com", It.IsAny <string>())).Returns(new Blog {
                Host = "example.com"
            });
            var service = new BlogLookupService(repository.Object, new HostInfo());

            //act
            BlogLookupResult result =
                service.Lookup(new BlogRequest("example.com", string.Empty, new Uri("http://example.com/foo/bar"), false));

            //assert
            Assert.IsNotNull(result.Blog);
            Assert.IsNull(result.AlternateUrl);
        }
コード例 #9
0
ファイル: BlogRequestModule.cs プロジェクト: ghjigan/Subtext
        public BlogRequest ConvertRequestToBlogRequest(HttpContextBase httpContext)
        {
            string redirectUrl = httpContext.Request.GetFileNotFoundRedirectUrl(HttpRuntime.UsingIntegratedPipeline);

            if (!String.IsNullOrEmpty(redirectUrl))
            {
                httpContext.Response.Redirect(redirectUrl, true);
                return(null);
            }

            // REVIEW: Maybe the BlogLookup.Lookup should take in an HttpContextBase
            // and return the BlogRequest as part of the result.
            var blogRequest = new BlogRequest(httpContext.Request);

            if (blogRequest.BlogNotRequired)
            {
                return(blogRequest);
            }
            BlogLookupResult result = BlogLookup.Lookup(blogRequest);

            if (result == null)
            {
                if (blogRequest.RequestLocation != RequestLocation.LoginPage)
                {
                    httpContext.Response.Redirect("~/install/BlogNotConfiguredError.aspx", true);
                }
                return(blogRequest);
            }

            if (result.Blog == null && result.AlternateUrl != null)
            {
                httpContext.Response.RedirectPermanent(result.AlternateUrl.ToString(), true);
                return(null);
            }

            if (result.Blog != null && !result.Blog.IsActive && blogRequest.RequestLocation == RequestLocation.Blog)
            {
                httpContext.Response.Redirect("~/SystemMessages/BlogNotActive.aspx", true);
                return(null);
            }

            blogRequest.Blog = result.Blog;
            return(blogRequest);
        }
コード例 #10
0
        public void Request_MatchingActiveAliasWithSubfolder_RedirectsToPrimaryWithoutSubfolder()
        {
            //arrange
            var repository = new Mock <ObjectRepository>();

            repository.Setup(r => r.GetBlog("blog.example.com", "sub")).Returns(new Blog {
                Host = "www.example.com", Subfolder = ""
            });
            var service = new BlogLookupService(repository.Object, new LazyNotNull <HostInfo>(CreateHostInfo));

            //act
            BlogLookupResult result =
                service.Lookup(new BlogRequest("blog.example.com", "sub", new Uri("http://blog.example.com/sub/foo/bar"),
                                               false));

            //assert
            Assert.IsNull(result.Blog);
            Assert.AreEqual("http://www.example.com/foo/bar", result.AlternateUrl.ToString());
        }
コード例 #11
0
        public void Request_MatchingActiveAlias_RedirectsToPrimary()
        {
            //arrange
            var repository = new Mock <ObjectProvider>();

            repository.Setup(r => r.GetBlog("blog.example.com", It.IsAny <string>())).Returns(new Blog
            {
                Host = "www.example.com"
            });
            var service = new BlogLookupService(repository.Object, new HostInfo());

            //act
            BlogLookupResult result =
                service.Lookup(new BlogRequest("blog.example.com", string.Empty,
                                               new Uri("http://blog.example.com/foo/bar"), false));

            //assert
            Assert.IsNull(result.Blog);
            Assert.AreEqual("http://www.example.com/foo/bar", result.AlternateUrl.ToString());
        }
コード例 #12
0
        public void Request_NotMatchingAnyBlog_ReturnsNull()
        {
            //arrange
            var repository = new Mock <ObjectProvider>();

            repository.Setup(r => r.GetBlog("example.com", It.IsAny <string>())).Returns((Blog)null);
            var pagedCollection = new Mock <IPagedCollection <Blog> >();

            pagedCollection.Setup(p => p.MaxItems).Returns(0);
            repository.Setup(r => r.GetPagedBlogs(null, 0, It.IsAny <int>(), ConfigurationFlags.None)).Returns(
                pagedCollection.Object);
            var service = new BlogLookupService(repository.Object, new HostInfo {
                BlogAggregationEnabled = false
            });

            //act
            BlogLookupResult result =
                service.Lookup(new BlogRequest("example.com", string.Empty, new Uri("http://example.com/foo/bar"), false));

            //assert
            Assert.IsNull(result);
        }
コード例 #13
0
        public void Request_NotMatchingAnyBlog_ReturnsNull()
        {
            //arrange
            var repository = new Mock <ObjectRepository>();

            repository.Setup(r => r.GetBlog("example.com", It.IsAny <string>())).Returns((Blog)null);
            var pagedCollection = new Mock <IPagedCollection <Blog> >();

            pagedCollection.Setup(p => p.MaxItems).Returns(0);
            repository.Setup(r => r.GetPagedBlogs(null, 0, It.IsAny <int>(), ConfigurationFlags.None)).Returns(
                pagedCollection.Object);
            var appSettings = new NameValueCollection();

            appSettings.Add("AggregateEnabled", "false");
            var service = new BlogLookupService(repository.Object, new LazyNotNull <HostInfo>(() => new HostInfo(appSettings)));

            //act
            BlogLookupResult result =
                service.Lookup(new BlogRequest("example.com", string.Empty, new Uri("http://example.com/foo/bar"), false));

            //assert
            Assert.IsNull(result);
        }
コード例 #14
0
        public void ConvertRequestToBlogRequestWithRequestForLoginPage_MatchingInactiveBlog_DoesNotRedirect()
        {
            //arrange
            var service = new Mock <IBlogLookupService>();
            var result  = new BlogLookupResult(new Blog {
                IsActive = false
            }, null);

            service.Setup(s => s.Lookup(It.IsAny <BlogRequest>())).Returns(result);
            var httpResponse = new Mock <HttpResponseBase>();
            Mock <HttpRequestBase> httpRequest = CreateRequest("example.com", "/", "/login.aspx", true);
            var httpContext = new Mock <HttpContextBase>();

            httpContext.Setup(c => c.Request).Returns(httpRequest.Object);
            httpContext.Setup(c => c.Response).Returns(httpResponse.Object);
            var module = new BlogRequestModule(service.Object);

            //act
            BlogRequest request = module.ConvertRequestToBlogRequest(httpContext.Object);

            //assert
            Assert.IsNotNull(request);
        }
コード例 #15
0
        public void Request_MatchingActiveAliasWithSubfolder_RedirectsToPrimaryWithDifferentSubfolder()
        {
            //arrange
            var repository = new Mock <ObjectProvider>();

            repository.Setup(r => r.GetBlog("blog.example.com", "notsub")).Returns(new Blog
            {
                Host = "www.example.com", Subfolder = "sub"
            });
            repository.Setup(r => r.GetBlogByDomainAlias("blog.example.com", "notsub", It.IsAny <bool>())).Returns(
                new Blog {
                Host = "www.example.com", Subfolder = "sub"
            });
            var service = new BlogLookupService(repository.Object, new HostInfo());

            //act
            BlogLookupResult result =
                service.Lookup(new BlogRequest("blog.example.com", "notsub",
                                               new Uri("http://blog.example.com/notsub/foo/bar"), false));

            //assert
            Assert.IsNull(result.Blog);
            Assert.AreEqual("http://www.example.com/sub/foo/bar", result.AlternateUrl.ToString());
        }
コード例 #16
0
        public void ConvertRequestToBlogRequest_MatchingInactiveBlog_RedirectsToBlogInactivePage()
        {
            //arrange
            var service = new Mock <IBlogLookupService>();
            var result  = new BlogLookupResult(new Blog {
                IsActive = false
            }, null);

            service.Setup(s => s.Lookup(It.IsAny <BlogRequest>())).Returns(result);
            var httpResponse = new Mock <HttpResponseBase>();
            Mock <HttpRequestBase> httpRequest = CreateRequest("example.com", "/", "/", true);
            var httpContext = new Mock <HttpContextBase>();

            httpContext.Setup(c => c.Request).Returns(httpRequest.Object);
            httpContext.Setup(c => c.Response).Returns(httpResponse.Object);
            var module = new BlogRequestModule(service.Object);

            //act
            BlogRequest request = module.ConvertRequestToBlogRequest(httpContext.Object);

            //assert
            Assert.IsNull(request);
            httpResponse.Verify(r => r.Redirect("~/SystemMessages/BlogNotActive.aspx", true));
        }