Skip to content

Commit

Permalink
ensure port is removed from context URL
Browse files Browse the repository at this point in the history
  • Loading branch information
tpluscode committed Apr 28, 2019
1 parent a367bfe commit 84ad149
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
33 changes: 31 additions & 2 deletions src/Nancy.Rdf.Tests/RdfResponseProcessorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void Should_pass_SiteBase_from_context_to_serializtion()
// then
A.CallTo(() => serializer.Serialize(
A<MediaRange>.That.Matches(mr => mr == RdfSerialization.RdfXml.MediaType),
A<WrappedModel>.That.Matches(wm => wm.BaseUrl == new Uri(NancyContext.Request.Url.SiteBase)),
A<WrappedModel>.That.Matches(wm => wm.BaseUrl == new Uri("http://example.com/")),
A<MemoryStream>._)).MustHaveHappened();
}

Expand Down Expand Up @@ -142,7 +142,36 @@ public void Should_use_SSL_if_x_Forwarded_Proto_header_present()
// then
A.CallTo(() => serializer.Serialize(
A<MediaRange>.That.Matches(mr => mr == RdfSerialization.RdfXml.MediaType),
A<WrappedModel>.That.Matches(wm => wm.BaseUrl == new Uri("https://example.com:80/")),
A<WrappedModel>.That.Matches(wm => wm.BaseUrl.ToString() == "https://example.com/"),
A<MemoryStream>._)).MustHaveHappened();
}

[Test]
public void Should_not_contain_unnecessary_443_port_in_base_url()
{
// given
var serializer = A.Fake<IRdfSerializer>();
A.CallTo(() => serializer.CanSerialize(A<MediaRange>.Ignored)).Returns(true);
var processor = new RdfResponseProcessorTestable(new[] { serializer });

var path = new Url("https://example.com/api/test")
{
BasePath = "api"
};

var nancyContext = new NancyContext
{
Request = new Request("GET", path)
};

// when
var response = processor.Process(new MediaRange("application/rdf+xml"), new object(), nancyContext);
response.Contents(new MemoryStream());

// then
A.CallTo(() => serializer.Serialize(
A<MediaRange>.That.Matches(mr => mr == RdfSerialization.RdfXml.MediaType),
A<WrappedModel>.That.Matches(wm => wm.BaseUrl.ToString() == "https://example.com/"),
A<MemoryStream>._)).MustHaveHappened();
}

Expand Down
20 changes: 18 additions & 2 deletions src/Nancy.Rdf/Responses/RdfResponseProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,27 @@ public ProcessorMatch CanProcess(MediaRange requestedMediaRange, [AllowNull] dyn
/// <returns>a response</returns>
public Response Process(MediaRange requestedMediaRange, dynamic model, NancyContext context)
{
var siteBase = context.Request.Url.SiteBase;
var siteBaseUri = new UriBuilder(context.Request.Url.SiteBase);

if (context.Request.Headers["X-Forwarded-Proto"].Any(v => v == "https"))
{
var siteBaseUri = new UriBuilder(siteBase) {Scheme = "HTTPS"};
siteBaseUri.Scheme = "HTTPS";
siteBaseUri.Port = 443;
}

string siteBase;
var usesHttpsDefaultPort = siteBaseUri.Scheme.Equals("https", StringComparison.OrdinalIgnoreCase) &&
siteBaseUri.Port == 443;
var usesHttpDefaultPort = siteBaseUri.Scheme.Equals("http", StringComparison.OrdinalIgnoreCase) &&
siteBaseUri.Port == 80;
if (usesHttpDefaultPort || usesHttpsDefaultPort)
{
siteBase = siteBaseUri.Uri.GetComponents(
UriComponents.AbsoluteUri & ~UriComponents.Port,
UriFormat.UriEscaped);
}
else
{
siteBase = siteBaseUri.ToString();
}

Expand Down
5 changes: 5 additions & 0 deletions src/Nancy.Rdf/Responses/WrappedModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,10 @@ public WrappedModel(object model, string siteBase)
/// Gets the actual serialized model.
/// </summary>
public object Model { get; private set; }

public override string ToString()
{
return $"{BaseUrl}";
}
}
}

0 comments on commit 84ad149

Please sign in to comment.