Skip to content
This repository has been archived by the owner on Nov 25, 2019. It is now read-only.

Commit

Permalink
Test updates for FormData-to-JToken parser.
Browse files Browse the repository at this point in the history
New rule: "x=" and "x" should be string.empty, not null.
  • Loading branch information
MikeStall committed Mar 23, 2012
1 parent 65a1fba commit d32f0d9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 27 deletions.
17 changes: 12 additions & 5 deletions src/System.Net.Http.Formatting/Formatting/FormUrlEncodedJson.cs
Expand Up @@ -110,9 +110,16 @@ private static JObject ParseInternal(IEnumerable<KeyValuePair<string, string>> n
JObject result = new JObject();
foreach (var nameValuePair in nameValuePairs)
{
if (nameValuePair.Key == null)
string key = nameValuePair.Key;
string value = nameValuePair.Value;
if (String.Compare(value, "null", StringComparison.Ordinal) == 0)
{
if (String.IsNullOrEmpty(nameValuePair.Value))
value = null;
}

if (key == null)
{
if (String.IsNullOrEmpty(value))
{
if (throwOnError)
{
Expand All @@ -122,16 +129,16 @@ private static JObject ParseInternal(IEnumerable<KeyValuePair<string, string>> n
return null;
}

string[] path = new string[] { nameValuePair.Value };
string[] path = new string[] { value };
if (!Insert(result, path, null, throwOnError))
{
return null;
}
}
else
{
string[] path = GetPath(nameValuePair.Key, maxDepth, throwOnError);
if (path == null || !Insert(result, path, nameValuePair.Value, throwOnError))
string[] path = GetPath(key, maxDepth, throwOnError);
if (path == null || !Insert(result, path, value, throwOnError))
{
return null;
}
Expand Down
Expand Up @@ -15,8 +15,8 @@ public class FormUrlEncodedJsonFromContentTests
#region Tests

[Theory,
InlineData("abc", "{\"abc\":null}"),
InlineData("%2eabc%2e", "{\".abc.\":null}"),
InlineData("abc", "{\"abc\":\"\"}"),
InlineData("%2eabc%2e", "{\".abc.\":\"\"}"),
InlineData("", "{}"),
InlineData("a=1", "{\"a\":\"1\"}")]
public void SimpleStringsTest(string encoded, string expectedResult)
Expand Down Expand Up @@ -100,8 +100,7 @@ public void EmptyKeyTest(string encoded, string expectedResult)
InlineData("[]=1"),
InlineData("a[][]=0"),
InlineData("a[][x]=0"),
InlineData("a&a[b]=1"),
InlineData("a&a=1")]
InlineData("a&a[b]=1")]
public void InvalidObjectGraphsTest(string encoded)
{
ParseInvalidFormUrlEncoded(encoded);
Expand All @@ -120,11 +119,11 @@ public void InvalidFormUrlEncodingTest(string encoded)
/// Tests for parsing form-urlencoded data originated from JS primitives.
/// </summary>
[Theory,
InlineData("abc", @"{""abc"":null}"),
InlineData("123", @"{""123"":null}"),
InlineData("true", @"{""true"":null}"),
InlineData("abc", @"{""abc"":""""}"),
InlineData("123", @"{""123"":""""}"),
InlineData("true", @"{""true"":""""}"),
InlineData("", "{}"),
InlineData("%2fabc%2f", @"{""/abc/"":null}")]
InlineData("%2fabc%2f", @"{""/abc/"":""""}")]
public void TestJValue(string encoded, string expectedResult)
{
ValidateFormUrlEncoded(encoded, expectedResult);
Expand Down Expand Up @@ -268,11 +267,11 @@ public static IEnumerable<object[]> TestObjectTestData
yield return new[] { encoded, resultStr };

encoded = "a%5B%5D=1&a";
resultStr = @"{""a"":[""1"",null]}";
resultStr = @"{""a"":[""1"",""""]}";
yield return new[] { encoded, resultStr };

encoded = "a=1&a";
resultStr = @"{""a"":[""1"",null]}";
resultStr = @"{""a"":[""1"",""""]}";
yield return new[] { encoded, resultStr };
}
}
Expand Down
Expand Up @@ -13,8 +13,8 @@ public class FormUrlEncodedJsonFromUriQueryTests
#region Tests

[Theory,
InlineData("abc", "{\"abc\":null}"),
InlineData("%2eabc%2e", "{\".abc.\":null}"),
InlineData("abc", "{\"abc\":\"\"}"),
InlineData("%2eabc%2e", "{\".abc.\":\"\"}"),
InlineData("", "{}"),
InlineData("a=1", "{\"a\":\"1\"}")]
public void SimpleStringsTest(string encoded, string expectedResult)
Expand Down Expand Up @@ -96,8 +96,7 @@ public void EmptyKeyTest(string encoded, string expectedResult)
InlineData("[]=1"),
InlineData("a[][]=0"),
InlineData("a[][x]=0"),
InlineData("a&a[b]=1"),
InlineData("a&a=1")]
InlineData("a&a[b]=1")]
public void InvalidObjectGraphsTest(string encoded)
{
ParseInvalidFormUrlEncoded(encoded);
Expand All @@ -116,11 +115,11 @@ public void InvalidFormUrlEncodingTest(string encoded)
/// Tests for parsing form-urlencoded data originated from JS primitives.
/// </summary>
[Theory,
InlineData("abc", @"{""abc"":null}"),
InlineData("123", @"{""123"":null}"),
InlineData("true", @"{""true"":null}"),
InlineData("abc", @"{""abc"":""""}"),
InlineData("123", @"{""123"":""""}"),
InlineData("true", @"{""true"":""""}"),
InlineData("", "{}"),
InlineData("%2fabc%2f", @"{""/abc/"":null}")]
InlineData("%2fabc%2f", @"{""/abc/"":""""}")]
public void TestJValue(string encoded, string expectedResult)
{
ValidateFormUrlEncoded(encoded, expectedResult);
Expand Down Expand Up @@ -265,11 +264,11 @@ public static IEnumerable<object[]> TestObjectPropertyData
yield return new[] { encoded, resultStr };

encoded = "a%5B%5D=1&a";
resultStr = @"{""a"":[""1"",null]}";
resultStr = @"{""a"":[""1"",""""]}";
yield return new[] { encoded, resultStr };

encoded = "a=1&a";
resultStr = @"{""a"":[""1"",null]}";
resultStr = @"{""a"":[""1"",""""]}";
yield return new[] { encoded, resultStr };
}
}
Expand Down
Expand Up @@ -125,8 +125,8 @@ public void ParseBufferHandlesEmptyBuffer()
}

[Theory]
[InlineData("N", "N", null)]
[InlineData("%26", "&", null)]
[InlineData("N", "N", "")]
[InlineData("%26", "&", "")]
[PropertyData("UriQueryData")]
public void ParseBufferCorrectly(string segment, string name, string value)
{
Expand Down

0 comments on commit d32f0d9

Please sign in to comment.