コード例 #1
0
        public void OnImageGetTest(string imageName)
        {
            //Arrange
            var serverTiming_mock = new Moq.Mock <Lib.AspNetCore.ServerTiming.IServerTiming>();

            serverTiming_mock.SetupGet(m => m.Metrics).Returns(() =>
            {
                return(new List <Lib.AspNetCore.ServerTiming.Http.Headers.ServerTimingMetric>());
            });
            WebCamImagesModel wcim = new WebCamImagesModel()
            {
                PageContext = this.LocalPageContext
            };

            //Act
            var result = wcim.OnGet(base.Configuration, serverTiming_mock.Object, imageName);

            //Assert
            if (!string.IsNullOrEmpty(Configuration["ImageDirectory"]))
            {
                Assert.NotNull(result);
                Assert.IsType <PhysicalFileResult>(result);
                Assert.Equal(MediaTypeNames.Image.Jpeg, ((PhysicalFileResult)result).ContentType);
                Assert.NotNull(((PhysicalFileResult)result).EntityTag);
                //Assert.NotNull(((PhysicalFileResult)result).LastModified);
            }


            //test strong caching with ETAG and date tag checking
            if (!string.IsNullOrEmpty(Configuration["ImageDirectory"]))
            {
                //Arrange
                var            fi       = new FileInfo(Path.Combine(Configuration["ImageDirectory"], imageName));
                DateTimeOffset last     = fi.LastWriteTime;
                long           etagHash = new DateTimeOffset(last.Year, last.Month, last.Day, last.Hour, last.Minute, last.Second, last.Offset)
                                          .ToUniversalTime().ToFileTime() ^ fi.Length;
                var etag_str = '\"' + Convert.ToString(etagHash, 16) + '\"';
                wcim.Request.Headers.Add(HeaderNames.IfNoneMatch, new StringValues(etag_str));

                //Act
                result = wcim.OnGet(base.Configuration, serverTiming_mock.Object, imageName);

                //Assert
                Assert.NotNull(result);
                Assert.IsType <StatusCodeResult>(result);
                Assert.Equal((int)HttpStatusCode.NotModified, ((StatusCodeResult)result).StatusCode);
            }
        }
コード例 #2
0
        public void On_NonExisting_ImageGetTest(params string[] badImageNames)
        {
            //Arrange
            var serverTiming_mock = new Moq.Mock <Lib.AspNetCore.ServerTiming.IServerTiming>();

            serverTiming_mock.SetupGet(m => m.Metrics).Returns(() =>
            {
                return(new List <Lib.AspNetCore.ServerTiming.Http.Headers.ServerTimingMetric>());
            });
            WebCamImagesModel wcim = new WebCamImagesModel()
            {
                PageContext = this.LocalPageContext
            };

            //Act
            foreach (var image_name in badImageNames)
            {
                var result = wcim.OnGet(base.Configuration, serverTiming_mock.Object, image_name);

                //Assert
                Assert.NotNull(result);
                if (!string.IsNullOrEmpty(Configuration["ImageDirectory"]))
                {
                    Assert.IsType <NotFoundResult>(result);
                }
                else
                {
                    Assert.IsType <NotFoundObjectResult>(result);
                }
            }
        }