From e4bb16100dd0797e97e0f2787aa77c35c5e67471 Mon Sep 17 00:00:00 2001 From: jvyden Date: Thu, 18 Nov 2021 15:08:51 -0500 Subject: [PATCH] Fix tests ..ish. --- ProjectLighthouse.Tests/LighthouseTest.cs | 2 +- ProjectLighthouse.Tests/Tests/SlotTests.cs | 16 +++++++++---- ProjectLighthouse.sln.DotSettings | 1 + .../FakeRemoteIPAddressMiddleware.cs | 24 +++++++++++++++++++ ProjectLighthouse/Startup.cs | 2 +- ProjectLighthouse/TestStartup.cs | 18 ++++++++++++++ 6 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 ProjectLighthouse/FakeRemoteIPAddressMiddleware.cs create mode 100644 ProjectLighthouse/TestStartup.cs diff --git a/ProjectLighthouse.Tests/LighthouseTest.cs b/ProjectLighthouse.Tests/LighthouseTest.cs index 00a7e191..df207d74 100644 --- a/ProjectLighthouse.Tests/LighthouseTest.cs +++ b/ProjectLighthouse.Tests/LighthouseTest.cs @@ -19,7 +19,7 @@ namespace LBPUnion.ProjectLighthouse.Tests public LighthouseTest() { - this.Server = new TestServer(new WebHostBuilder().UseStartup()); + this.Server = new TestServer(new WebHostBuilder().UseStartup()); this.Client = this.Server.CreateClient(); } diff --git a/ProjectLighthouse.Tests/Tests/SlotTests.cs b/ProjectLighthouse.Tests/Tests/SlotTests.cs index ea0ea3c3..65abac0f 100644 --- a/ProjectLighthouse.Tests/Tests/SlotTests.cs +++ b/ProjectLighthouse.Tests/Tests/SlotTests.cs @@ -1,3 +1,4 @@ +using System.Net.Http; using System.Threading.Tasks; using LBPUnion.ProjectLighthouse.Types; using LBPUnion.ProjectLighthouse.Types.Levels; @@ -48,10 +49,17 @@ namespace LBPUnion.ProjectLighthouse.Tests LoginResult loginResult = await this.Authenticate(); - string respA = await (await this.AuthenticatedRequest("LITTLEBIGPLANETPS3_XML/slots/by?u=unitTestUser0", loginResult.AuthTicket)).Content - .ReadAsStringAsync(); - string respB = await (await this.AuthenticatedRequest("LITTLEBIGPLANETPS3_XML/slots/by?u=unitTestUser1", loginResult.AuthTicket)).Content - .ReadAsStringAsync(); + HttpResponseMessage respMessageA = await this.AuthenticatedRequest("LITTLEBIGPLANETPS3_XML/slots/by?u=unitTestUser0", loginResult.AuthTicket); + HttpResponseMessage respMessageB = await this.AuthenticatedRequest("LITTLEBIGPLANETPS3_XML/slots/by?u=unitTestUser1", loginResult.AuthTicket); + + Assert.True(respMessageA.IsSuccessStatusCode); + Assert.True(respMessageB.IsSuccessStatusCode); + + string respA = await respMessageA.Content.ReadAsStringAsync(); + string respB = await respMessageB.Content.ReadAsStringAsync(); + + Assert.False(string.IsNullOrEmpty(respA)); + Assert.False(string.IsNullOrEmpty(respB)); Assert.NotEqual(respA, respB); Assert.DoesNotContain(respA, "slotB"); diff --git a/ProjectLighthouse.sln.DotSettings b/ProjectLighthouse.sln.DotSettings index 5e7b6c93..34c3a59a 100644 --- a/ProjectLighthouse.sln.DotSettings +++ b/ProjectLighthouse.sln.DotSettings @@ -73,6 +73,7 @@ UseExplicitType UseExplicitType DLC + IP LBP MM NAT diff --git a/ProjectLighthouse/FakeRemoteIPAddressMiddleware.cs b/ProjectLighthouse/FakeRemoteIPAddressMiddleware.cs new file mode 100644 index 00000000..6a5a200e --- /dev/null +++ b/ProjectLighthouse/FakeRemoteIPAddressMiddleware.cs @@ -0,0 +1,24 @@ +using System.Net; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; + +namespace LBPUnion.ProjectLighthouse +{ + public class FakeRemoteIPAddressMiddleware + { + private readonly RequestDelegate next; + private readonly IPAddress fakeIpAddress = IPAddress.Parse("127.0.0.1"); + + public FakeRemoteIPAddressMiddleware(RequestDelegate next) + { + this.next = next; + } + + public async Task Invoke(HttpContext httpContext) + { + httpContext.Connection.RemoteIpAddress = this.fakeIpAddress; + + await this.next(httpContext); + } + } +} \ No newline at end of file diff --git a/ProjectLighthouse/Startup.cs b/ProjectLighthouse/Startup.cs index 106b27da..93e41f3d 100644 --- a/ProjectLighthouse/Startup.cs +++ b/ProjectLighthouse/Startup.cs @@ -51,7 +51,7 @@ namespace LBPUnion.ProjectLighthouse } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + public virtual void Configure(IApplicationBuilder app, IWebHostEnvironment env) { bool computeDigests = true; string serverDigestKey = Environment.GetEnvironmentVariable("SERVER_DIGEST_KEY"); diff --git a/ProjectLighthouse/TestStartup.cs b/ProjectLighthouse/TestStartup.cs new file mode 100644 index 00000000..2601c9ca --- /dev/null +++ b/ProjectLighthouse/TestStartup.cs @@ -0,0 +1,18 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; + +namespace LBPUnion.ProjectLighthouse +{ + public class TestStartup : Startup + { + public TestStartup(IConfiguration configuration) : base(configuration) + {} + + public override void Configure(IApplicationBuilder app, IWebHostEnvironment env) + { + app.UseMiddleware(); + base.Configure(app, env); + } + } +} \ No newline at end of file