Merge pull request #895 from naveensrinivasan/master

Fix for Upload to Github releases stopped working #894
This commit is contained in:
Phil Haack
2015-09-16 17:06:56 -07:00
2 changed files with 15 additions and 7 deletions
@@ -57,9 +57,11 @@ namespace Octokit.Tests.Helpers
[InlineData("https://host.com/path?name=other", "https://host.com/path?name=other")]
[InlineData("https://host.com/path?name=example name.txt", "https://host.com/path{?name}")]
[InlineData("https://host.com/path", "https://host.com/path{?other}")]
[InlineData("https://host.com/path?name=example name.txt&label=labeltext", "https://host.com/path{?name,label}")]
[InlineData("https://host.com/path?name=example name.txt&label=labeltext", "https://host.com/path{?name,label,other}")]
public void ExpandsUriTemplates(string expected, string template)
{
Assert.Equal(expected, template.ExpandUriTemplate(new { name = "example name.txt" }).ToString());
Assert.Equal(expected, template.ExpandUriTemplate(new { name = "example name.txt",label="labeltext" }).ToString());
}
}
}
+12 -6
View File
@@ -48,14 +48,20 @@ namespace Octokit
public static Uri ExpandUriTemplate(this string template, object values)
{
var optionalQueryStringMatch = _optionalQueryStringRegex.Match(template);
if(optionalQueryStringMatch.Success)
if (optionalQueryStringMatch.Success)
{
var expansion = "";
var parameterName = optionalQueryStringMatch.Groups[1].Value;
var parameterProperty = values.GetType().GetProperty(parameterName);
if(parameterProperty != null)
var expansion = string.Empty;
var parameters = optionalQueryStringMatch.Groups[1].Value.Split(new char[] { ',' });
foreach (var parameter in parameters)
{
expansion = "?" + parameterName + "=" + Uri.EscapeDataString("" + parameterProperty.GetValue(values, new object[0]));
var parameterProperty = values.GetType().GetProperty(parameter);
if (parameterProperty != null)
{
expansion += string.IsNullOrWhiteSpace(expansion) ? "?" : "&";
expansion += parameter + "=" +
Uri.EscapeDataString("" + parameterProperty.GetValue(values, new object[0]));
}
}
template = _optionalQueryStringRegex.Replace(template, expansion);
}