diff --git a/Refit.Tests/GitHubApi.cs b/Refit.Tests/GitHubApi.cs index ce9bb87e6..187926db1 100644 --- a/Refit.Tests/GitHubApi.cs +++ b/Refit.Tests/GitHubApi.cs @@ -86,6 +86,9 @@ public interface IGitHubApi [Get("/users/{username}")] IObservable> GetUserObservableWithMetadata(string userName); + [Get("/users/{username}")] + IObservable> GetUserIApiResponseObservableWithMetadata(string userName); + [Post("/users")] Task CreateUser(User user); diff --git a/Refit.Tests/InterfaceStubGenerator.cs b/Refit.Tests/InterfaceStubGenerator.cs index 8dc92d447..44d497c9b 100644 --- a/Refit.Tests/InterfaceStubGenerator.cs +++ b/Refit.Tests/InterfaceStubGenerator.cs @@ -43,12 +43,6 @@ static InterfaceStubGeneratorTests() .AddAssemblies(ImmutableArray.Create("System.Web")) .AddPackages(ImmutableArray.Create(new PackageIdentity("System.Net.Http", "4.3.4"))); #endif - - // Initialise Verify - DerivePathInfo((file, _, type, method) => new(Path.Combine(Path.GetDirectoryName(file), "_snapshots"), type.Name, method.Name)); - - VerifySourceGenerators.Initialize(); - VerifyDiffPlex.Initialize(OutputType.Compact); } public static async Task VerifyGenerator(string input) diff --git a/Refit.Tests/ModuleInitializer.cs b/Refit.Tests/ModuleInitializer.cs new file mode 100644 index 000000000..e68bab095 --- /dev/null +++ b/Refit.Tests/ModuleInitializer.cs @@ -0,0 +1,19 @@ +using System.Runtime.CompilerServices; +using VerifyTests.DiffPlex; + +namespace Refit.Tests; + +static class ModuleInitializer +{ + // ModuleInitializer should only be used in apps +#pragma warning disable CA2255 + [ModuleInitializer] +#pragma warning restore CA2255 + public static void Init() + { + DerivePathInfo((file, _, type, method) => new(Path.Combine(Path.GetDirectoryName(file), "_snapshots"), type.Name, method.Name)); + + VerifySourceGenerators.Initialize(); + VerifyDiffPlex.Initialize(OutputType.Compact); + } +} diff --git a/Refit.Tests/ModuleInitializerAttribute.cs b/Refit.Tests/ModuleInitializerAttribute.cs new file mode 100644 index 000000000..3d63f06e5 --- /dev/null +++ b/Refit.Tests/ModuleInitializerAttribute.cs @@ -0,0 +1,5 @@ +// ReSharper disable once CheckNamespace +namespace System.Runtime.CompilerServices; + +[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] +sealed class ModuleInitializerAttribute : Attribute; diff --git a/Refit.Tests/RestService.cs b/Refit.Tests/RestService.cs index 48c91ca78..353c4c74f 100644 --- a/Refit.Tests/RestService.cs +++ b/Refit.Tests/RestService.cs @@ -1030,6 +1030,55 @@ public async Task HitTheGitHubUserApiAsObservableApiResponse() mockHttp.VerifyNoOutstandingExpectation(); } + [Fact] + public async Task HitTheGitHubUserApiAsObservableIApiResponse() + { + var mockHttp = new MockHttpMessageHandler(); + + var settings = new RefitSettings + { + HttpMessageHandlerFactory = () => mockHttp, + ContentSerializer = new NewtonsoftJsonContentSerializer( + new JsonSerializerSettings() + { + ContractResolver = new SnakeCasePropertyNamesContractResolver() + } + ) + }; + + var responseMessage = new HttpResponseMessage() + { + StatusCode = HttpStatusCode.OK, + Content = new StringContent( + "{ 'login':'octocat', 'avatar_url':'http://foo/bar' }", + System.Text.Encoding.UTF8, + "application/json" + ), + }; + responseMessage.Headers.Add("Cookie", "Value"); + + mockHttp + .Expect(HttpMethod.Get, "https://api.github.com/users/octocat") + .Respond(req => responseMessage); + + var fixture = RestService.For("https://api.github.com", settings); + + var result = await fixture + .GetUserIApiResponseObservableWithMetadata("octocat") + .Timeout(TimeSpan.FromSeconds(10)); + + Assert.True(result.Headers.Any()); + Assert.True(result.IsSuccessStatusCode); + Assert.NotNull(result.ReasonPhrase); + Assert.NotNull(result.RequestMessage); + Assert.False(result.StatusCode == default); + Assert.NotNull(result.Version); + Assert.Equal("octocat", result.Content.Login); + Assert.False(string.IsNullOrEmpty(result.Content.AvatarUrl)); + + mockHttp.VerifyNoOutstandingExpectation(); + } + [Fact] public async Task HitTheGitHubUserApi() { diff --git a/Refit.Tests/_snapshots/InterfaceStubGeneratorTests.FindInterfacesSmokeTest#IGitHubApi.g.verified.cs b/Refit.Tests/_snapshots/InterfaceStubGeneratorTests.FindInterfacesSmokeTest#IGitHubApi.g.verified.cs index 8c0c57321..e1f3523ac 100644 --- a/Refit.Tests/_snapshots/InterfaceStubGeneratorTests.FindInterfacesSmokeTest#IGitHubApi.g.verified.cs +++ b/Refit.Tests/_snapshots/InterfaceStubGeneratorTests.FindInterfacesSmokeTest#IGitHubApi.g.verified.cs @@ -141,79 +141,90 @@ public RefitTestsIGitHubApi(global::System.Net.Http.HttpClient client, global::R return (global::System.IObservable>)______func(this.Client, ______arguments); } - private static readonly global::System.Type[] ______typeParameters6 = new global::System.Type[] {typeof(global::Refit.Tests.User) }; + private static readonly global::System.Type[] ______typeParameters6 = new global::System.Type[] {typeof(string) }; + + /// + public global::System.IObservable> GetUserIApiResponseObservableWithMetadata(string @userName) + { + var ______arguments = new object[] { @userName }; + var ______func = requestBuilder.BuildRestResultFuncForMethod("GetUserIApiResponseObservableWithMetadata", ______typeParameters6 ); + + return (global::System.IObservable>)______func(this.Client, ______arguments); + } + + private static readonly global::System.Type[] ______typeParameters7 = new global::System.Type[] {typeof(global::Refit.Tests.User) }; /// public async global::System.Threading.Tasks.Task CreateUser(global::Refit.Tests.User @user) { var ______arguments = new object[] { @user }; - var ______func = requestBuilder.BuildRestResultFuncForMethod("CreateUser", ______typeParameters6 ); + var ______func = requestBuilder.BuildRestResultFuncForMethod("CreateUser", ______typeParameters7 ); return await ((global::System.Threading.Tasks.Task)______func(this.Client, ______arguments)).ConfigureAwait(false); } - private static readonly global::System.Type[] ______typeParameters7 = new global::System.Type[] {typeof(global::Refit.Tests.User) }; + private static readonly global::System.Type[] ______typeParameters8 = new global::System.Type[] {typeof(global::Refit.Tests.User) }; /// public async global::System.Threading.Tasks.Task> CreateUserWithMetadata(global::Refit.Tests.User @user) { var ______arguments = new object[] { @user }; - var ______func = requestBuilder.BuildRestResultFuncForMethod("CreateUserWithMetadata", ______typeParameters7 ); + var ______func = requestBuilder.BuildRestResultFuncForMethod("CreateUserWithMetadata", ______typeParameters8 ); return await ((global::System.Threading.Tasks.Task>)______func(this.Client, ______arguments)).ConfigureAwait(false); } - private static readonly global::System.Type[] ______typeParameters8 = new global::System.Type[] {typeof(string) }; + private static readonly global::System.Type[] ______typeParameters9 = new global::System.Type[] {typeof(string) }; /// async global::System.Threading.Tasks.Task global::Refit.Tests.IGitHubApi.GetUser(string @userName) { var ______arguments = new object[] { @userName }; - var ______func = requestBuilder.BuildRestResultFuncForMethod("GetUser", ______typeParameters8 ); + var ______func = requestBuilder.BuildRestResultFuncForMethod("GetUser", ______typeParameters9 ); return await ((global::System.Threading.Tasks.Task)______func(this.Client, ______arguments)).ConfigureAwait(false); } - private static readonly global::System.Type[] ______typeParameters9 = new global::System.Type[] {typeof(string) }; + private static readonly global::System.Type[] ______typeParameters10 = new global::System.Type[] {typeof(string) }; /// global::System.IObservable global::Refit.Tests.IGitHubApi.GetUserObservable(string @userName) { var ______arguments = new object[] { @userName }; - var ______func = requestBuilder.BuildRestResultFuncForMethod("GetUserObservable", ______typeParameters9 ); + var ______func = requestBuilder.BuildRestResultFuncForMethod("GetUserObservable", ______typeParameters10 ); return (global::System.IObservable)______func(this.Client, ______arguments); } - private static readonly global::System.Type[] ______typeParameters10 = new global::System.Type[] {typeof(string) }; + private static readonly global::System.Type[] ______typeParameters11 = new global::System.Type[] {typeof(string) }; /// global::System.IObservable global::Refit.Tests.IGitHubApi.GetUserCamelCase(string @userName) { var ______arguments = new object[] { @userName }; - var ______func = requestBuilder.BuildRestResultFuncForMethod("GetUserCamelCase", ______typeParameters10 ); + var ______func = requestBuilder.BuildRestResultFuncForMethod("GetUserCamelCase", ______typeParameters11 ); return (global::System.IObservable)______func(this.Client, ______arguments); } - private static readonly global::System.Type[] ______typeParameters11 = new global::System.Type[] {typeof(string), typeof(global::System.Threading.CancellationToken) }; + private static readonly global::System.Type[] ______typeParameters12 = new global::System.Type[] {typeof(string), typeof(global::System.Threading.CancellationToken) }; /// async global::System.Threading.Tasks.Task> global::Refit.Tests.IGitHubApi.GetOrgMembers(string @orgName, global::System.Threading.CancellationToken @cancellationToken) { var ______arguments = new object[] { @orgName, @cancellationToken }; - var ______func = requestBuilder.BuildRestResultFuncForMethod("GetOrgMembers", ______typeParameters11 ); + var ______func = requestBuilder.BuildRestResultFuncForMethod("GetOrgMembers", ______typeParameters12 ); return await ((global::System.Threading.Tasks.Task>)______func(this.Client, ______arguments)).ConfigureAwait(false); } - private static readonly global::System.Type[] ______typeParameters12 = new global::System.Type[] {typeof(string) }; + private static readonly global::System.Type[] ______typeParameters13 = new global::System.Type[] {typeof(string) }; /// async global::System.Threading.Tasks.Task global::Refit.Tests.IGitHubApi.FindUsers(string @q) { var ______arguments = new object[] { @q }; - var ______func = requestBuilder.BuildRestResultFuncForMethod("FindUsers", ______typeParameters12 ); + var ______func = requestBuilder.BuildRestResultFuncForMethod("FindUsers", ______typeParameters13 ); return await ((global::System.Threading.Tasks.Task)______func(this.Client, ______arguments)).ConfigureAwait(false); } @@ -254,46 +265,57 @@ public RefitTestsIGitHubApi(global::System.Net.Http.HttpClient client, global::R return await ((global::System.Threading.Tasks.Task>)______func(this.Client, ______arguments)).ConfigureAwait(false); } - private static readonly global::System.Type[] ______typeParameters13 = new global::System.Type[] {typeof(string) }; + private static readonly global::System.Type[] ______typeParameters14 = new global::System.Type[] {typeof(string) }; /// async global::System.Threading.Tasks.Task> global::Refit.Tests.IGitHubApi.GetUserWithMetadata(string @userName) { var ______arguments = new object[] { @userName }; - var ______func = requestBuilder.BuildRestResultFuncForMethod("GetUserWithMetadata", ______typeParameters13 ); + var ______func = requestBuilder.BuildRestResultFuncForMethod("GetUserWithMetadata", ______typeParameters14 ); return await ((global::System.Threading.Tasks.Task>)______func(this.Client, ______arguments)).ConfigureAwait(false); } - private static readonly global::System.Type[] ______typeParameters14 = new global::System.Type[] {typeof(string) }; + private static readonly global::System.Type[] ______typeParameters15 = new global::System.Type[] {typeof(string) }; /// global::System.IObservable> global::Refit.Tests.IGitHubApi.GetUserObservableWithMetadata(string @userName) { var ______arguments = new object[] { @userName }; - var ______func = requestBuilder.BuildRestResultFuncForMethod("GetUserObservableWithMetadata", ______typeParameters14 ); + var ______func = requestBuilder.BuildRestResultFuncForMethod("GetUserObservableWithMetadata", ______typeParameters15 ); return (global::System.IObservable>)______func(this.Client, ______arguments); } - private static readonly global::System.Type[] ______typeParameters15 = new global::System.Type[] {typeof(global::Refit.Tests.User) }; + private static readonly global::System.Type[] ______typeParameters16 = new global::System.Type[] {typeof(string) }; + + /// + global::System.IObservable> global::Refit.Tests.IGitHubApi.GetUserIApiResponseObservableWithMetadata(string @userName) + { + var ______arguments = new object[] { @userName }; + var ______func = requestBuilder.BuildRestResultFuncForMethod("GetUserIApiResponseObservableWithMetadata", ______typeParameters16 ); + + return (global::System.IObservable>)______func(this.Client, ______arguments); + } + + private static readonly global::System.Type[] ______typeParameters17 = new global::System.Type[] {typeof(global::Refit.Tests.User) }; /// async global::System.Threading.Tasks.Task global::Refit.Tests.IGitHubApi.CreateUser(global::Refit.Tests.User @user) { var ______arguments = new object[] { @user }; - var ______func = requestBuilder.BuildRestResultFuncForMethod("CreateUser", ______typeParameters15 ); + var ______func = requestBuilder.BuildRestResultFuncForMethod("CreateUser", ______typeParameters17 ); return await ((global::System.Threading.Tasks.Task)______func(this.Client, ______arguments)).ConfigureAwait(false); } - private static readonly global::System.Type[] ______typeParameters16 = new global::System.Type[] {typeof(global::Refit.Tests.User) }; + private static readonly global::System.Type[] ______typeParameters18 = new global::System.Type[] {typeof(global::Refit.Tests.User) }; /// async global::System.Threading.Tasks.Task> global::Refit.Tests.IGitHubApi.CreateUserWithMetadata(global::Refit.Tests.User @user) { var ______arguments = new object[] { @user }; - var ______func = requestBuilder.BuildRestResultFuncForMethod("CreateUserWithMetadata", ______typeParameters16 ); + var ______func = requestBuilder.BuildRestResultFuncForMethod("CreateUserWithMetadata", ______typeParameters18 ); return await ((global::System.Threading.Tasks.Task>)______func(this.Client, ______arguments)).ConfigureAwait(false); }