-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
60 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace WingetIntune.Tests; | ||
internal static class HttpContentExtensions | ||
{ | ||
public static bool IsJson(this HttpContent? content) | ||
{ | ||
return content?.Headers.ContentType?.MediaType == "application/json"; | ||
} | ||
|
||
public static bool ValidateJsonBody(this HttpContent? content, Dictionary<string, object> bodyValues) | ||
{ | ||
if (content is null || !content.IsJson()) | ||
{ | ||
return false; | ||
} | ||
|
||
var json = content.ReadAsStringAsync().Result; | ||
try | ||
{ | ||
var body = JsonSerializer.Deserialize<JsonContentBody>(json); | ||
|
||
if (body is null || body.Data is null) | ||
{ | ||
return false; | ||
} | ||
foreach (var kvp in bodyValues) | ||
{ | ||
if (!body.Data.ContainsKey(kvp.Key) || !body.Data[kvp.Key].ValueEquals(kvp.Value.ToString())) | ||
{ | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
catch (Exception ex) | ||
{ | ||
throw new InvalidOperationException($"Failed to deserialize JSON: {json}", ex); | ||
} | ||
} | ||
|
||
public class JsonContentBody | ||
{ | ||
[JsonExtensionData] | ||
public Dictionary<string, JsonElement>? Data { get; set; } | ||
} | ||
} |