Skip to content

Commit

Permalink
fix URL encoding (nmklotas#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
jetersen authored and MindaugasLaganeckas committed Mar 9, 2021
1 parent 53f2acc commit c3cb6ec
Show file tree
Hide file tree
Showing 13 changed files with 24 additions and 32 deletions.
1 change: 0 additions & 1 deletion src/GitLabApiClient/GitLabApiClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

<ItemGroup Condition="'$(TargetFramework)' == 'net48'">
<Reference Include="System.Net.Http" />
<Reference Include="System.Web" />
</ItemGroup>

</Project>
5 changes: 2 additions & 3 deletions src/GitLabApiClient/Internal/Queries/QueryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using GitLabApiClient.Internal.Utilities;
using GitLabApiClient.Models;

Expand Down Expand Up @@ -86,9 +85,9 @@ private static string ToQueryString(NameValueCollection nvc)
var array =
from key in nvc.AllKeys
from value in nvc.GetValues(key)
select string.Format("{0}={1}", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(value));
select $"{key.UrlEncode()}={value.UrlEncode()}";

return "?" + string.Join("&", array);
return $"?{string.Join("&", array)}";
}
}
}
4 changes: 1 addition & 3 deletions src/GitLabApiClient/Internal/Utilities/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Web;

namespace GitLabApiClient.Internal.Utilities
{
internal static class Extensions
Expand All @@ -19,6 +17,6 @@ public static string ToLowerCaseString(this object obj) =>
/// <param name="value">URL path</param>
/// <returns>Encoded URL path</returns>
public static string UrlEncode(this string value) =>
value.Contains("%") ? value : HttpUtility.UrlEncode(value);
value.Contains("%") ? value : System.Uri.EscapeDataString(value);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using GitLabApiClient.Internal.Utilities;
using GitLabApiClient.Internal.Utilities;
using Newtonsoft.Json;

namespace GitLabApiClient.Models.Webhooks.Requests
Expand Down
4 changes: 2 additions & 2 deletions test/GitLabApiClient.Test/Internal/GroupIdConversionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class GroupIdConversionTest
[Fact]
public void GroupId_String_Conversion()
{
string expected = "groups/group%2fmy_awe-some.subgroup%2fsubgroup";
string expected = "groups/group%2Fmy_awe-some.subgroup%2Fsubgroup";
GroupId sut = "group/my_awe-some.subgroup/subgroup";
string result = $"groups/{sut}";
result.Should().Be(expected);
Expand All @@ -38,7 +38,7 @@ public void GroupId_Int_Conversion()
[Fact]
public void GroupId_Group_Path_Conversion()
{
string expected = "groups/group%2fmy_awe-some.subgroup%2fsubgroup";
string expected = "groups/group%2Fmy_awe-some.subgroup%2Fsubgroup";
GroupId sut = new Group { FullPath = "group/my_awe-some.subgroup/subgroup" };
string result = $"groups/{sut}";
result.Should().Be(expected);
Expand Down
4 changes: 2 additions & 2 deletions test/GitLabApiClient.Test/Internal/ProjectIdConversionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class ProjectIdConversionTest
[Fact]
public void ProjectId_String_Conversion()
{
string expected = "projects/group%2fmy_awe-some.project";
string expected = "projects/group%2Fmy_awe-some.project";
ProjectId sut = "group/my_awe-some.project";
string result = $"projects/{sut}";
result.Should().Be(expected);
Expand All @@ -38,7 +38,7 @@ public void ProjectId_Int_Conversion()
[Fact]
public void ProjectId_Project_Path_Conversion()
{
string expected = "projects/group%2fmy_awe-some.project";
string expected = "projects/group%2Fmy_awe-some.project";
ProjectId sut = new Project { PathWithNamespace = "group/my_awe-some.project" };
string result = $"projects/{sut}";
result.Should().Be(expected);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void NonDefaultQueryBuilt()
});

query.Should().Be("https://gitlab.com/api/v4/groups?" +
"skip_groups%5b%5d=1&skip_groups%5b%5d=2&" +
"skip_groups%5B%5D=1&skip_groups%5B%5D=2&" +
"all_available=true&" +
"search=filter&" +
"order_by=path&" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ public void NonDefaultQueryBuilt()

query.Should().Be("https://gitlab.com/api/v4/issues?" +
"state=opened&" +
"labels=label1%2clabel2&" +
"labels=label1%2Clabel2&" +
"milestone=milestone1&" +
"scope=all&" +
"author_id=1&" +
"assignee_id=2&" +
"iids%5b%5d=3&iids%5b%5d=4&" +
"iids%5B%5D=3&iids%5B%5D=4&" +
"order_by=updated_at&" +
"sort=asc&" +
"search=filter");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public void NonDefaultQueryBuilt()
"sort=asc&" +
"milestone=milestone1&" +
"view=simple&" +
"labels=label1%2clabel2&" +
"created_after=1991-11-11T01%3a01%3a01.0000000&" +
"created_before=1991-12-12T02%3a02%3a02.0000000&" +
"labels=label1%2Clabel2&" +
"created_after=1991-11-11T01%3A01%3A01.0000000&" +
"created_before=1991-12-12T02%3A02%3A02.0000000&" +
"scope=all&" +
"author_id=1&" +
"assignee_id=2");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void NonDefaultQueryBuilt()
});

query.Should().Be("https://gitlab.com/api/v4/projects/projectId/milestones?" +
"iids%5b%5d=3&iids%5b%5d=4&" +
"iids%5B%5D=3&iids%5B%5D=4&" +
"state=active&" +
"search=filter");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void NonDefaultQueryBuilt()
});

query.Should().Be("https://https://gitlab.com/api/v4/pipelines?" +
"ref=feature%2ftest" +
"ref=feature%2Ftest" +
"&yaml_errors=true" +
"&sha=ff11ff11ff11ff11" +
"&status=failed" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ public void NonDefaultQueryBuilt()

query.Should().Be("https://gitlab.com/api/v4/issues?" +
"state=opened&" +
"labels=label1%2clabel2&" +
"labels=label1%2Clabel2&" +
"milestone=milestone1&" +
"scope=all&" +
"author_id=1&" +
"assignee_id=2&" +
"iids%5b%5d=3&iids%5b%5d=4&" +
"iids%5B%5D=3&iids%5B%5D=4&" +
"order_by=updated_at&" +
"sort=asc&" +
"search=filter&" +
"created_after=1991-11-11T01%3a01%3a01.0000000&" +
"created_before=1991-12-12T02%3a02%3a02.0000000");
"created_after=1991-11-11T01%3A01%3A01.0000000&" +
"created_before=1991-12-12T02%3A02%3A02.0000000");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ public void NonDefaultQueryBuilt()
});

query.Should().Be("https://gitlab.com/api/v4/merge_requests?" +
"iids%5b%5d=4&iids%5b%5d=5&" +
"iids%5B%5D=4&iids%5B%5D=5&" +
"state=opened&" +
"order_by=updated_at&" +
"sort=asc&" +
"milestone=milestone1&" +
"view=simple&" +
"labels=label1%2clabel2&" +
"created_after=1991-11-11T01%3a01%3a01.0000000&" +
"created_before=1991-12-12T02%3a02%3a02.0000000&" +
"labels=label1%2Clabel2&" +
"created_after=1991-11-11T01%3A01%3A01.0000000&" +
"created_before=1991-12-12T02%3A02%3A02.0000000&" +
"scope=all&" +
"author_id=1&" +
"assignee_id=2");
Expand Down

0 comments on commit c3cb6ec

Please sign in to comment.