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
Don't assume port 80 when generating nextLink in ASP.NET Core #1559 #1560
Merged
biaol-odata
merged 3 commits into
OData:master
from
andrewlock:fix-assuming-port-80-when-generating-https-links
Aug 10, 2018
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
test/UnitTest/Microsoft.AspNetCore.OData.Test/Extensions/HttpRequestExtensionsTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
using Microsoft.AspNet.OData.Extensions; | ||
using Microsoft.AspNet.OData.Test.Abstraction; | ||
using Microsoft.AspNetCore.Http; | ||
using System; | ||
using System.Net.Http; | ||
using Xunit; | ||
|
||
namespace Microsoft.AspNet.OData.Test | ||
{ | ||
public class HttpRequestMessageExtensionsTest | ||
{ | ||
[Theory] | ||
[InlineData("http://localhost/Customers", 10, "http://localhost/Customers?$skip=10")] | ||
[InlineData("http://localhost/Customers?$filter=Age ge 18", 10, "http://localhost/Customers?$filter=Age%20ge%2018&$skip=10")] | ||
[InlineData("http://localhost/Customers?$top=20", 10, "http://localhost/Customers?$top=10&$skip=10")] | ||
[InlineData("http://localhost/Customers?$skip=5&$top=10", 2, "http://localhost/Customers?$top=8&$skip=7")] | ||
[InlineData("http://localhost/Customers?$filter=Age ge 18&$orderby=Name&$top=11&$skip=6", 10, "http://localhost/Customers?$filter=Age%20ge%2018&$orderby=Name&$top=1&$skip=16")] | ||
[InlineData("http://localhost/Customers?testkey%23%2B%3D%3F%26=testvalue%23%2B%3D%3F%26", 10, "http://localhost/Customers?testkey%23%2B%3D%3F%26=testvalue%23%2B%3D%3F%26&$skip=10")] | ||
public void GetNextPageLink_GetsNextPageLink(string requestUri, int pageSize, string nextPageUri) | ||
{ | ||
// Arrange | ||
HttpRequest request = RequestFactory.Create(HttpMethod.Get, requestUri); | ||
|
||
// Act | ||
Uri nextPageLink = request.GetNextPageLink(pageSize); | ||
|
||
// Assert | ||
Assert.Equal(nextPageUri, nextPageLink.AbsoluteUri); | ||
} | ||
|
||
[Theory] | ||
[InlineData("http://localhost/Customers", 10, "http://localhost/Customers?$skip=10")] | ||
[InlineData("https://localhost/Customers", 10, "https://localhost/Customers?$skip=10")] | ||
[InlineData("http://example.com/Customers", 10, "http://example.com/Customers?$skip=10")] | ||
[InlineData("https://example.com/Customers", 10, "https://example.com/Customers?$skip=10")] | ||
public void GetNextPageLink_UsesNoPortWhenNotSpecified(string requestUri, int pageSize, string nextPageUri) | ||
{ | ||
// Arrange | ||
HttpRequest request = RequestFactory.Create(HttpMethod.Get, requestUri); | ||
|
||
// Act | ||
Uri nextPageLink = request.GetNextPageLink(pageSize); | ||
|
||
// Assert | ||
Assert.Equal(nextPageUri, nextPageLink.AbsoluteUri); | ||
} | ||
|
||
[Theory] | ||
[InlineData("http://example.com:80/Customers", 10, "http://example.com/Customers?$skip=10")] | ||
[InlineData("https://example.com:443/Customers", 10, "https://example.com/Customers?$skip=10")] | ||
public void GetNextPageLink_UsesPortWhenDefaultPortIsProvided(string requestUri, int pageSize, string nextPageUri) | ||
{ | ||
// Arrange | ||
// The RequestFactory removes the default port, so we explicitly add it | ||
HttpRequest request = RequestFactory.Create(HttpMethod.Get, requestUri); | ||
Uri uri = new Uri(requestUri); | ||
var port = request.Scheme == "http" ? 80 : 443; | ||
request.Host = new HostString(uri.Host, port); | ||
|
||
// Act | ||
Uri nextPageLink = request.GetNextPageLink(pageSize); | ||
|
||
// Assert | ||
Assert.Equal(nextPageUri, nextPageLink.AbsoluteUri); | ||
} | ||
|
||
[Theory] | ||
[InlineData("http://localhost:808/Customers", 10, "http://localhost:808/Customers?$skip=10")] | ||
[InlineData("https://localhost:4443/Customers", 10, "https://localhost:4443/Customers?$skip=10")] | ||
[InlineData("http://localhost:5000/Customers", 10, "http://localhost:5000/Customers?$skip=10")] | ||
public void GetNextPageLink_UsesCorrectPortWhenSpecified(string requestUri, int pageSize, string nextPageUri) | ||
{ | ||
// Arrange | ||
HttpRequest request = RequestFactory.Create(HttpMethod.Get, requestUri); | ||
|
||
// Act | ||
Uri nextPageLink = request.GetNextPageLink(pageSize); | ||
|
||
// Assert | ||
Assert.Equal(nextPageUri, nextPageLink.AbsoluteUri); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For this case, when the port specified in uri is same as the default value. request.Host is formalized, so we add it back, and the generated linked is still the one w/o port number?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah -
UriBuilder
formalizes the uri insideGetNextPageLink()
too. Which is absolutely fine, the problem before was that in these cases, port 80 would be enforced, even if it was https!