PageRenderTime 65ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/CTP/Source/Telerik.Web.Mvc.UI.UnitTest/SiteMap/SiteMapHandlerTests.cs

#
C# | 136 lines | 107 code | 29 blank | 0 comment | 0 complexity | 0a43f1187102f8dcc9fb9331b9512200 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. namespace Telerik.Web.Mvc.UnitTest
  2. {
  3. using System;
  4. using System.IO;
  5. using System.Web;
  6. using System.Web.Routing;
  7. using Moq;
  8. using Xunit;
  9. public class SiteMapHandlerTests
  10. {
  11. private readonly SiteMapDictionary _siteMaps;
  12. private readonly Mock<IHttpResponseCompressor> _compressor;
  13. private readonly Mock<IHttpResponseCacher> _cacher;
  14. private readonly Mock<IUrlGenerator> _urlGenerator;
  15. private readonly Mock<HttpContextBase> _httpContext;
  16. private readonly SiteMapHandler _handler;
  17. public SiteMapHandlerTests()
  18. {
  19. _siteMaps = new SiteMapDictionary();
  20. _compressor = new Mock<IHttpResponseCompressor>();
  21. _cacher = new Mock<IHttpResponseCacher>();
  22. _urlGenerator = new Mock<IUrlGenerator>();
  23. _httpContext = TestHelper.CreateMockedHttpContext();
  24. _handler = new SiteMapHandler(_siteMaps, _compressor.Object, _cacher.Object, _urlGenerator.Object);
  25. }
  26. [Fact]
  27. public void Default_constructor_should_not_throw_exception()
  28. {
  29. Assert.DoesNotThrow(() => new SiteMapHandler());
  30. }
  31. [Fact]
  32. public void Should_be_able_to_set_default_path()
  33. {
  34. SiteMapHandler.DefaultPath = "~/handlers/sitemap.axd";
  35. Assert.Equal("~/handlers/sitemap.axd", SiteMapHandler.DefaultPath);
  36. SiteMapHandler.DefaultPath = "~/sitemap.axd";
  37. }
  38. [Fact]
  39. public void ProcessRequest_should_write_site_map()
  40. {
  41. Mock<Stream> outputStream = new Mock<Stream>();
  42. Process(outputStream);
  43. outputStream.Verify();
  44. }
  45. [Fact]
  46. public void ProcessRequest_should_compress_response()
  47. {
  48. Process(new Mock<Stream>());
  49. _compressor.Verify();
  50. }
  51. [Fact]
  52. public void ProcessRequest_should_cache_response()
  53. {
  54. Process(new Mock<Stream>());
  55. _cacher.Verify();
  56. }
  57. [Fact]
  58. public void ProcessRequest_should_generate_url()
  59. {
  60. Process(new Mock<Stream>());
  61. _urlGenerator.Verify();
  62. }
  63. private void Process(Mock<Stream> outputStream)
  64. {
  65. TestHelper.RegisterDummyRoutes();
  66. _siteMaps.Register<XmlSiteMap>("x", siteMap =>
  67. {
  68. SiteMapNodeBuilder rootNode = siteMap.RootNode;
  69. rootNode.Title("Home")
  70. .Route("Default")
  71. .LastModifiedAt(DateTime.UtcNow)
  72. .ChangeFrequency(SiteMapChangeFrequency.Hourly)
  73. .UpdatePriority(SiteMapUpdatePriority.Critical)
  74. .ChildNodes(node =>
  75. {
  76. node.Add()
  77. .Title("Products")
  78. .Route("ProductList")
  79. .ChildNodes(childNode =>
  80. {
  81. childNode.Add()
  82. .Title("Product1")
  83. .Action("Product", "Detail", new { id = 1 });
  84. childNode.Add()
  85. .Title("Product2")
  86. .Action("Product", "Detail", new { id = 2 });
  87. }
  88. );
  89. node.Add()
  90. .Title("Faq")
  91. .Url("~Faq");
  92. });
  93. }
  94. );
  95. _httpContext.Setup(context => context.Response.OutputStream).Returns(outputStream.Object);
  96. _compressor.Setup(compressor => compressor.Compress(_httpContext.Object)).Verifiable();
  97. _cacher.Setup(cacher => cacher.Cache(_httpContext.Object, It.IsAny<TimeSpan>())).Verifiable();
  98. _urlGenerator.Setup(generater => generater.Generate(It.IsAny<RequestContext>(), It.IsAny<INavigationItem>())).Returns("http://localhost/Griffin/Home").Verifiable();
  99. outputStream.SetupGet(stream => stream.CanRead).Returns(true);
  100. outputStream.SetupGet(stream => stream.CanWrite).Returns(true);
  101. outputStream.SetupGet(stream => stream.CanSeek).Returns(true);
  102. outputStream.Setup(stream => stream.Write(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>())).Verifiable();
  103. _httpContext.Object.Request.QueryString.Add("name", "x");
  104. _handler.ProcessRequest(_httpContext.Object);
  105. }
  106. }
  107. }