-
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add IsJsonContent extensions
#54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
284 changes: 284 additions & 0 deletions
284
Source/aweXpect.Mockolate/Web/ItExtensions.HttpContent.IsJsonContent.cs
This file contains hidden or 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,284 @@ | ||
| #if NET8_0_OR_GREATER | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Net.Http; | ||
| using System.Text.Json; | ||
| using Mockolate.Parameters; | ||
|
|
||
| // ReSharper disable once CheckNamespace | ||
| namespace Mockolate.Web; | ||
|
|
||
| #pragma warning disable S2325 // Methods and properties that don't access instance data should be static | ||
| /// <summary> | ||
| /// Extensions for parameter matchers for HTTP-related types. | ||
| /// </summary> | ||
| public static class AweXpectItExtensions | ||
| { | ||
| /// <inheritdoc cref="ItExtensions" /> | ||
| extension(It _) | ||
| { | ||
| /// <summary> | ||
| /// Expects the <see cref="HttpContent" /> parameter to be a JSON content. | ||
| /// </summary> | ||
| public static IJsonContentParameter IsJsonContent() | ||
| => new JsonContentParameter(); | ||
|
|
||
| /// <summary> | ||
| /// Expects the <see cref="HttpContent" /> parameter to be a JSON content | ||
| /// with the given <paramref name="mediaType" />. | ||
| /// </summary> | ||
| public static IJsonContentParameter IsJsonContent(string mediaType) | ||
| => new JsonContentParameter().WithMediaType(mediaType); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Further expectations on the JSON <see cref="HttpContent" />. | ||
| /// </summary> | ||
| public interface IJsonContentParameter : ItExtensions.IHttpContentParameter<IJsonContentParameter> | ||
| { | ||
| /// <summary> | ||
| /// Expects the <see cref="StringContent" /> to have a body equal to the given <paramref name="json" />. | ||
| /// </summary> | ||
| IJsonContentBodyParameter WithBody(string json, JsonDocumentOptions? options = null); | ||
|
|
||
| /// <summary> | ||
| /// Expects the <see cref="StringContent" /> to have a JSON body which matches the <paramref name="expected" /> value. | ||
| /// </summary> | ||
| IJsonContentBodyParameter WithBodyMatching(object? expected, JsonDocumentOptions? options = null); | ||
|
|
||
| /// <summary> | ||
| /// Expects the <see cref="StringContent" /> to have a JSON body which matches the <paramref name="expected" /> value. | ||
| /// </summary> | ||
| IJsonContentBodyParameter WithBodyMatching<T>(IEnumerable<T> expected, JsonDocumentOptions? options = null); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Further expectations on the matching of a JSON body of the <see cref="HttpContent" />. | ||
| /// </summary> | ||
| public interface IJsonContentBodyParameter : IJsonContentParameter | ||
| { | ||
| /// <summary> | ||
| /// Ignores additional properties in JSON objects when comparing. | ||
| /// </summary> | ||
| IJsonContentBodyParameter IgnoringAdditionalProperties(bool ignoreAdditionalProperties = true); | ||
| } | ||
|
|
||
| private sealed class JsonContentParameter : HttpContentParameter<IJsonContentParameter>, IJsonContentBodyParameter | ||
| { | ||
| private string? _body; | ||
| private bool _ignoringAdditionalProperties = true; | ||
| private JsonDocumentOptions? _jsonDocumentOptions; | ||
|
|
||
| /// <inheritdoc cref="HttpContentParameter{TParameter}.GetThis" /> | ||
| protected override IJsonContentParameter GetThis => this; | ||
|
|
||
| /// <inheritdoc cref="IJsonContentParameter.WithBody(string, JsonDocumentOptions?)" /> | ||
| public IJsonContentBodyParameter WithBody(string json, | ||
| JsonDocumentOptions? options = null) | ||
| { | ||
| _body = json; | ||
| _jsonDocumentOptions = options; | ||
| return this; | ||
| } | ||
|
|
||
| /// <inheritdoc cref="IJsonContentParameter.WithBodyMatching(object, JsonDocumentOptions?)" /> | ||
| public IJsonContentBodyParameter WithBodyMatching(object? expected, | ||
| JsonDocumentOptions? options = null) | ||
| => WithBody(JsonSerializer.Serialize(expected, JsonSerializerOptions.Default), options); | ||
|
|
||
| public IJsonContentBodyParameter WithBodyMatching<T>(IEnumerable<T> expected, | ||
| JsonDocumentOptions? options = null) | ||
| => WithBody(JsonSerializer.Serialize<object>(expected, JsonSerializerOptions.Default), options); | ||
|
|
||
| /// <inheritdoc cref="IJsonContentBodyParameter.IgnoringAdditionalProperties(bool)" /> | ||
| public IJsonContentBodyParameter IgnoringAdditionalProperties(bool ignoreAdditionalProperties = true) | ||
| { | ||
| _ignoringAdditionalProperties = ignoreAdditionalProperties; | ||
| return this; | ||
| } | ||
|
|
||
| protected override bool Matches(HttpContent value) | ||
| { | ||
| if (!base.Matches(value)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| if (_body is not null) | ||
| { | ||
| try | ||
| { | ||
| JsonDocumentOptions options = _jsonDocumentOptions ?? GetDefaultOptions(); | ||
| using JsonDocument actualDocument = JsonDocument.Parse(value.ReadAsStream(), options); | ||
| using JsonDocument expectedDocument = JsonDocument.Parse(_body, options); | ||
|
|
||
| if (!Compare(actualDocument.RootElement, expectedDocument.RootElement, | ||
| _ignoringAdditionalProperties)) | ||
| { | ||
| return false; | ||
| } | ||
| } | ||
| catch (JsonException) | ||
| { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| private static JsonDocumentOptions GetDefaultOptions() => new() | ||
| { | ||
| AllowTrailingCommas = true, | ||
| }; | ||
|
|
||
| private static bool Compare( | ||
| JsonElement actualElement, | ||
| JsonElement expectedElement, | ||
| bool ignoreAdditionalProperties) | ||
| { | ||
| if (actualElement.ValueKind != expectedElement.ValueKind) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| return actualElement.ValueKind switch | ||
| { | ||
| JsonValueKind.Array => CompareJsonArray(actualElement, expectedElement, ignoreAdditionalProperties), | ||
| JsonValueKind.Number => CompareJsonNumber(actualElement, expectedElement), | ||
| JsonValueKind.String => CompareJsonString(actualElement, expectedElement), | ||
| JsonValueKind.Object => CompareJsonObject(actualElement, expectedElement, ignoreAdditionalProperties), | ||
| _ => true, | ||
| }; | ||
| } | ||
|
|
||
| private static bool CompareJsonObject(JsonElement actualElement, JsonElement expectedElement, | ||
| bool ignoreAdditionalProperties) | ||
| { | ||
| foreach (JsonProperty item in expectedElement.EnumerateObject()) | ||
| { | ||
| if (!actualElement.TryGetProperty(item.Name, out JsonElement property)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| if (!Compare(property, item.Value, ignoreAdditionalProperties)) | ||
| { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| if (!ignoreAdditionalProperties) | ||
| { | ||
| foreach (JsonProperty property in actualElement.EnumerateObject()) | ||
| { | ||
| if (!expectedElement.TryGetProperty(property.Name, out _)) | ||
| { | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| private static bool CompareJsonArray(JsonElement actualElement, JsonElement expectedElement, | ||
| bool ignoreAdditionalProperties) | ||
| { | ||
| for (int index = 0; index < expectedElement.GetArrayLength(); index++) | ||
| { | ||
| JsonElement expectedArrayElement = expectedElement[index]; | ||
| if (actualElement.GetArrayLength() <= index) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| JsonElement actualArrayElement = actualElement[index]; | ||
| if (!Compare(actualArrayElement, expectedArrayElement, ignoreAdditionalProperties)) | ||
| { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return ignoreAdditionalProperties || actualElement.GetArrayLength() <= expectedElement.GetArrayLength(); | ||
| } | ||
|
|
||
| private static bool CompareJsonString(JsonElement actualElement, JsonElement expectedElement) | ||
| { | ||
| string? value1 = actualElement.GetString(); | ||
| string? value2 = expectedElement.GetString(); | ||
| return value1 == value2; | ||
| } | ||
|
|
||
| private static bool CompareJsonNumber(JsonElement actualElement, JsonElement expectedElement) | ||
| { | ||
| if (actualElement.TryGetInt32(out int v1) && expectedElement.TryGetInt32(out int v2)) | ||
| { | ||
| return v1 == v2; | ||
| } | ||
|
|
||
| if (actualElement.TryGetDouble(out double n1) && expectedElement.TryGetDouble(out double n2)) | ||
| { | ||
| return n1.Equals(n2); | ||
|
vbreuss marked this conversation as resolved.
|
||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
|
|
||
| private abstract class HttpContentParameter<TParameter> | ||
| : ItExtensions.IHttpContentParameter<TParameter>, IParameter | ||
| { | ||
| private List<Action<HttpContent?>>? _callbacks; | ||
| private string? _mediaType; | ||
|
|
||
| /// <summary> | ||
| /// Returns <c>this</c> typed as <typeparamref name="TParameter" /> for fluent API. | ||
| /// </summary> | ||
| protected abstract TParameter GetThis { get; } | ||
|
|
||
| /// <inheritdoc cref="ItExtensions.IHttpContentParameter{TParameter}.WithMediaType(string?)" /> | ||
| public TParameter WithMediaType(string? mediaType) | ||
| { | ||
| _mediaType = mediaType; | ||
| return GetThis; | ||
| } | ||
|
|
||
| /// <inheritdoc cref="IParameter{T}.Do(Action{T})" /> | ||
| public IParameter<HttpContent?> Do(Action<HttpContent?> callback) | ||
| { | ||
| _callbacks ??= []; | ||
| _callbacks.Add(callback); | ||
| return this; | ||
| } | ||
|
|
||
| /// <inheritdoc cref="IParameter.Matches(object?)" /> | ||
| public bool Matches(object? value) | ||
| => value is HttpContent typedValue && Matches(typedValue); | ||
|
|
||
| /// <inheritdoc cref="IParameter.InvokeCallbacks(object?)" /> | ||
| public void InvokeCallbacks(object? value) | ||
| { | ||
| if (value is HttpContent httpContent) | ||
| { | ||
| _callbacks?.ForEach(a => a.Invoke(httpContent)); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Checks whether the given <see cref="HttpContent" /> <paramref name="value" /> matches the expectations. | ||
| /// </summary> | ||
| protected virtual bool Matches(HttpContent value) | ||
| { | ||
| if (_mediaType is not null && | ||
| value.Headers.ContentType?.MediaType?.Equals(_mediaType, StringComparison.OrdinalIgnoreCase) != true) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
| } | ||
| } | ||
| #pragma warning restore S2325 // Methods and properties that don't access instance data should be static | ||
| #endif | ||
21 changes: 21 additions & 0 deletions
21
Tests/aweXpect.Mockolate.Api.Tests/Expected/aweXpect.Mockolate_net10.0.txt
This file contains hidden or 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
21 changes: 21 additions & 0 deletions
21
Tests/aweXpect.Mockolate.Api.Tests/Expected/aweXpect.Mockolate_net8.0.txt
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.