Skip to content

Commit

Permalink
feat: add generic and enumerable tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyMakkison committed Oct 22, 2024
1 parent 76f80a6 commit 42177d4
Showing 1 changed file with 167 additions and 27 deletions.
194 changes: 167 additions & 27 deletions Refit.Tests/ReflectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,21 @@ public interface IBasicApi
[Get("/{value}")]
Task<string> GetGenericParam<T>(T value);

[Get("/")]
Task<string> GetQuery(string queryKey);

[Get("/")]
Task<string> GetGenericQuery<T>(T queryKey);

[Get("/")]
Task<string> GetPropertyQuery(BaseRecord queryKey);

[Get("/")]
Task<string> GetEnumerableQuery(IEnumerable<string> enums);

[Get("/")]
Task<string> GetEnumerablePropertyQuery(MyEnumerableParams enums);

[Get("/")]
Task<string> GetDictionaryQuery(IDictionary<string, object> dict);
}
Expand All @@ -32,6 +44,8 @@ public record BaseRecord(string Value);

public record MyParams(string PropValue);

public record MyEnumerableParams(int[] Enumerable);

public class TestUrlFormatter : IUrlParameterFormatter
{
private readonly ICustomAttributeProvider[] expectedAttributeProviders;
Expand All @@ -44,7 +58,10 @@ public TestUrlFormatter(ICustomAttributeProvider expectedAttributeProvider, Type
expectedTypes = [expectedType];
}

public TestUrlFormatter(ICustomAttributeProvider[] expectedAttributeProviders, Type[] expectedTypes)
public TestUrlFormatter(
ICustomAttributeProvider[] expectedAttributeProviders,
Type[] expectedTypes
)
{
this.expectedAttributeProviders = expectedAttributeProviders;
this.expectedTypes = expectedTypes;
Expand Down Expand Up @@ -80,8 +97,11 @@ public async Task UrlParameterShouldBeExpectedReflection()
var parameterInfo = methodInfo.GetParameters()[0];

var formatter = new TestUrlFormatter(parameterInfo, typeof(string));
var settings = new RefitSettings() { HttpMessageHandlerFactory = () => mockHandler };
settings.UrlParameterFormatter = formatter;
var settings = new RefitSettings
{
HttpMessageHandlerFactory = () => mockHandler,
UrlParameterFormatter = formatter
};
var service = RestService.For<IBasicApi>("http://foo", settings);

await service.GetParam("bar");
Expand All @@ -99,8 +119,11 @@ public async Task DerivedUrlParameterShouldBeExpectedReflection()
var parameterInfo = methodInfo.GetParameters()[0];

var formatter = new TestUrlFormatter(parameterInfo, typeof(BaseRecord));
var settings = new RefitSettings() { HttpMessageHandlerFactory = () => mockHandler };
settings.UrlParameterFormatter = formatter;
var settings = new RefitSettings
{
HttpMessageHandlerFactory = () => mockHandler,
UrlParameterFormatter = formatter
};
var service = RestService.For<IBasicApi>("http://foo", settings);

await service.GetDerivedParam(new DerivedRecord("Derived"));
Expand All @@ -117,8 +140,11 @@ public async Task PropertyParameterShouldBeExpectedReflection()
var propertyInfo = typeof(MyParams).GetProperties()[0];

var formatter = new TestUrlFormatter(propertyInfo, typeof(string));
var settings = new RefitSettings() { HttpMessageHandlerFactory = () => mockHandler };
settings.UrlParameterFormatter = formatter;
var settings = new RefitSettings
{
HttpMessageHandlerFactory = () => mockHandler,
UrlParameterFormatter = formatter
};
var service = RestService.For<IBasicApi>("http://foo", settings);

await service.GetPropertyParam(new MyParams("propVal"));
Expand All @@ -137,33 +163,59 @@ public async Task GenericParameterShouldBeExpectedReflection()
var parameterInfo = stringMethod.GetParameters()[0];

var formatter = new TestUrlFormatter(parameterInfo, typeof(string));
var settings = new RefitSettings() { HttpMessageHandlerFactory = () => mockHandler };
settings.UrlParameterFormatter = formatter;
var settings = new RefitSettings
{
HttpMessageHandlerFactory = () => mockHandler,
UrlParameterFormatter = formatter
};
var service = RestService.For<IBasicApi>("http://foo", settings);

await service.GetGenericParam("genericVal");
formatter.AssertNoOutstandingAssertions();
}

[Fact]
public async Task QueryPropertyParameterShouldBeExpectedReflection()
public async Task QueryParameterShouldBeExpectedReflection()
{
mockHandler
.Expect(HttpMethod.Get, "http://foo/")
.WithExactQueryString(
new[]
{
new KeyValuePair<string, string>("Value", "queryVal"),
}
new[] { new KeyValuePair<string, string>("queryKey", "queryValue"), }
)
.Respond("application/json", nameof(IBasicApi.GetQuery));

var methodInfo = typeof(IBasicApi).GetMethod(nameof(IBasicApi.GetQuery))!;
var parameterInfo = methodInfo.GetParameters()[0];

var formatter = new TestUrlFormatter(parameterInfo, typeof(string));
var settings = new RefitSettings
{
HttpMessageHandlerFactory = () => mockHandler,
UrlParameterFormatter = formatter
};
var service = RestService.For<IBasicApi>("http://foo", settings);

await service.GetQuery("queryValue");
formatter.AssertNoOutstandingAssertions();
}

[Fact]
public async Task QueryPropertyParameterShouldBeExpectedReflection()
{
mockHandler
.Expect(HttpMethod.Get, "http://foo/")
.WithExactQueryString(new[] { new KeyValuePair<string, string>("Value", "queryVal"), })
.Respond("application/json", nameof(IBasicApi.GetPropertyQuery));

var methodInfo = typeof(IBasicApi).GetMethod(nameof(IBasicApi.GetPropertyQuery))!;
var parameterInfo = methodInfo.GetParameters()[0];

var formatter = new TestUrlFormatter(parameterInfo, typeof(BaseRecord));
var settings = new RefitSettings() { HttpMessageHandlerFactory = () => mockHandler };
settings.UrlParameterFormatter = formatter;
var settings = new RefitSettings
{
HttpMessageHandlerFactory = () => mockHandler,
UrlParameterFormatter = formatter
};
var service = RestService.For<IBasicApi>("http://foo", settings);

await service.GetPropertyQuery(new BaseRecord("queryVal"));
Expand All @@ -175,26 +227,103 @@ public async Task DerivedQueryPropertyParameterShouldBeExpectedReflection()
{
mockHandler
.Expect(HttpMethod.Get, "http://foo/")
.WithExactQueryString(
new[]
{
new KeyValuePair<string, string>("Value", "queryVal"),
}
)
.WithExactQueryString(new[] { new KeyValuePair<string, string>("Value", "queryVal"), })
.Respond("application/json", nameof(IBasicApi.GetPropertyQuery));

var methodInfo = typeof(IBasicApi).GetMethod(nameof(IBasicApi.GetPropertyQuery))!;
var parameterInfo = methodInfo.GetParameters()[0];

var formatter = new TestUrlFormatter(parameterInfo, typeof(BaseRecord));
var settings = new RefitSettings() { HttpMessageHandlerFactory = () => mockHandler };
settings.UrlParameterFormatter = formatter;
var settings = new RefitSettings
{
HttpMessageHandlerFactory = () => mockHandler,
UrlParameterFormatter = formatter
};
var service = RestService.For<IBasicApi>("http://foo", settings);

await service.GetPropertyQuery(new DerivedRecord("queryVal"));
formatter.AssertNoOutstandingAssertions();
}

[Fact]
public async Task GenericQueryParameterShouldBeExpectedReflection()
{
mockHandler
.Expect(HttpMethod.Get, "http://foo/")
.WithExactQueryString(
new[] { new KeyValuePair<string, string>("queryKey", "queryValue"), }
)
.Respond("application/json", nameof(IBasicApi.GetGenericQuery));

var methodInfo = typeof(IBasicApi).GetMethod(nameof(IBasicApi.GetGenericQuery))!;
var stringMethod = methodInfo.MakeGenericMethod(typeof(string));
var parameterInfo = stringMethod.GetParameters()[0];

var formatter = new TestUrlFormatter(parameterInfo, typeof(string));
var settings = new RefitSettings
{
HttpMessageHandlerFactory = () => mockHandler,
UrlParameterFormatter = formatter
};
var service = RestService.For<IBasicApi>("http://foo", settings);

await service.GetGenericQuery("queryValue");
formatter.AssertNoOutstandingAssertions();
}

[Fact]
public async Task EnumerableQueryParameterShouldBeExpectedReflection()
{
mockHandler
.Expect(HttpMethod.Get, "http://foo/")
.WithExactQueryString(new[] { new KeyValuePair<string, string>("enums", "k0,k1"), })
.Respond("application/json", nameof(IBasicApi.GetEnumerableQuery));

var methodInfo = typeof(IBasicApi).GetMethod(nameof(IBasicApi.GetEnumerableQuery))!;
var parameterInfo = methodInfo.GetParameters()[0];

var formatter = new TestUrlFormatter(
[parameterInfo, parameterInfo],
[typeof(IEnumerable<string>), typeof(IEnumerable<string>)]
);
var settings = new RefitSettings
{
HttpMessageHandlerFactory = () => mockHandler,
UrlParameterFormatter = formatter
};
var service = RestService.For<IBasicApi>("http://foo", settings);

await service.GetEnumerableQuery(["k0", "k1"]);
formatter.AssertNoOutstandingAssertions();
}

[Fact]
public async Task EnumerablePropertyQueryParameterShouldBeExpectedReflection()
{
mockHandler
.Expect(HttpMethod.Get, "http://foo/")
.WithExactQueryString(new[] { new KeyValuePair<string, string>("Enumerable", "0,1"), })
.Respond("application/json", nameof(IBasicApi.GetEnumerablePropertyQuery));

var methodInfo = typeof(IBasicApi).GetMethod(nameof(IBasicApi.GetEnumerablePropertyQuery))!;
var parameterInfo = methodInfo.GetParameters()[0];
var propertyInfo = typeof(MyEnumerableParams).GetProperties()[0];

var formatter = new TestUrlFormatter(
[propertyInfo, propertyInfo, parameterInfo],
[typeof(int[]), typeof(int[]), typeof(MyEnumerableParams)]
);
var settings = new RefitSettings
{
HttpMessageHandlerFactory = () => mockHandler,
UrlParameterFormatter = formatter
};
var service = RestService.For<IBasicApi>("http://foo", settings);

await service.GetEnumerablePropertyQuery(new MyEnumerableParams([0, 1]));
formatter.AssertNoOutstandingAssertions();
}

[Fact]
public async Task QueryDictionaryParameterShouldBeExpectedReflection()
{
Expand All @@ -212,9 +341,20 @@ public async Task QueryDictionaryParameterShouldBeExpectedReflection()
var methodInfo = typeof(IBasicApi).GetMethod(nameof(IBasicApi.GetDictionaryQuery))!;
var parameterInfo = methodInfo.GetParameters()[0];

var formatter = new TestUrlFormatter([typeof(string), typeof(string), parameterInfo, parameterInfo], [typeof(string), typeof(string), typeof(IDictionary<string, object>),typeof(IDictionary<string, object>)]);
var settings = new RefitSettings() { HttpMessageHandlerFactory = () => mockHandler };
settings.UrlParameterFormatter = formatter;
var formatter = new TestUrlFormatter(
[typeof(string), typeof(string), parameterInfo, parameterInfo],
[
typeof(string),
typeof(string),
typeof(IDictionary<string, object>),
typeof(IDictionary<string, object>)
]
);
var settings = new RefitSettings
{
HttpMessageHandlerFactory = () => mockHandler,
UrlParameterFormatter = formatter
};
var service = RestService.For<IBasicApi>("http://foo", settings);

var dict = new Dictionary<string, object> { { "key0", 1 }, { "key1", 2 } };
Expand Down

0 comments on commit 42177d4

Please sign in to comment.