Skip to content

Commit

Permalink
VirtualFileSystem_Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Meowv committed Aug 5, 2020
1 parent 376f695 commit c4e4b26
Show file tree
Hide file tree
Showing 13 changed files with 338 additions and 0 deletions.
30 changes: 30 additions & 0 deletions plus.core.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PlusTestBase", "test\PlusTe
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Plus.AspNetCore.Authentication.OAuth.Tests", "test\Plus.AspNetCore.Authentication.OAuth.Tests\Plus.AspNetCore.Authentication.OAuth.Tests.csproj", "{84D50EB1-7E1A-4205-97E8-DA58DD76947C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Plus.AspNetCore.MultiTenancy.Tests", "test\Plus.AspNetCore.MultiTenancy.Tests\Plus.AspNetCore.MultiTenancy.Tests.csproj", "{CC19BD96-872F-4F56-AE38-7A65992F133C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Plus.AspNetCore.Tests", "test\Plus.AspNetCore.Tests\Plus.AspNetCore.Tests.csproj", "{5B18A7B3-2097-4557-96AB-D40C37C4B382}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -71,6 +75,30 @@ Global
{84D50EB1-7E1A-4205-97E8-DA58DD76947C}.Release|x64.Build.0 = Release|Any CPU
{84D50EB1-7E1A-4205-97E8-DA58DD76947C}.Release|x86.ActiveCfg = Release|Any CPU
{84D50EB1-7E1A-4205-97E8-DA58DD76947C}.Release|x86.Build.0 = Release|Any CPU
{CC19BD96-872F-4F56-AE38-7A65992F133C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CC19BD96-872F-4F56-AE38-7A65992F133C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CC19BD96-872F-4F56-AE38-7A65992F133C}.Debug|x64.ActiveCfg = Debug|Any CPU
{CC19BD96-872F-4F56-AE38-7A65992F133C}.Debug|x64.Build.0 = Debug|Any CPU
{CC19BD96-872F-4F56-AE38-7A65992F133C}.Debug|x86.ActiveCfg = Debug|Any CPU
{CC19BD96-872F-4F56-AE38-7A65992F133C}.Debug|x86.Build.0 = Debug|Any CPU
{CC19BD96-872F-4F56-AE38-7A65992F133C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CC19BD96-872F-4F56-AE38-7A65992F133C}.Release|Any CPU.Build.0 = Release|Any CPU
{CC19BD96-872F-4F56-AE38-7A65992F133C}.Release|x64.ActiveCfg = Release|Any CPU
{CC19BD96-872F-4F56-AE38-7A65992F133C}.Release|x64.Build.0 = Release|Any CPU
{CC19BD96-872F-4F56-AE38-7A65992F133C}.Release|x86.ActiveCfg = Release|Any CPU
{CC19BD96-872F-4F56-AE38-7A65992F133C}.Release|x86.Build.0 = Release|Any CPU
{5B18A7B3-2097-4557-96AB-D40C37C4B382}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5B18A7B3-2097-4557-96AB-D40C37C4B382}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5B18A7B3-2097-4557-96AB-D40C37C4B382}.Debug|x64.ActiveCfg = Debug|Any CPU
{5B18A7B3-2097-4557-96AB-D40C37C4B382}.Debug|x64.Build.0 = Debug|Any CPU
{5B18A7B3-2097-4557-96AB-D40C37C4B382}.Debug|x86.ActiveCfg = Debug|Any CPU
{5B18A7B3-2097-4557-96AB-D40C37C4B382}.Debug|x86.Build.0 = Debug|Any CPU
{5B18A7B3-2097-4557-96AB-D40C37C4B382}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5B18A7B3-2097-4557-96AB-D40C37C4B382}.Release|Any CPU.Build.0 = Release|Any CPU
{5B18A7B3-2097-4557-96AB-D40C37C4B382}.Release|x64.ActiveCfg = Release|Any CPU
{5B18A7B3-2097-4557-96AB-D40C37C4B382}.Release|x64.Build.0 = Release|Any CPU
{5B18A7B3-2097-4557-96AB-D40C37C4B382}.Release|x86.ActiveCfg = Release|Any CPU
{5B18A7B3-2097-4557-96AB-D40C37C4B382}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -79,6 +107,8 @@ Global
{2356B79F-07E4-480D-9DC8-C1564275CFBD} = {8641E46B-7E9B-4DBA-A758-80EE3A1DAE8C}
{B12A957A-1046-4268-BCEB-6B83D1AA9B92} = {8641E46B-7E9B-4DBA-A758-80EE3A1DAE8C}
{84D50EB1-7E1A-4205-97E8-DA58DD76947C} = {8641E46B-7E9B-4DBA-A758-80EE3A1DAE8C}
{CC19BD96-872F-4F56-AE38-7A65992F133C} = {8641E46B-7E9B-4DBA-A758-80EE3A1DAE8C}
{5B18A7B3-2097-4557-96AB-D40C37C4B382} = {8641E46B-7E9B-4DBA-A758-80EE3A1DAE8C}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E35AC516-C60E-4F94-A5E8-815F85C32769}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<RootNamespace />
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<ProjectReference Include="..\Plus.AspNetCore.Tests\Plus.AspNetCore.Tests.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Plus.AspNetCore.MultiTenancy;
using Plus.AspNetCore.TestBase;
using Plus.Json;
using Plus.Modularity;
using Plus.MultiTenancy;
using System.Collections.Generic;

