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); } } }
public async Task OnGetLiveAsyncTest() { //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>()); }); var environment_mock = new Moq.Mock <IHostingEnvironment>(); environment_mock.SetupGet(m => m.WebRootPath).Returns(() => { return(Path.Combine(ContentRoot, "wwwroot")); }); //MjpgStreamerHttpClientHandler.AddressWithProxy = "https://127.0.0.1/webcamgalleryFake/Fakelive"; using (var dummy_just_for_costructor = new MjpgStreamerHttpClientHandler(Configuration)) using (var cache = new MemoryCache(new MemoryCacheOptions())) using (var http_client_mock = new ImageMockHttpClient().GetMockClient(Configuration)) { var client = new MjpgStreamerHttpClient(http_client_mock, environment_mock.Object, cache, dummy_just_for_costructor); WebCamImagesModel wcim = new WebCamImagesModel() { PageContext = this.LocalPageContext }; //Act var result = await wcim.OnGetLiveAsync(serverTiming_mock.Object, client); //Assert if (!string.IsNullOrEmpty(Configuration["LiveWebCamURL"])) { Assert.NotNull(result); Assert.IsType <FileContentResult>(result); Assert.True(MediaTypeNames.Image.Jpeg == ((FileContentResult)result).ContentType || "image/png" == ((FileContentResult)result).ContentType); Assert.NotNull(((FileContentResult)result).LastModified); } else { Assert.NotNull(result); Assert.IsType <FileContentResult>(result); Assert.True(MediaTypeNames.Image.Jpeg == ((FileContentResult)result).ContentType || "image/png" == ((FileContentResult)result).ContentType); Assert.NotNull(((FileContentResult)result).LastModified); } //MjpgStreamerHttpClientHandler.AddressWithProxy = null; } }
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); } }