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

used _baseUrl.AbsoluteUri instead of _baseUrl to avoid included port number in the self link #206

Merged
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
2 changes: 1 addition & 1 deletion Saule/Serialization/ResourceSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private JToken CreateTopLevelLinks(int count)
// to preserve back compatibility if Self is enabled, then we also render it. Or if TopSelf is enabled
if (_resource.LinkType.HasFlag(LinkType.TopSelf) || _resource.LinkType.HasFlag(LinkType.Self))
{
result.Add("self", _baseUrl);
result.Add("self", _baseUrl.AbsoluteUri);
}

var queryStrings = new PaginationQuery(_paginationContext);
Expand Down
4 changes: 2 additions & 2 deletions Tests/Serialization/ResourceSerializerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,14 @@ public void UsesCustomIdInUrls()
var job = result["data"]["relationships"]["job"]["links"];
var friends = result["data"]["relationships"]["friends"]["links"];

var self = result["links"]["self"].Value<Uri>().AbsolutePath;
var self = result["links"]["self"].Value<string>();
var jobSelf = job["self"].Value<Uri>().AbsolutePath;
var jobRelated = job["related"].Value<Uri>().AbsolutePath;
var friendsSelf = friends["self"].Value<Uri>().AbsolutePath;
var friendsRelated = friends["related"].Value<Uri>().AbsolutePath;
var included = result["included"][0]["links"]["self"].Value<Uri>().AbsolutePath;

Assert.Equal("/api/people/abc", self);
Assert.EndsWith("/api/people/abc", self);
Assert.Equal("/api/people/abc/relationships/employer/", jobSelf);
Assert.Equal("/api/people/abc/employer/", jobRelated);
Assert.Equal("/api/people/abc/relationships/friends/", friendsSelf);
Expand Down
25 changes: 19 additions & 6 deletions Tests/Serialization/UrlConstructionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ public void HandlesQueryParams()

var jobLinks = result["data"]?["relationships"]?["job"]?["links"];

var selfLink = result["links"].Value<Uri>("self")?.PathAndQuery;
var selfLink = result["links"].Value<string>("self");
var jobSelfLink = jobLinks?.Value<Uri>("self")?.PathAndQuery;
var jobRelationLink = jobLinks?.Value<Uri>("related")?.PathAndQuery;

Assert.Equal("/api/people/123?a=b&c=d", selfLink);
Assert.EndsWith("/api/people/123?a=b&c=d", selfLink);
Assert.Equal("/api/people/123/relationships/employer/", jobSelfLink);
Assert.Equal("/api/people/123/employer/", jobRelationLink);
}
Expand Down Expand Up @@ -80,9 +80,22 @@ public void SelfLink()
var result = target.Serialize();
_output.WriteLine(result.ToString());

var selfLink = result["links"].Value<Uri>("self").AbsolutePath;
var selfLink = result["links"].Value<string>("self");

Assert.Equal("/api/people/123", selfLink);
Assert.EndsWith("/api/people/123", selfLink);
}

[Fact(DisplayName = "Adds top level self link without any port")]
public void SelfLinkNoPort()
{
var target = new ResourceSerializer(Get.Person(), new PersonResource(),
new Uri("http://localhost:80/api/people/123"), DefaultPathBuilder, null, null, null);
var result = target.Serialize();
_output.WriteLine(result.ToString());

var selfLink = result["links"].Value<string>("self");

Assert.Equal("http://localhost/api/people/123", selfLink);
}

[Fact(DisplayName = "Adds top level self link if only LinkType.TopSelf is specified")]
Expand All @@ -93,9 +106,9 @@ public void TopLinkOnlyLink()
var result = target.Serialize();
_output.WriteLine(result.ToString());

var selfLink = result["links"].Value<Uri>("self").AbsolutePath;
var selfLink = result["links"].Value<string>("self");

Assert.Equal("/api/people/123", selfLink);
Assert.EndsWith("/api/people/123", selfLink);

Assert.Null(result["data"][0]["links"]);
}
Expand Down