Exemplo n.º 1
0
        public ImageHandlerFixture()
        {
            _httpContext = MvcTestHelper.GetHttpContext();
            _httpContext.HttpResponse.Expect(r => r.OutputStream).Returns(new Mock <Stream>().Object);

            var story = new Mock <IStory>();

            story.ExpectGet(s => s.VoteCount).Returns(10);
            story.ExpectGet(s => s.CreatedAt).Returns(SystemTime.Now().AddDays(-8));

            var repository = new Mock <IStoryRepository>();

            repository.Expect(r => r.FindByUrl(It.IsAny <string>())).Returns(story.Object);

            _handler = new ImageHandler
            {
                Colors = new DefaultColors
                {
                    BorderColor    = "808080",
                    TextBackColor  = "404040",
                    TextForeColor  = "ffffff",
                    CountBackColor = "eb4c07",
                    CountForeColor = "ffffff"
                },
                Width       = 100,
                Height      = 22,
                BorderWidth = 1,
                FontName    = "Tahoma",
                FontSize    = 12,
                NewStoryCacheDurationInMinutes     = 5,
                ExpiredStoryCacheDurationInMinutes = 4320,
                StoryRepository = repository.Object,
                Settings        = settings.Object
            };
        }
Exemplo n.º 2
0
        public ErrorHandlerFixture()
        {
            _httpContext = MvcTestHelper.GetHttpContext();
            _httpContext.SetupGet(h => h.IsCustomErrorEnabled).Returns(true);

            _handler = new ErrorHandler();
        }
Exemplo n.º 3
0
        public OpenSearchHandlerFixture()
        {
            RouteTable.Routes.Clear();
            new RegisterRoutes(settings.Object).Execute();

            _httpContext = MvcTestHelper.GetHttpContext("/Kigg", null, null);
        }
Exemplo n.º 4
0
        public void Url_Should_Return_New_Helper()
        {
            var httpContext = MvcTestHelper.GetHttpContext();

            ControllerContext controllerContext = new ControllerContext(httpContext.Object, new RouteData(), new Mock <ControllerBase>().Object);

            Assert.NotNull(controllerContext.Url());
        }
Exemplo n.º 5
0
        public FeedResultFixture()
        {
            RouteTable.Routes.Clear();
            new RegisterRoutes(settings.Object).Execute();

            feedViewData = CreateViewData();
            _httpContext = MvcTestHelper.GetHttpContext("/Kigg", null, null);

            _httpContext.HttpRequest.ExpectGet(r => r.Url).Returns(new Uri("http://kigg.com"));
        }
Exemplo n.º 6
0
        public AutoRefreshAttributeFixture()
        {
            _httpContext = MvcTestHelper.GetHttpContext("/Kigg", null, null);

            var settings = new Mock <IConfigurationSettings>();

            settings.SetupGet(s => s.FeedStoryPerPage).Returns(25);
            settings.SetupGet(s => s.CarouselStoryCount).Returns(30);

            RouteTable.Routes.Clear();
            new RegisterRoutes(settings.Object).Execute();
        }
Exemplo n.º 7
0
        public void HandleIfNotModified_Should_Return_False_If_Content_Is_Modified()
        {
            const string ETag = "abcdef";

            var httpContext = MvcTestHelper.GetHttpContext();

            httpContext.HttpRequest.ExpectGet(r => r.Headers).Returns(new NameValueCollection {
                { "If-None-Match", "foobar" }
            });

            Assert.False(BaseHandler.HandleIfNotModified(httpContext.Object, ETag));
        }
Exemplo n.º 8
0
        public void OnResultExecuted_Should_Not_Compress_Response_When_Exception_Occurred_And_Exception_Is_Not_Handled()
        {
            HttpContextMock       httpContext       = MvcTestHelper.GetHttpContext();
            ControllerContext     controllerContext = new ControllerContext(httpContext.Object, new RouteData(), new Mock <ControllerBase>().Object);
            ResultExecutedContext executedContext   = new ResultExecutedContext(controllerContext, new EmptyResult(), false, new Exception());

            //httpContext.HttpResponse.Setup(r => r.AppendHeader(It.IsAny<string>(), It.IsAny<string>())).Never();
            httpContext.HttpResponse.Verify(r => r.AppendHeader(It.IsAny <string>(), It.IsAny <string>()), Times.Never());//.Never();
            _attribute.OnResultExecuted(executedContext);

            httpContext.Verify();
        }
