Skip to content

Commit

Permalink
Force not escape query
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeyzimarev committed May 27, 2024
1 parent 7334e28 commit 25c4124
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/RestSharp/Request/UriExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,26 @@ public static Uri MergeBaseUrlAndResource(this Uri? baseUrl, string? resource) {
: throw new ArgumentException("Both BaseUrl and Resource are empty", nameof(resource));
}

var usingBaseUri = baseUrl.AbsoluteUri.EndsWith("/") || assembled.IsEmpty() ? baseUrl : new Uri(baseUrl.AbsoluteUri + "/");
var usingBaseUri = baseUrl.AbsoluteUri.EndsWith("/") || assembled.IsEmpty() ? baseUrl : new Uri($"{baseUrl.AbsoluteUri}/");

return assembled != null ? new Uri(usingBaseUri, assembled) : baseUrl;
}

public static Uri AddQueryString(this Uri uri, string? query) {
if (query == null) return uri;

var absoluteUri = uri.AbsoluteUri;
var separator = absoluteUri.Contains('?') ? "&" : "?";

return new Uri($"{absoluteUri}{separator}{query}");
var absoluteUri = uri.AbsoluteUri;
var separator = absoluteUri.Contains('?') ? "&" : "?";

var result =
#if NET6_0_OR_GREATER
new Uri($"{absoluteUri}{separator}{query}", new UriCreationOptions{DangerousDisablePathAndQueryCanonicalization = true});
#else
#pragma warning disable CS0618 // Type or member is obsolete
new Uri($"{absoluteUri}{separator}{query}", false);
#pragma warning restore CS0618 // Type or member is obsolete
#endif
return result;
}

public static UrlSegmentParamsValues GetUrlSegmentParamsValues(
Expand Down Expand Up @@ -72,4 +80,4 @@ params ParametersCollection[] parametersCollections
}
}

record UrlSegmentParamsValues(Uri Uri, string Resource);
record UrlSegmentParamsValues(Uri Uri, string Resource);
12 changes: 12 additions & 0 deletions test/RestSharp.Tests/UrlBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -361,4 +361,16 @@ public void Should_use_ipv6_address() {
actual.HostNameType.Should().Be(UriHostNameType.IPv6);
actual.AbsoluteUri.Should().Be("https://[fe80::290:e8ff:fe8b:2537]:8443/api/v1/auth");
}

[Fact]
public async Task Should_not_encode_pipe() {
var client = new RestClient("https://enggmsn5amo79.x.pipedream.net");
var request = new RestRequest("resource");
request.AddQueryParameter("ids", "in:001|116", true);

// var actual = client.BuildUri(request);
// var expected = new Uri("http://example.com/resource?ids=in:001|116");
// actual.Should().Be(expected);
await client.GetAsync(request);
}
}

0 comments on commit 25c4124

Please sign in to comment.