From eda8d6cf6743623acd7abcfea480d1779130df85 Mon Sep 17 00:00:00 2001 From: Nick Blakely Date: Mon, 12 Sep 2022 20:06:37 -0700 Subject: [PATCH] Add tests for Get/Add servers. --- Giants.WebApi.Tests/ServersControllerTests.cs | 108 ++++++++++++++++++ .../Controllers/ServersController.cs | 2 +- Giants.WebApi/Program.cs | 21 +++- 3 files changed, 126 insertions(+), 5 deletions(-) create mode 100644 Giants.WebApi.Tests/ServersControllerTests.cs diff --git a/Giants.WebApi.Tests/ServersControllerTests.cs b/Giants.WebApi.Tests/ServersControllerTests.cs new file mode 100644 index 0000000..fd08dee --- /dev/null +++ b/Giants.WebApi.Tests/ServersControllerTests.cs @@ -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? application; + private static HttpClient? client; + + private class TestRegistrations : Module + { + protected override void Load(ContainerBuilder builder) + { + builder + .RegisterType() + .As() + .SingleInstance(); + } + } + + [ClassInitialize] + public static void Initialize(TestContext context) + { + application = new WebApplicationFactory() + .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>(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() + { + 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>(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)); + } + } +} diff --git a/Giants.WebApi/Controllers/ServersController.cs b/Giants.WebApi/Controllers/ServersController.cs index 44bac58..9b198fa 100644 --- a/Giants.WebApi/Controllers/ServersController.cs +++ b/Giants.WebApi/Controllers/ServersController.cs @@ -97,7 +97,7 @@ namespace Giants.Web.Controllers private string GetRequestIpAddress() { - return this.httpContextAccessor.HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString(); + return this.httpContextAccessor.HttpContext.Connection.RemoteIpAddress?.MapToIPv4().ToString() ?? ""; } } } diff --git a/Giants.WebApi/Program.cs b/Giants.WebApi/Program.cs index 36fd81a..4229886 100644 --- a/Giants.WebApi/Program.cs +++ b/Giants.WebApi/Program.cs @@ -1,7 +1,6 @@ namespace Giants.Web { using Autofac; - using Autofac.Core; using Autofac.Extensions.DependencyInjection; using AutoMapper; using Giants.Services; @@ -17,23 +16,25 @@ namespace Giants.Web using Microsoft.Extensions.Logging; using Microsoft.Identity.Web; using Microsoft.IdentityModel.Logging; - using NSwag.Generation.Processors; using System; + using System.Collections.Generic; using System.Linq; using System.Net.Http.Headers; using System.Threading.Tasks; public class Program { + private static readonly List AdditionalRegistrationModules = new(); + public static void Main(string[] args) { var builder = WebApplication.CreateBuilder(args); - ConfigureServices(builder); - builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory()); builder.Host.ConfigureContainer((containerBuilder) => ConfigureAutofacServices(containerBuilder, builder.Configuration)); + ConfigureServices(builder); + var app = builder .Build(); ConfigureApplication(app, app.Environment); @@ -41,6 +42,13 @@ namespace Giants.Web app.Run(); } + public static void AddAdditionalRegistrations(IList 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) { var services = builder.Services; @@ -112,6 +120,11 @@ namespace Giants.Web private static void ConfigureAutofacServices(ContainerBuilder containerBuilder, IConfiguration configuration) { containerBuilder.RegisterModule(new ServicesModule(configuration)); + + foreach (var module in AdditionalRegistrationModules) + { + containerBuilder.RegisterModule(module); + } } private static void RegisterHttpClients(IServiceCollection services, IConfiguration configuration)