From 3cbd7ad6380efd04f0c35e274d4cbb2b95e3ce84 Mon Sep 17 00:00:00 2001 From: charlie green Date: Wed, 18 Sep 2024 13:01:34 +0100 Subject: [PATCH] Allow leaving encoded slash in UrlSegmentParameter value --- .../Parameters/UrlSegmentParameter.cs | 5 ++-- test/RestSharp.Tests/ParametersTests.cs | 24 +++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/RestSharp/Parameters/UrlSegmentParameter.cs b/src/RestSharp/Parameters/UrlSegmentParameter.cs index b9da7752a..c3c4ff98a 100644 --- a/src/RestSharp/Parameters/UrlSegmentParameter.cs +++ b/src/RestSharp/Parameters/UrlSegmentParameter.cs @@ -26,10 +26,11 @@ public partial record UrlSegmentParameter : NamedParameter { /// Parameter name /// Parameter value /// Optional: encode the value, default is true - public UrlSegmentParameter(string name, string value, bool encode = true) + /// Optional: whether to replace all %2f and %2F in the parameter value with '/', default is true + public UrlSegmentParameter(string name, string value, bool encode = true, bool replaceEncodedSlash = true) : base( name, - RegexPattern.Replace(Ensure.NotEmptyString(value, nameof(value)), "/"), + replaceEncodedSlash ? RegexPattern.Replace(Ensure.NotEmptyString(value, nameof(value)), "/") : value, ParameterType.UrlSegment, encode ) { } diff --git a/test/RestSharp.Tests/ParametersTests.cs b/test/RestSharp.Tests/ParametersTests.cs index e9c702fbc..b8807ed20 100644 --- a/test/RestSharp.Tests/ParametersTests.cs +++ b/test/RestSharp.Tests/ParametersTests.cs @@ -63,4 +63,28 @@ public void AddUrlSegmentModifiesUrlSegmentWithString() { expected.Should().BeEquivalentTo(actual); } + + [Theory] + [InlineData("bar%2fBAR")] + [InlineData("bar%2FBAR")] + public void UrlSegmentParameter_WithValueWithEncodedSlash_WillReplaceEncodedSlashByDefault(string inputValue) { + var urlSegmentParameter = new UrlSegmentParameter("foo", inputValue); + urlSegmentParameter.Value.Should().BeEquivalentTo("bar/BAR"); + } + + [Theory] + [InlineData("bar%2fBAR")] + [InlineData("bar%2FBAR")] + public void UrlSegmentParameter_WithValueWithEncodedSlash_CanReplaceEncodedSlash(string inputValue) { + var urlSegmentParameter = new UrlSegmentParameter("foo", inputValue, replaceEncodedSlash: true); + urlSegmentParameter.Value.Should().BeEquivalentTo("bar/BAR"); + } + + [Theory] + [InlineData("bar%2fBAR")] + [InlineData("bar%2FBAR")] + public void UrlSegmentParameter_WithValueWithEncodedSlash_CanLeaveEncodedSlash(string inputValue) { + var urlSegmentParameter = new UrlSegmentParameter("foo", inputValue, replaceEncodedSlash: false); + urlSegmentParameter.Value.Should().BeEquivalentTo(inputValue); + } } \ No newline at end of file