From 7552fb8d3f15291cef79214b67790b5ae089469c Mon Sep 17 00:00:00 2001 From: Tomasz Pluskiewicz Date: Sun, 28 Apr 2019 16:28:57 +0200 Subject: [PATCH] Generate HTTP doc link behind proxy (#14) * add test for doc link behind proxy * handle x-forwarded-proto when generating doc link --- NuGet.config | 1 + .../RequestResponseExtensions.cs | 6 ++++ .../RequestResponseExtensionsTests.cs | 33 +++++++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 src/Argolis.Tests/ApiDocumentation/RequestResponseExtensionsTests.cs diff --git a/NuGet.config b/NuGet.config index 3f0e003..8256d05 100644 --- a/NuGet.config +++ b/NuGet.config @@ -2,5 +2,6 @@ + \ No newline at end of file diff --git a/src/Argolis.Hydra.Nancy/RequestResponseExtensions.cs b/src/Argolis.Hydra.Nancy/RequestResponseExtensions.cs index c50c95d..7ce9653 100644 --- a/src/Argolis.Hydra.Nancy/RequestResponseExtensions.cs +++ b/src/Argolis.Hydra.Nancy/RequestResponseExtensions.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using Nancy; namespace Argolis.Hydra.Nancy @@ -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) { diff --git a/src/Argolis.Tests/ApiDocumentation/RequestResponseExtensionsTests.cs b/src/Argolis.Tests/ApiDocumentation/RequestResponseExtensionsTests.cs new file mode 100644 index 0000000..b444434 --- /dev/null +++ b/src/Argolis.Tests/ApiDocumentation/RequestResponseExtensionsTests.cs @@ -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> + { + { "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"); + } + } +} \ No newline at end of file