Skip to content

Commit

Permalink
Merge branch 'finbourne-dev' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeyzimarev committed Dec 17, 2024
2 parents 25922ce + 40b9416 commit d5739bb
Show file tree
Hide file tree
Showing 17 changed files with 356 additions and 323 deletions.
6 changes: 4 additions & 2 deletions src/RestSharp/Parameters/UrlSegmentParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

using System.Text.RegularExpressions;
using RestSharp.Extensions;

namespace RestSharp;

Expand All @@ -26,10 +27,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)), "/"),
value.IsEmpty() ? value : replaceEncodedSlash ? RegexPattern.Replace(value, "/") : value,
ParameterType.UrlSegment,
encode
) { }
Expand Down
23 changes: 23 additions & 0 deletions test/RestSharp.Tests/Headers/DefaultHeaderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace RestSharp.Tests.Headers;

public class DefaultHeaderTests {
const string BaseUrl = "http://localhost:8888/";

[Fact]
public void AddDefaultHeadersUsingDictionary() {
var headers = new Dictionary<string, string> {
{ KnownHeaders.ContentType, ContentType.Json },
{ KnownHeaders.Accept, ContentType.Json },
{ KnownHeaders.ContentEncoding, "gzip, deflate" }
};

var expected = headers.Select(x => new HeaderParameter(x.Key, x.Value));

using var client = new RestClient(BaseUrl);
client.AddDefaultHeaders(headers);

var actual = client.DefaultParameters.Select(x => x as HeaderParameter);
expected.Should().BeSubsetOf(actual);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace RestSharp.Tests;
namespace RestSharp.Tests.Parameters;

public class AddRangeTests {
public class HeaderRangeTests {
[Fact]
public async Task ShouldParseOutLongRangeSpecifier() {
using var restClient = new RestClient("http://localhost");
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace RestSharp.Tests;
namespace RestSharp.Tests.Parameters;

public partial class ObjectParameterTests {
sealed record ArrayData<TEnumerable>([property: RequestProperty(ArrayQueryType = RequestArrayQueryType.ArrayParameters)] TEnumerable Array) where TEnumerable : notnull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace RestSharp.Tests;
namespace RestSharp.Tests.Parameters;

public partial class ObjectParameterTests {
sealed record CsvData<TEnumerable>([property: RequestProperty(ArrayQueryType = RequestArrayQueryType.CommaSeparated)] TEnumerable Csv) where TEnumerable : notnull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace RestSharp.Tests;
namespace RestSharp.Tests.Parameters;

public partial class ObjectParameterTests {
sealed record FormattedData<TDateTime>([property: RequestProperty(Format = "hh:mm tt")] TDateTime FormattedParameter) where TDateTime : notnull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace RestSharp.Tests;
namespace RestSharp.Tests.Parameters;

public partial class ObjectParameterTests {
sealed record NamedData([property: RequestProperty(Name = "CustomName")] object NamedParameter);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Collections;
using System.Globalization;

namespace RestSharp.Tests;
namespace RestSharp.Tests.Parameters;

public partial class ObjectParameterTests {
public ObjectParameterTests() => Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace RestSharp.Tests;
namespace RestSharp.Tests.Parameters;

public class ParameterValidationTests {
[Fact]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,8 @@
namespace RestSharp.Tests;
namespace RestSharp.Tests.Parameters;

public class ParametersTests {
public class UrlSegmentTests {
const string BaseUrl = "http://localhost:8888/";

[Fact]
public void AddDefaultHeadersUsingDictionary() {
var headers = new Dictionary<string, string> {
{ KnownHeaders.ContentType, ContentType.Json },
{ KnownHeaders.Accept, ContentType.Json },
{ KnownHeaders.ContentEncoding, "gzip, deflate" }
};

var expected = headers.Select(x => new HeaderParameter(x.Key, x.Value));

using var client = new RestClient(BaseUrl);
client.AddDefaultHeaders(headers);

var actual = client.DefaultParameters.Select(x => x as HeaderParameter);
expected.Should().BeSubsetOf(actual);
}

[Fact]
public void AddUrlSegmentWithInt() {
const string name = "foo";
Expand Down Expand Up @@ -63,4 +46,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);
}
}
32 changes: 0 additions & 32 deletions test/RestSharp.Tests/RestClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,38 +32,6 @@ public async Task ConfigureHttp_will_set_proxy_to_null_with_no_exceptions_When_n
await client.ExecuteAsync(req);
}

[Fact]
public void BuildUri_should_build_with_passing_link_as_Uri() {
// arrange
var relative = new Uri("/foo/bar/baz", UriKind.Relative);
var absoluteUri = new Uri(new Uri(BaseUrl), relative);
var req = new RestRequest(absoluteUri);

// act
using var client = new RestClient();

var builtUri = client.BuildUri(req);

// assert
absoluteUri.Should().Be(builtUri);
}

[Fact]
public void BuildUri_should_build_with_passing_link_as_Uri_with_set_BaseUrl() {
// arrange
var baseUrl = new Uri(BaseUrl);
var relative = new Uri("/foo/bar/baz", UriKind.Relative);
var req = new RestRequest(relative);

// act
using var client = new RestClient(baseUrl);

var builtUri = client.BuildUri(req);

// assert
new Uri(baseUrl, relative).Should().Be(builtUri);
}

[Fact]
public void UseJson_leaves_only_json_serializer() {
// arrange
Expand Down
14 changes: 4 additions & 10 deletions test/RestSharp.Tests/RestSharp.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,11 @@
<None Update="SampleData\underscore_prefix.json" CopyToOutputDirectory="PreserveNewest"/>
</ItemGroup>
<ItemGroup>
<Compile Update="ObjectParameterTests.ArrayData.cs">
<DependentUpon>ObjectParameterTests.cs</DependentUpon>
<Compile Update="UrlBuilderTests.Get.cs">
<DependentUpon>UrlBuilderTests.cs</DependentUpon>
</Compile>
<Compile Update="ObjectParameterTests.CsvData.cs">
<DependentUpon>ObjectParameterTests.cs</DependentUpon>
</Compile>
<Compile Update="ObjectParameterTests.FormattedData.cs">
<DependentUpon>ObjectParameterTests.cs</DependentUpon>
</Compile>
<Compile Update="ObjectParameterTests.NamedData.cs">
<DependentUpon>ObjectParameterTests.cs</DependentUpon>
<Compile Update="UrlBuilderTests.Post.cs">
<DependentUpon>UrlBuilderTests.cs</DependentUpon>
</Compile>
</ItemGroup>
</Project>
Loading

0 comments on commit d5739bb

Please sign in to comment.