Skip to content
This repository was archived by the owner on Nov 21, 2018. It is now read-only.

Add test for DI scope validation defaults #76

Merged
merged 5 commits into from
May 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion MetaPackages.sln
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,30 @@ await ExecuteTestApp(applicationName, async (deploymentResult, logger) =>
}, 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]")]
Expand Down Expand Up @@ -247,7 +271,10 @@ await ExecuteTestApp(applicationName, async (deploymentResult, logger) =>
});
}

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))
{
Expand All @@ -256,7 +283,7 @@ private async Task ExecuteTestApp(string applicationName, Func<DeploymentResult,

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"));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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>
67 changes: 67 additions & 0 deletions test/TestSites/DependencyInjectionApp/Program.cs
Original file line number Diff line number Diff line change
@@ -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
{
}
}
}