초기커밋
This commit is contained in:
135
BrokerApiTest/Helper/BrokerTestServer.cs
Normal file
135
BrokerApiTest/Helper/BrokerTestServer.cs
Normal file
@@ -0,0 +1,135 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
using NLog.Extensions.Logging;
|
||||
|
||||
using ServerCore; using ServerBase;
|
||||
|
||||
namespace BrokerTest.Helper;
|
||||
|
||||
using System.Reflection;
|
||||
|
||||
using BrokerApiServer.Common;
|
||||
using BrokerApiServer.Controllers;
|
||||
using BrokerApiServer.Extensions;
|
||||
|
||||
using CaliGameApi.Middlewares;
|
||||
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
public interface IBrokerTestServer
|
||||
{
|
||||
HttpClient getTestClient();
|
||||
TService? getRequiredService<TService>() where TService : class;
|
||||
}
|
||||
|
||||
public interface IBrokerTestServerLocal: IBrokerTestServer
|
||||
{
|
||||
TService? getService<TService>(in IServiceScope scope) where TService : class;
|
||||
}
|
||||
|
||||
public class BrokerTestServerFactory
|
||||
{
|
||||
public static IBrokerTestServer createServer()
|
||||
{
|
||||
return new BrokerTestServer();
|
||||
}
|
||||
|
||||
public static IBrokerTestServer createRemoteServer(string baseUrl)
|
||||
{
|
||||
return new BrokerTestRemoteServer(baseUrl);
|
||||
}
|
||||
}
|
||||
|
||||
public class BrokerTestRemoteServer: IBrokerTestServer
|
||||
{
|
||||
private readonly string m_base_url;
|
||||
|
||||
public BrokerTestRemoteServer(string baseUrl)
|
||||
{
|
||||
m_base_url = baseUrl;
|
||||
}
|
||||
|
||||
public HttpClient getTestClient()
|
||||
{
|
||||
return new HttpClient { BaseAddress = new Uri(m_base_url) };
|
||||
}
|
||||
|
||||
public TService? getRequiredService<TService>() where TService : class
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public class BrokerTestServer: IBrokerTestServerLocal
|
||||
{
|
||||
private readonly WebApplication m_server;
|
||||
|
||||
public BrokerTestServer()
|
||||
{
|
||||
m_server = runWebApp();
|
||||
Assert.NotNull(m_server);
|
||||
}
|
||||
|
||||
WebApplication runWebApp()
|
||||
{
|
||||
var port = 12000;
|
||||
var cmd_options = new string[] { "--urls", $"http://localhost:{port}" };
|
||||
// Arrange
|
||||
var builder = WebApplication.CreateBuilder(cmd_options);
|
||||
builder.Logging.AddNLog(new NLogProviderOptions{ ReplaceLoggerFactory = true });
|
||||
builder.Logging.AddConsole();
|
||||
builder.addAppServices();
|
||||
builder.Services.AddHttpContextAccessor();
|
||||
// builder.Services.AddControllers(options => { options.Filters.Add(new ResultExceptionFilter()); });
|
||||
var assembly = Assembly.GetAssembly(typeof(PlanetUserController));
|
||||
Assert.NotNull(assembly);
|
||||
builder.Services.AddControllers(options => { options.Filters.Add(new ResultExceptionFilter()); })
|
||||
.AddApplicationPart(assembly); // 이유는 모르겠지만 이게 없으면 테스트에서 404 에러 발생 - 컨트롤러가 없음
|
||||
builder.Services.AddHealthChecks();
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
|
||||
var app = builder.Build();
|
||||
app.UseHttpsRedirection();
|
||||
app.UseRouting();
|
||||
app.UseCors("Everything");
|
||||
app.UseMiddleware<ResultLoggingMiddleware>();
|
||||
app.UseAuthorization();
|
||||
app.MapControllers();
|
||||
app.MapHealthChecks("/healthcheck");
|
||||
|
||||
app.validateRepoConnections();
|
||||
app.brokerServerLogicInit();
|
||||
app.metadataMangerInit(TestDefines.MetaDataPath);
|
||||
|
||||
app.Services.GetRequiredService<IHostApplicationLifetime>().ApplicationStarted.Register(() =>
|
||||
{
|
||||
// Log.getLogger().info($"Env : {app.Environment.EnvironmentName}");
|
||||
// Log.getLogger().info($"BrokerApiServer started {port}");
|
||||
// todo 왜 enpoints가 1개 뿐이지?
|
||||
Console.WriteLine($"Env : {app.Environment.EnvironmentName}");
|
||||
});
|
||||
// app.Services.GetRequiredService<IHostApplicationLifetime>().ApplicationStopped.Register(Log.shutdown);
|
||||
app.RunAsync().ConfigureAwait(false);
|
||||
return app;
|
||||
}
|
||||
|
||||
public HttpClient getTestClient()
|
||||
{
|
||||
var url = m_server.Urls.FirstOrDefault()?.ToString() ?? "http://localhost:12000";
|
||||
var http_client = new HttpClient();
|
||||
http_client.BaseAddress = new Uri(url);
|
||||
return http_client;
|
||||
}
|
||||
|
||||
public TService? getRequiredService<TService>() where TService : class
|
||||
{
|
||||
return m_server.Services.GetRequiredService<TService>();
|
||||
}
|
||||
|
||||
public TService? getService<TService>(in IServiceScope scope) where TService : class
|
||||
{
|
||||
return scope.ServiceProvider.GetRequiredService<TService>();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user