Skip to content

Commit 47e6c87

Browse files
Unit test for Forwarded Headers Middleware (#967)
* Enabled Forwarded Headers Middleware. * Consider the scheme in the unit test. * Remove Forward headers Middleware as it is enabled by default when ASPNETCORE_FORWARDEDHEADERS_ENABLED environment variable is true.
1 parent 6cc29e1 commit 47e6c87

File tree

5 files changed

+102
-0
lines changed

5 files changed

+102
-0
lines changed

dotnet/src/dotnetcore/GxNetCoreStartup/Startup.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using Microsoft.AspNetCore.Hosting;
1919
using Microsoft.AspNetCore.Http;
2020
using Microsoft.AspNetCore.Http.Features;
21+
using Microsoft.AspNetCore.HttpOverrides;
2122
using Microsoft.AspNetCore.Mvc;
2223
using Microsoft.AspNetCore.Rewrite;
2324
using Microsoft.AspNetCore.Routing;

dotnet/test/DotNetCoreWebUnitTest/DotNetCoreWebUnitTest.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@
5959
<None Update="apps\httpcors.svc">
6060
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
6161
</None>
62+
<None Update="apps\httpheaders.svc">
63+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
64+
</None>
6265
<None Update="apps\returnsession.svc">
6366
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
6467
</None>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.Net.Http;
3+
using System.Reflection;
4+
using System.Threading.Tasks;
5+
using GeneXus.Metadata;
6+
using Xunit;
7+
namespace xUnitTesting
8+
{
9+
public class HeadersTest : MiddlewareTest
10+
{
11+
public HeadersTest() : base()
12+
{
13+
ClassLoader.FindType("apps.httpheaders", "GeneXus.Programs.apps", "httpheaders", Assembly.GetExecutingAssembly(), true);//Force loading assembly for append procedure
14+
server.AllowSynchronousIO = true;
15+
16+
}
17+
protected override void SetEnvironmentVars()
18+
{
19+
Environment.SetEnvironmentVariable("ASPNETCORE_FORWARDEDHEADERS_ENABLED", "true", EnvironmentVariableTarget.Process);
20+
21+
}
22+
[Fact]
23+
public async Task TestForwardedHeaders()
24+
{
25+
const string host = "192.168.1.100";
26+
const string scheme = "https";
27+
const string remoteUrl = $"{scheme}:\\/\\/{host}";
28+
HttpClient client = server.CreateClient();
29+
client.DefaultRequestHeaders.Add("X-Forwarded-For", host);
30+
client.DefaultRequestHeaders.Add("X-Forwarded-Proto", scheme);
31+
32+
HttpResponseMessage response = await client.GetAsync("/rest/apps/httpheaders");
33+
response.EnsureSuccessStatusCode();
34+
string resp = await response.Content.ReadAsStringAsync();
35+
Assert.Contains(remoteUrl, resp, System.StringComparison.OrdinalIgnoreCase);
36+
Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode);
37+
}
38+
39+
}
40+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using GeneXus.Application;
2+
using GeneXus.Data.NTier;
3+
using GeneXus.Procedure;
4+
5+
namespace GeneXus.Programs.apps
6+
{
7+
public class httpheaders : GXWebProcedure
8+
{
9+
10+
public httpheaders()
11+
{
12+
context = new GxContext();
13+
DataStoreUtil.LoadDataStores(context);
14+
IsMain = true;
15+
context.SetDefaultTheme("Carmine");
16+
}
17+
18+
public httpheaders(IGxContext context)
19+
{
20+
this.context = context;
21+
IsMain = false;
22+
}
23+
24+
public void execute(out string result)
25+
{
26+
initialize();
27+
executePrivate(out result);
28+
}
29+
30+
void executePrivate(out string result)
31+
{
32+
result = (context.GetHttpSecure() == 1 ? "https://" : "http://") + context.GetRemoteAddress();
33+
34+
cleanup();
35+
}
36+
37+
public override void cleanup()
38+
{
39+
CloseOpenCursors();
40+
base.cleanup();
41+
if (IsMain)
42+
{
43+
context.CloseConnections();
44+
}
45+
ExitApp();
46+
}
47+
48+
protected void CloseOpenCursors()
49+
{
50+
}
51+
52+
public override void initialize()
53+
{
54+
}
55+
}
56+
57+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<%@ServiceHost Service= "GeneXus.Programs.apps.httpheaders,apps.httpheaders" %>

0 commit comments

Comments
 (0)