mirror of https://github.com/abpframework/abp.git
39 changed files with 558 additions and 285 deletions
@ -0,0 +1,118 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Net.Http; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Builder; |
|||
using Microsoft.AspNetCore.Hosting.Server; |
|||
using Microsoft.AspNetCore.Routing; |
|||
using Microsoft.AspNetCore.TestHost; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Hosting; |
|||
using Volo.Abp.Modularity; |
|||
|
|||
namespace Volo.Abp.AspNetCore.TestBase; |
|||
|
|||
public class AbpAspNetCoreAsyncIntegratedTestBase<TModule> |
|||
where TModule : AbpModule |
|||
{ |
|||
protected IHost Host { get; set; } |
|||
|
|||
protected TestServer Server { get; set; } |
|||
|
|||
protected HttpClient Client { get; set; } |
|||
|
|||
protected IServiceProvider ServiceProvider { get; set; } |
|||
|
|||
protected virtual T GetService<T>() |
|||
{ |
|||
return ServiceProvider.GetService<T>(); |
|||
} |
|||
|
|||
protected virtual T GetRequiredService<T>() |
|||
{ |
|||
return ServiceProvider.GetRequiredService<T>(); |
|||
} |
|||
|
|||
public virtual async Task InitializeAsync() |
|||
{ |
|||
var builder = WebApplication.CreateBuilder(); |
|||
builder.Host.ConfigureServices(services => |
|||
{ |
|||
services.AddSingleton<IHostLifetime, TestNoopHostLifetime>(); |
|||
services.AddSingleton<IServer, TestServer>(); |
|||
}) |
|||
.UseAutofac(); |
|||
|
|||
await builder.Services.AddApplicationAsync<TModule>(options => |
|||
{ |
|||
options.Services.ReplaceConfiguration(builder.Configuration); |
|||
}); |
|||
|
|||
await ConfigureServicesAsync(builder.Services); |
|||
|
|||
var app = builder.Build(); |
|||
|
|||
await app.InitializeApplicationAsync(); |
|||
|
|||
await app.StartAsync(); |
|||
|
|||
Host = app.Services.GetRequiredService<IHost>(); |
|||
|
|||
Server = Host.GetTestServer(); |
|||
Client = Host.GetTestClient(); |
|||
|
|||
ServiceProvider = Server.Services; |
|||
|
|||
ServiceProvider.GetRequiredService<ITestServerAccessor>().Server = Server; |
|||
} |
|||
|
|||
public virtual Task DisposeAsync() |
|||
{ |
|||
return Task.CompletedTask; |
|||
} |
|||
|
|||
protected virtual Task ConfigureServicesAsync(IServiceCollection services) |
|||
{ |
|||
return Task.CompletedTask; |
|||
} |
|||
|
|||
#region GetUrl
|
|||
|
|||
/// <summary>
|
|||
/// Gets default URL for given controller type.
|
|||
/// </summary>
|
|||
/// <typeparam name="TController">The type of the controller.</typeparam>
|
|||
protected virtual string GetUrl<TController>() |
|||
{ |
|||
return "/" + typeof(TController).Name.RemovePostFix("Controller", "AppService", "ApplicationService", "Service"); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets default URL for given controller type's given action.
|
|||
/// </summary>
|
|||
/// <typeparam name="TController">The type of the controller.</typeparam>
|
|||
protected virtual string GetUrl<TController>(string actionName) |
|||
{ |
|||
return GetUrl<TController>() + "/" + actionName; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets default URL for given controller type's given action with query string parameters (as anonymous object).
|
|||
/// </summary>
|
|||
/// <typeparam name="TController">The type of the controller.</typeparam>
|
|||
protected virtual string GetUrl<TController>(string actionName, object queryStringParamsAsAnonymousObject) |
|||
{ |
|||
var url = GetUrl<TController>(actionName); |
|||
|
|||
var dictionary = new RouteValueDictionary(queryStringParamsAsAnonymousObject); |
|||
if (dictionary.Any()) |
|||
{ |
|||
url += "?" + dictionary.Select(d => $"{d.Key}={d.Value}").JoinAsString("&"); |
|||
} |
|||
|
|||
return url; |
|||
} |
|||
|
|||
#endregion
|
|||
} |
|||
@ -0,0 +1,18 @@ |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.Extensions.Hosting; |
|||
|
|||
namespace Volo.Abp.AspNetCore.TestBase; |
|||
|
|||
public class TestNoopHostLifetime : IHostLifetime |
|||
{ |
|||
public Task StopAsync(CancellationToken cancellationToken) |
|||
{ |
|||
return Task.CompletedTask; |
|||
} |
|||
|
|||
public Task WaitForStartAsync(CancellationToken cancellationToken) |
|||
{ |
|||
return Task.CompletedTask; |
|||
} |
|||
} |
|||
@ -0,0 +1,62 @@ |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Builder; |
|||
using Microsoft.AspNetCore.Http; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Shouldly; |
|||
using Volo.Abp.AspNetCore.TestBase; |
|||
using Volo.Abp.Autofac; |
|||
using Volo.Abp.Modularity; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Mvc; |
|||
|
|||
public class AbpAspNetCoreAsyncTestBase_Tests : AbpAspNetCoreAsyncTestBase<AbpAspNetCoreAsyncTestModule> |
|||
{ |
|||
[Fact] |
|||
public async Task Get_API_Response_Test() |
|||
{ |
|||
var result = await GetResponseAsStringAsync("/api"); |
|||
result.ShouldBe(await GetRequiredService<DataBaseService>().GetResponseAsync()); |
|||
} |
|||
} |
|||
|
|||
[DependsOn( |
|||
typeof(AbpAspNetCoreMvcModule), |
|||
typeof(AbpAspNetCoreTestBaseModule), |
|||
typeof(AbpAutofacModule) |
|||
)] |
|||
public class AbpAspNetCoreAsyncTestModule : AbpModule |
|||
{ |
|||
public override Task ConfigureServicesAsync(ServiceConfigurationContext context) |
|||
{ |
|||
context.Services.AddTransient<DataBaseService>(); |
|||
return Task.CompletedTask; |
|||
} |
|||
|
|||
public async override Task OnApplicationInitializationAsync(ApplicationInitializationContext context) |
|||
{ |
|||
var app = context.GetApplicationBuilder(); |
|||
|
|||
var dataBaseService = app.ApplicationServices.GetRequiredService<DataBaseService>(); |
|||
|
|||
var apiResponse = await dataBaseService.GetResponseAsync(); |
|||
app.Map("/api", _ => |
|||
{ |
|||
app.Run(async httpContext => |
|||
{ |
|||
await httpContext.Response.WriteAsync(apiResponse); |
|||
}); |
|||
}); |
|||
|
|||
app.UseRouting(); |
|||
app.UseConfiguredEndpoints(); |
|||
} |
|||
} |
|||
|
|||
public class DataBaseService |
|||
{ |
|||
public Task<string> GetResponseAsync() |
|||
{ |
|||
return Task.FromResult("hello api!"); |
|||
} |
|||
} |
|||
@ -0,0 +1,45 @@ |
|||
using System.Globalization; |
|||
using System.Net; |
|||
using System.Net.Http; |
|||
using System.Text.Json; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.Net.Http.Headers; |
|||
using Shouldly; |
|||
using Volo.Abp.AspNetCore.TestBase; |
|||
using Volo.Abp.Modularity; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.AspNetCore; |
|||
|
|||
public abstract class AbpAspNetCoreAsyncTestBase<TModule> : AbpAspNetCoreAsyncIntegratedTestBase<TModule>, IAsyncLifetime |
|||
where TModule : AbpModule |
|||
{ |
|||
protected virtual async Task<T> GetResponseAsObjectAsync<T>(string url, HttpStatusCode expectedStatusCode = HttpStatusCode.OK) |
|||
{ |
|||
var strResponse = await GetResponseAsStringAsync(url, expectedStatusCode); |
|||
return JsonSerializer.Deserialize<T>(strResponse, new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }); |
|||
} |
|||
|
|||
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, bool xmlHttpRequest = false) |
|||
{ |
|||
using (var requestMessage = new HttpRequestMessage(HttpMethod.Get, url)) |
|||
{ |
|||
requestMessage.Headers.Add("Accept-Language", CultureInfo.CurrentUICulture.Name); |
|||
if (xmlHttpRequest) |
|||
{ |
|||
requestMessage.Headers.Add(HeaderNames.XRequestedWith, "XMLHttpRequest"); |
|||
} |
|||
var response = await Client.SendAsync(requestMessage); |
|||
response.StatusCode.ShouldBe(expectedStatusCode); |
|||
return response; |
|||
} |
|||
} |
|||
} |
|||
@ -1,18 +0,0 @@ |
|||
using Microsoft.AspNetCore.Builder; |
|||
using Microsoft.AspNetCore.Hosting; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
|
|||
namespace MyCompanyName.MyProjectName.Blazor.Server.Host; |
|||
|
|||
public class Startup |
|||
{ |
|||
public void ConfigureServices(IServiceCollection services) |
|||
{ |
|||
services.AddApplication<MyProjectNameBlazorHostModule>(); |
|||
} |
|||
|
|||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) |
|||
{ |
|||
app.InitializeApplication(); |
|||
} |
|||
} |
|||
@ -1,20 +0,0 @@ |
|||
using System; |
|||
using Microsoft.AspNetCore.Builder; |
|||
using Microsoft.AspNetCore.Hosting; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Logging; |
|||
|
|||
namespace MyCompanyName.MyProjectName; |
|||
|
|||
public class Startup |
|||
{ |
|||
public void ConfigureServices(IServiceCollection services) |
|||
{ |
|||
services.AddApplication<MyProjectNameHttpApiHostModule>(); |
|||
} |
|||
|
|||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) |
|||
{ |
|||
app.InitializeApplication(); |
|||
} |
|||
} |
|||
@ -1,20 +0,0 @@ |
|||
using System; |
|||
using Microsoft.AspNetCore.Builder; |
|||
using Microsoft.AspNetCore.Hosting; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Logging; |
|||
|
|||
namespace MyCompanyName.MyProjectName; |
|||
|
|||
public class Startup |
|||
{ |
|||
public void ConfigureServices(IServiceCollection services) |
|||
{ |
|||
services.AddApplication<MyProjectNameIdentityServerModule>(); |
|||
} |
|||
|
|||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) |
|||
{ |
|||
app.InitializeApplication(); |
|||
} |
|||
} |
|||
@ -1,18 +0,0 @@ |
|||
using Microsoft.AspNetCore.Builder; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Logging; |
|||
|
|||
namespace MyCompanyName.MyProjectName; |
|||
|
|||
public class Startup |
|||
{ |
|||
public void ConfigureServices(IServiceCollection services) |
|||
{ |
|||
services.AddApplication<MyProjectNameWebHostModule>(); |
|||
} |
|||
|
|||
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory) |
|||
{ |
|||
app.InitializeApplication(); |
|||
} |
|||
} |
|||
@ -1,19 +0,0 @@ |
|||
using Microsoft.AspNetCore.Builder; |
|||
using Microsoft.AspNetCore.Hosting; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Logging; |
|||
|
|||
namespace MyCompanyName.MyProjectName; |
|||
|
|||
public class Startup |
|||
{ |
|||
public void ConfigureServices(IServiceCollection services) |
|||
{ |
|||
services.AddApplication<MyProjectNameWebUnifiedModule>(); |
|||
} |
|||
|
|||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) |
|||
{ |
|||
app.InitializeApplication(); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue