-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: set up uri matcher for reusability so we can more simply add ot…
…her types of matchers that check portions of the URI, like path/route values, etc.
- Loading branch information
Showing
8 changed files
with
126 additions
and
17 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 was deleted.
Oops, something went wrong.
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
34 changes: 34 additions & 0 deletions
34
src/MockHttp/Matchers/Patterns/RelativeOrAbsoluteUriPatternMatcher.cs
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,34 @@ | ||
using System.Diagnostics; | ||
using MockHttp.Http; | ||
|
||
namespace MockHttp.Matchers.Patterns; | ||
|
||
[DebuggerDisplay($"{{{nameof(_originalUri)}}}")] | ||
internal sealed class RelativeOrAbsoluteUriPatternMatcher : UriPatternMatcher | ||
{ | ||
private readonly Uri _originalUri; | ||
private readonly Uri _uri; | ||
|
||
public RelativeOrAbsoluteUriPatternMatcher(Uri uri) | ||
{ | ||
_originalUri = uri; | ||
_uri = uri.EnsureIsRooted(); | ||
} | ||
|
||
public override bool IsMatch(Uri value) | ||
{ | ||
return IsAbsoluteUriMatch(value) || IsRelativeUriMatch(value); | ||
} | ||
|
||
private bool IsAbsoluteUriMatch(Uri uri) | ||
{ | ||
return _uri.IsAbsoluteUri && uri.Equals(_uri); | ||
} | ||
|
||
private bool IsRelativeUriMatch(Uri uri) | ||
{ | ||
return !_uri.IsAbsoluteUri | ||
&& uri.IsBaseOf(_uri) | ||
&& uri.ToString().EndsWith(_uri.ToString(), StringComparison.Ordinal); | ||
} | ||
} |
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,6 @@ | ||
namespace MockHttp.Matchers.Patterns; | ||
|
||
internal abstract class UriPatternMatcher : IPatternMatcher<Uri> | ||
{ | ||
public abstract bool IsMatch(Uri value); | ||
} |
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,19 @@ | ||
namespace MockHttp.Matchers.Patterns; | ||
|
||
internal class UriStringPatternMatcher : UriPatternMatcher | ||
{ | ||
private readonly Func<Uri, string> _selector; | ||
private readonly IPatternMatcher<string> _stringPatternMatcher; | ||
|
||
public UriStringPatternMatcher(Func<Uri, string> selector, IPatternMatcher<string> stringPatternMatcher) | ||
{ | ||
_selector = selector ?? throw new ArgumentNullException(nameof(selector)); | ||
_stringPatternMatcher = stringPatternMatcher ?? throw new ArgumentNullException(nameof(stringPatternMatcher)); | ||
} | ||
|
||
public override bool IsMatch(Uri value) | ||
{ | ||
string v = _selector(value); | ||
return _stringPatternMatcher.IsMatch(v); | ||
} | ||
} |
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,59 @@ | ||
using System.Runtime.CompilerServices; | ||
using MockHttp.Matchers.Patterns; | ||
using MockHttp.Responses; | ||
|
||
namespace MockHttp.Matchers; | ||
|
||
/// <summary> | ||
/// Matches a request by the URI. | ||
/// </summary> | ||
public class UriMatcher : HttpRequestMatcher | ||
{ | ||
private readonly IPatternMatcher<Uri> _patternMatcher; | ||
private readonly string _name; | ||
private readonly string _patternDescription; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="UriMatcher" /> class. | ||
/// </summary> | ||
/// <param name="patternMatcher">A matcher implementation that validates the URI.</param> | ||
/// <param name="patternDescription">A description of the pattern.</param> | ||
/// <param name="name">The name of this matcher.</param> | ||
/// <exception cref="ArgumentNullException">Thrown when a required argument is <see langword="null" />.</exception> | ||
internal UriMatcher | ||
( | ||
IPatternMatcher<Uri> patternMatcher, | ||
string patternDescription, | ||
[CallerMemberName] string? name = null | ||
) | ||
{ | ||
_patternMatcher = patternMatcher ?? throw new ArgumentNullException(nameof(patternMatcher)); | ||
_patternDescription = patternDescription ?? throw new ArgumentNullException(nameof(patternDescription)); | ||
|
||
name ??= GetType().Name; | ||
if (name.EndsWith("Matcher", StringComparison.Ordinal)) | ||
{ | ||
name = name.Remove(name.Length - "Matcher".Length); | ||
} | ||
|
||
_name = name; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override bool IsMatch(MockHttpRequestContext requestContext) | ||
{ | ||
if (requestContext is null) | ||
{ | ||
throw new ArgumentNullException(nameof(requestContext)); | ||
} | ||
|
||
Uri? uri = requestContext.Request.RequestUri; | ||
return uri is not null && _patternMatcher.IsMatch(uri); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override string ToString() | ||
{ | ||
return $"{_name}: '{_patternDescription}'"; | ||
} | ||
} |