mirror of
https://github.com/ncblakely/GiantsTools
synced 2024-11-21 13:45:37 +01:00
Add tests for Get/Add servers.
This commit is contained in:
parent
a0e81ce824
commit
eda8d6cf67
108
Giants.WebApi.Tests/ServersControllerTests.cs
Normal file
108
Giants.WebApi.Tests/ServersControllerTests.cs
Normal file
@ -0,0 +1,108 @@
|
|||||||
|
using Autofac;
|
||||||
|
using Giants.DataContract.V1;
|
||||||
|
using Giants.Services;
|
||||||
|
using Giants.Web;
|
||||||
|
using Microsoft.AspNetCore.Mvc.Testing;
|
||||||
|
using Microsoft.AspNetCore.TestHost;
|
||||||
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||||
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||||
|
using System.Text.Json;
|
||||||
|
using YamlDotNet.Core.Tokens;
|
||||||
|
|
||||||
|
namespace Giants.WebApi.Tests
|
||||||
|
{
|
||||||
|
[TestClass]
|
||||||
|
public class ServersControllerTests
|
||||||
|
{
|
||||||
|
private const string ServersRoute = "/api/servers";
|
||||||
|
|
||||||
|
private readonly JsonSerializerOptions options = new(JsonSerializerDefaults.Web);
|
||||||
|
private static WebApplicationFactory<Program>? application;
|
||||||
|
private static HttpClient? client;
|
||||||
|
|
||||||
|
private class TestRegistrations : Module
|
||||||
|
{
|
||||||
|
protected override void Load(ContainerBuilder builder)
|
||||||
|
{
|
||||||
|
builder
|
||||||
|
.RegisterType<InMemoryServerRegistryStore>()
|
||||||
|
.As<IServerRegistryStore>()
|
||||||
|
.SingleInstance();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[ClassInitialize]
|
||||||
|
public static void Initialize(TestContext context)
|
||||||
|
{
|
||||||
|
application = new WebApplicationFactory<Program>()
|
||||||
|
.WithWebHostBuilder(builder =>
|
||||||
|
{
|
||||||
|
builder.ConfigureTestServices(x =>
|
||||||
|
{
|
||||||
|
Program.AddAdditionalRegistrations(new[] { new TestRegistrations() });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
client = application.CreateClient();
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task GetServersWithNoRegistrations()
|
||||||
|
{
|
||||||
|
using var response = await client!.GetAsync(ServersRoute);
|
||||||
|
response.EnsureSuccessStatusCode();
|
||||||
|
|
||||||
|
var responseContent = await response.Content.ReadAsStringAsync();
|
||||||
|
var responseObject = JsonSerializer.Deserialize<IEnumerable<ServerInfoWithHostAddress>>(responseContent, options);
|
||||||
|
Assert.IsNotNull(responseObject);
|
||||||
|
|
||||||
|
Assert.AreEqual(0, responseObject.Count());
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task AddAndGetServerRegistration()
|
||||||
|
{
|
||||||
|
var serverInfo = new DataContract.V1.ServerInfo
|
||||||
|
{
|
||||||
|
GameName = "Giants",
|
||||||
|
Version = new AppVersion() { Build = 1, Major = 1, Minor = 1, Revision = 1 },
|
||||||
|
SessionName = "Session name",
|
||||||
|
MapName = "Map name",
|
||||||
|
GameState = "openplaying",
|
||||||
|
GameType = "Game type",
|
||||||
|
PlayerInfo = new List<PlayerInfo>()
|
||||||
|
{
|
||||||
|
new PlayerInfo
|
||||||
|
{
|
||||||
|
Name = "test player",
|
||||||
|
TeamName = "Green",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
var postContent = JsonContent.Create(serverInfo);
|
||||||
|
|
||||||
|
using var postResponse = await client!.PostAsync(ServersRoute, postContent);
|
||||||
|
postResponse.EnsureSuccessStatusCode();
|
||||||
|
|
||||||
|
using var getResponse = await client!.GetAsync(ServersRoute);
|
||||||
|
getResponse.EnsureSuccessStatusCode();
|
||||||
|
|
||||||
|
var responseContent = await getResponse.Content.ReadAsStringAsync();
|
||||||
|
var responseObject = JsonSerializer.Deserialize<IEnumerable<ServerInfoWithHostAddress>>(responseContent, options);
|
||||||
|
|
||||||
|
Assert.IsNotNull(responseObject);
|
||||||
|
Assert.AreEqual(1, responseObject.Count());
|
||||||
|
|
||||||
|
var returnedServerInfo = responseObject.First();
|
||||||
|
Assert.AreEqual(serverInfo.GameName, returnedServerInfo.GameName);
|
||||||
|
Assert.AreEqual(serverInfo.Version, returnedServerInfo.Version);
|
||||||
|
Assert.AreEqual(serverInfo.SessionName, returnedServerInfo.SessionName);
|
||||||
|
Assert.AreEqual(serverInfo.MapName, returnedServerInfo.MapName);
|
||||||
|
Assert.AreEqual(serverInfo.GameState, returnedServerInfo.GameState);
|
||||||
|
Assert.AreEqual(serverInfo.GameType, returnedServerInfo.GameType);
|
||||||
|
Assert.IsTrue(serverInfo.PlayerInfo.SequenceEqual(returnedServerInfo.PlayerInfo));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -97,7 +97,7 @@ namespace Giants.Web.Controllers
|
|||||||
|
|
||||||
private string GetRequestIpAddress()
|
private string GetRequestIpAddress()
|
||||||
{
|
{
|
||||||
return this.httpContextAccessor.HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();
|
return this.httpContextAccessor.HttpContext.Connection.RemoteIpAddress?.MapToIPv4().ToString() ?? "<no ip>";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
namespace Giants.Web
|
namespace Giants.Web
|
||||||
{
|
{
|
||||||
using Autofac;
|
using Autofac;
|
||||||
using Autofac.Core;
|
|
||||||
using Autofac.Extensions.DependencyInjection;
|
using Autofac.Extensions.DependencyInjection;
|
||||||
using AutoMapper;
|
using AutoMapper;
|
||||||
using Giants.Services;
|
using Giants.Services;
|
||||||
@ -17,23 +16,25 @@ namespace Giants.Web
|
|||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.Identity.Web;
|
using Microsoft.Identity.Web;
|
||||||
using Microsoft.IdentityModel.Logging;
|
using Microsoft.IdentityModel.Logging;
|
||||||
using NSwag.Generation.Processors;
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net.Http.Headers;
|
using System.Net.Http.Headers;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
public class Program
|
public class Program
|
||||||
{
|
{
|
||||||
|
private static readonly List<Module> AdditionalRegistrationModules = new();
|
||||||
|
|
||||||
public static void Main(string[] args)
|
public static void Main(string[] args)
|
||||||
{
|
{
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
ConfigureServices(builder);
|
|
||||||
|
|
||||||
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
|
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
|
||||||
builder.Host.ConfigureContainer<ContainerBuilder>((containerBuilder) => ConfigureAutofacServices(containerBuilder, builder.Configuration));
|
builder.Host.ConfigureContainer<ContainerBuilder>((containerBuilder) => ConfigureAutofacServices(containerBuilder, builder.Configuration));
|
||||||
|
|
||||||
|
ConfigureServices(builder);
|
||||||
|
|
||||||
var app = builder
|
var app = builder
|
||||||
.Build();
|
.Build();
|
||||||
ConfigureApplication(app, app.Environment);
|
ConfigureApplication(app, app.Environment);
|
||||||
@ -41,6 +42,13 @@ namespace Giants.Web
|
|||||||
app.Run();
|
app.Run();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void AddAdditionalRegistrations(IList<Module> modules)
|
||||||
|
{
|
||||||
|
// Hack: ConfigureTestServices doesn't work with Autofac containers in .NET 6.
|
||||||
|
// Add test registrations to a static list, to be registered last.
|
||||||
|
AdditionalRegistrationModules.AddRange(modules);
|
||||||
|
}
|
||||||
|
|
||||||
private static void ConfigureServices(WebApplicationBuilder builder)
|
private static void ConfigureServices(WebApplicationBuilder builder)
|
||||||
{
|
{
|
||||||
var services = builder.Services;
|
var services = builder.Services;
|
||||||
@ -112,6 +120,11 @@ namespace Giants.Web
|
|||||||
private static void ConfigureAutofacServices(ContainerBuilder containerBuilder, IConfiguration configuration)
|
private static void ConfigureAutofacServices(ContainerBuilder containerBuilder, IConfiguration configuration)
|
||||||
{
|
{
|
||||||
containerBuilder.RegisterModule(new ServicesModule(configuration));
|
containerBuilder.RegisterModule(new ServicesModule(configuration));
|
||||||
|
|
||||||
|
foreach (var module in AdditionalRegistrationModules)
|
||||||
|
{
|
||||||
|
containerBuilder.RegisterModule(module);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void RegisterHttpClients(IServiceCollection services, IConfiguration configuration)
|
private static void RegisterHttpClients(IServiceCollection services, IConfiguration configuration)
|
||||||
|
Loading…
Reference in New Issue
Block a user