mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-11 21:10:32 +01:00
Check if the path is valid when using WithPath(...) (#1377)
This commit is contained in:
@@ -109,7 +109,7 @@ public partial class MappingConverterTests
|
||||
var guid = new Guid("8e7b9ab7-e18e-4502-8bc9-11e6679811cc");
|
||||
var request = Request.Create()
|
||||
.UsingGet()
|
||||
.WithPath("test_path")
|
||||
.WithPath("/test_path")
|
||||
.WithParam("q", "42")
|
||||
.WithClientIP("112.123.100.99")
|
||||
.WithHeader("h-key", "h-value")
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
builder
|
||||
.Given(Request.Create()
|
||||
.UsingMethod("GET")
|
||||
.WithPath(new WildcardMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, "test_path", false, WireMock.Matchers.MatchOperator.Or))
|
||||
.WithPath(new WildcardMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, "/test_path", false, WireMock.Matchers.MatchOperator.Or))
|
||||
.WithParam("q", new ExactMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, false, WireMock.Matchers.MatchOperator.And, "42"))
|
||||
.WithClientIP("112.123.100.99")
|
||||
.WithHeader("h-key", "h-value", true)
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
builder
|
||||
.Given(Request.Create()
|
||||
.UsingMethod("GET")
|
||||
.WithPath(new WildcardMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, "test_path", false, WireMock.Matchers.MatchOperator.Or))
|
||||
.WithPath(new WildcardMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, "/test_path", false, WireMock.Matchers.MatchOperator.Or))
|
||||
.WithParam("q", new ExactMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, false, WireMock.Matchers.MatchOperator.And, "42"))
|
||||
.WithClientIP("112.123.100.99")
|
||||
.WithHeader("h-key", "h-value", true)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
server
|
||||
.Given(Request.Create()
|
||||
.UsingMethod("GET")
|
||||
.WithPath(new WildcardMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, "test_path", false, WireMock.Matchers.MatchOperator.Or))
|
||||
.WithPath(new WildcardMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, "/test_path", false, WireMock.Matchers.MatchOperator.Or))
|
||||
.WithParam("q", new ExactMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, false, WireMock.Matchers.MatchOperator.And, "42"))
|
||||
.WithClientIP("112.123.100.99")
|
||||
.WithHeader("h-key", "h-value", true)
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
server
|
||||
.Given(Request.Create()
|
||||
.UsingMethod("GET")
|
||||
.WithPath(new WildcardMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, "test_path", false, WireMock.Matchers.MatchOperator.Or))
|
||||
.WithPath(new WildcardMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, "/test_path", false, WireMock.Matchers.MatchOperator.Or))
|
||||
.WithParam("q", new ExactMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, false, WireMock.Matchers.MatchOperator.And, "42"))
|
||||
.WithClientIP("112.123.100.99")
|
||||
.WithHeader("h-key", "h-value", true)
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
Matchers: [
|
||||
{
|
||||
Name: WildcardMatcher,
|
||||
Pattern: x,
|
||||
Pattern: /x,
|
||||
IgnoreCase: false
|
||||
}
|
||||
]
|
||||
|
||||
@@ -56,7 +56,7 @@ public class ProxyMappingConverterTests
|
||||
|
||||
var request = Request.Create()
|
||||
.UsingPost()
|
||||
.WithPath("x")
|
||||
.WithPath("/x")
|
||||
.WithParam("p1", "p1-v")
|
||||
.WithParam("p2", "p2-v")
|
||||
.WithHeader("Content-Type", new ContentTypeMatcher("text/plain"))
|
||||
|
||||
42
test/WireMock.Net.Tests/Validators/PathValidatorTests.cs
Normal file
42
test/WireMock.Net.Tests/Validators/PathValidatorTests.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using FluentAssertions;
|
||||
using WireMock.Validators;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.Validators;
|
||||
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class PathValidatorTests
|
||||
{
|
||||
[Fact]
|
||||
public void ValidateAndThrow_ValidPath_DoesNotThrow()
|
||||
{
|
||||
Action act = () => PathValidator.ValidateAndThrow("/valid/path");
|
||||
act.Should().NotThrow();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null)]
|
||||
[InlineData("")]
|
||||
[InlineData("\r")]
|
||||
[InlineData("\n")]
|
||||
[InlineData("\t")]
|
||||
public void ValidateAndThrow_InvalidPath_ThrowsArgumentException_WithDefaultParamName(string? path)
|
||||
{
|
||||
Action act = () => PathValidator.ValidateAndThrow(path);
|
||||
var ex = act.Should().Throw<ArgumentException>().Which;
|
||||
ex.Message.Should().StartWith("Path must start with a '/' and cannot be null, empty or whitespace.");
|
||||
ex.ParamName.Should().Be("path");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ValidateAndThrow_NoLeadingSlash_ThrowsArgumentException_WithProvidedParamName()
|
||||
{
|
||||
Action act = () => PathValidator.ValidateAndThrow("noSlash", "myParam");
|
||||
var ex = act.Should().Throw<ArgumentException>().Which;
|
||||
ex.ParamName.Should().Be("myParam");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user