Skip to content

Commit d9e0b81

Browse files
chore: convert to Refit.Tests
1 parent 80f5356 commit d9e0b81

1 file changed

Lines changed: 73 additions & 37 deletions

File tree

src/tests/Refit.Tests/ReflectionTests.cs

Lines changed: 73 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) 2019-2026 ReactiveUI and Contributors. All rights reserved.
22
// ReactiveUI and Contributors licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for full license information.
4-
using System.Net.Http;
54
using Refit.Testing;
65

76
namespace Refit.Tests;
@@ -18,6 +17,15 @@ public sealed class ReflectionTests
1817
/// <summary>The query value exercised by the query parameter tests.</summary>
1918
private const string QueryValue = "queryValue";
2019

20+
/// <summary>A constant value to use as a route parameter.</summary>
21+
private const string RouteValue = "Empty";
22+
23+
/// <summary>A constant value to use as a route parameter.</summary>
24+
private const int IntegerValue = 10;
25+
26+
/// <summary>A constant value to use as a route parameter.</summary>
27+
private const long LongValue = 10L;
28+
2129
/// <summary>A Refit interface with many overloaded generic methods.</summary>
2230
public interface IGenericMethod
2331
{
@@ -48,7 +56,7 @@ public interface IGenericMethod
4856

4957
/// <summary>A generic Refit interface with many overloaded generic methods.</summary>
5058
/// <typeparam name="TInterface">Generic type of interface.</typeparam>
51-
public interface IGenericMethodAndInterface<TInterface>
59+
public interface IGenericMethodAndInterface<in TInterface>
5260
{
5361
/// <summary>Request with a generic route parameter.</summary>
5462
/// <param name="value1">Generic route parameter.</param>
@@ -65,13 +73,14 @@ public interface IGenericMethodAndInterface<TInterface>
6573
[Get("/{value1}/{value2}")]
6674
Task GetGenericParameter<T1>(T1 value1, int value2);
6775

76+
// This should be renamed to GetGenericParameter once #2197 is fixed.
6877
/// <summary>Request with two generic route parameters.</summary>
6978
/// <param name="value1">First generic route parameter.</param>
7079
/// <param name="value2">Second generic route parameter.</param>
7180
/// <typeparam name="T1">Generic type of first route parameter.</typeparam>
7281
/// <returns>A task that completes when the request finishes.</returns>
7382
[Get("/{value1}/{value2}")]
74-
Task GetGenericParameter<T1>(T1 value1, TInterface value2);
83+
Task GetClassGenericParameter<T1>(T1 value1, TInterface value2);
7584
}
7685

7786
/// <summary>Verifies a simple string path parameter is formatted with the expected metadata.</summary>
@@ -348,8 +357,13 @@ public async Task QueryDictionaryParameterShouldBeExpectedReflection()
348357
[Test]
349358
public async Task OverloadedGenericParameterShouldBeExpectedReflection()
350359
{
351-
_ = _mockHandler.Fallback
352-
.Respond("application/json", nameof(IGenericMethod.GetGenericParameter));
360+
var handler = new StubHttp
361+
{
362+
{
363+
Route.Get("https://foo/Empty"),
364+
Reply.Json(nameof(IGenericMethod.GetGenericParameter))
365+
},
366+
};
353367

354368
var methodInfo = typeof(IGenericMethod).GetMethod(nameof(IGenericMethod.GetGenericParameter), 1, [Type.MakeGenericMethodParameter(0)])!;
355369
var parameterInfo = methodInfo.MakeGenericMethod(typeof(string)).GetParameters()[0];
@@ -359,12 +373,12 @@ public async Task OverloadedGenericParameterShouldBeExpectedReflection()
359373
[typeof(string)]);
360374
var settings = new RefitSettings
361375
{
362-
HttpMessageHandlerFactory = () => _mockHandler,
376+
HttpMessageHandlerFactory = () => handler,
363377
UrlParameterFormatter = formatter
364378
};
365-
var service = RestService.For<IGenericMethod>("https://foo", settings);
379+
var service = RestService.For<IGenericMethod>(BaseUrl, settings);
366380

367-
await service.GetGenericParameter("Empty");
381+
await service.GetGenericParameter(RouteValue);
368382
await formatter.AssertNoOutstandingAssertions();
369383
}
370384

