// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Collections.Generic; using System.IO; using System.Threading.Tasks; using Microsoft.AspNet.Builder; using Microsoft.AspNet.FeatureModel; using Microsoft.AspNet.Hosting.Server; using Microsoft.Framework.ConfigurationModel; using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.DependencyInjection.Fallback; using Xunit; namespace Microsoft.AspNet.Hosting { public class HostingEngineTests : IServerFactory { private readonly IList _startInstances = new List(); [Fact] public void HostingEngineCanBeResolvedWithDefaultServices() { var services = HostingServices.Create().BuildServiceProvider(); var engine = services.GetRequiredService(); Assert.NotNull(engine); } [Fact] public void HostingEngineCanBeStarted() { var services = HostingServices.Create().BuildServiceProvider(); var engine = services.GetRequiredService(); var applicationLifetime = services.GetRequiredService(); var context = new HostingContext { ApplicationLifetime = applicationLifetime, ServerFactory = this, ApplicationName = "Microsoft.AspNet.Hosting.Tests" }; var engineStart = engine.Start(context); Assert.NotNull(engineStart); Assert.Equal(1, _startInstances.Count); Assert.Equal(0, _startInstances[0].DisposeCalls); engineStart.Dispose(); Assert.Equal(1, _startInstances[0].DisposeCalls); } [Fact] public void WebRootCanBeResolvedFromTheProjectJson() { var services = HostingServices.Create().BuildServiceProvider(); var env = services.GetRequiredService(); Assert.Equal(Path.GetFullPath("testroot"), env.WebRootPath); Assert.True(env.WebRootFileProvider.GetFileInfo("TextFile.txt").Exists); } public void Initialize(IApplicationBuilder builder) { } public IServerInformation Initialize(IConfiguration configuration) { return null; } public IDisposable Start(IServerInformation serverInformation, Func application) { var startInstance = new StartInstance(application); _startInstances.Add(startInstance); return startInstance; } public class StartInstance : IDisposable { private readonly Func _application; public StartInstance(Func application) { _application = application; } public int DisposeCalls { get; set; } public void Dispose() { DisposeCalls += 1; } } } }