Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit test for Forwarded Headers Middleware #967

Merged
merged 3 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dotnet/src/dotnetcore/GxNetCoreStartup/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Rewrite;
using Microsoft.AspNetCore.Routing;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
<None Update="apps\httpcors.svc">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="apps\httpheaders.svc">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="apps\returnsession.svc">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
40 changes: 40 additions & 0 deletions dotnet/test/DotNetCoreWebUnitTest/Middleware/HeadersTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Net.Http;
using System.Reflection;
using System.Threading.Tasks;
using GeneXus.Metadata;
using Xunit;
namespace xUnitTesting
{
public class HeadersTest : MiddlewareTest
{
public HeadersTest() : base()
{
ClassLoader.FindType("apps.httpheaders", "GeneXus.Programs.apps", "httpheaders", Assembly.GetExecutingAssembly(), true);//Force loading assembly for append procedure
server.AllowSynchronousIO = true;

}
protected override void SetEnvironmentVars()
{
Environment.SetEnvironmentVariable("ASPNETCORE_FORWARDEDHEADERS_ENABLED", "true", EnvironmentVariableTarget.Process);

}
[Fact]
public async Task TestForwardedHeaders()
{
const string host = "192.168.1.100";
const string scheme = "https";
const string remoteUrl = $"{scheme}:\\/\\/{host}";
HttpClient client = server.CreateClient();
client.DefaultRequestHeaders.Add("X-Forwarded-For", host);
client.DefaultRequestHeaders.Add("X-Forwarded-Proto", scheme);

HttpResponseMessage response = await client.GetAsync("/rest/apps/httpheaders");
response.EnsureSuccessStatusCode();
string resp = await response.Content.ReadAsStringAsync();
Assert.Contains(remoteUrl, resp, System.StringComparison.OrdinalIgnoreCase);
Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode);
}

}
}
57 changes: 57 additions & 0 deletions dotnet/test/DotNetCoreWebUnitTest/apps/httpheaders.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using GeneXus.Application;
using GeneXus.Data.NTier;
using GeneXus.Procedure;

namespace GeneXus.Programs.apps
{
public class httpheaders : GXWebProcedure
{

public httpheaders()
{
context = new GxContext();
DataStoreUtil.LoadDataStores(context);
IsMain = true;
context.SetDefaultTheme("Carmine");
}

public httpheaders(IGxContext context)
{
this.context = context;
IsMain = false;
}

public void execute(out string result)
{
initialize();
executePrivate(out result);
}

void executePrivate(out string result)
{
result = (context.GetHttpSecure() == 1 ? "https://" : "http://") + context.GetRemoteAddress();

cleanup();
}

public override void cleanup()
{
CloseOpenCursors();
base.cleanup();
if (IsMain)
{
context.CloseConnections();
}
ExitApp();
}

protected void CloseOpenCursors()
{
}

public override void initialize()
{
}
}

}
1 change: 1 addition & 0 deletions dotnet/test/DotNetCoreWebUnitTest/apps/httpheaders.svc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%@ServiceHost Service= "GeneXus.Programs.apps.httpheaders,apps.httpheaders" %>
Loading