-
-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed failing admin requests when content type includes a charset (ba…
- Loading branch information
Showing
6 changed files
with
179 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using System.Net.Http.Headers; | ||
using JetBrains.Annotations; | ||
|
||
namespace WireMock.Matchers | ||
{ | ||
/// <summary> | ||
/// ContentTypeMatcher which accepts also all charsets | ||
/// </summary> | ||
/// <seealso cref="RegexMatcher" /> | ||
public class ContentTypeMatcher : WildcardMatcher | ||
{ | ||
private readonly string[] _patterns; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ContentTypeMatcher"/> class. | ||
/// </summary> | ||
/// <param name="pattern">The pattern.</param> | ||
/// <param name="ignoreCase">IgnoreCase (default false)</param> | ||
public ContentTypeMatcher([NotNull] string pattern, bool ignoreCase = false) : this(new[] { pattern }, ignoreCase) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ContentTypeMatcher"/> class. | ||
/// </summary> | ||
/// <param name="matchBehaviour">The match behaviour.</param> | ||
/// <param name="pattern">The pattern.</param> | ||
/// <param name="ignoreCase">IgnoreCase (default false)</param> | ||
public ContentTypeMatcher(MatchBehaviour matchBehaviour, [NotNull] string pattern, bool ignoreCase = false) : this(matchBehaviour, new[] { pattern }, ignoreCase) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ContentTypeMatcher"/> class. | ||
/// </summary> | ||
/// <param name="patterns">The patterns.</param> | ||
/// <param name="ignoreCase">IgnoreCase (default false)</param> | ||
public ContentTypeMatcher([NotNull] string[] patterns, bool ignoreCase = false) : this(MatchBehaviour.AcceptOnMatch, patterns, ignoreCase) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ContentTypeMatcher"/> class. | ||
/// </summary> | ||
/// <param name="matchBehaviour">The match behaviour.</param> | ||
/// <param name="patterns">The patterns.</param> | ||
/// <param name="ignoreCase">IgnoreCase (default false)</param> | ||
public ContentTypeMatcher(MatchBehaviour matchBehaviour, [NotNull] string[] patterns, bool ignoreCase = false) : base(matchBehaviour, patterns, ignoreCase) | ||
{ | ||
_patterns = patterns; | ||
} | ||
|
||
/// <inheritdoc cref="RegexMatcher.IsMatch"/> | ||
public override double IsMatch(string input) | ||
{ | ||
if (string.IsNullOrEmpty(input) || !MediaTypeHeaderValue.TryParse(input, out MediaTypeHeaderValue contentType)) | ||
{ | ||
return MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.Mismatch); | ||
} | ||
|
||
return base.IsMatch(contentType.MediaType); | ||
} | ||
|
||
/// <inheritdoc cref="IStringMatcher.GetPatterns"/> | ||
public override string[] GetPatterns() | ||
{ | ||
return _patterns; | ||
} | ||
|
||
/// <inheritdoc cref="IMatcher.Name"/> | ||
public override string Name => "ContentTypeMatcher"; | ||
} | ||
} |
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
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
58 changes: 58 additions & 0 deletions
58
test/WireMock.Net.Tests/Matchers/ContentTypeMatcherTests.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,58 @@ | ||
using NFluent; | ||
using WireMock.Matchers; | ||
using Xunit; | ||
|
||
namespace WireMock.Net.Tests.Matchers | ||
{ | ||
public class ContentTypeMatcherTests | ||
{ | ||
[Theory] | ||
[InlineData("application/json")] | ||
[InlineData("application/json; charset=ascii")] | ||
[InlineData("application/json; charset=utf-8")] | ||
[InlineData("application/json; charset=UTF-8")] | ||
public void ContentTypeMatcher_IsMatchWithIgnoreCaseFalse_Positive(string contentType) | ||
{ | ||
var matcher = new ContentTypeMatcher("application/json"); | ||
Check.That(matcher.IsMatch(contentType)).IsEqualTo(1.0d); | ||
} | ||
|
||
[Theory] | ||
[InlineData("application/json")] | ||
[InlineData("application/JSON")] | ||
[InlineData("application/json; CharSet=ascii")] | ||
[InlineData("application/json; charset=utf-8")] | ||
[InlineData("application/json; charset=UTF-8")] | ||
public void ContentTypeMatcher_IsMatchWithIgnoreCaseTrue_Positive(string contentType) | ||
{ | ||
var matcher = new ContentTypeMatcher("application/json", true); | ||
Check.That(matcher.IsMatch(contentType)).IsEqualTo(1.0d); | ||
} | ||
|
||
[Fact] | ||
public void ContentTypeMatcher_GetName() | ||
{ | ||
// Assign | ||
var matcher = new ContentTypeMatcher("x"); | ||
|
||
// Act | ||
string name = matcher.Name; | ||
|
||
// Assert | ||
Check.That(name).Equals("ContentTypeMatcher"); | ||
} | ||
|
||
[Fact] | ||
public void ContentTypeMatcher_GetPatterns() | ||
{ | ||
// Assign | ||
var matcher = new ContentTypeMatcher("x"); | ||
|
||
// Act | ||
string[] patterns = matcher.GetPatterns(); | ||
|
||
// Assert | ||
Check.That(patterns).ContainsExactly("x"); | ||
} | ||
} | ||
} |