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

Added method to get query string parameters insignificant of order #36

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
156 changes: 153 additions & 3 deletions src/UriTemplateTests/ParameterMatchingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public void MatchUriToTemplate()
Assert.True(match);
}

#region GetParameters()

[Fact]
public void GetParameters()
{
Expand All @@ -38,7 +40,7 @@ public void GetParameters()

var match = regex.Match(uri.AbsoluteUri);

Assert.Equal("foo",match.Groups["p1"].Value);
Assert.Equal("foo", match.Groups["p1"].Value);
Assert.Equal("bar", match.Groups["p2"].Value);
}

Expand Down Expand Up @@ -87,6 +89,7 @@ public void GetParametersFromMultipleQueryString()
Assert.Equal("45", parameters["blur"]);

}

[Fact]
public void GetParametersFromMultipleQueryStringWithTwoParamValues()
{
Expand Down Expand Up @@ -135,6 +138,155 @@ public void GetParametersFromMultipleQueryStringWithOptionalParameters()

}

[Fact]
public void GetParametersRespectsOrderOfQueryParameters()
{
var uri = new Uri("http://example.com/foo/bar?blob=23&blur=45");

var template = new UriTemplate("http://example.com/{+p1}/{p2*}{?blur,blob}");

var parameters = template.GetParameters(uri);

Assert.Null(parameters);
}

[Fact]
public void GetParametersReturnsNullWhenTheresNoMatch()
{
var uri = new Uri("http://example.com/foo/bar?one=23&two=45");

var template = new UriTemplate("http://example.com/foo/bar{?blur,blob}");

var parameters = template.GetParameters(uri);

Assert.Null(parameters);
}

#endregion

#region GetParametersNonStrict()

[Fact]
public void GetParametersNonStrictWithOperators()
{
var uri = new Uri("http://example.com/foo/bar");

var template = new UriTemplate("http://example.com/{+p1}/{p2*}");

var parameters = template.GetParametersNonStrict(uri);

Assert.Equal(2, parameters.Count);
Assert.Equal("foo", parameters["p1"]);
Assert.Equal("bar", parameters["p2"]);
}

[Fact]
public void GetParametersNonStrictFromQueryString()
{
var uri = new Uri("http://example.com/foo/bar?blur=45");

var template = new UriTemplate("http://example.com/{+p1}/{p2*}{?blur}");

var parameters = template.GetParametersNonStrict(uri);

Assert.Equal(3, parameters.Count);

Assert.Equal("foo", parameters["p1"]);
Assert.Equal("bar", parameters["p2"]);
Assert.Equal("45", parameters["blur"]);
}

[Fact]
public void GetParametersNonStrictFromMultipleQueryString()
{
var uri = new Uri("http://example.com/foo/bar?blur=45");

var template = new UriTemplate("http://example.com/{+p1}/{p2*}{?blur,blob}");

var parameters = template.GetParametersNonStrict(uri);

Assert.Equal(3, parameters.Count);
Assert.Equal("foo", parameters["p1"]);
Assert.Equal("bar", parameters["p2"]);
Assert.Equal("45", parameters["blur"]);

}

[Fact]
public void GetParametersNonStrictFromMultipleQueryStringWithTwoParamValues()
{
var uri = new Uri("http://example.com/foo/bar?blur=45&blob=23");

var template = new UriTemplate("http://example.com/{+p1}/{p2*}{?blur,blob}");

var parameters = template.GetParametersNonStrict(uri);

Assert.Equal(4, parameters.Count);
Assert.Equal("foo", parameters["p1"]);
Assert.Equal("bar", parameters["p2"]);
Assert.Equal("45", parameters["blur"]);
Assert.Equal("23", parameters["blob"]);
}

[Fact]
public void GetParametersNonStrictFromMultipleQueryStringWithOptionalAndMandatoryParameters()
{
var uri = new Uri("http://example.com/foo/bar?blur=45&blob=23");

var template = new UriTemplate("http://example.com/{+p1}/{p2*}{?blur}{&blob}");

var parameters = template.GetParametersNonStrict(uri);

Assert.Equal(4, parameters.Count);
Assert.Equal("foo", parameters["p1"]);
Assert.Equal("bar", parameters["p2"]);
Assert.Equal("45", parameters["blur"]);
Assert.Equal("23", parameters["blob"]);
}

[Fact]
public void GetParametersNonStrictFromMultipleQueryStringWithOptionalParameters()
{
var uri = new Uri("http://example.com/foo/bar");

var template = new UriTemplate("http://example.com/{+p1}/{p2*}{?blur,blob}");

var parameters = template.GetParametersNonStrict(uri);

Assert.Equal("foo", parameters["p1"]);
Assert.Equal("bar", parameters["p2"]);
}

[Fact]
public void GetParametersNonStrictIgnoresOrderOfQueryParameters()
{
var uri = new Uri("http://example.com/foo/bar?blob=23&blur=45&irrelevant=true");

var template = new UriTemplate("http://example.com/{+p1}/{p2*}{?blur,blob}");

var parameters = template.GetParametersNonStrict(uri);

Assert.Equal(4, parameters.Count);
Assert.Equal("foo", parameters["p1"]);
Assert.Equal("bar", parameters["p2"]);
Assert.Equal("23", parameters["blob"]);
Assert.Equal("45", parameters["blur"]);
}

[Fact]
public void GetParametersNonStrictReturnsEmptyDictionaryWhenTheresNoMatch()
{
var uri = new Uri("http://example.com/foo/bar?one=23&two=45");

var template = new UriTemplate("http://example.com/foo/bar{?blur,blob}");

var parameters = template.GetParametersNonStrict(uri);

Assert.NotNull(parameters);
Assert.Equal(0, parameters.Count);
}

#endregion

[Fact]
public void TestGlimpseUrl()
Expand Down Expand Up @@ -163,8 +315,6 @@ public void TestUrlWithQuestionMarkAsFirstCharacter()

}



[Fact]
public void TestExactParameterCount()
{
Expand Down
Loading