@@ -373,8 +387,13 @@ public async Task OverloadedGenericParameterShouldBeExpectedReflection()
373387
[Test]
374388
public async Task OverloadedGenericParameterWithNonGenericShouldBeExpectedReflection()
375389
{
376-
_ = _mockHandler.Fallback
377-
.Respond("application/json", nameof(IGenericMethod.GetGenericParameter));
390+
var handler = new StubHttp
391+
{
392+
{
393+
Route.Get("https://foo/Empty/10"),
394+
Reply.Json(nameof(IGenericMethod.GetGenericParameter))
395+
},
396+
};
378397

379398
var methodInfo = typeof(IGenericMethod).GetMethod(nameof(IGenericMethod.GetGenericParameter), 1, [Type.MakeGenericMethodParameter(0), typeof(int)])!.MakeGenericMethod(typeof(string));
380399
var parameterInfo1 = methodInfo.GetParameters()[0];
@@ -385,12 +404,12 @@ public async Task OverloadedGenericParameterWithNonGenericShouldBeExpectedReflec
385404
[typeof(string), typeof(int)]);
386405
var settings = new RefitSettings
387406
{
388-
HttpMessageHandlerFactory = () => _mockHandler,
407+
HttpMessageHandlerFactory = () => handler,
389408
UrlParameterFormatter = formatter
390409
};
391-
var service = RestService.For<IGenericMethod>("https://foo", settings);
410+
var service = RestService.For<IGenericMethod>(BaseUrl, settings);
392411

393-
await service.GetGenericParameter("Empty", 10);
412+
await service.GetGenericParameter("Empty", IntegerValue);
394413
await formatter.AssertNoOutstandingAssertions();
395414
}
396415

@@ -399,8 +418,13 @@ public async Task OverloadedGenericParameterWithNonGenericShouldBeExpectedReflec
399418
[Test]
400419
public async Task OverloadedWithTwoGenericParameterShouldBeExpectedReflection()
401420
{
402-
_ = _mockHandler.Fallback
403-
.Respond("application/json", nameof(IGenericMethod.GetGenericParameter));
421+
var handler = new StubHttp
422+
{
423+
{
424+
Route.Get("https://foo/Empty/10"),
425+
Reply.Json(nameof(IGenericMethod.GetGenericParameter))
426+
},
427+
};
404428

405429
var methodInfo = typeof(IGenericMethod).GetMethod(nameof(IGenericMethod.GetGenericParameter), 2, [Type.MakeGenericMethodParameter(0), Type.MakeGenericMethodParameter(1)])!;
406430
methodInfo = methodInfo.MakeGenericMethod(typeof(string), typeof(long));
@@ -412,12 +436,12 @@ public async Task OverloadedWithTwoGenericParameterShouldBeExpectedReflection()
412436
[typeof(string), typeof(long)]);
413437
var settings = new RefitSettings
414438
{
415-
HttpMessageHandlerFactory = () => _mockHandler,
439+
HttpMessageHandlerFactory = () => handler,
416440
UrlParameterFormatter = formatter
417441
};
418-
var service = RestService.For<IGenericMethod>("https://foo", settings);
442+
var service = RestService.For<IGenericMethod>(BaseUrl, settings);
419443

420-
await service.GetGenericParameter("Empty", 10L);
444+
await service.GetGenericParameter(RouteValue, LongValue);
421445
await formatter.AssertNoOutstandingAssertions();
422446
}
423447

@@ -426,8 +450,13 @@ public async Task OverloadedWithTwoGenericParameterShouldBeExpectedReflection()
426450
[Test]
427451
public async Task GenericOverloadedInterfaceWithGenericParameterShouldBeExpectedReflection()
428452
{
429-
_ = _mockHandler.Fallback
430-
.Respond("application/json", nameof(IGenericMethodAndInterface<>.GetGenericParameter));
453+
var handler = new StubHttp
454+
{
455+
{
456+
Route.Get("https://foo/10"),
457+
Reply.Json(nameof(IGenericMethodAndInterface<>.GetGenericParameter))
458+
},
459+
};
431460

432461
var methodInfo = typeof(IGenericMethodAndInterface<string>).GetMethod(nameof(IGenericMethodAndInterface<>.GetGenericParameter), 1, [Type.MakeGenericMethodParameter(0)])!;
433462
methodInfo = methodInfo.MakeGenericMethod(typeof(int));
@@ -438,12 +467,12 @@ public async Task GenericOverloadedInterfaceWithGenericParameterShouldBeExpected
438467
[typeof(int)]);
439468
var settings = new RefitSettings
440469
{
441-
HttpMessageHandlerFactory = () => _mockHandler,
470+
HttpMessageHandlerFactory = () => handler,
442471
UrlParameterFormatter = formatter
443472
};
444-
var service = RestService.For<IGenericMethodAndInterface<string>>("https://foo", settings);
473+
var service = RestService.For<IGenericMethodAndInterface<string>>(BaseUrl, settings);
445474