Exemplo n.º 9
0
        public void HandleIfNotModified_Should_Set_NotModified_Status_Code_In_Response()
        {
            const string ETag = "abcdef";

            var httpContext = MvcTestHelper.GetHttpContext();

            httpContext.HttpRequest.ExpectGet(r => r.Headers).Returns(new NameValueCollection {
                { "If-None-Match", ETag }
            });
            httpContext.HttpResponse.ExpectSet(r => r.StatusCode);

            httpContext.Verify();
        }
Exemplo n.º 10
0
        public ShareHandlerFixture()
        {
            _httpContext = MvcTestHelper.GetHttpContext();

            _redirector      = new Mock <ISocialServiceRedirector>();
            _storyRepository = new Mock <IStoryRepository>();

            resolver.Expect(r => r.Resolve <ISocialServiceRedirector>(It.IsAny <string>())).Returns(_redirector.Object);

            _handler = new ShareHandler {
                StoryRepository = _storyRepository.Object
            };
        }
Exemplo n.º 11
0
        public void OnBeginRequest_Should_Redirect_When_Requesting_WWW_Url()
        {
            var httpContext = MvcTestHelper.GetHttpContext();

            httpContext.HttpRequest.SetupGet(r => r.Url).Returns(new Uri("http://www.dotnetshoutout.com/Upcoming"));

            httpContext.HttpResponse.SetupSet(r => r.StatusCode       = (int)HttpStatusCode.MovedPermanently).Verifiable();
            httpContext.HttpResponse.SetupSet(r => r.Status           = "301 Moved Permanently").Verifiable();
            httpContext.HttpResponse.SetupSet(r => r.RedirectLocation = "http://dotnetshoutout.com/Upcoming").Verifiable();
            httpContext.HttpResponse.Setup(r => r.End()).Verifiable();

            var module = new RemoveWwwRedirector();

            module.OnBeginRequest(httpContext.Object);

            httpContext.Verify();
        }
Exemplo n.º 12
0
        public void OnBeginRequest_Should_Redirect_WWW_Less_Url_When_Requesting_WWW_Url()
        {
            var httpContext = MvcTestHelper.GetHttpContext();

            httpContext.HttpRequest.ExpectGet(r => r.Url).Returns(new Uri("http://www.dotnetshoutout.com/Upcoming"));

            httpContext.HttpResponse.ExpectSet(r => r.StatusCode).Verifiable();
            httpContext.HttpResponse.ExpectSet(r => r.Status).Verifiable();
            httpContext.HttpResponse.ExpectSet(r => r.RedirectLocation).Verifiable();
            httpContext.HttpResponse.Expect(r => r.End()).Verifiable();

            var module = new RemoveWwwRedirector();

            module.OnBeginRequest(httpContext.Object);

            httpContext.Verify();
        }
Exemplo n.º 13
0
        public IPBlockFixture()
        {
            const string BlockedIP = "192.168.0.1";

            file.Expect(f => f.ReadAllText(It.IsAny <string>())).Returns(string.Empty);

            var collection = new Mock <IBlockedIPCollection>();

            collection.Expect(c => c.Contains(BlockedIP)).Returns(true);
            resolver.Expect(r => r.Resolve <IBlockedIPCollection>()).Returns(collection.Object);

            _httpContext = MvcTestHelper.GetHttpContext();

            _httpContext.HttpRequest.ExpectGet(r => r.UserHostAddress).Returns(BlockedIP);
            _httpContext.HttpRequest.ExpectGet(r => r.Url).Returns(new Uri("http://dotnetshoutout.com/Upcoming"));

            _module = new IPBlock();
        }
