mirror of
https://github.com/ncblakely/GiantsTools
synced 2024-12-05 02:53:08 +01:00
Compare commits
No commits in common. "e817ee78b5ce6dcc44d1a2c45b784d582d4a3b72" and "a0e81ce82444b16320312b441470a85bb53adac0" have entirely different histories.
e817ee78b5
...
a0e81ce824
@ -1,108 +0,0 @@
|
||||
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()
|
||||
{
|
||||
return this.httpContextAccessor.HttpContext.Connection.RemoteIpAddress?.MapToIPv4().ToString() ?? "<no ip>";
|
||||
return this.httpContextAccessor.HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
namespace Giants.Web
|
||||
{
|
||||
using Autofac;
|
||||
using Autofac.Core;
|
||||
using Autofac.Extensions.DependencyInjection;
|
||||
using AutoMapper;
|
||||
using Giants.Services;
|
||||
@ -16,25 +17,23 @@ 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<Module> AdditionalRegistrationModules = new();
|
||||
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
ConfigureServices(builder);
|
||||
|
||||
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
|
||||
builder.Host.ConfigureContainer<ContainerBuilder>((containerBuilder) => ConfigureAutofacServices(containerBuilder, builder.Configuration));
|
||||
|
||||
ConfigureServices(builder);
|
||||
|
||||
var app = builder
|
||||
.Build();
|
||||
ConfigureApplication(app, app.Environment);
|
||||
@ -42,13 +41,6 @@ namespace Giants.Web
|
||||
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)
|
||||
{
|
||||
var services = builder.Services;
|
||||
@ -120,11 +112,6 @@ 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)
|
||||
|
Loading…
Reference in New Issue
Block a user