Skip to content
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

Allow leaving encoded slash in UrlSegmentParameter value #2265

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/RestSharp/Parameters/UrlSegmentParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ public partial record UrlSegmentParameter : NamedParameter {
/// <param name="name">Parameter name</param>
/// <param name="value">Parameter value</param>
/// <param name="encode">Optional: encode the value, default is true</param>
public UrlSegmentParameter(string name, string value, bool encode = true)
/// <param name="replaceEncodedSlash">Optional: whether to replace all %2f and %2F in the parameter value with '/', default is true</param>
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
) { }
Expand Down
24 changes: 24 additions & 0 deletions test/RestSharp.Tests/ParametersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Loading