namespace Plus.AspNetCore.App
{
[DependsOn(
typeof(PlusAspNetCoreMultiTenancyModule),
typeof(PlusAspNetCoreTestBaseModule)
)]
public class AppModule : PlusModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
Configure<PlusMultiTenancyOptions>(options =>
{
options.IsEnabled = true;
});
}

public override void OnApplicationInitialization(ApplicationInitializationContext context)
{
var app = context.GetApplicationBuilder();

app.UseMultiTenancy();

app.Run(async (ctx) =>
{
var currentTenant = ctx.RequestServices.GetRequiredService<ICurrentTenant>();
var jsonSerializer = ctx.RequestServices.GetRequiredService<IJsonSerializer>();

var dictionary = new Dictionary<string, string>
{
["TenantId"] = currentTenant.IsAvailable ? currentTenant.Id.ToString() : ""
};

var result = jsonSerializer.Serialize(dictionary, camelCase: false);
await ctx.Response.WriteAsync(result);
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace Plus.AspNetCore.App
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddApplication<AppModule>();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
{
app.InitializeApplication();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

namespace Plus.AspNetCore.MultiTenancy
{
public abstract class AspNetCoreMultiTenancyTestBase : PlusAspNetCoreTestBase<Startup>
{

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Threading.Tasks;
using Xunit;

namespace Plus.AspNetCore.MultiTenancy
{
public class AspNetCoreMultiTenancy_WithDomainResolver_Tests : AspNetCoreMultiTenancyTestBase
{
[Fact]
public async Task Should_Use_Host_If_Tenant_Is_Not_Specified()
{

}

[Fact]
public async Task Should_Use_Domain_If_Specified()
{

}

[Fact]
public async Task Should_Use_Domain_As_First_Priority_If_Specified()
{

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Threading.Tasks;
using Xunit;

namespace Plus.AspNetCore.MultiTenancy
{
public class AspNetCoreMultiTenancy_Without_DomainResolver_Tests : AspNetCoreMultiTenancyTestBase
{

[Fact]
public async Task Should_Use_Host_If_Tenant_Is_Not_Specified()
{

}

[Fact]
public async Task Should_Use_QueryString_Tenant_Id_If_Specified()
{

}

[Fact]
public async Task Should_Use_Header_Tenant_Id_If_Specified()
{

}

[Fact]
public async Task Should_Use_Cookie_Tenant_Id_If_Specified()
{

}
}
}
17 changes: 17 additions & 0 deletions test/Plus.AspNetCore.Tests/Plus.AspNetCore.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<RootNamespace />
</PropertyGroup>

<ItemGroup>
<EmbeddedResource Include="wwwroot\SampleFiles\**\*.*" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<ProjectReference Include="..\PlusTestBase\PlusTestBase.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Plus.AspNetCore.TestBase;
using Shouldly;
using System.Globalization;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;

namespace Plus.AspNetCore
{
public class PlusAspNetCoreTestBase : PlusAspNetCoreTestBase<Startup>
{

}

public abstract class PlusAspNetCoreTestBase<TStartup> : PlusAspNetCoreIntegratedTestBase<TStartup>
where TStartup : class
{
private static readonly JsonSerializerSettings SharedJsonSerializerSettings =
new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
};

protected virtual async Task<T> GetResponseAsObjectAsync<T>(string url, HttpStatusCode expectedStatusCode = HttpStatusCode.OK)
{
var strResponse = await GetResponseAsStringAsync(url, expectedStatusCode);
return JsonConvert.DeserializeObject<T>(strResponse, SharedJsonSerializerSettings);
}

protected virtual async Task<string> GetResponseAsStringAsync(string url, HttpStatusCode expectedStatusCode = HttpStatusCode.OK)
{
using (var response = await GetResponseAsync(url, expectedStatusCode))
{
return await response.Content.ReadAsStringAsync();
}
}

protected virtual async Task<HttpResponseMessage> GetResponseAsync(string url, HttpStatusCode expectedStatusCode = HttpStatusCode.OK)
{
using (var requestMessage = new HttpRequestMessage(HttpMethod.Get, url))
{
requestMessage.Headers.Add("Accept-Language", CultureInfo.CurrentUICulture.Name);
var response = await Client.SendAsync(requestMessage);
response.StatusCode.ShouldBe(expectedStatusCode);
return response;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Plus.AspNetCore.TestBase;
using Plus.Autofac;
using Plus.Modularity;
using Plus.VirtualFileSystem;
using System;
using System.IO;

namespace Plus.AspNetCore
{
[DependsOn(
typeof(PlusAspNetCoreTestBaseModule),
typeof(PlusAspNetCoreModule),
typeof(PlusAutofacModule)
)]
public class PlusAspNetCoreTestModule : PlusModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
var hostingEnvironment = context.Services.GetHostingEnvironment();

Configure<PlusVirtualFileSystemOptions>(options =>
{
options.FileSets.AddEmbedded<PlusAspNetCoreTestModule>();
//options.FileSets.ReplaceEmbeddedByPhysical<PlusAspNetCoreTestModule>(FindProjectPath(hostingEnvironment));
});
}

public override void OnApplicationInitialization(ApplicationInitializationContext context)
{
var app = context.GetApplicationBuilder();

app.UseCorrelationId();
app.UseVirtualFiles();
}

private string FindProjectPath(IWebHostEnvironment hostEnvironment)
{
var directory = new DirectoryInfo(hostEnvironment.ContentRootPath);

while (directory != null && directory.Name != "Plus.AspNetCore.Tests")
{
directory = directory.Parent;
}

return directory?.FullName
?? throw new Exception("Could not find the project path by beginning from " + hostEnvironment.ContentRootPath + ", going through to parents and looking for Plus.AspNetCore.Tests");
}
}
}
20 changes: 20 additions & 0 deletions test/Plus.AspNetCore.Tests/Plus/AspNetCore/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace Plus.AspNetCore
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddApplication<PlusAspNetCoreTestModule>();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
{
app.InitializeApplication();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Shouldly;
using System.Threading.Tasks;
using Xunit;

namespace Plus.AspNetCore.VirtualFileSystem
{
public class VirtualFileSystem_Tests : PlusAspNetCoreTestBase
{
[Fact]
public async Task Get_Virtual_File()
{
var result = await GetResponseAsStringAsync(
"/SampleFiles/test1.js"
);

result.ShouldBe("test1.js-content");
}
}
}
1 change: 1 addition & 0 deletions test/Plus.AspNetCore.Tests/wwwroot/SampleFiles/test1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test1.js-content

0 comments on commit c4e4b26

Please sign in to comment.