Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions DevBase.Net/AGENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ Request UseJwtAuthentication(string rawToken)
// Request Body
Request WithRawBody(RequestRawBodyBuilder bodyBuilder)
Request WithRawBody(string content, Encoding? encoding = null)
Request WithJsonBody(string jsonContent, Encoding? encoding = null)
Request WithJsonBody<T>(T obj)
Request WithJsonBody(string jsonContent, Encoding? encoding = null) // Supports both objects {} and arrays []
Request WithJsonBody<T>(T obj) // Serializes any object including List<T>, arrays, etc.
Request WithBufferBody(byte[] buffer)
Request WithBufferBody(Memory<byte> buffer)
Request WithEncodedForm(RequestEncodedKeyValueListBodyBuilder formBuilder)
Expand Down
3 changes: 2 additions & 1 deletion DevBase.Net/Data/Body/Content/JsonRequestContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public override bool IsValid(ReadOnlySpan<byte> content)

try
{
JObject.Parse(stringContent);
// Use JToken.Parse to support both JSON objects {} and arrays []
JToken.Parse(stringContent);
return true;
}
catch
Expand Down
2 changes: 1 addition & 1 deletion DevBase.Net/DevBase.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<PackageProjectUrl>https://github.com/AlexanderDotH/DevBase.git</PackageProjectUrl>
<RepositoryUrl>https://github.com/AlexanderDotH/DevBase.git</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<Version>1.3.1</Version>
<Version>1.3.2</Version>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageTags>http;client;requests;proxy;socks5;jwt;authentication;fluent-api;async;retry;rate-limiting;json;html-parsing</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,66 @@ public void IsValidTest()

stopwatch.PrintTimeTable();
}

[Test]
public void IsValidArrayTest()
{
JsonRequestContent jsonRequestContent = new JsonRequestContent(Encoding.UTF8);

// Test simple array
string jsonArray = @"[1, 2, 3]";
byte[] arrayBytes = Encoding.UTF8.GetBytes(jsonArray);
Assert.That(jsonRequestContent.IsValid(arrayBytes), Is.True);

// Test array of objects
string jsonArrayOfObjects = @"[{""id"":1,""name"":""Item1""},{""id"":2,""name"":""Item2""}]";
byte[] arrayOfObjectsBytes = Encoding.UTF8.GetBytes(jsonArrayOfObjects);
Assert.That(jsonRequestContent.IsValid(arrayOfObjectsBytes), Is.True);

// Test nested arrays
string nestedArray = @"[[1,2],[3,4]]";
byte[] nestedArrayBytes = Encoding.UTF8.GetBytes(nestedArray);
Assert.That(jsonRequestContent.IsValid(nestedArrayBytes), Is.True);

// Test empty array
string emptyArray = @"[]";
byte[] emptyArrayBytes = Encoding.UTF8.GetBytes(emptyArray);
Assert.That(jsonRequestContent.IsValid(emptyArrayBytes), Is.True);

Console.WriteLine("All JSON array validation tests passed");
}

[Test]
public void IsValidArrayPerformanceTest()
{
JsonRequestContent jsonRequestContent = new JsonRequestContent(Encoding.UTF8);
string jsonArray = @"[{""position"":0,""questionId"":""abc123"",""textAnswer"":{""response"":""Hello""}}]";
byte[] arrayBytes = Encoding.UTF8.GetBytes(jsonArray);

Stopwatch stopwatch = PenetrationTest.Run(() =>
{
Assert.That(jsonRequestContent.IsValid(arrayBytes), Is.True);
}, Count);

Console.WriteLine($"Validated json array {Count} times");
stopwatch.PrintTimeTable();
}

[Test]
public void IsInvalidJsonTest()
{
JsonRequestContent jsonRequestContent = new JsonRequestContent(Encoding.UTF8);

// Test invalid JSON
string invalidJson = @"not valid json";
byte[] invalidBytes = Encoding.UTF8.GetBytes(invalidJson);
Assert.That(jsonRequestContent.IsValid(invalidBytes), Is.False);

// Test malformed array
string malformedArray = @"[1, 2, 3";
byte[] malformedBytes = Encoding.UTF8.GetBytes(malformedArray);
Assert.That(jsonRequestContent.IsValid(malformedBytes), Is.False);

Console.WriteLine("Invalid JSON detection tests passed");
}
}
Loading