446-
await service.GetGenericParameter(10);
475+
await service.GetGenericParameter(IntegerValue);
447476
await formatter.AssertNoOutstandingAssertions();
448477
}
449478

@@ -452,8 +481,13 @@ public async Task GenericOverloadedInterfaceWithGenericParameterShouldBeExpected
452481
[Test]
453482
public async Task GenericOverloadedInterfaceWithNonGenericShouldBeExpectedReflection()
454483
{
455-
_ = _mockHandler.Fallback
456-
.Respond("application/json", nameof(IGenericMethodAndInterface<>.GetGenericParameter));
484+
var handler = new StubHttp
485+
{
486+
{
487+
Route.Get("https://foo/10/10"),
488+
Reply.Json(nameof(IGenericMethodAndInterface<>.GetGenericParameter))
489+
},
490+
};
457491

458492
var methodInfo = typeof(IGenericMethodAndInterface<string>).GetMethod(nameof(IGenericMethodAndInterface<>.GetGenericParameter), 1, [Type.MakeGenericMethodParameter(0), typeof(int)])!;
459493
methodInfo = methodInfo.MakeGenericMethod(typeof(long));
@@ -465,12 +499,12 @@ public async Task GenericOverloadedInterfaceWithNonGenericShouldBeExpectedReflec
465499
[typeof(long), typeof(int)]);
466500
var settings = new RefitSettings
467501
{
468-
HttpMessageHandlerFactory = () => _mockHandler,
502+
HttpMessageHandlerFactory = () => handler,
469503
UrlParameterFormatter = formatter
470504
};
471-
var service = RestService.For<IGenericMethodAndInterface<string>>("https://foo", settings);
505+
var service = RestService.For<IGenericMethodAndInterface<string>>(BaseUrl, settings);
472506

473-
await service.GetGenericParameter(10L, 10);
507+
await service.GetGenericParameter(LongValue, IntegerValue);
474508
await formatter.AssertNoOutstandingAssertions();
475509
}
476510

@@ -479,11 +513,16 @@ public async Task GenericOverloadedInterfaceWithNonGenericShouldBeExpectedReflec
479513
[Test]
480514
public async Task GenericOverloadedInterfaceWithTwoGenericParameterShouldBeExpectedReflection()
481515
{
482-
_ = _mockHandler.Fallback
483-
.Respond("application/json", nameof(IGenericMethodAndInterface<>.GetGenericParameter));
516+
var handler = new StubHttp
517+
{
518+
{
519+
Route.Get("https://foo/10/Empty"),
520+
Reply.Json(nameof(IGenericMethodAndInterface<>.GetClassGenericParameter))
521+
},
522+
};
484523

485524
var methodInfo = typeof(IGenericMethodAndInterface<string>).GetMethod(
486-
nameof(IGenericMethodAndInterface<>.GetGenericParameter),
525+
nameof(IGenericMethodAndInterface<>.GetClassGenericParameter),
487526
1,
488527
[Type.MakeGenericMethodParameter(0), typeof(string)])!;
489528
methodInfo = methodInfo.MakeGenericMethod(typeof(long));
@@ -495,15 +534,12 @@ public async Task GenericOverloadedInterfaceWithTwoGenericParameterShouldBeExpec
495534
[typeof(long), typeof(string)]);
496535
var settings = new RefitSettings
497536
{
498-
HttpMessageHandlerFactory = () => _mockHandler,
537+
HttpMessageHandlerFactory = () => handler,
499538
UrlParameterFormatter = formatter
500539
};
501-
var service = RestService.For<IGenericMethodAndInterface<string>>("https://foo", settings);
540+
var service = RestService.For<IGenericMethodAndInterface<string>>(BaseUrl, settings);
502541

503-
await service.GetGenericParameter(10L, "Empty");
542+
await service.GetClassGenericParameter(LongValue, "Empty");
504543
await formatter.AssertNoOutstandingAssertions();
505544
}
506-
507-
/// <summary>Disposes the mock HTTP handler owned by the test.</summary>
508-
public void Dispose() => _mockHandler?.Dispose();
509545
}

0 commit comments

Comments
 (0)