diff --git a/tests/FunctionalTests/FunctionalTests.csproj b/tests/FunctionalTests/FunctionalTests.csproj index f03259659..fbb8fcba0 100644 --- a/tests/FunctionalTests/FunctionalTests.csproj +++ b/tests/FunctionalTests/FunctionalTests.csproj @@ -1,4 +1,4 @@ - + netcoreapp1.1 @@ -8,6 +8,17 @@ + + + + + + + + + + + diff --git a/tests/FunctionalTests/Web/Controllers/BaseWebTest.cs b/tests/FunctionalTests/Web/Controllers/BaseWebTest.cs new file mode 100644 index 000000000..adf7a0526 --- /dev/null +++ b/tests/FunctionalTests/Web/Controllers/BaseWebTest.cs @@ -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(); + + var server = new TestServer(builder); + var client = server.CreateClient(); + + return client; + } + + /// + /// Gets the full path to the target project path that we wish to test + /// + /// + /// The parent directory of the target project. + /// e.g. src, samples, test, or test/Websites + /// + /// The target project's assembly. + /// The full path to the target project. + 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}."); + } + + } +} diff --git a/tests/FunctionalTests/Web/Controllers/CatalogControllerGetImage.cs b/tests/FunctionalTests/Web/Controllers/CatalogControllerGetImage.cs new file mode 100644 index 000000000..b6afacc98 --- /dev/null +++ b/tests/FunctionalTests/Web/Controllers/CatalogControllerGetImage.cs @@ -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); + } + } +} diff --git a/tests/IntegrationTests/Web/Controllers/CatalogControllerGetImage.cs b/tests/IntegrationTests/Web/Controllers/CatalogControllerGetImage.cs deleted file mode 100644 index 0217ad14b..000000000 --- a/tests/IntegrationTests/Web/Controllers/CatalogControllerGetImage.cs +++ /dev/null @@ -1,17 +0,0 @@ -using Microsoft.eShopWeb.Controllers; -using System; -using System.Collections.Generic; -using System.Text; -using Xunit; - -namespace IntegrationTests.Web.Controllers -{ - public class CatalogControllerGetImage - { - [Fact] - public void ReturnsFileContentResultGivenValidId() - { - //var controller = new CatalogController() - } - } -}