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);
        }
        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));
        }
        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);
        }
        public void ConvertRequestToBlogRequest_WithNoMatchingBlogButWithRequestForLoginPage_SetsBlogRequestBlogToNull()
        {
            //arrange
            var service = new Mock<IBlogLookupService>();
            service.Setup(s => s.Lookup(It.IsAny<BlogRequest>())).Returns((BlogLookupResult)null);
            var httpResponse = new Mock<HttpResponseBase>();
            httpResponse.Setup(r => r.Redirect(It.IsAny<string>(), true)).Throws(
                new InvalidOperationException("Method should not have been called"));
            Mock<HttpRequestBase> httpRequest = CreateRequest("example.com", "/", "/login.aspx", true);
            httpRequest.Setup(r => r.FilePath).Returns("/Login.aspx");
            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.Blog);
        }
        public void ConvertRequestToBlogRequest_WithRequestForCorrectHost_ReturnsBlogRequest()
        {
            //arrange
            var service = new Mock<IBlogLookupService>();
            service.Setup(s => s.Lookup(It.IsAny<BlogRequest>())).Returns(
                new BlogLookupResult(new Blog {IsActive = true}, null));
            var httpResponse = new Mock<HttpResponseBase>();
            httpResponse.Setup(r => r.End()).Throws(
                new InvalidOperationException("This method should not have been called"));
            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.IsNotNull(request);
        }
        public void ConvertRequestToBlogRequest_WithRequestForAlternateHost_RedirectsToPrimaryHost()
        {
            //arrange
            var service = new Mock<IBlogLookupService>();
            service.Setup(s => s.Lookup(It.IsAny<BlogRequest>())).Returns(new BlogLookupResult(null,
                                                                                               new Uri(
                                                                                                   "http://www.example.com/")));
            var httpResponse = new Mock<HttpResponseBase>();
            httpResponse.Stub(r => r.StatusCode);
            httpResponse.Stub(r => r.StatusDescription);
            httpResponse.Stub(r => r.RedirectLocation);
            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.End());
            Assert.AreEqual(301, httpResponse.Object.StatusCode);
            Assert.AreEqual("301 Moved Permanently", httpResponse.Object.StatusDescription);
            Assert.AreEqual("http://www.example.com/", httpResponse.Object.RedirectLocation);
        }
        public void ConvertRequestToBlogRequest_WithNoMatchingBlog_RedirectsToBlogNotConfiguredPage()
        {
            //arrange
            var service = new Mock<IBlogLookupService>();
            service.Setup(s => s.Lookup(It.IsAny<BlogRequest>())).Returns((BlogLookupResult)null);
            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.Blog);
            httpResponse.Verify(r => r.Redirect("~/install/BlogNotConfiguredError.aspx", true));
        }