Skip to content

Commit

Permalink
functional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ardalis committed May 1, 2017
1 parent 235b946 commit 8a4edfe
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 18 deletions.
13 changes: 12 additions & 1 deletion tests/FunctionalTests/FunctionalTests.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
Expand All @@ -8,6 +8,17 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="1.1.1" />

</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\ApplicationCore\ApplicationCore.csproj" />
<ProjectReference Include="..\..\src\Web\Web.csproj" />
</ItemGroup>

<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>

</Project>
72 changes: 72 additions & 0 deletions tests/FunctionalTests/Web/Controllers/BaseWebTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using System.IO;
using System.Net.Http;
using System.Reflection;
using Microsoft.Extensions.PlatformAbstractions;
using Microsoft.eShopWeb;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;

namespace FunctionalTests.Web.Controllers
{
public abstract class BaseWebTest
{
protected readonly HttpClient _client;
protected string _contentRoot;

public BaseWebTest()
{
_client = GetClient();
}

protected HttpClient GetClient()
{
var startupAssembly = typeof(Startup).GetTypeInfo().Assembly;
_contentRoot = GetProjectPath("src", startupAssembly);
var builder = new WebHostBuilder()
.UseContentRoot(_contentRoot)
.UseStartup<Startup>();

var server = new TestServer(builder);
var client = server.CreateClient();

return client;
}

/// <summary>
/// Gets the full path to the target project path that we wish to test
/// </summary>
/// <param name="solutionRelativePath">
/// The parent directory of the target project.
/// e.g. src, samples, test, or test/Websites
/// </param>
/// <param name="startupAssembly">The target project's assembly.</param>
/// <returns>The full path to the target project.</returns>
protected static string GetProjectPath(string solutionRelativePath, Assembly startupAssembly)
{
// Get name of the target project which we want to test
var projectName = startupAssembly.GetName().Name;

// Get currently executing test project path
var applicationBasePath = PlatformServices.Default.Application.ApplicationBasePath;

// Find the folder which contains the solution file. We then use this information to find the target
// project which we want to test.
var directoryInfo = new DirectoryInfo(applicationBasePath);
do
{
var solutionFileInfo = new FileInfo(Path.Combine(directoryInfo.FullName, "eShopOnWeb.sln"));
if (solutionFileInfo.Exists)
{
return Path.GetFullPath(Path.Combine(directoryInfo.FullName, solutionRelativePath, projectName));
}

directoryInfo = directoryInfo.Parent;
}
while (directoryInfo.Parent != null);

throw new Exception($"Solution root could not be located using application root {applicationBasePath}.");
}

}
}
28 changes: 28 additions & 0 deletions tests/FunctionalTests/Web/Controllers/CatalogControllerGetImage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.IO;
using Xunit;
using System.Threading.Tasks;

namespace FunctionalTests.Web.Controllers
{
public class CatalogControllerGetImage : BaseWebTest
{
[Fact]
public async Task ReturnsFileContentResultGivenValidId()
{
var testFilePath = Path.Combine(_contentRoot, "pics//1.png");
var expectedFileBytes = File.ReadAllBytes(testFilePath);

var response = await _client.GetAsync("/catalog/pic/1");
response.EnsureSuccessStatusCode();
var streamResponse = await response.Content.ReadAsStreamAsync();
byte[] byteResult;
using (var ms = new MemoryStream())
{
streamResponse.CopyTo(ms);
byteResult = ms.ToArray();
}

Assert.Equal(expectedFileBytes, byteResult);
}
}
}

This file was deleted.

0 comments on commit 8a4edfe

Please sign in to comment.