* Add functional tests for samples

This commit is contained in:
ryanbrandenburg 2016-01-29 14:47:16 -08:00
parent 4e5c99fabc
commit 6411fdcf29
14 changed files with 471 additions and 3 deletions

View File

@ -1,7 +1,6 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.23107.0
VisualStudioVersion = 14.0.24720.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{509A6F36-AD80-4A18-B5B1-717D38DFF29D}"
EndProject
@ -50,6 +49,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "MiddlewareAnalysisSample",
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNetCore.MiddlewareAnalysis.Tests", "test\Microsoft.AspNetCore.MiddlewareAnalysis.Tests\Microsoft.AspNetCore.MiddlewareAnalysis.Tests.xproj", "{B0166AED-738F-42EE-AF4D-C487C8B21521}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNetCore.Diagnostics.FunctionalTests", "test\Microsoft.AspNetCore.Diagnostics.FunctionalTests\Microsoft.AspNetCore.Diagnostics.FunctionalTests.xproj", "{BFDFE1E3-A6A9-4DAB-8939-1DA306FC69C8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -262,6 +263,18 @@ Global
{B0166AED-738F-42EE-AF4D-C487C8B21521}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{B0166AED-738F-42EE-AF4D-C487C8B21521}.Release|x86.ActiveCfg = Release|Any CPU
{B0166AED-738F-42EE-AF4D-C487C8B21521}.Release|x86.Build.0 = Release|Any CPU
{BFDFE1E3-A6A9-4DAB-8939-1DA306FC69C8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BFDFE1E3-A6A9-4DAB-8939-1DA306FC69C8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BFDFE1E3-A6A9-4DAB-8939-1DA306FC69C8}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{BFDFE1E3-A6A9-4DAB-8939-1DA306FC69C8}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{BFDFE1E3-A6A9-4DAB-8939-1DA306FC69C8}.Debug|x86.ActiveCfg = Debug|Any CPU
{BFDFE1E3-A6A9-4DAB-8939-1DA306FC69C8}.Debug|x86.Build.0 = Debug|Any CPU
{BFDFE1E3-A6A9-4DAB-8939-1DA306FC69C8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BFDFE1E3-A6A9-4DAB-8939-1DA306FC69C8}.Release|Any CPU.Build.0 = Release|Any CPU
{BFDFE1E3-A6A9-4DAB-8939-1DA306FC69C8}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{BFDFE1E3-A6A9-4DAB-8939-1DA306FC69C8}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{BFDFE1E3-A6A9-4DAB-8939-1DA306FC69C8}.Release|x86.ActiveCfg = Release|Any CPU
{BFDFE1E3-A6A9-4DAB-8939-1DA306FC69C8}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -285,5 +298,6 @@ Global
{C2DEDD37-D1EB-4819-9A9C-475E37A52358} = {509A6F36-AD80-4A18-B5B1-717D38DFF29D}
{95981429-F212-44E1-83AA-32DA9E2A517C} = {ACAA0157-A8C4-4152-93DE-90CCDF304087}
{B0166AED-738F-42EE-AF4D-C487C8B21521} = {2AF90579-B118-4583-AE88-672EFACB5BC4}
{BFDFE1E3-A6A9-4DAB-8939-1DA306FC69C8} = {2AF90579-B118-4583-AE88-672EFACB5BC4}
EndGlobalSection
EndGlobal

View File

@ -1,3 +1,3 @@
{
"projects": ["src"]
"projects": [ "src", "samples" ]
}

View File

@ -0,0 +1,35 @@
// 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.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Xunit;
namespace Microsoft.AspNetCore.Diagnostics.FunctionalTests
{
public class DatabaseErrorPageSampleTest : IClassFixture<TestFixture<DatabaseErrorPageSample.Startup>>
{
public DatabaseErrorPageSampleTest(TestFixture<DatabaseErrorPageSample.Startup> fixture)
{
Client = fixture.Client;
}
public HttpClient Client { get; }
[Fact]
public async Task DatabaseErrorPage_ShowsError()
{
// Arrange
var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/");
// Act
var response = await Client.SendAsync(request);
// Assert
var body = await response.Content.ReadAsStringAsync();
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
Assert.Contains("From the command line, scaffold a new migration and apply it to the database:", body);
}
}
}

View File

@ -0,0 +1,36 @@
// 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.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Xunit;
namespace Microsoft.AspNetCore.Diagnostics.FunctionalTests
{
public class DeveloperExceptionPageSampleTest : IClassFixture<TestFixture<DeveloperExceptionPageSample.Startup>>
{
public DeveloperExceptionPageSampleTest(TestFixture<DeveloperExceptionPageSample.Startup> fixture)
{
Client = fixture.Client;
}
public HttpClient Client { get; }
[Fact]
public async Task DeveloperExceptionPage_ShowsError()
{
// Arrange
var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/");
// Act
var response = await Client.SendAsync(request);
// Assert
var body = await response.Content.ReadAsStringAsync();
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
Assert.Contains("Exception: Demonstration exception.", body);
}
}
}

View File

@ -0,0 +1,35 @@
// 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.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Xunit;
namespace Microsoft.AspNetCore.Diagnostics.FunctionalTests
{
public class ElmPageSampleTest : IClassFixture<TestFixture<ElmPageSample.Startup>>
{
public ElmPageSampleTest(TestFixture<ElmPageSample.Startup> fixture)
{
Client = fixture.Client;
}
public HttpClient Client { get; }
[Fact]
public async Task ElmPagePage_ShowsError()
{
// Arrange
var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/throws");
// Act
var response = await Client.SendAsync(request);
// Assert
var body = await response.Content.ReadAsStringAsync();
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Contains("Elm Logs", body);
}
}
}

View File

@ -0,0 +1,35 @@
// 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.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Xunit;
namespace Microsoft.AspNetCore.Diagnostics.FunctionalTests
{
public class ExceptionHandlerSampleTest : IClassFixture<TestFixture<ExceptionHandlerSample.Startup>>
{
public ExceptionHandlerSampleTest(TestFixture<ExceptionHandlerSample.Startup> fixture)
{
Client = fixture.Client;
}
public HttpClient Client { get; }
[Fact]
public async Task ExceptionHandlerPage_ShowsError()
{
// Arrange
var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/throw");
// Act
var response = await Client.SendAsync(request);
// Assert
var body = await response.Content.ReadAsStringAsync();
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
Assert.Contains("we encountered an un-expected issue with your application.", body);
}
}
}

View File

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>bfdfe1e3-a6a9-4dab-8939-1da306fc69c8</ProjectGuid>
<RootNamespace>Microsoft.AspNetCore.Diagnostics.FunctionalTests</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<ItemGroup>
<DnxInvisibleContent Include="bower.json" />
<DnxInvisibleContent Include=".bowerrc" />
<DnxInvisibleContent Include="package.json" />
</ItemGroup>
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>

View File

@ -0,0 +1,34 @@
// 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.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Xunit;
namespace Microsoft.AspNetCore.Diagnostics.FunctionalTests
{
public class MiddlewareAnalysisSampleTest : IClassFixture<TestFixture<MiddlewareAnaysisSample.Startup>>
{
public MiddlewareAnalysisSampleTest(TestFixture<MiddlewareAnaysisSample.Startup> fixture)
{
Client = fixture.Client;
}
public HttpClient Client { get; }
[Fact]
public async Task MiddlewareAnalysisPage_ShowsAnalysis()
{
// Arrange
var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/");
// Act
var response = await Client.SendAsync(request);
// Assert
var body = await response.Content.ReadAsStringAsync();
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
}
}

View File

@ -0,0 +1,38 @@
// 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.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Extensions.PlatformAbstractions;
using Xunit;
namespace Microsoft.AspNetCore.Diagnostics.FunctionalTests
{
public class RuntimeInfoPageSampleTest : IClassFixture<TestFixture<RuntimeInfoPageSample.Startup>>
{
public RuntimeInfoPageSampleTest(TestFixture<RuntimeInfoPageSample.Startup> fixture)
{
Client = fixture.Client;
}
public HttpClient Client { get; }
[Fact]
public async Task RuntimeInfoPage_ShowsInfo()
{
// Arrange
var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/runtimeinfo");
// Act
var response = await Client.SendAsync(request);
var os = PlatformServices.Default.Runtime.OperatingSystem;
// Assert
var body = await response.Content.ReadAsStringAsync();
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Contains($"Operating System: {os}", body);
}
}
}

View File

@ -0,0 +1,35 @@
// 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.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Xunit;
namespace Microsoft.AspNetCore.Diagnostics.FunctionalTests
{
public class StatusCodeSampleTest : IClassFixture<TestFixture<StatusCodePagesSample.Startup>>
{
public StatusCodeSampleTest(TestFixture<StatusCodePagesSample.Startup> fixture)
{
Client = fixture.Client;
}
public HttpClient Client { get; }
[Fact]
public async Task StatusCodePage_ShowsError()
{
// Arrange
var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/errors/417");
// Act
var response = await Client.SendAsync(request);
// Assert
var body = await response.Content.ReadAsStringAsync();
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Contains("Status Code: 417", body);
}
}
}

View File

@ -0,0 +1,37 @@
// 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.Runtime.Versioning;
using Microsoft.Extensions.PlatformAbstractions;
public class TestApplicationEnvironment : IApplicationEnvironment
{
private readonly IApplicationEnvironment _original;
public TestApplicationEnvironment(IApplicationEnvironment original, string name, string basePath)
{
_original = original;
ApplicationName = name;
ApplicationBasePath = basePath;
}
public string ApplicationName { get; }
public string ApplicationVersion
{
get
{
return _original.ApplicationVersion;
}
}
public string ApplicationBasePath { get; }
public FrameworkName RuntimeFramework
{
get
{
return _original.RuntimeFramework;
}
}
}

View File

@ -0,0 +1,73 @@
// 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.IO;
using System.Net.Http;
using System.Reflection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.TestHost;
using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.PlatformAbstractions;
namespace Microsoft.AspNetCore.Diagnostics.FunctionalTests
{
public class TestFixture<TStartup> : IDisposable
{
private readonly TestServer _server;
public TestFixture()
{
// RequestLocalizationOptions saves the current culture when constructed, potentially changing response
// localization i.e. RequestLocalizationMiddleware behavior. Ensure the saved culture
// (DefaultRequestCulture) is consistent regardless of system configuration or personal preferences.
using (new CultureReplacer())
{
var builder = new WebHostBuilder()
.ConfigureServices(InitializeServices)
.UseStartup(typeof(TStartup));
_server = new TestServer(builder);
}
Client = _server.CreateClient();
Client.BaseAddress = new Uri("http://localhost");
}
public HttpClient Client { get; }
public void Dispose()
{
Client.Dispose();
_server.Dispose();
}
protected virtual void InitializeServices(IServiceCollection services)
{
var libraryManager = DnxPlatformServices.Default.LibraryManager;
// When an application executes in a regular context, the application base path points to the root
// directory where the application is located, for example .../samples/MvcSample.Web. However, when
// executing an application as part of a test, the ApplicationBasePath of the IApplicationEnvironment
// points to the root folder of the test project.
// To compensate, we need to calculate the correct project path and override the application
// environment value so that components like the view engine work properly in the context of the test.
var startupAssembly = typeof(TStartup).GetTypeInfo().Assembly;
var applicationName = startupAssembly.GetName().Name;
var library = libraryManager.GetLibrary(applicationName);
var applicationRoot = Path.GetDirectoryName(library.Path);
var applicationEnvironment = PlatformServices.Default.Application;
services.AddSingleton<IApplicationEnvironment>(
new TestApplicationEnvironment(applicationEnvironment, applicationName, applicationRoot));
// Inject a custom assembly provider. Overrides AddMvc() because that uses TryAdd().
var assemblyProvider = new StaticAssemblyProvider();
assemblyProvider.CandidateAssemblies.Add(startupAssembly);
services.AddSingleton<IAssemblyProvider>(assemblyProvider);
}
}
}

View File

@ -0,0 +1,35 @@
// 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.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Xunit;
namespace Microsoft.AspNetCore.Diagnostics.FunctionalTests
{
public class WelcomePageSampleTest : IClassFixture<TestFixture<WelcomePageSample.Startup>>
{
public WelcomePageSampleTest(TestFixture<WelcomePageSample.Startup> fixture)
{
Client = fixture.Client;
}
public HttpClient Client { get; }
[Fact]
public async Task WelcomePage_ShowsWelcome()
{
// Arrange
var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/");
// Act
var response = await Client.SendAsync(request);
// Assert
var body = await response.Content.ReadAsStringAsync();
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Contains("Your ASP.NET 5 application has been successfully started", body);
}
}
}

View File

@ -0,0 +1,35 @@
{
"commands": {
"test": "xunit.runner.aspnet"
},
"dependencies": {
"DatabaseErrorPageSample": "1.0.0",
"DeveloperExceptionPageSample": "1.0.0",
"ElmPageSample": "1.0.0",
"ExceptionHandlerSample": "1.0.0",
"Microsoft.AspNetCore.IISPlatformHandler": "1.0.0-*",
"Microsoft.AspNetCore.Mvc.Core": "1.0.0-*",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0-*",
"Microsoft.AspNetCore.TestHost": "1.0.0-*",
"Microsoft.AspNetCore.Testing": "1.0.0-*",
"MiddlewareAnalysisSample": "1.0.0",
"RuntimeInfoPageSample": "1.0.0",
"StatusCodePagesSample": "1.0.0",
"WelcomePageSample": "1.0.0",
"xunit.runner.aspnet": "2.0.0-aspnet-*"
},
"exclude": [
"wwwroot",
"node_modules",
"bower_components"
],
"frameworks": {
"dnx451": { },
"dnxcore50": { }
},
"publishExclude": [
"**.user",
"**.vspscc"
],
"version": "1.0.0-*"
}