forked from Meowv/Plus.Core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
338 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
test/Plus.AspNetCore.MultiTenancy.Tests/Plus.AspNetCore.MultiTenancy.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
48 changes: 48 additions & 0 deletions
48
test/Plus.AspNetCore.MultiTenancy.Tests/Plus/AspNetCore/App/AppModule.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
test/Plus.AspNetCore.MultiTenancy.Tests/Plus/AspNetCore/App/Startup.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...NetCore.MultiTenancy.Tests/Plus/AspNetCore/MultiTenancy/AspNetCoreMultiTenancyTestBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
namespace Plus.AspNetCore.MultiTenancy | ||
{ | ||
public abstract class AspNetCoreMultiTenancyTestBase : PlusAspNetCoreTestBase<Startup> | ||
{ | ||
|
||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...ncy.Tests/Plus/AspNetCore/MultiTenancy/AspNetCoreMultiTenancy_WithDomainResolver_Tests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
{ | ||
|
||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...Tests/Plus/AspNetCore/MultiTenancy/AspNetCoreMultiTenancy_Without_DomainResolver_Tests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
{ | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
51 changes: 51 additions & 0 deletions
51
test/Plus.AspNetCore.Tests/Plus/AspNetCore/PlusAspNetCoreTestBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
test/Plus.AspNetCore.Tests/Plus/AspNetCore/PlusAspNetCoreTestModule.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
test/Plus.AspNetCore.Tests/Plus/AspNetCore/VirtualFileSystem/VirtualFileSystem_Tests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
test1.js-content |