Exemplo n.º 14
0
        public void OnResultExecuted_Should_Compress_Response_When_Exception_Occurred_But_Exception_Is_Handled()
        {
            HttpContextMock       httpContext       = MvcTestHelper.GetHttpContext();
            ControllerContext     controllerContext = new ControllerContext(httpContext.Object, new RouteData(), new Mock <ControllerBase>().Object);
            ResultExecutedContext executedContext   = new ResultExecutedContext(controllerContext, new EmptyResult(), false, new Exception())
            {
                ExceptionHandled = true
            };

            httpContext.HttpRequest.Object.Headers.Add("Accept-Encoding", "gzip");
            httpContext.HttpResponse.Setup(r => r.AppendHeader(It.IsAny <string>(), It.IsAny <string>())).Verifiable();
            httpContext.HttpResponse.SetupGet(r => r.Filter).Returns(new MemoryStream());
            httpContext.HttpResponse.Setup(r => r.AppendHeader(It.IsAny <string>(), It.IsAny <string>())).Verifiable();
            httpContext.HttpResponse.SetupSet(r => r.Filter = It.IsAny <Stream>());

            _attribute.OnResultExecuted(executedContext);

            httpContext.Verify();
        }
Exemplo n.º 15
0
        public void ProcessRequest_Should_Not_Write_Assert_When_Asset_Is_Not_Modified()
        {
            var configuration = new Mock <IConfigurationManager>();

            var assetSettings = new AssetSettingsSection
            {
                CacheDurationInDays = 10,
                Version             = "1.5"
            };

            assetSettings.Assets.Add(new AssetElement {
                Name = "js", ContentType = "application/x-javascript", Directory = "~/Assets/JavaScript", Files = "Search.min.js"
            });

            var responseStream = new MemoryStream();
            var httpContext    = MvcTestHelper.GetHttpContext();

            httpContext.HttpRequest.ExpectGet(r => r.QueryString).Returns(new NameValueCollection {
                { "name", "js" }
            });
            httpContext.HttpRequest.ExpectGet(r => r.Headers).Returns(new NameValueCollection {
                { "If-None-Match", (AssetContent + "\r\n\r\n").Hash() }
            });
            httpContext.HttpResponse.ExpectGet(r => r.OutputStream).Returns(responseStream);

            configuration.Expect(c => c.GetSection <AssetSettingsSection>(It.IsAny <string>())).Returns(assetSettings);

            string content;

            cache.Expect(c => c.TryGet(It.IsAny <string>(), out content)).Returns(false);
            file.Expect(f => f.ReadAllText(It.IsAny <string>())).Returns(AssetContent);

            var handler = new AssetHandler {
                Configuration = configuration.Object, FileReader = file.Object
            };

            handler.ProcessRequest(httpContext.Object);

            Assert.True(responseStream.Length == 0);
        }
Exemplo n.º 16
0
        public void OnResultExecuted_Should_Compress_Response(string encoding)
        {
            HttpContextMock       httpContext       = MvcTestHelper.GetHttpContext();
            ControllerContext     controllerContext = new ControllerContext(httpContext.Object, new RouteData(), new Mock <ControllerBase>().Object);
            ResultExecutedContext executedContext   = new ResultExecutedContext(controllerContext, new EmptyResult(), false, null);

            httpContext.HttpRequest.Object.Headers.Add("Accept-Encoding", encoding);

            if (!string.IsNullOrEmpty(encoding))
            {
                httpContext.HttpResponse.SetupGet(r => r.Filter).Returns(new MemoryStream());
                httpContext.HttpResponse.Setup(r => r.AppendHeader(It.IsAny <string>(), It.IsAny <string>())).Verifiable();
                httpContext.HttpResponse.SetupSet(r => r.Filter = It.IsAny <Stream>());
            }

            _attribute.OnResultExecuted(executedContext);

            if (!string.IsNullOrEmpty(encoding))
            {
                httpContext.Verify();
            }
        }
