Skip to content

Commit 0cf2c94

Browse files
Merge pull request #9 from AlexanderDotH/develop
feat: Add JSON array support to WithJsonBody - v1.3.2
2 parents 44d4637 + c0fb77b commit 0cf2c94

File tree

4 files changed

+67
-4
lines changed

4 files changed

+67
-4
lines changed

DevBase.Net/AGENT.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ Request UseJwtAuthentication(string rawToken)
154154
// Request Body
155155
Request WithRawBody(RequestRawBodyBuilder bodyBuilder)
156156
Request WithRawBody(string content, Encoding? encoding = null)
157-
Request WithJsonBody(string jsonContent, Encoding? encoding = null)
158-
Request WithJsonBody<T>(T obj)
157+
Request WithJsonBody(string jsonContent, Encoding? encoding = null) // Supports both objects {} and arrays []
158+
Request WithJsonBody<T>(T obj) // Serializes any object including List<T>, arrays, etc.
159159
Request WithBufferBody(byte[] buffer)
160160
Request WithBufferBody(Memory<byte> buffer)
161161
Request WithEncodedForm(RequestEncodedKeyValueListBodyBuilder formBuilder)

DevBase.Net/Data/Body/Content/JsonRequestContent.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ public override bool IsValid(ReadOnlySpan<byte> content)
1616

1717
try
1818
{
19-
JObject.Parse(stringContent);
19+
// Use JToken.Parse to support both JSON objects {} and arrays []
20+
JToken.Parse(stringContent);
2021
return true;
2122
}
2223
catch

DevBase.Net/DevBase.Net.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<PackageProjectUrl>https://github.com/AlexanderDotH/DevBase.git</PackageProjectUrl>
1616
<RepositoryUrl>https://github.com/AlexanderDotH/DevBase.git</RepositoryUrl>
1717
<RepositoryType>git</RepositoryType>
18-
<Version>1.3.1</Version>
18+
<Version>1.3.2</Version>
1919
<PackageLicenseExpression>MIT</PackageLicenseExpression>
2020
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
2121
<PackageTags>http;client;requests;proxy;socks5;jwt;authentication;fluent-api;async;retry;rate-limiting;json;html-parsing</PackageTags>

DevBase.Test/DevBaseRequests/Preparation/Body/Content/JsonRequestContentTest.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,66 @@ public void IsValidTest()
3333

3434
stopwatch.PrintTimeTable();
3535
}
36+
37+
[Test]
38+
public void IsValidArrayTest()
39+
{
40+
JsonRequestContent jsonRequestContent = new JsonRequestContent(Encoding.UTF8);
41+
42+
// Test simple array
43+
string jsonArray = @"[1, 2, 3]";
44+
byte[] arrayBytes = Encoding.UTF8.GetBytes(jsonArray);
45+
Assert.That(jsonRequestContent.IsValid(arrayBytes), Is.True);
46+
47+
// Test array of objects
48+
string jsonArrayOfObjects = @"[{""id"":1,""name"":""Item1""},{""id"":2,""name"":""Item2""}]";
49+
byte[] arrayOfObjectsBytes = Encoding.UTF8.GetBytes(jsonArrayOfObjects);
50+
Assert.That(jsonRequestContent.IsValid(arrayOfObjectsBytes), Is.True);
51+
52+
// Test nested arrays
53+
string nestedArray = @"[[1,2],[3,4]]";
54+
byte[] nestedArrayBytes = Encoding.UTF8.GetBytes(nestedArray);
55+
Assert.That(jsonRequestContent.IsValid(nestedArrayBytes), Is.True);
56+
57+
// Test empty array
58+
string emptyArray = @"[]";
59+
byte[] emptyArrayBytes = Encoding.UTF8.GetBytes(emptyArray);
60+
Assert.That(jsonRequestContent.IsValid(emptyArrayBytes), Is.True);
61+
62+
Console.WriteLine("All JSON array validation tests passed");
63+
}
64+
65+
[Test]
66+
public void IsValidArrayPerformanceTest()
67+
{
68+
JsonRequestContent jsonRequestContent = new JsonRequestContent(Encoding.UTF8);
69+
string jsonArray = @"[{""position"":0,""questionId"":""abc123"",""textAnswer"":{""response"":""Hello""}}]";
70+
byte[] arrayBytes = Encoding.UTF8.GetBytes(jsonArray);
71+
72+
Stopwatch stopwatch = PenetrationTest.Run(() =>
73+
{
74+
Assert.That(jsonRequestContent.IsValid(arrayBytes), Is.True);
75+
}, Count);
76+
77+
Console.WriteLine($"Validated json array {Count} times");
78+
stopwatch.PrintTimeTable();
79+
}
80+
81+
[Test]
82+
public void IsInvalidJsonTest()
83+
{
84+
JsonRequestContent jsonRequestContent = new JsonRequestContent(Encoding.UTF8);
85+
86+
// Test invalid JSON
87+
string invalidJson = @"not valid json";
88+
byte[] invalidBytes = Encoding.UTF8.GetBytes(invalidJson);
89+
Assert.That(jsonRequestContent.IsValid(invalidBytes), Is.False);
90+
91+
// Test malformed array
92+
string malformedArray = @"[1, 2, 3";
93+
byte[] malformedBytes = Encoding.UTF8.GetBytes(malformedArray);
94+
Assert.That(jsonRequestContent.IsValid(malformedBytes), Is.False);
95+
96+
Console.WriteLine("Invalid JSON detection tests passed");
97+
}
3698
}

0 commit comments

Comments
 (0)