diff --git a/ProxyInterfaceSourceGenerator Solution.sln b/ProxyInterfaceSourceGenerator Solution.sln index 684e76f..5f7017b 100644 --- a/ProxyInterfaceSourceGenerator Solution.sln +++ b/ProxyInterfaceSourceGenerator Solution.sln @@ -32,6 +32,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProxyInterfaceConsumerForAk EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProxyInterfaceConsumerViaNuGet", "src-examples\ProxyInterfaceConsumerViaNuGet\ProxyInterfaceConsumerViaNuGet.csproj", "{1EA000E4-6103-4577-8D98-BDDA3BE458A2}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IHttpClient", "src\IHttpClient\IHttpClient.csproj", "{38C2BB6E-EE23-4C4F-B8D5-A2AD592DE5E3}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleAppIHttpClient", "src-examples\ConsoleAppIHttpClient\ConsoleAppIHttpClient.csproj", "{3E93C092-0E42-4200-B71A-5EEE410FE1F5}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -74,6 +78,18 @@ Global {1EA000E4-6103-4577-8D98-BDDA3BE458A2}.DebugAttach|Any CPU.Build.0 = Debug|Any CPU {1EA000E4-6103-4577-8D98-BDDA3BE458A2}.Release|Any CPU.ActiveCfg = Release|Any CPU {1EA000E4-6103-4577-8D98-BDDA3BE458A2}.Release|Any CPU.Build.0 = Release|Any CPU + {38C2BB6E-EE23-4C4F-B8D5-A2AD592DE5E3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {38C2BB6E-EE23-4C4F-B8D5-A2AD592DE5E3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {38C2BB6E-EE23-4C4F-B8D5-A2AD592DE5E3}.DebugAttach|Any CPU.ActiveCfg = Debug|Any CPU + {38C2BB6E-EE23-4C4F-B8D5-A2AD592DE5E3}.DebugAttach|Any CPU.Build.0 = Debug|Any CPU + {38C2BB6E-EE23-4C4F-B8D5-A2AD592DE5E3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {38C2BB6E-EE23-4C4F-B8D5-A2AD592DE5E3}.Release|Any CPU.Build.0 = Release|Any CPU + {3E93C092-0E42-4200-B71A-5EEE410FE1F5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3E93C092-0E42-4200-B71A-5EEE410FE1F5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3E93C092-0E42-4200-B71A-5EEE410FE1F5}.DebugAttach|Any CPU.ActiveCfg = Debug|Any CPU + {3E93C092-0E42-4200-B71A-5EEE410FE1F5}.DebugAttach|Any CPU.Build.0 = Debug|Any CPU + {3E93C092-0E42-4200-B71A-5EEE410FE1F5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3E93C092-0E42-4200-B71A-5EEE410FE1F5}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -85,6 +101,8 @@ Global {5F7DA2C5-B908-4B57-9F5F-BADF1216D89C} = {38BA087F-EDA1-4F8A-A140-85B84791B815} {590908DF-A813-467A-94E4-3500020D0D54} = {38BA087F-EDA1-4F8A-A140-85B84791B815} {1EA000E4-6103-4577-8D98-BDDA3BE458A2} = {38BA087F-EDA1-4F8A-A140-85B84791B815} + {38C2BB6E-EE23-4C4F-B8D5-A2AD592DE5E3} = {ED3DA9DD-1E07-444B-A2D7-2DBA280F96D4} + {3E93C092-0E42-4200-B71A-5EEE410FE1F5} = {38BA087F-EDA1-4F8A-A140-85B84791B815} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {585F071D-051D-441C-9C6B-226D9E15A1F5} diff --git a/resources/ihttpclient-icon.png b/resources/ihttpclient-icon.png new file mode 100644 index 0000000..291cfe1 Binary files /dev/null and b/resources/ihttpclient-icon.png differ diff --git a/src-examples/ConsoleAppIHttpClient/ConsoleAppIHttpClient.csproj b/src-examples/ConsoleAppIHttpClient/ConsoleAppIHttpClient.csproj new file mode 100644 index 0000000..9f082cc --- /dev/null +++ b/src-examples/ConsoleAppIHttpClient/ConsoleAppIHttpClient.csproj @@ -0,0 +1,13 @@ + + + + Exe + net8.0 + enable + + + + + + + diff --git a/src-examples/ConsoleAppIHttpClient/Program.cs b/src-examples/ConsoleAppIHttpClient/Program.cs new file mode 100644 index 0000000..ead450e --- /dev/null +++ b/src-examples/ConsoleAppIHttpClient/Program.cs @@ -0,0 +1,13 @@ +using System.Net.Http; +using System.Net.Http.Json; + +var httpClient = new HttpClient(); +var httpClientProxy = new HttpClientProxy(httpClient); + +var result = await httpClientProxy.GetAsync("https://www.google.nl"); +var todo = await httpClientProxy.GetFromJsonAsync("https://jsonplaceholder.typicode.com/todos/1"); +var postResult = await httpClientProxy.PostAsJsonAsync("https://jsonplaceholder.typicode.com/todos", new Todo { Id = 123 }); +var patchResult = await httpClientProxy.PatchAsJsonAsync("https://jsonplaceholder.typicode.com/todos", new Todo { Id = 400 }); +var putResult = await httpClientProxy.PutAsJsonAsync("https://jsonplaceholder.typicode.com/todos", new Todo { Id = 444 }); + +int x = 0; \ No newline at end of file diff --git a/src-examples/ConsoleAppIHttpClient/Todo.cs b/src-examples/ConsoleAppIHttpClient/Todo.cs new file mode 100644 index 0000000..158af1b --- /dev/null +++ b/src-examples/ConsoleAppIHttpClient/Todo.cs @@ -0,0 +1,4 @@ +public class Todo +{ + public int Id { get; set; } +} \ No newline at end of file diff --git a/src-examples/ProxyInterfaceConsumer/Address.cs b/src-examples/ProxyInterfaceConsumer/Address.cs index 055c152..c9e80b8 100644 --- a/src-examples/ProxyInterfaceConsumer/Address.cs +++ b/src-examples/ProxyInterfaceConsumer/Address.cs @@ -1,6 +1,6 @@ using System; -namespace DifferentNamespace +namespace ProxyInterfaceConsumer { public class Address { diff --git a/src-examples/ProxyInterfaceConsumer/Http/IHttpClient.cs b/src-examples/ProxyInterfaceConsumer/Http/IHttpClient.cs index d9bae73..cbd0a8a 100644 --- a/src-examples/ProxyInterfaceConsumer/Http/IHttpClient.cs +++ b/src-examples/ProxyInterfaceConsumer/Http/IHttpClient.cs @@ -1,14 +1,13 @@ -namespace ProxyInterfaceConsumer.Http +using System.Net.Http; + +namespace ProxyInterfaceConsumer.Http; + +[ProxyInterfaceGenerator.Proxy(typeof(HttpClient), true)] +public partial interface IHttpClient : IHttpMessageInvoker { - [ProxyInterfaceGenerator.Proxy(typeof(System.Net.Http.HttpClient), true)] - public partial interface IHttpClient : IHttpMessageInvoker - { - - } +} - [ProxyInterfaceGenerator.Proxy(typeof(System.Net.Http.HttpMessageInvoker))] - public partial interface IHttpMessageInvoker - { - - } +[ProxyInterfaceGenerator.Proxy(typeof(HttpMessageInvoker))] +public partial interface IHttpMessageInvoker +{ } \ No newline at end of file diff --git a/src-examples/ProxyInterfaceConsumer/Http/IHttpClientExtensions.cs b/src-examples/ProxyInterfaceConsumer/Http/IHttpClientExtensions.cs new file mode 100644 index 0000000..0ad8080 --- /dev/null +++ b/src-examples/ProxyInterfaceConsumer/Http/IHttpClientExtensions.cs @@ -0,0 +1,108 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.Net.Http; +using System.Net.Http.Json; +using System.Text.Json; +using System.Text.Json.Serialization; +using System.Text.Json.Serialization.Metadata; +using System.Threading; +using System.Threading.Tasks; + +namespace ProxyInterfaceConsumer.Http; + +public static class IHttpClientExtensions +{ + #region PostAsJsonAsync + public static Task PostAsJsonAsync(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, TValue value, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default) + { + return client._Instance.PostAsJsonAsync(requestUri, value, options, cancellationToken); + } + + public static Task PostAsJsonAsync(this IHttpClient client, Uri? requestUri, TValue value, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default) + { + return client._Instance.PostAsJsonAsync(requestUri, value, options, cancellationToken); + } + + public static Task PostAsJsonAsync(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, TValue value, CancellationToken cancellationToken) + { + return client._Instance.PostAsJsonAsync(requestUri, value, options: null, cancellationToken); + } + + public static Task PostAsJsonAsync(this IHttpClient client, Uri? requestUri, TValue value, CancellationToken cancellationToken) + { + return client._Instance.PostAsJsonAsync(requestUri, value, options: null, cancellationToken); + } + + public static Task PostAsJsonAsync(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, TValue value, JsonTypeInfo jsonTypeInfo, CancellationToken cancellationToken = default) + { + return client._Instance.PostAsJsonAsync(requestUri, value, jsonTypeInfo, cancellationToken); + } + + public static Task PostAsJsonAsync(this IHttpClient client, Uri? requestUri, TValue value, JsonTypeInfo jsonTypeInfo, CancellationToken cancellationToken = default) + { + return client._Instance.PostAsJsonAsync(requestUri, value, jsonTypeInfo, cancellationToken); + } + #endregion + + #region GetFromJsonAsync + public static Task GetFromJsonAsync(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, Type type, JsonSerializerOptions? options, CancellationToken cancellationToken = default) + { + return client._Instance.GetFromJsonAsync(requestUri, type, options, cancellationToken); + } + + public static Task GetFromJsonAsync(this IHttpClient client, Uri? requestUri, Type type, JsonSerializerOptions? options, CancellationToken cancellationToken = default) + { + return client._Instance.GetFromJsonAsync(requestUri, type, options, cancellationToken); + } + + public static Task GetFromJsonAsync(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, JsonSerializerOptions? options, CancellationToken cancellationToken = default) + { + return client._Instance.GetFromJsonAsync(requestUri, options, cancellationToken); + } + + public static Task GetFromJsonAsync(this IHttpClient client, Uri? requestUri, JsonSerializerOptions? options, CancellationToken cancellationToken = default) + { + return client._Instance.GetFromJsonAsync(requestUri, options, cancellationToken); + } + + public static Task GetFromJsonAsync(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, Type type, JsonSerializerContext context, CancellationToken cancellationToken = default) + { + return client._Instance.GetFromJsonAsync(requestUri, type, context, cancellationToken); + } + + public static Task GetFromJsonAsync(this IHttpClient client, Uri? requestUri, Type type, JsonSerializerContext context, CancellationToken cancellationToken = default) + { + return client._Instance.GetFromJsonAsync(requestUri, type, context, cancellationToken); + } + + public static Task GetFromJsonAsync(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, JsonTypeInfo jsonTypeInfo, CancellationToken cancellationToken = default) + { + return client._Instance.GetFromJsonAsync(requestUri, jsonTypeInfo, cancellationToken); + } + + public static Task GetFromJsonAsync(this IHttpClient client, Uri? requestUri, JsonTypeInfo jsonTypeInfo, CancellationToken cancellationToken = default) + { + return client._Instance.GetFromJsonAsync(requestUri, jsonTypeInfo, cancellationToken); + } + + public static Task GetFromJsonAsync(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, Type type, CancellationToken cancellationToken = default) + { + return client._Instance.GetFromJsonAsync(requestUri, type, options: null, cancellationToken); + } + + public static Task GetFromJsonAsync(this IHttpClient client, Uri? requestUri, Type type, CancellationToken cancellationToken = default) + { + return client._Instance.GetFromJsonAsync(requestUri, type, options: null, cancellationToken); + } + + public static Task GetFromJsonAsync(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, CancellationToken cancellationToken = default) + { + return client._Instance.GetFromJsonAsync(requestUri, cancellationToken); + } + + public static Task GetFromJsonAsync(this IHttpClient client, Uri? requestUri, CancellationToken cancellationToken = default) + { + return client._Instance.GetFromJsonAsync(requestUri, cancellationToken); + } + #endregion +} \ No newline at end of file diff --git a/src-examples/ProxyInterfaceConsumer/IAddress.cs b/src-examples/ProxyInterfaceConsumer/IAddress.cs index 1366e7f..f7e3242 100644 --- a/src-examples/ProxyInterfaceConsumer/IAddress.cs +++ b/src-examples/ProxyInterfaceConsumer/IAddress.cs @@ -1,5 +1,3 @@ -using DifferentNamespace; - namespace ProxyInterfaceConsumer { [ProxyInterfaceGenerator.Proxy(typeof(Address))] diff --git a/src-examples/ProxyInterfaceConsumer/Person.cs b/src-examples/ProxyInterfaceConsumer/Person.cs index d0e9432..209f687 100644 --- a/src-examples/ProxyInterfaceConsumer/Person.cs +++ b/src-examples/ProxyInterfaceConsumer/Person.cs @@ -1,6 +1,5 @@ using System.Collections.Generic; using System.Threading.Tasks; -using DifferentNamespace; namespace ProxyInterfaceConsumer { @@ -71,12 +70,12 @@ namespace ProxyInterfaceConsumer c++; } - public int In_Out_Ref2(in Address a, out Address b, ref Address c) - { - b = new Address(); - c.HouseNumber = 11; - return 404; - } + //public int In_Out_Ref2(in Address a, out Address b, ref Address c) + //{ + // b = new Address(); + // c.HouseNumber = 11; + // return 404; + //} public void Void() { diff --git a/src-examples/ProxyInterfaceConsumer/Program.cs b/src-examples/ProxyInterfaceConsumer/Program.cs index 3d432d3..35e7a76 100644 --- a/src-examples/ProxyInterfaceConsumer/Program.cs +++ b/src-examples/ProxyInterfaceConsumer/Program.cs @@ -1,86 +1,101 @@ using System; using System.Collections.Generic; +using System.Net.Http; +using System.Net.Http.Json; using System.Text.Json; -using DifferentNamespace; +using System.Threading.Tasks; +using ProxyInterfaceConsumer.Http; -namespace ProxyInterfaceConsumer +namespace ProxyInterfaceConsumer; + +public class Program { - public class Program + private static JsonSerializerOptions JsonSerializerOptions = new () { - private static JsonSerializerOptions JsonSerializerOptions = new () + WriteIndented = true + }; + + public static async Task Main() + { + var h = new HttpClient(); + var ph = new HttpClientProxy(h); + + var result = await ph.GetAsync("https://www.google.nl"); + var todo = await ph.GetFromJsonAsync("https://jsonplaceholder.typicode.com/todos/1"); + + var postResult = await h.PostAsJsonAsync("https://jsonplaceholder.typicode.com/todos", new Todo { Id = 123 }); + + var t = new TestProxy(new Test()); + + IPersonT pT = new PersonTProxy(new PersonT()); + pT.TVal = 1; + Console.WriteLine(JsonSerializer.Serialize(pT, JsonSerializerOptions)); + Console.WriteLine(new string('-', 80)); + + //IPersonTT pTT = new PersonTTProxy(new PersonTT()); + //pTT.TVal1 = 42; + //pTT.TVal2 = new Program(); + //Console.WriteLine(JsonSerializer.Serialize(pTT, JsonSerializerOptions)); + //Console.WriteLine(new string('-', 80)); + + var ap = new AddressProxy(new Address { HouseNumber = 42 }); + ap.HouseNumber = -1; + ap.MyEvent += delegate (object x, EventArgs a) { - WriteIndented = true }; - public static void Main() - { - var t = new TestProxy(new Test()); + IPerson p = new PersonProxy(new Person()); + p.Name = "test"; + p.HelloWorld("stef"); + // p.Address = ap; - IPersonT pT = new PersonTProxy(new PersonT()); - pT.TVal = 1; - Console.WriteLine(JsonSerializer.Serialize(pT, JsonSerializerOptions)); - Console.WriteLine(new string('-', 80)); + Console.WriteLine("DefaultValue " + p.DefaultValue()); + Console.WriteLine("DefaultValue " + p.DefaultValue(42)); - //IPersonTT pTT = new PersonTTProxy(new PersonTT()); - //pTT.TVal1 = 42; - //pTT.TVal2 = new Program(); - //Console.WriteLine(JsonSerializer.Serialize(pTT, JsonSerializerOptions)); - //Console.WriteLine(new string('-', 80)); - - var ap = new AddressProxy(new Address { HouseNumber = 42 }); - ap.HouseNumber = -1; - ap.MyEvent += delegate (object x, EventArgs a) - { - }; - - IPerson p = new PersonProxy(new Person()); - p.Name = "test"; - p.HelloWorld("stef"); - // p.Address = ap; - - Console.WriteLine("DefaultValue " + p.DefaultValue()); - Console.WriteLine("DefaultValue " + p.DefaultValue(42)); - - Console.WriteLine(JsonSerializer.Serialize(p, JsonSerializerOptions)); - } + Console.WriteLine(JsonSerializer.Serialize(p, JsonSerializerOptions)); } +} - public class Test +public class Todo +{ + public int Id { get; set; } +} + +public class Test +{ + public int Id { get; set; } + + public Clazz C { get; } + + public IList Cs { get; set; } + + public int AddString(string s) { - public int Id { get; set; } - - public Clazz C { get; } - - public IList Cs { get; set; } - - public int AddString(string s) - { - return 600; - } - - public Test AddTest(Test t) - { - return new Test(); - } - - public Clazz AddClazz(Clazz c) - { - return new Clazz(); - } + return 600; } - public sealed class Clazz + public Test AddTest(Test t) { - public string Name { get; set; } + return new Test(); } - [ProxyInterfaceGenerator.Proxy(typeof(Test))] - public partial interface ITest + public Clazz AddClazz(Clazz c) { + return new Clazz(); } +} - [ProxyInterfaceGenerator.Proxy(typeof(Clazz))] - public partial interface IClazz - { - } +public sealed class Clazz +{ + public string Name { get; set; } +} + +[ProxyInterfaceGenerator.Proxy(typeof(Test))] +public partial interface ITest +{ +} + +[ProxyInterfaceGenerator.Proxy(typeof(Clazz))] +public partial interface IClazz +{ } \ No newline at end of file diff --git a/src-examples/ProxyInterfaceConsumer/ProxyInterfaceConsumer - Backup.csproj b/src-examples/ProxyInterfaceConsumer/ProxyInterfaceConsumer - Backup.csproj deleted file mode 100644 index 6e1a196..0000000 --- a/src-examples/ProxyInterfaceConsumer/ProxyInterfaceConsumer - Backup.csproj +++ /dev/null @@ -1,33 +0,0 @@ - - - - net7.0 - Exe - enable - - - - - - - - - - - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/IHttpClient/IHttpClient.cs b/src/IHttpClient/IHttpClient.cs new file mode 100644 index 0000000..f7cc9fd --- /dev/null +++ b/src/IHttpClient/IHttpClient.cs @@ -0,0 +1,7 @@ +// ReSharper disable once CheckNamespace +namespace System.Net.Http; + +[ProxyInterfaceGenerator.Proxy(typeof(HttpClient), true)] +public partial interface IHttpClient : IHttpMessageInvoker +{ +} \ No newline at end of file diff --git a/src/IHttpClient/IHttpClient.csproj b/src/IHttpClient/IHttpClient.csproj new file mode 100644 index 0000000..ae3686c --- /dev/null +++ b/src/IHttpClient/IHttpClient.csproj @@ -0,0 +1,54 @@ + + + + 0.0.1-preview-01 + netstandard2.1;net6.0;net7.0;net8.0 + enable + latest + {38C2BB6E-EE23-4C4F-B8D5-A2AD592DE5E3} + Stef Heyenrath + + IHttpClient + IHttpClient + This project uses source generation to generate an IHttpClient interface and HttpClientProxy from the HttpClient to make it injectable and unit-testable. + HttpClient;interface;IHttpClient;Proxy;HttpClientProxy + MIT + + https://github.com/StefH/ProxyInterfaceGenerator/src/IHttpClient + git + https://github.com/StefH/ProxyInterfaceGenerator/src/IHttpClient + PackageReadme.md + ihttpclient-icon.png + + + + + true + + + + + + + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + diff --git a/src/IHttpClient/IHttpClientJsonExtensions.cs b/src/IHttpClient/IHttpClientJsonExtensions.cs new file mode 100644 index 0000000..3a55272 --- /dev/null +++ b/src/IHttpClient/IHttpClientJsonExtensions.cs @@ -0,0 +1,173 @@ +using System.Diagnostics.CodeAnalysis; +using System.Text.Json; +using System.Text.Json.Serialization; +using System.Text.Json.Serialization.Metadata; +using System.Threading; +using System.Threading.Tasks; + +// ReSharper disable once CheckNamespace +namespace System.Net.Http.Json; + +/// +public static class IHttpClientJsonExtensions +{ + #region PostAsJsonAsync + public static Task PostAsJsonAsync(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, TValue value, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default) + { + return client._Instance.PostAsJsonAsync(requestUri, value, options, cancellationToken); + } + + public static Task PostAsJsonAsync(this IHttpClient client, Uri? requestUri, TValue value, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default) + { + return client._Instance.PostAsJsonAsync(requestUri, value, options, cancellationToken); + } + + public static Task PostAsJsonAsync(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, TValue value, CancellationToken cancellationToken) + { + return client._Instance.PostAsJsonAsync(requestUri, value, options: null, cancellationToken); + } + + public static Task PostAsJsonAsync(this IHttpClient client, Uri? requestUri, TValue value, CancellationToken cancellationToken) + { + return client._Instance.PostAsJsonAsync(requestUri, value, options: null, cancellationToken); + } + + public static Task PostAsJsonAsync(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, TValue value, JsonTypeInfo jsonTypeInfo, CancellationToken cancellationToken = default) + { + return client._Instance.PostAsJsonAsync(requestUri, value, jsonTypeInfo, cancellationToken); + } + + public static Task PostAsJsonAsync(this IHttpClient client, Uri? requestUri, TValue value, JsonTypeInfo jsonTypeInfo, CancellationToken cancellationToken = default) + { + return client._Instance.PostAsJsonAsync(requestUri, value, jsonTypeInfo, cancellationToken); + } + #endregion + +#if NET7_0_OR_GREATER + #region PatchAsJsonAsync + public static Task PatchAsJsonAsync(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, TValue value, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default) + { + return client._Instance.PatchAsJsonAsync(requestUri, value, options, cancellationToken); + } + + public static Task PatchAsJsonAsync(this IHttpClient client, Uri? requestUri, TValue value, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default) + { + return client._Instance.PatchAsJsonAsync(requestUri, value, options, cancellationToken); + } + + public static Task PatchAsJsonAsync(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, TValue value, CancellationToken cancellationToken) + { + return client._Instance.PatchAsJsonAsync(requestUri, value, cancellationToken); + } + + public static Task PatchAsJsonAsync(this IHttpClient client, Uri? requestUri, TValue value, CancellationToken cancellationToken) + { + return client._Instance.PatchAsJsonAsync(requestUri, value, cancellationToken); + } + + public static Task PatchAsJsonAsync(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, TValue value, JsonTypeInfo jsonTypeInfo, CancellationToken cancellationToken = default) + { + return client._Instance.PatchAsJsonAsync(requestUri, value, jsonTypeInfo, cancellationToken); + } + + public static Task PatchAsJsonAsync(this IHttpClient client, Uri? requestUri, TValue value, JsonTypeInfo jsonTypeInfo, CancellationToken cancellationToken = default) + { + return client._Instance.PatchAsJsonAsync(requestUri, value, jsonTypeInfo, cancellationToken); + } +#endregion +#endif + + #region PutAsJsonAsync + public static Task PutAsJsonAsync(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, TValue value, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default) + { + return client._Instance.PutAsJsonAsync(requestUri, value, options, cancellationToken); + } + + public static Task PutAsJsonAsync(this IHttpClient client, Uri? requestUri, TValue value, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default) + { + return client._Instance.PutAsJsonAsync(requestUri, value, options, cancellationToken); + } + + public static Task PutAsJsonAsync(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, TValue value, CancellationToken cancellationToken) + { + return client._Instance.PutAsJsonAsync(requestUri, value, cancellationToken); + } + + public static Task PutAsJsonAsync(this IHttpClient client, Uri? requestUri, TValue value, CancellationToken cancellationToken) + { + return client._Instance.PutAsJsonAsync(requestUri, value, cancellationToken); + } + + public static Task PutAsJsonAsync(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, TValue value, JsonTypeInfo jsonTypeInfo, CancellationToken cancellationToken = default) + { + return client._Instance.PutAsJsonAsync(requestUri, value, jsonTypeInfo, cancellationToken); + } + + public static Task PutAsJsonAsync(this IHttpClient client, Uri? requestUri, TValue value, JsonTypeInfo jsonTypeInfo, CancellationToken cancellationToken = default) + { + return client._Instance.PutAsJsonAsync(requestUri, value, jsonTypeInfo, cancellationToken); + } + #endregion + + #region GetFromJsonAsync + public static Task GetFromJsonAsync(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, Type type, JsonSerializerOptions? options, CancellationToken cancellationToken = default) + { + return client._Instance.GetFromJsonAsync(requestUri, type, options, cancellationToken); + } + + public static Task GetFromJsonAsync(this IHttpClient client, Uri? requestUri, Type type, JsonSerializerOptions? options, CancellationToken cancellationToken = default) + { + return client._Instance.GetFromJsonAsync(requestUri, type, options, cancellationToken); + } + + public static Task GetFromJsonAsync(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, JsonSerializerOptions? options, CancellationToken cancellationToken = default) + { + return client._Instance.GetFromJsonAsync(requestUri, options, cancellationToken); + } + + public static Task GetFromJsonAsync(this IHttpClient client, Uri? requestUri, JsonSerializerOptions? options, CancellationToken cancellationToken = default) + { + return client._Instance.GetFromJsonAsync(requestUri, options, cancellationToken); + } + + public static Task GetFromJsonAsync(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, Type type, JsonSerializerContext context, CancellationToken cancellationToken = default) + { + return client._Instance.GetFromJsonAsync(requestUri, type, context, cancellationToken); + } + + public static Task GetFromJsonAsync(this IHttpClient client, Uri? requestUri, Type type, JsonSerializerContext context, CancellationToken cancellationToken = default) + { + return client._Instance.GetFromJsonAsync(requestUri, type, context, cancellationToken); + } + + public static Task GetFromJsonAsync(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, JsonTypeInfo jsonTypeInfo, CancellationToken cancellationToken = default) + { + return client._Instance.GetFromJsonAsync(requestUri, jsonTypeInfo, cancellationToken); + } + + public static Task GetFromJsonAsync(this IHttpClient client, Uri? requestUri, JsonTypeInfo jsonTypeInfo, CancellationToken cancellationToken = default) + { + return client._Instance.GetFromJsonAsync(requestUri, jsonTypeInfo, cancellationToken); + } + + public static Task GetFromJsonAsync(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, Type type, CancellationToken cancellationToken = default) + { + return client._Instance.GetFromJsonAsync(requestUri, type, options: null, cancellationToken); + } + + public static Task GetFromJsonAsync(this IHttpClient client, Uri? requestUri, Type type, CancellationToken cancellationToken = default) + { + return client._Instance.GetFromJsonAsync(requestUri, type, options: null, cancellationToken); + } + + public static Task GetFromJsonAsync(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, CancellationToken cancellationToken = default) + { + return client._Instance.GetFromJsonAsync(requestUri, cancellationToken); + } + + public static Task GetFromJsonAsync(this IHttpClient client, Uri? requestUri, CancellationToken cancellationToken = default) + { + return client._Instance.GetFromJsonAsync(requestUri, cancellationToken); + } + #endregion +} \ No newline at end of file diff --git a/src/IHttpClient/IHttpMessageInvoker.cs b/src/IHttpClient/IHttpMessageInvoker.cs new file mode 100644 index 0000000..67d7c10 --- /dev/null +++ b/src/IHttpClient/IHttpMessageInvoker.cs @@ -0,0 +1,7 @@ +// ReSharper disable once CheckNamespace +namespace System.Net.Http; + +[ProxyInterfaceGenerator.Proxy(typeof(HttpMessageInvoker))] +public partial interface IHttpMessageInvoker +{ +} \ No newline at end of file diff --git a/src/IHttpClient/PackageReadme.md b/src/IHttpClient/PackageReadme.md new file mode 100644 index 0000000..bd873d4 --- /dev/null +++ b/src/IHttpClient/PackageReadme.md @@ -0,0 +1,17 @@ +## Info + +This project uses source generation to generate an `IHttpClient` interface and `HttpClientProxy` from the `HttpClient` to make it injectable and unit-testable. + +All the methods and properties from the `HttpClient` are replicated to `IHttpClient`. + +## Use it +``` c# +HttpClient httpClient = new HttpClient(); +IHttpClient httpClientProxy = new HttpClientProxy(httpClient); + +var result = await httpClientProxy.GetAsync("https://www.google.nl"); +var todo = await httpClientProxy.GetFromJsonAsync("https://jsonplaceholder.typicode.com/todos/1"); +var postResult = await httpClientProxy.PostAsJsonAsync("https://jsonplaceholder.typicode.com/todos", new Todo { Id = 123 }); +var patchResult = await httpClientProxy.PatchAsJsonAsync("https://jsonplaceholder.typicode.com/todos", new Todo { Id = 400 }); +var putResult = await httpClientProxy.PutAsJsonAsync("https://jsonplaceholder.typicode.com/todos", new Todo { Id = 444 }); +``` \ No newline at end of file diff --git a/src/ProxyInterfaceSourceGenerator/Constants/InternalClassNames.cs b/src/ProxyInterfaceSourceGenerator/Constants/InternalClassNames.cs deleted file mode 100644 index cdc9a9a..0000000 --- a/src/ProxyInterfaceSourceGenerator/Constants/InternalClassNames.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace ProxyInterfaceSourceGenerator.Constants; - -internal static class InternalClassNames -{ - public const string NullableAttribute = "System.Runtime.CompilerServices.NullableAttribute"; - - public const string AsyncStateMachineAttribute = "System.Runtime.CompilerServices.AsyncStateMachineAttribute"; -} \ No newline at end of file diff --git a/src/ProxyInterfaceSourceGenerator/Extensions/SymbolExtensions.cs b/src/ProxyInterfaceSourceGenerator/Extensions/SymbolExtensions.cs index b76d44e..5d015c5 100644 --- a/src/ProxyInterfaceSourceGenerator/Extensions/SymbolExtensions.cs +++ b/src/ProxyInterfaceSourceGenerator/Extensions/SymbolExtensions.cs @@ -1,6 +1,5 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; -using ProxyInterfaceSourceGenerator.Constants; namespace ProxyInterfaceSourceGenerator.Extensions; @@ -8,8 +7,9 @@ internal static class SymbolExtensions { private static readonly string[] ExcludedAttributes = { - InternalClassNames.AsyncStateMachineAttribute , - InternalClassNames.NullableAttribute + "System.Runtime.CompilerServices.NullableAttribute", + "System.Runtime.CompilerServices.NullableContextAttribute", + "System.Runtime.CompilerServices.AsyncStateMachineAttribute" }; public static string GetAttributesPrefix(this ISymbol symbol) diff --git a/src/ProxyInterfaceSourceGenerator/FileGenerators/ProxyClassesGenerator.cs b/src/ProxyInterfaceSourceGenerator/FileGenerators/ProxyClassesGenerator.cs index df7f5f1..991c013 100644 --- a/src/ProxyInterfaceSourceGenerator/FileGenerators/ProxyClassesGenerator.cs +++ b/src/ProxyInterfaceSourceGenerator/FileGenerators/ProxyClassesGenerator.cs @@ -198,7 +198,10 @@ using System; private string GeneratePublicMethods(ClassSymbol targetClassSymbol, bool proxyBaseClasses) { var str = new StringBuilder(); - foreach (var method in MemberHelper.GetPublicMethods(targetClassSymbol, proxyBaseClasses)) + + var methods = MemberHelper.GetPublicMethods(targetClassSymbol, proxyBaseClasses); + + foreach (var method in methods) { var methodParameters = new List(); var invokeParameters = new List(); diff --git a/src/ProxyInterfaceSourceGenerator/ProxyInterfaceSourceGenerator.csproj b/src/ProxyInterfaceSourceGenerator/ProxyInterfaceSourceGenerator.csproj index e7b557b..b1420b0 100644 --- a/src/ProxyInterfaceSourceGenerator/ProxyInterfaceSourceGenerator.csproj +++ b/src/ProxyInterfaceSourceGenerator/ProxyInterfaceSourceGenerator.csproj @@ -1,7 +1,7 @@ - 0.0.36 + 0.0.37 netstandard2.0 {12344228-91F4-4502-9595-39584E5ABB34} 10 diff --git a/src/ProxyInterfaceSourceGenerator/Utils/MemberHelper.cs b/src/ProxyInterfaceSourceGenerator/Utils/MemberHelper.cs index 2c2bb98..39236b9 100644 --- a/src/ProxyInterfaceSourceGenerator/Utils/MemberHelper.cs +++ b/src/ProxyInterfaceSourceGenerator/Utils/MemberHelper.cs @@ -92,7 +92,7 @@ internal static class MemberHelper } var ownMembers = membersQuery.ToList(); - var ownPropertyNames = ownMembers.Select(x => x.Name); + var ownMemberNames = ownMembers.Select(x => x.Name); if (!proxyBaseClasses) { @@ -106,7 +106,7 @@ internal static class MemberHelper { var baseMembers = baseType.GetMembers().OfType() .Where(m => m.DeclaredAccessibility == Accessibility.Public) - .Where(x => !ownPropertyNames.Contains(x.Name)); + .Where(x => !ownMemberNames.Contains(x.Name)); foreach (var filter in filters) { diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.HumanProxy.g.cs b/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.HumanProxy.g.cs index bd3ee61..eb7e0bf 100644 --- a/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.HumanProxy.g.cs +++ b/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.HumanProxy.g.cs @@ -23,6 +23,11 @@ namespace ProxyInterfaceSourceGeneratorTests.Source + public void Dispose() + { + _Instance.Dispose(); + } + diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.IHttpClient.g.cs b/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.IHttpClient.g.cs new file mode 100644 index 0000000..1ec8623 --- /dev/null +++ b/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.IHttpClient.g.cs @@ -0,0 +1,134 @@ +//---------------------------------------------------------------------------------------- +// +// This code was generated by https://github.com/StefH/ProxyInterfaceSourceGenerator. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//---------------------------------------------------------------------------------------- + +#nullable enable +using System; + +namespace ProxyInterfaceSourceGeneratorTests.Source +{ + public partial interface IHttpClient + { + new System.Net.Http.HttpClient _Instance { get; } + + System.Net.IWebProxy DefaultProxy { get; set; } + + System.Net.Http.Headers.HttpRequestHeaders DefaultRequestHeaders { get; } + + System.Version DefaultRequestVersion { get; set; } + + System.Net.Http.HttpVersionPolicy DefaultVersionPolicy { get; set; } + + System.Uri? BaseAddress { get; set; } + + System.TimeSpan Timeout { get; set; } + + long MaxResponseContentBufferSize { get; set; } + + + + System.Threading.Tasks.Task GetStringAsync(string? requestUri); + + System.Threading.Tasks.Task GetStringAsync(System.Uri? requestUri); + + System.Threading.Tasks.Task GetStringAsync(string? requestUri, System.Threading.CancellationToken cancellationToken); + + System.Threading.Tasks.Task GetStringAsync(System.Uri? requestUri, System.Threading.CancellationToken cancellationToken); + + System.Threading.Tasks.Task GetByteArrayAsync(string? requestUri); + + System.Threading.Tasks.Task GetByteArrayAsync(System.Uri? requestUri); + + System.Threading.Tasks.Task GetByteArrayAsync(string? requestUri, System.Threading.CancellationToken cancellationToken); + + System.Threading.Tasks.Task GetByteArrayAsync(System.Uri? requestUri, System.Threading.CancellationToken cancellationToken); + + System.Threading.Tasks.Task GetStreamAsync(string? requestUri); + + System.Threading.Tasks.Task GetStreamAsync(string? requestUri, System.Threading.CancellationToken cancellationToken); + + System.Threading.Tasks.Task GetStreamAsync(System.Uri? requestUri); + + System.Threading.Tasks.Task GetStreamAsync(System.Uri? requestUri, System.Threading.CancellationToken cancellationToken); + + System.Threading.Tasks.Task GetAsync(string? requestUri); + + System.Threading.Tasks.Task GetAsync(System.Uri? requestUri); + + System.Threading.Tasks.Task GetAsync(string? requestUri, System.Net.Http.HttpCompletionOption completionOption); + + System.Threading.Tasks.Task GetAsync(System.Uri? requestUri, System.Net.Http.HttpCompletionOption completionOption); + + System.Threading.Tasks.Task GetAsync(string? requestUri, System.Threading.CancellationToken cancellationToken); + + System.Threading.Tasks.Task GetAsync(System.Uri? requestUri, System.Threading.CancellationToken cancellationToken); + + System.Threading.Tasks.Task GetAsync(string? requestUri, System.Net.Http.HttpCompletionOption completionOption, System.Threading.CancellationToken cancellationToken); + + System.Threading.Tasks.Task GetAsync(System.Uri? requestUri, System.Net.Http.HttpCompletionOption completionOption, System.Threading.CancellationToken cancellationToken); + + System.Threading.Tasks.Task PostAsync(string? requestUri, System.Net.Http.HttpContent? content); + + System.Threading.Tasks.Task PostAsync(System.Uri? requestUri, System.Net.Http.HttpContent? content); + + System.Threading.Tasks.Task PostAsync(string? requestUri, System.Net.Http.HttpContent? content, System.Threading.CancellationToken cancellationToken); + + System.Threading.Tasks.Task PostAsync(System.Uri? requestUri, System.Net.Http.HttpContent? content, System.Threading.CancellationToken cancellationToken); + + System.Threading.Tasks.Task PutAsync(string? requestUri, System.Net.Http.HttpContent? content); + + System.Threading.Tasks.Task PutAsync(System.Uri? requestUri, System.Net.Http.HttpContent? content); + + System.Threading.Tasks.Task PutAsync(string? requestUri, System.Net.Http.HttpContent? content, System.Threading.CancellationToken cancellationToken); + + System.Threading.Tasks.Task PutAsync(System.Uri? requestUri, System.Net.Http.HttpContent? content, System.Threading.CancellationToken cancellationToken); + + System.Threading.Tasks.Task PatchAsync(string? requestUri, System.Net.Http.HttpContent? content); + + System.Threading.Tasks.Task PatchAsync(System.Uri? requestUri, System.Net.Http.HttpContent? content); + + System.Threading.Tasks.Task PatchAsync(string? requestUri, System.Net.Http.HttpContent? content, System.Threading.CancellationToken cancellationToken); + + System.Threading.Tasks.Task PatchAsync(System.Uri? requestUri, System.Net.Http.HttpContent? content, System.Threading.CancellationToken cancellationToken); + + System.Threading.Tasks.Task DeleteAsync(string? requestUri); + + System.Threading.Tasks.Task DeleteAsync(System.Uri? requestUri); + + System.Threading.Tasks.Task DeleteAsync(string? requestUri, System.Threading.CancellationToken cancellationToken); + + System.Threading.Tasks.Task DeleteAsync(System.Uri? requestUri, System.Threading.CancellationToken cancellationToken); + + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] + System.Net.Http.HttpResponseMessage Send(System.Net.Http.HttpRequestMessage request); + + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] + System.Net.Http.HttpResponseMessage Send(System.Net.Http.HttpRequestMessage request, System.Net.Http.HttpCompletionOption completionOption); + + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] + System.Net.Http.HttpResponseMessage Send(System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken); + + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] + System.Net.Http.HttpResponseMessage Send(System.Net.Http.HttpRequestMessage request, System.Net.Http.HttpCompletionOption completionOption, System.Threading.CancellationToken cancellationToken); + + System.Threading.Tasks.Task SendAsync(System.Net.Http.HttpRequestMessage request); + + System.Threading.Tasks.Task SendAsync(System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken); + + System.Threading.Tasks.Task SendAsync(System.Net.Http.HttpRequestMessage request, System.Net.Http.HttpCompletionOption completionOption); + + System.Threading.Tasks.Task SendAsync(System.Net.Http.HttpRequestMessage request, System.Net.Http.HttpCompletionOption completionOption, System.Threading.CancellationToken cancellationToken); + + void CancelPendingRequests(); + + + + + } +} +#nullable restore \ No newline at end of file diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.IHttpMessageInvoker.g.cs b/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.IHttpMessageInvoker.g.cs new file mode 100644 index 0000000..2bdd002 --- /dev/null +++ b/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.IHttpMessageInvoker.g.cs @@ -0,0 +1,33 @@ +//---------------------------------------------------------------------------------------- +// +// This code was generated by https://github.com/StefH/ProxyInterfaceSourceGenerator. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//---------------------------------------------------------------------------------------- + +#nullable enable +using System; + +namespace ProxyInterfaceSourceGeneratorTests.Source +{ + public partial interface IHttpMessageInvoker + { + System.Net.Http.HttpMessageInvoker _Instance { get; } + + + + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] + System.Net.Http.HttpResponseMessage Send(System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken); + + System.Threading.Tasks.Task SendAsync(System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken); + + void Dispose(); + + + + + } +} +#nullable restore \ No newline at end of file diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.IHuman.g.cs b/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.IHuman.g.cs index 6c6318a..11198de 100644 --- a/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.IHuman.g.cs +++ b/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.IHuman.g.cs @@ -22,6 +22,8 @@ namespace ProxyInterfaceSourceGeneratorTests.Source + void Dispose(); + diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.IPersonExtends.g.cs b/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.IPersonExtends.g.cs index d015b6e..fd82868 100644 --- a/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.IPersonExtends.g.cs +++ b/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.IPersonExtends.g.cs @@ -54,6 +54,8 @@ namespace ProxyInterfaceSourceGeneratorTests.Source System.Threading.Tasks.Task Method3Async(); + void Dispose(); + diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.PersonExtendsProxy.g.cs b/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.PersonExtendsProxy.g.cs index 08ec654..355b017 100644 --- a/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.PersonExtendsProxy.g.cs +++ b/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.PersonExtendsProxy.g.cs @@ -108,6 +108,11 @@ namespace ProxyInterfaceSourceGeneratorTests.Source return result__57684656; } + public void Dispose() + { + _Instance.Dispose(); + } + diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Destination/System.Net.Http.HttpClientProxy.g.cs b/tests/ProxyInterfaceSourceGeneratorTests/Destination/System.Net.Http.HttpClientProxy.g.cs new file mode 100644 index 0000000..7d41b8b --- /dev/null +++ b/tests/ProxyInterfaceSourceGeneratorTests/Destination/System.Net.Http.HttpClientProxy.g.cs @@ -0,0 +1,410 @@ +//---------------------------------------------------------------------------------------- +// +// This code was generated by https://github.com/StefH/ProxyInterfaceSourceGenerator. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//---------------------------------------------------------------------------------------- + +#nullable enable +using System; + +namespace ProxyInterfaceSourceGeneratorTests.Source +{ + public partial class HttpClientProxy : ProxyInterfaceSourceGeneratorTests.Source.HttpMessageInvokerProxy, IHttpClient + { + public new System.Net.Http.HttpClient _Instance { get; } + public System.Net.Http.HttpMessageInvoker _InstanceHttpMessageInvoker { get; } + + public System.Net.IWebProxy DefaultProxy { get => System.Net.Http.HttpClient.DefaultProxy; set => System.Net.Http.HttpClient.DefaultProxy = value; } + + public System.Net.Http.Headers.HttpRequestHeaders DefaultRequestHeaders { get => _Instance.DefaultRequestHeaders; } + + public System.Version DefaultRequestVersion { get => _Instance.DefaultRequestVersion; set => _Instance.DefaultRequestVersion = value; } + + public System.Net.Http.HttpVersionPolicy DefaultVersionPolicy { get => _Instance.DefaultVersionPolicy; set => _Instance.DefaultVersionPolicy = value; } + + public System.Uri? BaseAddress { get => _Instance.BaseAddress; set => _Instance.BaseAddress = value; } + + public System.TimeSpan Timeout { get => _Instance.Timeout; set => _Instance.Timeout = value; } + + public long MaxResponseContentBufferSize { get => _Instance.MaxResponseContentBufferSize; set => _Instance.MaxResponseContentBufferSize = value; } + + + + public System.Threading.Tasks.Task GetStringAsync(string? requestUri) + { + string? requestUri_ = requestUri; + var result_1347886741 = _Instance.GetStringAsync(requestUri_); + return result_1347886741; + } + + public System.Threading.Tasks.Task GetStringAsync(System.Uri? requestUri) + { + System.Uri? requestUri_ = requestUri; + var result_1347886741 = _Instance.GetStringAsync(requestUri_); + return result_1347886741; + } + + public System.Threading.Tasks.Task GetStringAsync(string? requestUri, System.Threading.CancellationToken cancellationToken) + { + string? requestUri_ = requestUri; + System.Threading.CancellationToken cancellationToken_ = cancellationToken; + var result_1347886741 = _Instance.GetStringAsync(requestUri_, cancellationToken_); + return result_1347886741; + } + + public System.Threading.Tasks.Task GetStringAsync(System.Uri? requestUri, System.Threading.CancellationToken cancellationToken) + { + System.Uri? requestUri_ = requestUri; + System.Threading.CancellationToken cancellationToken_ = cancellationToken; + var result_1347886741 = _Instance.GetStringAsync(requestUri_, cancellationToken_); + return result_1347886741; + } + + public System.Threading.Tasks.Task GetByteArrayAsync(string? requestUri) + { + string? requestUri_ = requestUri; + var result__1359336953 = _Instance.GetByteArrayAsync(requestUri_); + return result__1359336953; + } + + public System.Threading.Tasks.Task GetByteArrayAsync(System.Uri? requestUri) + { + System.Uri? requestUri_ = requestUri; + var result__1359336953 = _Instance.GetByteArrayAsync(requestUri_); + return result__1359336953; + } + + public System.Threading.Tasks.Task GetByteArrayAsync(string? requestUri, System.Threading.CancellationToken cancellationToken) + { + string? requestUri_ = requestUri; + System.Threading.CancellationToken cancellationToken_ = cancellationToken; + var result__1359336953 = _Instance.GetByteArrayAsync(requestUri_, cancellationToken_); + return result__1359336953; + } + + public System.Threading.Tasks.Task GetByteArrayAsync(System.Uri? requestUri, System.Threading.CancellationToken cancellationToken) + { + System.Uri? requestUri_ = requestUri; + System.Threading.CancellationToken cancellationToken_ = cancellationToken; + var result__1359336953 = _Instance.GetByteArrayAsync(requestUri_, cancellationToken_); + return result__1359336953; + } + + public System.Threading.Tasks.Task GetStreamAsync(string? requestUri) + { + string? requestUri_ = requestUri; + var result_355326142 = _Instance.GetStreamAsync(requestUri_); + return result_355326142; + } + + public System.Threading.Tasks.Task GetStreamAsync(string? requestUri, System.Threading.CancellationToken cancellationToken) + { + string? requestUri_ = requestUri; + System.Threading.CancellationToken cancellationToken_ = cancellationToken; + var result_355326142 = _Instance.GetStreamAsync(requestUri_, cancellationToken_); + return result_355326142; + } + + public System.Threading.Tasks.Task GetStreamAsync(System.Uri? requestUri) + { + System.Uri? requestUri_ = requestUri; + var result_355326142 = _Instance.GetStreamAsync(requestUri_); + return result_355326142; + } + + public System.Threading.Tasks.Task GetStreamAsync(System.Uri? requestUri, System.Threading.CancellationToken cancellationToken) + { + System.Uri? requestUri_ = requestUri; + System.Threading.CancellationToken cancellationToken_ = cancellationToken; + var result_355326142 = _Instance.GetStreamAsync(requestUri_, cancellationToken_); + return result_355326142; + } + + public System.Threading.Tasks.Task GetAsync(string? requestUri) + { + string? requestUri_ = requestUri; + var result_1805284658 = _Instance.GetAsync(requestUri_); + return result_1805284658; + } + + public System.Threading.Tasks.Task GetAsync(System.Uri? requestUri) + { + System.Uri? requestUri_ = requestUri; + var result_1805284658 = _Instance.GetAsync(requestUri_); + return result_1805284658; + } + + public System.Threading.Tasks.Task GetAsync(string? requestUri, System.Net.Http.HttpCompletionOption completionOption) + { + string? requestUri_ = requestUri; + System.Net.Http.HttpCompletionOption completionOption_ = completionOption; + var result_1805284658 = _Instance.GetAsync(requestUri_, completionOption_); + return result_1805284658; + } + + public System.Threading.Tasks.Task GetAsync(System.Uri? requestUri, System.Net.Http.HttpCompletionOption completionOption) + { + System.Uri? requestUri_ = requestUri; + System.Net.Http.HttpCompletionOption completionOption_ = completionOption; + var result_1805284658 = _Instance.GetAsync(requestUri_, completionOption_); + return result_1805284658; + } + + public System.Threading.Tasks.Task GetAsync(string? requestUri, System.Threading.CancellationToken cancellationToken) + { + string? requestUri_ = requestUri; + System.Threading.CancellationToken cancellationToken_ = cancellationToken; + var result_1805284658 = _Instance.GetAsync(requestUri_, cancellationToken_); + return result_1805284658; + } + + public System.Threading.Tasks.Task GetAsync(System.Uri? requestUri, System.Threading.CancellationToken cancellationToken) + { + System.Uri? requestUri_ = requestUri; + System.Threading.CancellationToken cancellationToken_ = cancellationToken; + var result_1805284658 = _Instance.GetAsync(requestUri_, cancellationToken_); + return result_1805284658; + } + + public System.Threading.Tasks.Task GetAsync(string? requestUri, System.Net.Http.HttpCompletionOption completionOption, System.Threading.CancellationToken cancellationToken) + { + string? requestUri_ = requestUri; + System.Net.Http.HttpCompletionOption completionOption_ = completionOption; + System.Threading.CancellationToken cancellationToken_ = cancellationToken; + var result_1805284658 = _Instance.GetAsync(requestUri_, completionOption_, cancellationToken_); + return result_1805284658; + } + + public System.Threading.Tasks.Task GetAsync(System.Uri? requestUri, System.Net.Http.HttpCompletionOption completionOption, System.Threading.CancellationToken cancellationToken) + { + System.Uri? requestUri_ = requestUri; + System.Net.Http.HttpCompletionOption completionOption_ = completionOption; + System.Threading.CancellationToken cancellationToken_ = cancellationToken; + var result_1805284658 = _Instance.GetAsync(requestUri_, completionOption_, cancellationToken_); + return result_1805284658; + } + + public System.Threading.Tasks.Task PostAsync(string? requestUri, System.Net.Http.HttpContent? content) + { + string? requestUri_ = requestUri; + System.Net.Http.HttpContent? content_ = content; + var result__1705712948 = _Instance.PostAsync(requestUri_, content_); + return result__1705712948; + } + + public System.Threading.Tasks.Task PostAsync(System.Uri? requestUri, System.Net.Http.HttpContent? content) + { + System.Uri? requestUri_ = requestUri; + System.Net.Http.HttpContent? content_ = content; + var result__1705712948 = _Instance.PostAsync(requestUri_, content_); + return result__1705712948; + } + + public System.Threading.Tasks.Task PostAsync(string? requestUri, System.Net.Http.HttpContent? content, System.Threading.CancellationToken cancellationToken) + { + string? requestUri_ = requestUri; + System.Net.Http.HttpContent? content_ = content; + System.Threading.CancellationToken cancellationToken_ = cancellationToken; + var result__1705712948 = _Instance.PostAsync(requestUri_, content_, cancellationToken_); + return result__1705712948; + } + + public System.Threading.Tasks.Task PostAsync(System.Uri? requestUri, System.Net.Http.HttpContent? content, System.Threading.CancellationToken cancellationToken) + { + System.Uri? requestUri_ = requestUri; + System.Net.Http.HttpContent? content_ = content; + System.Threading.CancellationToken cancellationToken_ = cancellationToken; + var result__1705712948 = _Instance.PostAsync(requestUri_, content_, cancellationToken_); + return result__1705712948; + } + + public System.Threading.Tasks.Task PutAsync(string? requestUri, System.Net.Http.HttpContent? content) + { + string? requestUri_ = requestUri; + System.Net.Http.HttpContent? content_ = content; + var result_182918739 = _Instance.PutAsync(requestUri_, content_); + return result_182918739; + } + + public System.Threading.Tasks.Task PutAsync(System.Uri? requestUri, System.Net.Http.HttpContent? content) + { + System.Uri? requestUri_ = requestUri; + System.Net.Http.HttpContent? content_ = content; + var result_182918739 = _Instance.PutAsync(requestUri_, content_); + return result_182918739; + } + + public System.Threading.Tasks.Task PutAsync(string? requestUri, System.Net.Http.HttpContent? content, System.Threading.CancellationToken cancellationToken) + { + string? requestUri_ = requestUri; + System.Net.Http.HttpContent? content_ = content; + System.Threading.CancellationToken cancellationToken_ = cancellationToken; + var result_182918739 = _Instance.PutAsync(requestUri_, content_, cancellationToken_); + return result_182918739; + } + + public System.Threading.Tasks.Task PutAsync(System.Uri? requestUri, System.Net.Http.HttpContent? content, System.Threading.CancellationToken cancellationToken) + { + System.Uri? requestUri_ = requestUri; + System.Net.Http.HttpContent? content_ = content; + System.Threading.CancellationToken cancellationToken_ = cancellationToken; + var result_182918739 = _Instance.PutAsync(requestUri_, content_, cancellationToken_); + return result_182918739; + } + + public System.Threading.Tasks.Task PatchAsync(string? requestUri, System.Net.Http.HttpContent? content) + { + string? requestUri_ = requestUri; + System.Net.Http.HttpContent? content_ = content; + var result_910894592 = _Instance.PatchAsync(requestUri_, content_); + return result_910894592; + } + + public System.Threading.Tasks.Task PatchAsync(System.Uri? requestUri, System.Net.Http.HttpContent? content) + { + System.Uri? requestUri_ = requestUri; + System.Net.Http.HttpContent? content_ = content; + var result_910894592 = _Instance.PatchAsync(requestUri_, content_); + return result_910894592; + } + + public System.Threading.Tasks.Task PatchAsync(string? requestUri, System.Net.Http.HttpContent? content, System.Threading.CancellationToken cancellationToken) + { + string? requestUri_ = requestUri; + System.Net.Http.HttpContent? content_ = content; + System.Threading.CancellationToken cancellationToken_ = cancellationToken; + var result_910894592 = _Instance.PatchAsync(requestUri_, content_, cancellationToken_); + return result_910894592; + } + + public System.Threading.Tasks.Task PatchAsync(System.Uri? requestUri, System.Net.Http.HttpContent? content, System.Threading.CancellationToken cancellationToken) + { + System.Uri? requestUri_ = requestUri; + System.Net.Http.HttpContent? content_ = content; + System.Threading.CancellationToken cancellationToken_ = cancellationToken; + var result_910894592 = _Instance.PatchAsync(requestUri_, content_, cancellationToken_); + return result_910894592; + } + + public System.Threading.Tasks.Task DeleteAsync(string? requestUri) + { + string? requestUri_ = requestUri; + var result_534537427 = _Instance.DeleteAsync(requestUri_); + return result_534537427; + } + + public System.Threading.Tasks.Task DeleteAsync(System.Uri? requestUri) + { + System.Uri? requestUri_ = requestUri; + var result_534537427 = _Instance.DeleteAsync(requestUri_); + return result_534537427; + } + + public System.Threading.Tasks.Task DeleteAsync(string? requestUri, System.Threading.CancellationToken cancellationToken) + { + string? requestUri_ = requestUri; + System.Threading.CancellationToken cancellationToken_ = cancellationToken; + var result_534537427 = _Instance.DeleteAsync(requestUri_, cancellationToken_); + return result_534537427; + } + + public System.Threading.Tasks.Task DeleteAsync(System.Uri? requestUri, System.Threading.CancellationToken cancellationToken) + { + System.Uri? requestUri_ = requestUri; + System.Threading.CancellationToken cancellationToken_ = cancellationToken; + var result_534537427 = _Instance.DeleteAsync(requestUri_, cancellationToken_); + return result_534537427; + } + + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] + public System.Net.Http.HttpResponseMessage Send(System.Net.Http.HttpRequestMessage request) + { + System.Net.Http.HttpRequestMessage request_ = request; + var result__989347188 = _Instance.Send(request_); + return result__989347188; + } + + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] + public System.Net.Http.HttpResponseMessage Send(System.Net.Http.HttpRequestMessage request, System.Net.Http.HttpCompletionOption completionOption) + { + System.Net.Http.HttpRequestMessage request_ = request; + System.Net.Http.HttpCompletionOption completionOption_ = completionOption; + var result__989347188 = _Instance.Send(request_, completionOption_); + return result__989347188; + } + + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] + public override System.Net.Http.HttpResponseMessage Send(System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) + { + System.Net.Http.HttpRequestMessage request_ = request; + System.Threading.CancellationToken cancellationToken_ = cancellationToken; + var result__989347188 = _Instance.Send(request_, cancellationToken_); + return result__989347188; + } + + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] + public System.Net.Http.HttpResponseMessage Send(System.Net.Http.HttpRequestMessage request, System.Net.Http.HttpCompletionOption completionOption, System.Threading.CancellationToken cancellationToken) + { + System.Net.Http.HttpRequestMessage request_ = request; + System.Net.Http.HttpCompletionOption completionOption_ = completionOption; + System.Threading.CancellationToken cancellationToken_ = cancellationToken; + var result__989347188 = _Instance.Send(request_, completionOption_, cancellationToken_); + return result__989347188; + } + + public System.Threading.Tasks.Task SendAsync(System.Net.Http.HttpRequestMessage request) + { + System.Net.Http.HttpRequestMessage request_ = request; + var result__1161702976 = _Instance.SendAsync(request_); + return result__1161702976; + } + + public override System.Threading.Tasks.Task SendAsync(System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) + { + System.Net.Http.HttpRequestMessage request_ = request; + System.Threading.CancellationToken cancellationToken_ = cancellationToken; + var result__1161702976 = _Instance.SendAsync(request_, cancellationToken_); + return result__1161702976; + } + + public System.Threading.Tasks.Task SendAsync(System.Net.Http.HttpRequestMessage request, System.Net.Http.HttpCompletionOption completionOption) + { + System.Net.Http.HttpRequestMessage request_ = request; + System.Net.Http.HttpCompletionOption completionOption_ = completionOption; + var result__1161702976 = _Instance.SendAsync(request_, completionOption_); + return result__1161702976; + } + + public System.Threading.Tasks.Task SendAsync(System.Net.Http.HttpRequestMessage request, System.Net.Http.HttpCompletionOption completionOption, System.Threading.CancellationToken cancellationToken) + { + System.Net.Http.HttpRequestMessage request_ = request; + System.Net.Http.HttpCompletionOption completionOption_ = completionOption; + System.Threading.CancellationToken cancellationToken_ = cancellationToken; + var result__1161702976 = _Instance.SendAsync(request_, completionOption_, cancellationToken_); + return result__1161702976; + } + + public void CancelPendingRequests() + { + _Instance.CancelPendingRequests(); + } + + + + + + + + public HttpClientProxy(System.Net.Http.HttpClient instance) : base(instance) + { + _Instance = instance; + _InstanceHttpMessageInvoker = instance; + + + } + } +} +#nullable restore \ No newline at end of file diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Destination/System.Net.Http.HttpMessageInvokerProxy.g.cs b/tests/ProxyInterfaceSourceGeneratorTests/Destination/System.Net.Http.HttpMessageInvokerProxy.g.cs new file mode 100644 index 0000000..8e95486 --- /dev/null +++ b/tests/ProxyInterfaceSourceGeneratorTests/Destination/System.Net.Http.HttpMessageInvokerProxy.g.cs @@ -0,0 +1,59 @@ +//---------------------------------------------------------------------------------------- +// +// This code was generated by https://github.com/StefH/ProxyInterfaceSourceGenerator. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//---------------------------------------------------------------------------------------- + +#nullable enable +using System; + +namespace ProxyInterfaceSourceGeneratorTests.Source +{ + public partial class HttpMessageInvokerProxy : IHttpMessageInvoker + { + public System.Net.Http.HttpMessageInvoker _Instance { get; } + + + + + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] + public virtual System.Net.Http.HttpResponseMessage Send(System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) + { + System.Net.Http.HttpRequestMessage request_ = request; + System.Threading.CancellationToken cancellationToken_ = cancellationToken; + var result__989347188 = _Instance.Send(request_, cancellationToken_); + return result__989347188; + } + + public virtual System.Threading.Tasks.Task SendAsync(System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) + { + System.Net.Http.HttpRequestMessage request_ = request; + System.Threading.CancellationToken cancellationToken_ = cancellationToken; + var result__1161702976 = _Instance.SendAsync(request_, cancellationToken_); + return result__1161702976; + } + + public void Dispose() + { + _Instance.Dispose(); + } + + + + + + + + public HttpMessageInvokerProxy(System.Net.Http.HttpMessageInvoker instance) + { + _Instance = instance; + + + + } + } +} +#nullable restore \ No newline at end of file diff --git a/tests/ProxyInterfaceSourceGeneratorTests/ProxyInterfaceSourceGeneratorTest.cs b/tests/ProxyInterfaceSourceGeneratorTests/ProxyInterfaceSourceGeneratorTest.cs index b7f4d4b..d12249f 100644 --- a/tests/ProxyInterfaceSourceGeneratorTests/ProxyInterfaceSourceGeneratorTest.cs +++ b/tests/ProxyInterfaceSourceGeneratorTests/ProxyInterfaceSourceGeneratorTest.cs @@ -448,4 +448,86 @@ public class ProxyInterfaceSourceGeneratorTest c.Should().Be(101); } + + [Fact] + public void GenerateFiles_HttpClient() + { + // Arrange + var attributeFilename = "ProxyInterfaceGenerator.Extra.g.cs"; + var interfaceIHttpClientFilename = "ProxyInterfaceSourceGeneratorTests.Source.IHttpClient.g.cs"; + var proxyClassIHttpClientFilename = "System.Net.Http.HttpClientProxy.g.cs"; + var interfaceIHttpMessageInvokerFilename = "ProxyInterfaceSourceGeneratorTests.Source.IHttpMessageInvoker.g.cs"; + var proxyClassIHttpMessageInvokerFilename = "System.Net.Http.HttpMessageInvokerProxy.g.cs"; + + var pathIHttpClient = "./Source/IHttpClient.cs"; + var sourceFileIHttpClient = new SourceFile + { + Path = pathIHttpClient, + Text = File.ReadAllText(pathIHttpClient), + AttributeToAddToInterface = new ExtraAttribute + { + Name = "ProxyInterfaceGenerator.Proxy", + ArgumentList = "typeof(System.Net.Http.HttpClient)" + } + }; + + var pathIHttpMessageInvoker = "./Source/IHttpMessageInvoker.cs"; + var sourceFileIHttpMessageInvoker = new SourceFile + { + Path = pathIHttpMessageInvoker, + Text = File.ReadAllText(pathIHttpMessageInvoker), + AttributeToAddToInterface = new ExtraAttribute + { + Name = "ProxyInterfaceGenerator.Proxy", + ArgumentList = "typeof(System.Net.Http.HttpMessageInvoker)" + } + }; + + // Act + var result = _sut.Execute(new[] { sourceFileIHttpClient, sourceFileIHttpMessageInvoker }); + + // Assert + result.Valid.Should().BeTrue(); + result.Files.Should().HaveCount(5); + + // Assert attribute + var attribute = result.Files[0].SyntaxTree; + attribute.FilePath.Should().EndWith(attributeFilename); + + + // Assert interface IHttpClient + var interfaceIHttpClient = result.Files[1].SyntaxTree; + interfaceIHttpClient.FilePath.Should().EndWith(interfaceIHttpClientFilename); + + var interfaceCodeIHttpClient = interfaceIHttpClient.ToString(); + if (Write) File.WriteAllText($"../../../Destination/{interfaceIHttpClientFilename}", interfaceCodeIHttpClient); + interfaceCodeIHttpClient.Should().NotBeNullOrEmpty().And.Be(File.ReadAllText($"../../../Destination/{interfaceIHttpClientFilename}")); + + + // Assert interface IHttpMessageInvoker + var interfaceIMessageInvoker = result.Files[2].SyntaxTree; + interfaceIMessageInvoker.FilePath.Should().EndWith(interfaceIHttpMessageInvokerFilename); + + var interfaceCodeIMessageInvoker = interfaceIMessageInvoker.ToString(); + if (Write) File.WriteAllText($"../../../Destination/{interfaceIHttpMessageInvokerFilename}", interfaceCodeIMessageInvoker); + interfaceCodeIMessageInvoker.Should().NotBeNullOrEmpty().And.Be(File.ReadAllText($"../../../Destination/{interfaceIHttpMessageInvokerFilename}")); + + + // Assert Proxy IHttpClient + var proxyClassIHttpClient = result.Files[3].SyntaxTree; + proxyClassIHttpClient.FilePath.Should().EndWith(proxyClassIHttpClientFilename); + + var proxyCodeIHttpClient = proxyClassIHttpClient.ToString(); + if (Write) File.WriteAllText($"../../../Destination/{proxyClassIHttpClientFilename}", proxyCodeIHttpClient); + proxyCodeIHttpClient.Should().NotBeNullOrEmpty().And.Be(File.ReadAllText($"../../../Destination/{proxyClassIHttpClientFilename}")); + + + // Assert Proxy IHttpMessageInvoker + var proxyClassIMessageInvoker = result.Files[4].SyntaxTree; + proxyClassIMessageInvoker.FilePath.Should().EndWith(proxyClassIHttpMessageInvokerFilename); + + var proxyIMessageInvoker = proxyClassIMessageInvoker.ToString(); + if (Write) File.WriteAllText($"../../../Destination/{proxyClassIHttpMessageInvokerFilename}", proxyIMessageInvoker); + proxyIMessageInvoker.Should().NotBeNullOrEmpty().And.Be(File.ReadAllText($"../../../Destination/{proxyClassIHttpMessageInvokerFilename}")); + } } \ No newline at end of file diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Source/Human.cs b/tests/ProxyInterfaceSourceGeneratorTests/Source/Human.cs index eef45d3..75a9fd2 100644 --- a/tests/ProxyInterfaceSourceGeneratorTests/Source/Human.cs +++ b/tests/ProxyInterfaceSourceGeneratorTests/Source/Human.cs @@ -5,5 +5,10 @@ namespace ProxyInterfaceSourceGeneratorTests.Source public bool IsAlive { get; set; } public string GetterOnly => "x"; + + public void Dispose() + { + + } } } \ No newline at end of file diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Source/IHttpClient.cs b/tests/ProxyInterfaceSourceGeneratorTests/Source/IHttpClient.cs new file mode 100644 index 0000000..d0898e9 --- /dev/null +++ b/tests/ProxyInterfaceSourceGeneratorTests/Source/IHttpClient.cs @@ -0,0 +1,5 @@ +namespace ProxyInterfaceSourceGeneratorTests.Source; + +public partial interface IHttpClient : IHttpMessageInvoker +{ +} \ No newline at end of file diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Source/IHttpMessageInvoker.cs b/tests/ProxyInterfaceSourceGeneratorTests/Source/IHttpMessageInvoker.cs new file mode 100644 index 0000000..f19317e --- /dev/null +++ b/tests/ProxyInterfaceSourceGeneratorTests/Source/IHttpMessageInvoker.cs @@ -0,0 +1,5 @@ +namespace ProxyInterfaceSourceGeneratorTests.Source; + +public partial interface IHttpMessageInvoker +{ +} \ No newline at end of file