From c0fb77b23328c7cb0e2c0dfdedc0418ceaaa7cb6 Mon Sep 17 00:00:00 2001 From: AlexanderDotH Date: Fri, 26 Dec 2025 20:46:59 +0100 Subject: [PATCH] feat: Add JSON array support to WithJsonBody - v1.3.2 --- DevBase.Net/AGENT.md | 4 +- .../Data/Body/Content/JsonRequestContent.cs | 3 +- DevBase.Net/DevBase.Net.csproj | 2 +- .../Body/Content/JsonRequestContentTest.cs | 62 +++++++++++++++++++ 4 files changed, 67 insertions(+), 4 deletions(-) diff --git a/DevBase.Net/AGENT.md b/DevBase.Net/AGENT.md index bc36333..c3c19fe 100644 --- a/DevBase.Net/AGENT.md +++ b/DevBase.Net/AGENT.md @@ -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 obj) +Request WithJsonBody(string jsonContent, Encoding? encoding = null) // Supports both objects {} and arrays [] +Request WithJsonBody(T obj) // Serializes any object including List, arrays, etc. Request WithBufferBody(byte[] buffer) Request WithBufferBody(Memory buffer) Request WithEncodedForm(RequestEncodedKeyValueListBodyBuilder formBuilder) diff --git a/DevBase.Net/Data/Body/Content/JsonRequestContent.cs b/DevBase.Net/Data/Body/Content/JsonRequestContent.cs index d8ea4f6..6c6cef3 100644 --- a/DevBase.Net/Data/Body/Content/JsonRequestContent.cs +++ b/DevBase.Net/Data/Body/Content/JsonRequestContent.cs @@ -16,7 +16,8 @@ public override bool IsValid(ReadOnlySpan content) try { - JObject.Parse(stringContent); + // Use JToken.Parse to support both JSON objects {} and arrays [] + JToken.Parse(stringContent); return true; } catch diff --git a/DevBase.Net/DevBase.Net.csproj b/DevBase.Net/DevBase.Net.csproj index 67746a3..d3da0fe 100644 --- a/DevBase.Net/DevBase.Net.csproj +++ b/DevBase.Net/DevBase.Net.csproj @@ -15,7 +15,7 @@ https://github.com/AlexanderDotH/DevBase.git https://github.com/AlexanderDotH/DevBase.git git - 1.3.1 + 1.3.2 MIT false http;client;requests;proxy;socks5;jwt;authentication;fluent-api;async;retry;rate-limiting;json;html-parsing diff --git a/DevBase.Test/DevBaseRequests/Preparation/Body/Content/JsonRequestContentTest.cs b/DevBase.Test/DevBaseRequests/Preparation/Body/Content/JsonRequestContentTest.cs index 01f7d1c..1c0bd0b 100644 --- a/DevBase.Test/DevBaseRequests/Preparation/Body/Content/JsonRequestContentTest.cs +++ b/DevBase.Test/DevBaseRequests/Preparation/Body/Content/JsonRequestContentTest.cs @@ -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"); + } }