Skip to content

Commit

Permalink
Encode the resource query as it should be.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeyzimarev committed May 27, 2024
1 parent af14a20 commit 41a2ba4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/RestSharp/Request/RestRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Net;
using System.Net.Http.Headers;
using RestSharp.Authenticators;
using RestSharp.Extensions;
using RestSharp.Interceptors;

// ReSharper disable ReplaceSubstringWithRangeIndexer
// ReSharper disable UnusedAutoPropertyAccessor.Global

namespace RestSharp;

using Authenticators;
using Extensions;
using Interceptors;

/// <summary>
/// Container for data used to make requests
/// </summary>
Expand Down Expand Up @@ -53,7 +53,7 @@ public RestRequest(string? resource, Method method = Method.Get) : this() {
var queryParams = ParseQuery(Resource.Substring(queryStringStart + 1));
Resource = Resource.Substring(0, queryStringStart);

foreach (var param in queryParams) this.AddQueryParameter(param.Key, param.Value, false);
foreach (var param in queryParams) this.AddQueryParameter(param.Key, param.Value);

return;

Expand Down Expand Up @@ -84,12 +84,12 @@ public RestRequest(Uri resource, Method method = Method.Get)
/// Always send a multipart/form-data request - even when no Files are present.
/// </summary>
public bool AlwaysMultipartFormData { get; set; }

/// <summary>
/// Always send a file as request content without multipart/form-data request - even when the request contains only one file parameter
/// </summary>
public bool AlwaysSingleFileAsContent { get; set; }

/// <summary>
/// When set to true, parameter values in a multipart form data requests will be enclosed in
/// quotation marks. Default is false. Enable it if the remote endpoint requires parameters
Expand Down Expand Up @@ -232,7 +232,7 @@ public Func<HttpResponseMessage, RestRequest, RestResponse>? AdvancedResponseWri
_advancedResponseHandler = value;
}
}

/// <summary>
/// Request-level interceptors. Will be combined with client-level interceptors if set.
/// </summary>
Expand All @@ -255,4 +255,4 @@ public RestRequest RemoveParameter(Parameter parameter) {
}

internal RestRequest AddFile(FileParameter file) => this.With(x => x._files.Add(file));
}
}
10 changes: 10 additions & 0 deletions test/RestSharp.Tests/UrlBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,16 @@ public void Should_use_ipv6_address() {
actual.AbsoluteUri.Should().Be("https://[fe80::290:e8ff:fe8b:2537]:8443/api/v1/auth");
}

[Fact]
public void Should_encode_resource() {
const string baseUrl = "https://example.com";
const string resource = "resource?param=value with spaces";

var request = new RestRequest(resource);
var uri = new Uri($"{baseUrl}/{resource}");
AssertUri(baseUrl, request, uri.AbsoluteUri);
}

[Fact]
public void Should_not_encode_pipe() {
var request = new RestRequest("resource").AddQueryParameter("ids", "in:001|116", false);
Expand Down

0 comments on commit 41a2ba4

Please sign in to comment.