Merge pull request #6165 from Bond-009/tests4

Fix duplicate keys causing an exception
This commit is contained in:
Claus Vium 2021-06-09 10:23:29 +02:00 committed by GitHub
commit d366fd3fd7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 4 deletions

View File

@ -68,15 +68,20 @@ namespace Jellyfin.Server.Middleware
foreach (var pair in queryString)
{
var i = pair.IndexOf('=');
if (i == -1)
{
// encoded is an equals.
pairs.Add(pair.ToString(), StringValues.Empty);
// We use TryAdd so duplicate keys get ignored
pairs.TryAdd(pair.ToString(), StringValues.Empty);
continue;
}
pairs.Add(pair[..i].ToString(), new StringValues(pair[(i + 1)..].ToString()));
var k = pair[..i].ToString();
var v = pair[(i + 1)..].ToString();
if (!pairs.TryAdd(k, new StringValues(v)))
{
pairs[k] = StringValues.Concat(pairs[k], v);
}
}
_store = new QueryCollection(pairs);

View File

@ -29,5 +29,25 @@ namespace Jellyfin.Api.Controllers
StatusCode = 200
};
}
/// <summary>
/// Tests the url decoding.
/// </summary>
/// <param name="params">Parameters to echo back in the response.</param>
/// <returns>An <see cref="OkResult"/>.</returns>
/// <response code="200">Information retrieved.</response>
[HttpGet("UrlArrayDecode")]
[ProducesResponseType(StatusCodes.Status200OK)]
public ContentResult TestUrlArrayDecoding([FromQuery] Dictionary<string, string[]>? @params = null)
{
return new ContentResult()
{
Content = (@params != null && @params.Count > 0)
? string.Join("&", @params.Select(x => x.Key + "=" + string.Join(',', x.Value)))
: string.Empty,
ContentType = "text/plain; charset=utf-8",
StatusCode = 200
};
}
}
}

View File

@ -20,6 +20,8 @@ namespace Jellyfin.Server.Integration.Tests
[InlineData("a=1&b=2&c=3", "a=1&b=2&c=3")] // won't be processed as there is more than 1.
[InlineData("a=1", "a=1")] // won't be processed as it has a value
[InlineData("a%3D1%26b%3D2%26c%3D3", "a=1&b=2&c=3")] // will be processed.
[InlineData("a=b&a=c", "a=b")]
[InlineData("a%3Db%26a%3Dc", "a=b")]
public async Task Ensure_Decoding_Of_Urls_Is_Working(string sourceUrl, string unencodedUrl)
{
var client = _factory.CreateClient();
@ -29,5 +31,18 @@ namespace Jellyfin.Server.Integration.Tests
string reply = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
Assert.Equal(unencodedUrl, reply);
}
[Theory]
[InlineData("a=b&a=c", "a=b,c")]
[InlineData("a%3Db%26a%3Dc", "a=b,c")]
public async Task Ensure_Array_Decoding_Of_Urls_Is_Working(string sourceUrl, string unencodedUrl)
{
var client = _factory.CreateClient();
var response = await client.GetAsync("Encoder/UrlArrayDecode?" + sourceUrl).ConfigureAwait(false);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
string reply = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
Assert.Equal(unencodedUrl, reply);
}
}
}

View File

@ -14,6 +14,7 @@ namespace Jellyfin.Server.Tests
[InlineData("e0a72cb2a2c7", "e0a72cb2a2c7")] // isn't encoded
[InlineData("random+test", "random test")] // encoded
[InlineData("random%20test", "random test")] // encoded
[InlineData("++", " ")] // encoded
public static void EmptyValueTest(string query, string key)
{
var dict = new Dictionary<string, StringValues>
@ -23,7 +24,7 @@ namespace Jellyfin.Server.Tests
var test = new UrlDecodeQueryFeature(new QueryFeature(new QueryCollection(dict)));
Assert.Single(test.Query);
var (k, v) = test.Query.First();
Assert.Equal(k, key);
Assert.Equal(key, k);
Assert.Empty(v);
}
}