|
| 1 | +// Copyright (c) 2019-2026 ReactiveUI and Contributors. All rights reserved. |
| 2 | +// ReactiveUI and Contributors licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for full license information. |
| 4 | +using Refit; |
| 5 | +using Refit.NetFrameworkSmoke; |
| 6 | + |
| 7 | +const int ExpectedTodoId = 42; |
| 8 | + |
| 9 | +const int SearchPage = 3; |
| 10 | + |
| 11 | +const string MissingClientFragment = "doesn't look like a Refit interface"; |
| 12 | + |
| 13 | +var handler = new NetFrameworkSmokeHandler(); |
| 14 | + |
| 15 | +using var client = new HttpClient(handler) { BaseAddress = new("https://netfx.refit.test") }; |
| 16 | + |
| 17 | +// .NET Framework has no [ModuleInitializer], so the generator emits no factory registrations here and the registries |
| 18 | +// stay empty. ForGenerated has to resolve the generated implementation by name for any of this to work at all. |
| 19 | +var api = RestService.ForGenerated<INetFrameworkSmokeApi>(client); |
| 20 | + |
| 21 | +var fetched = await api.GetTodoAsync(ExpectedTodoId).ConfigureAwait(false); |
| 22 | + |
| 23 | +if (fetched.Id != ExpectedTodoId || fetched.Title != "fetched on net framework") |
| 24 | +{ |
| 25 | + throw new InvalidOperationException("The .NET Framework GET response was not deserialized correctly."); |
| 26 | +} |
| 27 | + |
| 28 | +var created = await api.CreateTodoAsync(new(ExpectedTodoId, "prove net framework")).ConfigureAwait(false); |
| 29 | + |
| 30 | +if (created.Id != ExpectedTodoId || created.Title != "prove net framework") |
| 31 | +{ |
| 32 | + throw new InvalidOperationException("The .NET Framework POST response was not deserialized correctly."); |
| 33 | +} |
| 34 | + |
| 35 | +if (!handler.SawPostBody) |
| 36 | +{ |
| 37 | + throw new InvalidOperationException("The .NET Framework request body was not serialized through Refit."); |
| 38 | +} |
| 39 | + |
| 40 | +var searched = await api.SearchAsync("a b", SearchPage).ConfigureAwait(false); |
| 41 | + |
| 42 | +if (searched != "found" || !handler.SawExpectedQuery) |
| 43 | +{ |
| 44 | + throw new InvalidOperationException("The .NET Framework generated query string was not constructed correctly."); |
| 45 | +} |
| 46 | + |
| 47 | +// Resolving the same interface twice must reuse the cached generated type rather than resolving it again. |
| 48 | +if (RestService.ForGenerated<INetFrameworkSmokeApi>(client) is null) |
| 49 | +{ |
| 50 | + throw new InvalidOperationException("The .NET Framework repeat lookup did not return a generated client."); |
| 51 | +} |
| 52 | + |
| 53 | +// An interface the generator never saw must still report that clearly instead of failing some other way. |
| 54 | +string? missingClientMessage = null; |
| 55 | + |
| 56 | +try |
| 57 | +{ |
| 58 | + _ = RestService.ForGenerated<INoGeneratedClientApi>(client); |
| 59 | +} |
| 60 | +catch (InvalidOperationException ex) |
| 61 | +{ |
| 62 | + missingClientMessage = ex.Message; |
| 63 | +} |
| 64 | + |
| 65 | +if (missingClientMessage?.IndexOf(MissingClientFragment, StringComparison.Ordinal) < 0 |
| 66 | + || missingClientMessage is null) |
| 67 | +{ |
| 68 | + throw new InvalidOperationException("The .NET Framework lookup for a missing generated client did not report it."); |
| 69 | +} |
| 70 | + |
| 71 | +Console.WriteLine("Refit .NET Framework smoke test passed."); |
| 72 | + |
| 73 | +/// <summary>The generated top-level program's declaring type, sealed so the JIT can devirtualize its members.</summary> |
| 74 | +internal sealed partial class Program |
| 75 | +{ |
| 76 | + /// <summary>Initializes a new instance of the <see cref="Program"/> class. Unused; the entry point is the generated top-level <c>Main</c>.</summary> |
| 77 | + private Program() |
| 78 | + { |
| 79 | + } |
| 80 | +} |
0 commit comments