public IActionResult GetCover() { if (!ITunesService.Instance.IsActive) { return(new StatusCodeResult((int)HttpStatusCode.ServiceUnavailable)); } var track = ITunesService.Instance.GetCurrenTrack(); FileStream image; try { image = ControllerServices.GetCoverForTrack(track); } catch (Exception e) { Console.WriteLine("Could not get cover for track"); Console.WriteLine(e); return(NotFound(e)); } if (image == null) { return(NotFound(track)); } return(File(image, "image/" + track.GetArtworkFileExtension())); }
public void Controller_Overrides_DependencyInjection() { // Setting on Controller config overrides the DI container. HttpConfiguration config = new HttpConfiguration(); IActionValueBinder newDIService = new Mock <IActionValueBinder>().Object; var mockDependencyResolver = new Mock <IDependencyResolver>(); mockDependencyResolver.Setup(dr => dr.GetService(typeof(IActionValueBinder))).Returns(newDIService); config.DependencyResolver = mockDependencyResolver.Object; ControllerServices cs = new ControllerServices(config.Services); IActionValueBinder newLocalService = new Mock <IActionValueBinder>().Object; cs.Replace(typeof(IActionValueBinder), newLocalService); // Act IActionValueBinder localVal = (IActionValueBinder)cs.GetService(typeof(IActionValueBinder)); IActionValueBinder globalVal = (IActionValueBinder)config.Services.GetService(typeof(IActionValueBinder)); // Assert // Local controller didn't override, should get same value as global case. Assert.Same(newDIService, globalVal); // asking the config will give back the DI service Assert.Same(newLocalService, localVal); // but asking locally will get back the local service. }
public void Get_Falls_Through_To_Global() { HttpConfiguration config = new HttpConfiguration(); ControllerServices cs = new ControllerServices(config.Services); // Act IActionValueBinder localVal = (IActionValueBinder)cs.GetService(typeof(IActionValueBinder)); IActionValueBinder globalVal = (IActionValueBinder)config.Services.GetService(typeof(IActionValueBinder)); // Assert // Local controller didn't override, should get same value as global case. Assert.Same(localVal, globalVal); }
public IActionResult GetPlaylistCoverByFolderAndIndex(string folderName, int index) { if (!ITunesService.Instance.IsActive) { return(new StatusCodeResult((int)HttpStatusCode.ServiceUnavailable)); } var requestedFolder = SearchFolder(folderName); if (requestedFolder == null) { return(NotFound(folderName)); } if (requestedFolder.Nodes < index) { return(NotFound(new IntegerResponse() { Name = "index out of bounds", Data = index })); } var reqestedPlaylist = requestedFolder.PlaylistNodes[index].Playlist; if (reqestedPlaylist.Songs == 0) { throw new Exception($"The requested playlist with name {reqestedPlaylist.Name} does not contain any songs."); } //var requestedPlaylistFirstTrack = requestedFolder.PlaylistNodes[index].Playlist.TrackList[0]; FileStream image; try { image = ControllerServices.GetCoverForPlaylist(reqestedPlaylist); } catch (Exception e) { Console.WriteLine("Could not artwork"); Console.WriteLine(e); return(NotFound(e)); } if (image == null) { return(NotFound(reqestedPlaylist)); } return(File(image, "image/jpeg")); }
public void Controller_Set_Null() { HttpConfiguration config = new HttpConfiguration(); ServicesContainer global = config.Services; ControllerServices cs = new ControllerServices(config.Services); // Act // Setting to null is not the same as clear. Clear() means fall through to global config. cs.Replace(typeof(IActionValueBinder), null); // Assert IActionValueBinder localVal = (IActionValueBinder)cs.GetService(typeof(IActionValueBinder)); Assert.Null(localVal); }
public void Controller_Overrides_Global() { HttpConfiguration config = new HttpConfiguration(); ControllerServices cs = new ControllerServices(config.Services); IActionValueBinder newLocalService = new Mock <IActionValueBinder>().Object; cs.Replace(typeof(IActionValueBinder), newLocalService); // Act IActionValueBinder localVal = (IActionValueBinder)cs.GetService(typeof(IActionValueBinder)); IActionValueBinder globalVal = (IActionValueBinder)config.Services.GetService(typeof(IActionValueBinder)); // Assert // Local controller didn't override, should get same value as global case. Assert.Same(localVal, newLocalService); Assert.NotSame(localVal, globalVal); }
public void Controller_Clear_Single_Item() { HttpConfiguration config = new HttpConfiguration(); ServicesContainer global = config.Services; ControllerServices cs = new ControllerServices(config.Services); IActionValueBinder newLocalService = new Mock <IActionValueBinder>().Object; cs.Replace(typeof(IActionValueBinder), newLocalService); // Act cs.Clear(typeof(IActionValueBinder)); // Assert IActionValueBinder localVal = (IActionValueBinder)cs.GetService(typeof(IActionValueBinder)); IActionValueBinder globalVal = (IActionValueBinder)config.Services.GetService(typeof(IActionValueBinder)); Assert.Same(globalVal, localVal); }
private void Controller_Overrides_DependencyInjection_Impl(IDependencyResolver resolver, TestActionValueBinder newDIService) { if (resolver is null) { throw new ArgumentNullException(nameof(resolver)); } // Calling `.BeginScope()` here is to simulate what happens inside `DefaultHttpControllerActivator.GetInstanceOrActivator` using (IDependencyScope requestScope = resolver.BeginScope()) { _ = requestScope.ShouldNotBeNull(); // Setting on Controller config overrides the DI container. HttpConfiguration config = new HttpConfiguration(); // IActionValueBinder newDIService = new TestActionValueBinder(); config.DependencyResolver = resolver; ControllerServices cs = new ControllerServices(config.Services); IActionValueBinder newLocalService = new TestActionValueBinder(); cs.Replace(typeof(IActionValueBinder), newLocalService); // Act IActionValueBinder localVal = (IActionValueBinder)cs.GetService(typeof(IActionValueBinder)); IActionValueBinder globalVal = (IActionValueBinder)config.Services.GetService(typeof(IActionValueBinder)); _ = localVal.ShouldNotBeNull().ShouldBeOfType <TestActionValueBinder>(); _ = globalVal.ShouldNotBeNull().ShouldBeOfType <TestActionValueBinder>(); // Assert // Local controller didn't override, should get same value as global case. Object.ReferenceEquals(newDIService, globalVal).ShouldBeTrue(); // asking the config will give back the DI service Object.ReferenceEquals(newLocalService, localVal).ShouldBeTrue(); // but asking locally will get back the local service. } }
public void Controller_Appends_Inherited_List() { // Controller Services has "copy on write" semantics for inherited list. // It can get the inherited list and mutate it. HttpConfiguration config = new HttpConfiguration(); ServicesContainer global = config.Services; ControllerServices cs = new ControllerServices(config.Services); ValueProviderFactory vpf = new Mock <ValueProviderFactory>().Object; // Act cs.Add(typeof(ValueProviderFactory), vpf); // appends to end // Assert IEnumerable <object> original = global.GetServices(typeof(ValueProviderFactory)); object[] modified = cs.GetServices(typeof(ValueProviderFactory)).ToArray(); Assert.True(original.Count() > 1); object[] expected = original.Concat(new object[] { vpf }).ToArray(); Assert.Equal(expected, modified); }
public void Call_With_Null() { ControllerServices sc = null; Assert.ThrowsArgumentNull(() => { sc.GetValueProviderFactories(); }, "services"); }
public StudiTrainController(IAppSettings settings) { DbConn = new PostgresContext(settings.ControllerSetup.ConnectionString); Services = new ControllerServices(DbConn, settings.JwtSetup); }