Add test for DI scope validation defaults (#76)

This commit is contained in:
Pavel Krymets 2017-05-08 10:16:43 -07:00 committed by GitHub
parent 906f1d8f3e
commit dd19150ea4
4 changed files with 117 additions and 3 deletions

View File

@ -1,6 +1,6 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26424.2
VisualStudioVersion = 15.0.26412.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{ED834E68-51C3-4ADE-ACC8-6BA6D4207C09}"
EndProject
@ -44,6 +44,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.DotNet.Archive",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TrimDeps", "tools\TrimDeps\TrimDeps.csproj", "{67E4C92F-6D12-4C52-BB79-B8D11BFC6B82}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DependencyInjectionApp", "test\TestSites\DependencyInjectionApp\DependencyInjectionApp.csproj", "{65FE2E38-4529-4C93-A7B0-CF12DD7A70C3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -94,6 +96,10 @@ Global
{67E4C92F-6D12-4C52-BB79-B8D11BFC6B82}.Debug|Any CPU.Build.0 = Debug|Any CPU
{67E4C92F-6D12-4C52-BB79-B8D11BFC6B82}.Release|Any CPU.ActiveCfg = Release|Any CPU
{67E4C92F-6D12-4C52-BB79-B8D11BFC6B82}.Release|Any CPU.Build.0 = Release|Any CPU
{65FE2E38-4529-4C93-A7B0-CF12DD7A70C3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{65FE2E38-4529-4C93-A7B0-CF12DD7A70C3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{65FE2E38-4529-4C93-A7B0-CF12DD7A70C3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{65FE2E38-4529-4C93-A7B0-CF12DD7A70C3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -112,5 +118,7 @@ Global
{AE4216BF-D471-471B-82F3-6B6D004F7D17} = {ED834E68-51C3-4ADE-ACC8-6BA6D4207C09}
{302400A0-98BB-4C04-88D4-C32DC2D4B945} = {ED834E68-51C3-4ADE-ACC8-6BA6D4207C09}
{67E4C92F-6D12-4C52-BB79-B8D11BFC6B82} = {ED834E68-51C3-4ADE-ACC8-6BA6D4207C09}
{302400A0-98BB-4C04-88D4-C32DC2D4B945} = {ED834E68-51C3-4ADE-ACC8-6BA6D4207C09}
{65FE2E38-4529-4C93-A7B0-CF12DD7A70C3} = {EC22261D-0DE1-47DE-8F7C-072675D6F5B4}
EndGlobalSection
EndGlobal

View File

@ -73,6 +73,30 @@ namespace Microsoft.AspNetCore.Tests
}, setTestEnvVars: true);
}
[Theory]
[InlineData("Development", "InvalidOperationException: Cannot consume scoped service")]
[InlineData("Production", "Success")]
public async Task CreateDefaultBuilder_InitializesDependencyInjectionSettingsBasedOnEnv(string environment, string expected)
{
var applicationName = "DependencyInjectionApp";
await ExecuteTestApp(applicationName, async (deploymentResult, logger) =>
{
var response = await RetryHelper.RetryRequest(() => deploymentResult.HttpClient.GetAsync(string.Empty), logger, deploymentResult.HostShutdownToken);
var responseText = await response.Content.ReadAsStringAsync();
try
{
// Assert UseDeveloperExceptionPage is called in WebHostStartupFilter.
Assert.Contains(expected, responseText);
}
catch (XunitException)
{
logger.LogWarning(response.ToString());
logger.LogWarning(responseText);
throw;
}
}, setTestEnvVars: true, environment: environment);
}
[Theory]
[InlineData("127.0.0.1", "127.0.0.1")]
[InlineData("::1", "[::1]")]
@ -247,7 +271,10 @@ namespace Microsoft.AspNetCore.Tests
});
}
private async Task ExecuteTestApp(string applicationName, Func<DeploymentResult, ILogger, Task> assertAction, bool setTestEnvVars = false)
private async Task ExecuteTestApp(string applicationName,
Func<DeploymentResult, ILogger, Task> assertAction,
bool setTestEnvVars = false,
string environment = "Development")
{
using (StartLog(out var loggerFactory, applicationName))
{
@ -256,7 +283,7 @@ namespace Microsoft.AspNetCore.Tests
if (setTestEnvVars)
{
deploymentParameters.EnvironmentVariables.Add(new KeyValuePair<string, string>("aspnetcore_environment", "Development"));
deploymentParameters.EnvironmentVariables.Add(new KeyValuePair<string, string>("aspnetcore_environment", environment));
deploymentParameters.EnvironmentVariables.Add(new KeyValuePair<string, string>("envKey", "envValue"));
}

View File

@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\src\Microsoft.AspNetCore\Microsoft.AspNetCore.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,67 @@
// Copyright (c) .NET Foundation. 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.Linq;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Logging.Console;
using Microsoft.Extensions.Logging.Debug;
namespace CreateDefaultBuilderApp
{
public class Program
{
static void Main(string[] args)
{
WebHost.CreateDefaultBuilder()
.ConfigureServices((context, services) =>
{
services.AddSingleton(typeof(IService<>), typeof(Service<>));
services.AddScoped<IAnotherService, AnotherService>();
})
.Configure(app =>
{
app.Run(context =>
{
try
{
context.RequestServices.GetService<IService<IAnotherService>>();
return context.Response.WriteAsync("Success");
}
catch (Exception ex)
{
return context.Response.WriteAsync(ex.ToString());
}
});
})
.Build().Run();
}
interface IService<T>
{
}
interface IAnotherService
{
}
class Service<T>: IService<T>
{
public Service(T t)
{
}
}
class AnotherService: IAnotherService
{
}
}
}