-
-
Notifications
You must be signed in to change notification settings - Fork 0
fix: enable validation when using form-data access #474
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
6 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
18 changes: 18 additions & 0 deletions
18
Source/Mockolate/Web/ItExtensions.HttpContent.WithBytes.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,18 @@ | ||
| using System.Linq; | ||
|
|
||
| namespace Mockolate.Web; | ||
|
|
||
| #pragma warning disable S2325 // Methods and properties that don't access instance data should be static | ||
| public static partial class ItExtensions | ||
| { | ||
| /// <inheritdoc cref="IHttpContentParameter" /> | ||
| extension(IHttpContentParameter parameter) | ||
| { | ||
| /// <summary> | ||
| /// Expects the binary content to be equal to the given <paramref name="bytes" />. | ||
| /// </summary> | ||
| public IHttpContentParameter WithBytes(byte[] bytes) | ||
| => parameter.WithBytes(bytes.SequenceEqual); | ||
| } | ||
| } | ||
| #pragma warning restore S2325 // Methods and properties that don't access instance data should be static |
142 changes: 142 additions & 0 deletions
142
Source/Mockolate/Web/ItExtensions.HttpContent.WithFormData.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,142 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Net; | ||
| using System.Net.Http; | ||
| using System.Text; | ||
| #if NETSTANDARD2_0 | ||
| using Mockolate.Internals.Polyfills; | ||
| #endif | ||
|
|
||
| namespace Mockolate.Web; | ||
|
|
||
| #pragma warning disable S2325 // Methods and properties that don't access instance data should be static | ||
| public static partial class ItExtensions | ||
| { | ||
| /// <inheritdoc cref="IHttpContentParameter" /> | ||
| extension(IHttpContentParameter parameter) | ||
| { | ||
| /// <summary> | ||
| /// Expects the form data content to contain the given <paramref name="key" />-<paramref name="value" /> pair. | ||
| /// </summary> | ||
| public IFormDataContentParameter WithFormData(string key, HttpFormDataValue value) | ||
| => parameter.WithFormData((key, value)!); | ||
|
|
||
| /// <summary> | ||
| /// Expects the form data content to contain the given <paramref name="values" />. | ||
| /// </summary> | ||
| public IFormDataContentParameter WithFormData(params IEnumerable<(string Key, HttpFormDataValue Value)> values) | ||
| { | ||
| FormDataMatcher data = new(values); | ||
| FormDataContentParameter contentParameter = new(parameter, data); | ||
| return contentParameter; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Expects the form data content to contain the given <paramref name="values" />. | ||
| /// </summary> | ||
| public IFormDataContentParameter WithFormData(string values) | ||
| => parameter.WithFormData(FormDataMatcher.ParseFormDataParameters(values) | ||
| .Select(pair => (pair.Key, new HttpFormDataValue(pair.Value)))); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Further expectations on the form-data <see cref="HttpContent" />. | ||
| /// </summary> | ||
| public interface IFormDataContentParameter : IHttpContentParameter | ||
| { | ||
| /// <summary> | ||
| /// Expects the form data content to not contain any additional key-value pairs other than the ones already specified. | ||
| /// </summary> | ||
| IFormDataContentParameter Exactly(); | ||
| } | ||
|
|
||
| private sealed class FormDataMatcher | ||
| { | ||
| private readonly List<(string Name, HttpFormDataValue Value)> _requiredFormDataParameters = []; | ||
| private bool _isExactly; | ||
|
|
||
| public FormDataMatcher(IEnumerable<(string Name, HttpFormDataValue Value)> formDataParameters) | ||
| { | ||
| _requiredFormDataParameters.AddRange(formDataParameters); | ||
| } | ||
|
|
||
| public bool Matches(string content) | ||
| { | ||
| List<(string Key, string Value)> formDataParameters = GetFormData(content).ToList(); | ||
|
vbreuss marked this conversation as resolved.
|
||
| return _isExactly | ||
| ? _requiredFormDataParameters.All(requiredParameter | ||
| => formDataParameters.Any(parameter | ||
| => parameter.Key == requiredParameter.Name && | ||
| requiredParameter.Value.Matches(parameter.Value))) && | ||
| formDataParameters.All(parameter => _requiredFormDataParameters | ||
| .Any(requiredParameter => parameter.Key == requiredParameter.Name && | ||
| requiredParameter.Value.Matches(parameter.Value))) | ||
| : _requiredFormDataParameters.All(requiredParameter | ||
| => formDataParameters.Any(parameter | ||
| => parameter.Key == requiredParameter.Name && | ||
| requiredParameter.Value.Matches(parameter.Value))); | ||
| } | ||
|
|
||
| public void Exactly() | ||
| => _isExactly = true; | ||
|
|
||
| internal static IEnumerable<(string Key, string Value)> ParseFormDataParameters(string input) | ||
| => input.TrimStart('?') | ||
| .Split('&') | ||
| .Select(pair => pair.Split('=', 2)) | ||
| .Where(pair => !string.IsNullOrWhiteSpace(pair[0])) | ||
| .Select(pair => | ||
| ( | ||
| WebUtility.UrlDecode(pair[0]), | ||
| pair.Length == 2 ? WebUtility.UrlDecode(pair[1]) : "" | ||
| ) | ||
| ); | ||
|
|
||
| private static IEnumerable<(string, string)> GetFormData(string rawContent) | ||
| { | ||
| string rawFormData = ExtractFormDataFromMultipartContent(rawContent); | ||
| return ParseFormDataParameters(rawFormData); | ||
| } | ||
|
|
||
| private static string ExtractFormDataFromMultipartContent(string rawContent) | ||
| { | ||
| string[] lines = rawContent.Split('\n'); | ||
| StringBuilder sb = new(); | ||
| foreach (string line in lines) | ||
| { | ||
| if (line.StartsWith("--") || | ||
| line.StartsWith("Content-Type: ", StringComparison.OrdinalIgnoreCase) || | ||
| line.StartsWith("Content-Disposition: ", StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
|
vbreuss marked this conversation as resolved.
|
||
| continue; | ||
| } | ||
|
|
||
| if (!string.IsNullOrWhiteSpace(line)) | ||
| { | ||
| sb.AppendLine(line.TrimEnd()); | ||
| } | ||
| } | ||
|
|
||
| return sb.ToString().Trim(); | ||
| } | ||
| } | ||
|
|
||
| private sealed class FormDataContentParameter : HttpContentParameterWrapper, IFormDataContentParameter | ||
| { | ||
| private readonly FormDataMatcher _data; | ||
|
|
||
| public FormDataContentParameter(IHttpContentParameter parameter, FormDataMatcher data) : base(parameter) | ||
| { | ||
| _data = data; | ||
| parameter.WithString(data.Matches); | ||
| } | ||
|
|
||
| public IFormDataContentParameter Exactly() | ||
| { | ||
| _data.Exactly(); | ||
| return this; | ||
| } | ||
| } | ||
| } | ||
| #pragma warning restore S2325 // Methods and properties that don't access instance data should be static | ||
93 changes: 93 additions & 0 deletions
93
Source/Mockolate/Web/ItExtensions.HttpContent.WithHeaders.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,93 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Net.Http; | ||
| using System.Net.Http.Headers; | ||
| #if NETSTANDARD2_0 | ||
| using Mockolate.Internals.Polyfills; | ||
| #endif | ||
|
|
||
| namespace Mockolate.Web; | ||
|
|
||
| /// <summary> | ||
| /// Extensions for parameter matchers for HTTP-related types. | ||
| /// </summary> | ||
| public static partial class ItExtensions | ||
| { | ||
| /// <inheritdoc cref="IHttpHeaderParameter{TParameter}" /> | ||
| extension<TParameter>(IHttpHeaderParameter<TParameter> parameter) | ||
|
vbreuss marked this conversation as resolved.
|
||
| { | ||
| /// <summary> | ||
| /// Expects the <see cref="HttpContent" /> to contain a header matching the <paramref name="name" /> and | ||
| /// <paramref name="value" />. | ||
| /// </summary> | ||
| public TParameter WithHeaders(string name, HttpHeaderValue value) | ||
| => parameter.WithHeaders((name, value)); | ||
|
vbreuss marked this conversation as resolved.
|
||
|
|
||
| /// <summary> | ||
| /// Expects the <see cref="HttpContent" /> to contain the given <paramref name="headers" />. | ||
| /// </summary> | ||
| public TParameter WithHeaders(string headers) | ||
| { | ||
| List<(string, HttpHeaderValue)> headerList = new(); | ||
| using StringReader reader = new(headers); | ||
| string? line = reader.ReadLine(); | ||
| while (!string.IsNullOrWhiteSpace(line)) | ||
| { | ||
| string[] parts = line.Split(':', 2); | ||
|
|
||
| if (parts.Length != 2) | ||
| { | ||
| // ReSharper disable once LocalizableElement | ||
| throw new ArgumentException("The header contained an invalid line: " + line, nameof(headers)); | ||
| } | ||
|
|
||
| headerList.Add((parts[0].Trim(), parts[1].TrimStart(' '))); | ||
|
vbreuss marked this conversation as resolved.
|
||
| line = reader.ReadLine(); | ||
| } | ||
|
|
||
| return parameter.WithHeaders(headerList); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Further expectations on the headers of the <see cref="HttpContent" />. | ||
| /// </summary> | ||
|
vbreuss marked this conversation as resolved.
|
||
| public interface IHttpHeaderParameter<out TParameter> | ||
| { | ||
| /// <summary> | ||
| /// Expects the <see cref="HttpContent" /> to contain the given <paramref name="headers" />. | ||
| /// </summary> | ||
| TParameter WithHeaders(params IEnumerable<(string Name, HttpHeaderValue Value)> headers); | ||
| } | ||
|
|
||
| private sealed class HttpHeadersMatcher | ||
| { | ||
| private readonly List<(string Name, HttpHeaderValue Value)> _requiredHeaders = []; | ||
|
|
||
| public bool IncludeRequestHeaders { get; private set; } | ||
|
|
||
| public void AddRequiredHeader(IEnumerable<(string Name, HttpHeaderValue Value)> headers) | ||
| => _requiredHeaders.AddRange(headers); | ||
|
|
||
| public bool Matches(HttpHeaders messageHeaders, HttpHeaders? alternativeHeaders = null) | ||
| => alternativeHeaders is null | ||
| ? _requiredHeaders.All(header => MatchesHeader(header.Name, header.Value, messageHeaders)) | ||
| : _requiredHeaders.All(header => MatchesHeader(header.Name, header.Value, messageHeaders) || | ||
| MatchesHeader(header.Name, header.Value, alternativeHeaders)); | ||
|
|
||
| private static bool MatchesHeader(string name, HttpHeaderValue value, HttpHeaders messageHeaders) | ||
| { | ||
| if (!messageHeaders.TryGetValues(name, out IEnumerable<string>? values)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| return values.Any(value.Matches); | ||
| } | ||
|
|
||
| public void IncludingRequestHeaders() | ||
| => IncludeRequestHeaders = true; | ||
| } | ||
| } | ||
138 changes: 138 additions & 0 deletions
138
Source/Mockolate/Web/ItExtensions.HttpContent.WithString.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,138 @@ | ||
| using System; | ||
| using System.Net.Http; | ||
| using System.Text.RegularExpressions; | ||
| using Mockolate.Internals; | ||
|
|
||
| namespace Mockolate.Web; | ||
|
|
||
| #pragma warning disable S2325 // Methods and properties that don't access instance data should be static | ||
| public static partial class ItExtensions | ||
| { | ||
| /// <inheritdoc cref="IHttpContentParameter" /> | ||
| extension(IHttpContentParameter parameter) | ||
| { | ||
| /// <summary> | ||
| /// Expects the content to have a string body equal to the <paramref name="expected" /> value. | ||
| /// </summary> | ||
| public IStringContentBodyParameter WithString(string expected) | ||
| { | ||
| StringMatcher data = new(expected, true); | ||
| StringContentParameter contentParameter = new(parameter, data); | ||
| return contentParameter; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Expects the content to have a string body that matches the given wildcard <paramref name="pattern" />. | ||
| /// </summary> | ||
| public IStringContentBodyMatchingParameter WithStringMatching(string pattern) | ||
| { | ||
| StringMatcher data = new(pattern, false); | ||
| StringContentParameter contentParameter = new(parameter, data); | ||
| return contentParameter; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Further expectations on the matching of a body of the <see cref="StringContent" />. | ||
| /// </summary> | ||
| public interface IStringContentBodyMatchingParameter : IStringContentBodyParameter | ||
| { | ||
| /// <summary> | ||
| /// Expects the body match pattern to be a <see cref="Regex" />. | ||
| /// </summary> | ||
| IStringContentBodyParameter AsRegex( | ||
| RegexOptions options = RegexOptions.None, | ||
| TimeSpan? timeout = null); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Further expectations on the body of the <see cref="StringContent" />. | ||
| /// </summary> | ||
| public interface IStringContentBodyParameter : IHttpContentParameter | ||
| { | ||
| /// <summary> | ||
| /// Ignores case when matching the body. | ||
| /// </summary> | ||
| IStringContentBodyParameter IgnoringCase(); | ||
| } | ||
|
|
||
| private sealed class StringMatcher(string value, bool isExact) | ||
| { | ||
| private BodyMatchType _bodyMatchType = isExact ? BodyMatchType.Exact : BodyMatchType.Wildcard; | ||
| private bool _ignoringCase; | ||
| private RegexOptions _regexOptions; | ||
| private TimeSpan? _timeout; | ||
|
|
||
| public bool Matches(string stringContent) | ||
| { | ||
| switch (_bodyMatchType) | ||
| { | ||
| case BodyMatchType.Exact when | ||
| !stringContent.Equals(value, _ignoringCase | ||
| ? StringComparison.OrdinalIgnoreCase | ||
| : StringComparison.Ordinal): | ||
| return false; | ||
| case BodyMatchType.Wildcard when | ||
| !Wildcard.Pattern(value, _ignoringCase).Matches(stringContent): | ||
| return false; | ||
| case BodyMatchType.Regex: | ||
| { | ||
| Regex regex = new(value, | ||
| _ignoringCase ? _regexOptions | RegexOptions.IgnoreCase : _regexOptions, | ||
| _timeout ?? Regex.InfiniteMatchTimeout); | ||
| if (!regex.IsMatch(stringContent)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| break; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| public void AsRegex( | ||
| RegexOptions options = RegexOptions.None, | ||
| TimeSpan? timeout = null) | ||
| { | ||
| _regexOptions = options; | ||
| _timeout = timeout; | ||
| _bodyMatchType = BodyMatchType.Regex; | ||
| } | ||
|
|
||
| public void IgnoringCase() | ||
| => _ignoringCase = true; | ||
|
|
||
| private enum BodyMatchType | ||
| { | ||
| Exact, | ||
| Wildcard, | ||
| Regex, | ||
| } | ||
| } | ||
|
|
||
| private sealed class StringContentParameter : HttpContentParameterWrapper, IStringContentBodyMatchingParameter | ||
| { | ||
| private readonly StringMatcher _data; | ||
|
|
||
| public StringContentParameter(IHttpContentParameter parameter, StringMatcher data) : base(parameter) | ||
| { | ||
| _data = data; | ||
| parameter.WithString(data.Matches); | ||
| } | ||
|
|
||
| public IStringContentBodyParameter IgnoringCase() | ||
| { | ||
| _data.IgnoringCase(); | ||
| return this; | ||
| } | ||
|
|
||
| public IStringContentBodyParameter AsRegex(RegexOptions options = RegexOptions.None, TimeSpan? timeout = null) | ||
| { | ||
| _data.AsRegex(options, timeout); | ||
| return this; | ||
| } | ||
| } | ||
| } | ||
| #pragma warning restore S2325 // Methods and properties that don't access instance data should be static |
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.