Exemplo n.º 17
0
        public void ProcessRequest_Should_Write_Asset()
        {
            var configuration = new Mock <IConfigurationManager>();

            var assetSettings = new AssetSettingsSection
            {
                CacheDurationInDays = 10,
                Version             = "1.5",
                GenerateETag        = false,
                Compress            = false
            };

            assetSettings.Assets.Add(new AssetElement {
                Name = "js", ContentType = "application/x-javascript", Directory = "~/Assets/JavaScript", Files = "Search.min.js"
            });

            var responseStream = new MemoryStream();
            var httpContext    = MvcTestHelper.GetHttpContext();

            httpContext.HttpRequest.ExpectGet(r => r.QueryString).Returns(new NameValueCollection {
                { "name", "js" }
            });
            httpContext.HttpResponse.ExpectGet(r => r.OutputStream).Returns(responseStream);

            configuration.Expect(c => c.GetSection <AssetSettingsSection>(It.IsAny <string>())).Returns(assetSettings);

            string content;

            cache.Expect(c => c.TryGet(It.IsAny <string>(), out content)).Returns(false);
            file.Expect(f => f.ReadAllText(It.IsAny <string>())).Returns(AssetContent);

            var handler = new AssetHandler {
                Configuration = configuration.Object, FileReader = file.Object
            };

            handler.ProcessRequest(httpContext.Object);

            Assert.Throws <ObjectDisposedException>(() => responseStream.Seek(0, SeekOrigin.Begin));
        }
Exemplo n.º 18
0
        public void ProcessRequest_Should_Cache_Asset()
        {
            var configuration = new Mock <IConfigurationManager>();

            var assetSettings = new AssetSettingsSection
            {
                CacheDurationInDays = 10,
                Version             = "1.5"
            };

            assetSettings.Assets.Add(new AssetElement {
                Name = "js", ContentType = "application/x-javascript", Directory = "~/Assets/JavaScript", Files = "Search.min.js"
            });

            var responseStream = new MemoryStream();
            var httpContext    = MvcTestHelper.GetHttpContext();

            httpContext.HttpRequest.ExpectGet(r => r.QueryString).Returns(new NameValueCollection {
                { "name", "js" }
            });
            httpContext.HttpResponse.ExpectGet(r => r.OutputStream).Returns(responseStream);

            configuration.Expect(c => c.GetSection <AssetSettingsSection>(It.IsAny <string>())).Returns(assetSettings);

            string content;

            cache.Expect(c => c.TryGet(It.IsAny <string>(), out content)).Returns(false);
            cache.Expect(c => c.Contains(It.IsAny <string>())).Returns(false);
            cache.Expect(c => c.Set(It.IsAny <string>(), It.IsAny <HandlerCacheItem>(), It.IsAny <DateTime>())).Verifiable();
            file.Expect(f => f.ReadAllText(It.IsAny <string>())).Returns(AssetContent);

            var handler = new AssetHandler {
                Configuration = configuration.Object, FileReader = file.Object
            };

            handler.ProcessRequest(httpContext.Object);

            cache.Verify();
        }
Exemplo n.º 19
0
        public void CreateUrlHelper_Should_Return_New_UrlHelper()
        {
            var httpContext = MvcTestHelper.GetHttpContext();

            Assert.NotNull(new BaseHandlerTestDouble().CreateUrlHelperForTest(httpContext.Object));
        }
