Skip to content

Commit

Permalink
feature: Add RestMethodInfo in HttpRequestMessage (Options or Propert…
Browse files Browse the repository at this point in the history
…ies) (#1352)

Add Unit-Test
  • Loading branch information
Int32Overflow committed Apr 12, 2023
1 parent b608846 commit c97fcb6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
20 changes: 20 additions & 0 deletions Refit.Tests/RequestBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2141,6 +2141,26 @@ public void InterfaceTypeShouldBeInProperties()

}

[Fact]
public void RestMethodInfoShouldBeInProperties()
{
var someProperty = new object();
var fixture = new RequestBuilderImplementation<IContainAandB>();
var factory = fixture.BuildRequestFactoryForMethod(nameof(IContainAandB.Ping));
var output = factory(new object[] { });

#if NET5_0_OR_GREATER
Assert.NotEmpty(output.Options);
Assert.True(output.Options.TryGetValue(new HttpRequestOptionsKey<RestMethodInfo>(HttpRequestMessageOptions.RestMethodInfo), out var restMethodInfo));
#else
Assert.NotEmpty(output.Properties);
Assert.True(output.Properties.TryGetValue(HttpRequestMessageOptions.RestMethodInfo, out var restMethodInfoObj));
Assert.IsType<RestMethodInfo>(restMethodInfoObj);
var restMethodInfo = restMethodInfoObj as RestMethodInfo;
#endif
Assert.Equal(nameof(IContainAandB.Ping), restMethodInfo.Name);
}

[Fact]
public void DynamicRequestPropertiesWithDefaultKeysShouldBeInProperties()
{
Expand Down
13 changes: 6 additions & 7 deletions Refit/HttpRequestMessageProperties.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Refit
namespace Refit
{
/// <summary>
/// Contains Refit-defined properties on the HttpRequestMessage.Properties/Options
Expand All @@ -15,5 +9,10 @@ public static class HttpRequestMessageOptions
/// Returns the <see cref="System.Type"/> of the top-level interface where the method was called from
/// </summary>
public static string InterfaceType { get; } = "Refit.InterfaceType";

/// <summary>
/// Returns the <see cref="Refit.RestMethodInfo"/> of the top-level interface
/// </summary>
public static string RestMethodInfo { get; } = "Refit.RestMethodInfo";
}
}
6 changes: 3 additions & 3 deletions Refit/RequestBuilderImplementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -736,11 +736,11 @@ void AddMultipartItem(MultipartFormDataContent multiPartContent, string fileName
// Always add the top-level type of the interface to the properties
#if NET5_0_OR_GREATER
ret.Options.Set(new HttpRequestOptionsKey<Type>(HttpRequestMessageOptions.InterfaceType), TargetType);
ret.Options.Set(new HttpRequestOptionsKey<RestMethodInfo>(HttpRequestMessageOptions.RestMethodInfo), restMethod);
#else
ret.Properties[HttpRequestMessageOptions.InterfaceType] = TargetType;
#endif
;
ret.Properties[HttpRequestMessageOptions.RestMethodInfo] = restMethod;
#endif
// NB: The URI methods in .NET are dumb. Also, we do this
// UriBuilder business so that we preserve any hardcoded query
Expand Down

0 comments on commit c97fcb6

Please sign in to comment.