Skip to content

Commit

Permalink
Generate HTTP doc link behind proxy (#14)
Browse files Browse the repository at this point in the history
* add test for doc link behind proxy

* handle x-forwarded-proto when generating doc link
  • Loading branch information
tpluscode committed Apr 28, 2019
1 parent 92ec2da commit 7552fb8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions NuGet.config
Expand Up @@ -2,5 +2,6 @@
<configuration>
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
<add key="Rulesets" value="https://www.myget.org/F/tpluscode/api/v3/index.json" protocolVersion="3" />
</packageSources>
</configuration>
6 changes: 6 additions & 0 deletions src/Argolis.Hydra.Nancy/RequestResponseExtensions.cs
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using Nancy;

namespace Argolis.Hydra.Nancy
Expand Down Expand Up @@ -46,6 +47,11 @@ public static string GetApiDocumentationUri(this Request request, string documen
Path = request.Url.BasePath + documentationPath
};

if (request.Headers["X-Forwarded-Proto"].Contains("https", StringComparer.OrdinalIgnoreCase))
{
uriBuilder.Scheme = "https";
}

string apiDocPath = uriBuilder.Uri.ToString();
if (uriBuilder.Port == 80)
{
Expand Down
@@ -0,0 +1,33 @@
using System.Collections.Generic;
using Argolis.Hydra.Nancy;
using FluentAssertions;
using Nancy;
using Xunit;

namespace Argolis.Tests.ApiDocumentation
{
public class RequestResponseExtensionsTests
{
[Theory]
[InlineData("https")]
[InlineData("HTTPS")]
public void GetApiDocumentationUri_should_honor_X_Forwarded_Proto(string proto)
{
// given
var headers = new Dictionary<string, IEnumerable<string>>
{
{ "X-Forwarded-Proto", new[] { proto } }
};
var request = new Request(
"GET",
new Url("http://hydra.app/some/resource"),
headers: headers);

// when
var apiDocUri = request.GetApiDocumentationUri("/api/doc");

// then
apiDocUri.Should().Be("https://hydra.app/api/doc");
}
}
}

0 comments on commit 7552fb8

Please sign in to comment.