Exemplo n.º 20
0
        public SiteMapHandlerFixture()
        {
            RouteTable.Routes.Clear();
            new RegisterRoutes(settings.Object).Execute();

            var userRepository     = new Mock <IUserRepository>();
            var storyRepository    = new Mock <IStoryRepository>();
            var categoryRepository = new Mock <ICategoryRepository>();
            var tagRepository      = new Mock <ITagRepository>();

            List <IUser> topMovers = new List <IUser>();

            for (int i = 1; i <= settings.Object.TopUsers; i++)
            {
                var user = new Mock <IUser>();

                user.ExpectGet(u => u.Id).Returns(Guid.NewGuid());
                user.ExpectGet(u => u.UserName).Returns("Top Mover {0}".FormatWith(i));

                topMovers.Add(user.Object);
            }

            userRepository.Expect(r => r.FindTop(It.IsAny <DateTime>(), It.IsAny <DateTime>(), It.IsAny <int>(), It.IsAny <int>())).Returns(new PagedResult <IUser>(topMovers, settings.Object.TopUsers));

            Func <string, Mock <IStory> > createStory = name =>
            {
                var category = new Mock <ICategory>();
                category.ExpectGet(c => c.Name).Returns("Dummy");

                var tag = new Mock <ITag>();
                tag.ExpectGet(t => t.Name).Returns("Dummy");

                var story = new Mock <IStory>();
                story.ExpectGet(s => s.BelongsTo).Returns(category.Object);
                story.ExpectGet(s => s.UniqueName).Returns(name);
                story.ExpectGet(s => s.Tags).Returns(new List <ITag> {
                    tag.Object
                });
                story.ExpectGet(s => s.TagCount).Returns(1);

                return(story);
            };

            List <IStory> publishedStories = new List <IStory>();

            for (int i = 1; i <= 50; i++)
            {
                var story = createStory("Published Story {0}".FormatWith(i));

                story.ExpectGet(s => s.PublishedAt).Returns(Constants.ProductionDate);

                publishedStories.Add(story.Object);
            }

            List <IStory> upcomingStories = new List <IStory>();

            for (int i = 1; i <= 50; i++)
            {
                var story = createStory("Upcoming Story {0}".FormatWith(i));

                upcomingStories.Add(story.Object);
            }

            storyRepository.Expect(r => r.FindPublished(It.IsAny <int>(), It.IsAny <int>())).Returns(new PagedResult <IStory>(publishedStories, 1000));
            storyRepository.Expect(r => r.FindUpcoming(It.IsAny <int>(), It.IsAny <int>())).Returns(new PagedResult <IStory>(upcomingStories, 1000));

            storyRepository.Expect(r => r.CountByPublished()).Returns(300);
            storyRepository.Expect(r => r.CountByUpcoming()).Returns(100);

            List <ICategory> categories = new List <ICategory>();

            for (int i = 1; i <= 10; i++)
            {
                var category = new Mock <ICategory>();

                category.ExpectGet(c => c.UniqueName).Returns("Category {0}".FormatWith(i));

                categories.Add(category.Object);
            }

            categoryRepository.Expect(r => r.FindAll()).Returns(new ReadOnlyCollection <ICategory>(categories));
            storyRepository.Expect(r => r.CountByCategory(It.IsAny <Guid>())).Returns(5);

            List <ITag> tags = new List <ITag>();

            for (int i = 1; i <= settings.Object.TopTags; i++)
            {
                var tag = new Mock <ITag>();

                tag.ExpectGet(t => t.UniqueName).Returns("Tag {0}".FormatWith(i));

                tags.Add(tag.Object);
            }

            tagRepository.Expect(r => r.FindByUsage(It.IsAny <int>())).Returns(new ReadOnlyCollection <ITag>(tags));
            storyRepository.Expect(r => r.CountByTag(It.IsAny <Guid>())).Returns(5);

            _httpContext = MvcTestHelper.GetHttpContext("/Kigg", null, null);

            _handler = new SiteMapHandler
            {
                Settings               = settings.Object,
                UserRepository         = userRepository.Object,
                CategoryRepository     = categoryRepository.Object,
                TagRepository          = tagRepository.Object,
                StoryRepository        = storyRepository.Object,
                PublishedStoryMaxCount = 5,
                UpcomingStoryMaxCount  = 5,
                CacheDurationInMinutes = 45,
                Compress               = true,
                GenerateETag           = true
            };
        }
Exemplo n.º 21
0
        private RouteData GetRouteDataFor(string url, bool forPost)
        {
            var context = MvcTestHelper.GetHttpContext("/", url, forPost ? "POST" : null);

            return(_routes.GetRouteData(context.Object));
        }
 public HtmlHelperExtensionFixture()
 {
     _httpContext = MvcTestHelper.GetHttpContext(AppPath, null, null);
 }
Exemplo n.º 23
0
        public void HandleIfNotModified_Should_Return_False_When_ETag_Is_Blank()
        {
            var httpContext = MvcTestHelper.GetHttpContext();

            Assert.False(BaseHandler.HandleIfNotModified(httpContext.Object, string.Empty));
        }
Exemplo n.º 24
0
 public ErrorHandlerFixture()
 {
     _httpContext = MvcTestHelper.GetHttpContext();
     _handler     = new ErrorHandler();
 }
Exemplo n.º 25
0
 public BaseHttpModuleFixture()
 {
     _httpContext = MvcTestHelper.GetHttpContext();
     _module      = new BaseHttpModuleTestDouble();
 }
        public UrlHelperExtensionFixture()
        {
            var httpContext = MvcTestHelper.GetHttpContext("/Kigg", null, null);

            _urlHelper = new UrlHelper(new RequestContext(httpContext.Object, new RouteData()), new RouteCollection());
        }