1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using Microsoft.TestCommon;
using Newtonsoft.Json.Linq;
using Xunit;
using Assert = Microsoft.TestCommon.AssertEx;
namespace System.Net.Http.Formatting
{
public class FormUrlEncodedJsonTests
{
[Fact]
public void TypeIsCorrect()
{
Assert.Type.HasProperties(typeof(FormUrlEncodedJson), TypeAssert.TypeProperties.IsClass | TypeAssert.TypeProperties.IsStatic);
}
[Fact]
public void ParseThrowsOnNull()
{
Assert.ThrowsArgumentNull(() => FormUrlEncodedJson.Parse(null), null);
}
[Fact]
public void ParseThrowsInvalidMaxDepth()
{
Assert.ThrowsArgumentGreaterThan(() => FormUrlEncodedJson.Parse(CreateQuery(), -1), "maxDepth", "0", -1);
Assert.ThrowsArgumentGreaterThan(() => FormUrlEncodedJson.Parse(CreateQuery(), 0), "maxDepth", "0", 0);
}
[Fact]
public void ParseThrowsMaxDepthExceeded()
{
// Depth of 'a[b]=1' is 3
IEnumerable<KeyValuePair<string, string>> query = CreateQuery(new KeyValuePair<string, string>("a[b]", "1"));
Assert.ThrowsArgument(() => { FormUrlEncodedJson.Parse(query, 2); }, null);
// This should succeed
Assert.NotNull(FormUrlEncodedJson.Parse(query, 3));
}
[Fact]
public void TryParseThrowsOnNull()
{
JObject value;
Assert.ThrowsArgumentNull(() => FormUrlEncodedJson.TryParse(null, out value), null);
}
[Fact]
public void TryParseThrowsInvalidMaxDepth()
{
JObject value;
Assert.ThrowsArgumentGreaterThan(() => FormUrlEncodedJson.TryParse(CreateQuery(), -1, out value), "maxDepth", "0", -1);
Assert.ThrowsArgumentGreaterThan(() => FormUrlEncodedJson.TryParse(CreateQuery(), 0, out value), "maxDepth", "0", 0);
}
[Fact]
public void TryParseReturnsFalseMaxDepthExceeded()
{
JObject value;
// Depth of 'a[b]=1' is 3
IEnumerable<KeyValuePair<string, string>> query = CreateQuery(new KeyValuePair<string, string>("a[b]", "1"));
Assert.False(FormUrlEncodedJson.TryParse(query, 2, out value), "Parse should have failed due to too high depth.");
// This should succeed
Assert.True(FormUrlEncodedJson.TryParse(query, 3, out value), "Expected non-null JsonObject instance");
Assert.NotNull(value);
}
private static IEnumerable<KeyValuePair<string, string>> CreateQuery(params KeyValuePair<string, string>[] namevaluepairs)
{
return new List<KeyValuePair<string, string>>(namevaluepairs);
}
}
}
|