-
-
Notifications
You must be signed in to change notification settings - Fork 746
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: custom query key formatters (#1570)
* feature: Introduce support for custom URL query key formatters - Implements a key formatter for `camelCase` * docs: Adds querystrings examples * removes redundant code from `CamelCaseUrlParameterKeyFormatter.cs` * fix: restores binary-compability * Update after merge * chore: remove useless piece of code * feat(tests): CamelCaseUrlParameterKeyFormatter tests * feat(Tests): RefitSettings tests --------- Co-authored-by: Chris Pulman <chris.pulman@yahoo.com>
- Loading branch information
1 parent
d85edef
commit 1b45219
Showing
8 changed files
with
3,089 additions
and
4,095 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using Xunit; | ||
|
||
namespace Refit.Tests; | ||
|
||
public class CamelCaselTestsRequest | ||
{ | ||
public string alreadyCamelCased { get; set; } | ||
public string NOTCAMELCased { get; set; } | ||
} | ||
|
||
public class CamelCaseUrlParameterKeyFormatterTests | ||
{ | ||
[Fact] | ||
public void Format_EmptyKey_ReturnsEmptyKey() | ||
{ | ||
var urlParameterKeyFormatter = new CamelCaseUrlParameterKeyFormatter(); | ||
|
||
var output = urlParameterKeyFormatter.Format(string.Empty); | ||
Assert.Equal(string.Empty, output); | ||
} | ||
|
||
[Fact] | ||
public void FormatKey_Returns_ExpectedValue() | ||
{ | ||
var urlParameterKeyFormatter = new CamelCaseUrlParameterKeyFormatter(); | ||
|
||
var refitSettings = new RefitSettings { UrlParameterKeyFormatter = urlParameterKeyFormatter }; | ||
var fixture = new RequestBuilderImplementation<IDummyHttpApi>(refitSettings); | ||
var factory = fixture.BuildRequestFactoryForMethod(nameof(IDummyHttpApi.ComplexQueryObjectWithDictionary)); | ||
|
||
var complexQuery = new CamelCaselTestsRequest | ||
{ | ||
alreadyCamelCased = "value1", | ||
NOTCAMELCased = "value2" | ||
}; | ||
|
||
var output = factory([complexQuery]); | ||
var uri = new Uri(new Uri("http://api"), output.RequestUri); | ||
|
||
Assert.Equal("/foo?alreadyCamelCased=value1¬camelCased=value2", uri.PathAndQuery); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using Xunit; | ||
|
||
namespace Refit.Tests; | ||
|
||
public class RefitSettingsTests | ||
{ | ||
[Fact] | ||
public void Can_CreateRefitSettings_WithoutException() | ||
{ | ||
var contentSerializer = new NewtonsoftJsonContentSerializer(); | ||
var urlParameterFormatter = new DefaultUrlParameterFormatter(); | ||
var urlParameterKeyFormatter = new CamelCaseUrlParameterKeyFormatter(); | ||
var formUrlEncodedParameterFormatter = new DefaultFormUrlEncodedParameterFormatter(); | ||
|
||
var exception = Record.Exception(() => new RefitSettings()); | ||
Assert.Null(exception); | ||
|
||
exception = Record.Exception(() => new RefitSettings(contentSerializer)); | ||
Assert.Null(exception); | ||
|
||
exception = Record.Exception(() => new RefitSettings(contentSerializer, urlParameterFormatter)); | ||
Assert.Null(exception); | ||
|
||
exception = Record.Exception(() => new RefitSettings(contentSerializer, urlParameterFormatter, formUrlEncodedParameterFormatter)); | ||
Assert.Null(exception); | ||
|
||
exception = Record.Exception(() => new RefitSettings(contentSerializer, urlParameterFormatter, formUrlEncodedParameterFormatter, urlParameterKeyFormatter)); | ||
Assert.Null(exception); | ||
} | ||
} |
Oops, something went wrong.