Skip to content

Commit

Permalink
Add HttpRequestMessageOptions to RefitSettings and add this dictionar…
Browse files Browse the repository at this point in the history
…y to HttpRequestMessage.(Options/Properties) (#1353)

Co-authored-by: Glenn <[email protected]>
Co-authored-by: Chris Pulman <[email protected]>
  • Loading branch information
3 people authored Apr 12, 2023
1 parent b06ef7c commit c1516a8
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
36 changes: 36 additions & 0 deletions Refit.Tests/RequestBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2120,6 +2120,42 @@ public void DynamicRequestPropertiesShouldBeInProperties(string interfaceMethodN
#pragma warning restore CS0618 // Type or member is obsolete

}
[Fact]
public void OptionsFromSettingsShouldBeInProperties()
{
const string nameProp1 = "UnitTest.Property1";
string valueProp1 = "TestValue";
const string nameProp2 = "UnitTest.Property2";
object valueProp2 = new List<string>() { "123", "345"};
var fixture = new RequestBuilderImplementation<IContainAandB>(new RefitSettings()
{
HttpRequestMessageOptions=new Dictionary<string, object>()
{
[nameProp1] = valueProp1,
[nameProp2] = valueProp2,
},
});
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<string>(nameProp1), out var resultValueProp1));
Assert.Equal(valueProp1, resultValueProp1);

Assert.True(output.Options.TryGetValue(new HttpRequestOptionsKey<List<string>>(nameProp2), out var resultValueProp2));
Assert.Equal(valueProp2, resultValueProp2);
#else
Assert.NotEmpty(output.Properties);
Assert.True(output.Properties.TryGetValue(nameProp1, out var resultValueProp1));
Assert.IsType<string>(resultValueProp1);
Assert.Equal(valueProp1, (string)resultValueProp1);

Assert.True(output.Properties.TryGetValue(nameProp2, out var resultValueProp2));
Assert.IsType<List<string>>(resultValueProp2);
Assert.Equal(valueProp2, (List<string>)resultValueProp2);
#endif
}

[Fact]
public void InterfaceTypeShouldBeInProperties()
Expand Down
8 changes: 7 additions & 1 deletion Refit/RefitSettings.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net.Http;
Expand Down Expand Up @@ -108,7 +109,12 @@ public RefitSettings(
/// </summary>
public bool Buffered { get; set; } = false;

#if NET5_0_OR_GREATER
/// <summary>
/// Optional Key-Value pairs, which are displayed in the property <see cref="HttpRequestMessage.Options"/> or <see cref="HttpRequestMessage.Properties"/>.
/// </summary>
public Dictionary<string, object> HttpRequestMessageOptions { get; set; }

#if NET6_0_OR_GREATER
/// <summary>
/// Controls injecting the <see cref="MethodInfo"/> of the method on the Refit client interface that was invoked into the HttpRequestMessage.Options (defaults to false)
/// </summary>
Expand Down
13 changes: 13 additions & 0 deletions Refit/RequestBuilderImplementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,19 @@ Func<object[], HttpRequestMessage> BuildRequestFactoryForMethod(RestMethodInfo r
}
}

// Add RefitSetting.HttpRequestMessageOptions to the HttpRequestMessage
if (this.settings.HttpRequestMessageOptions != null)
{
foreach(var p in this.settings.HttpRequestMessageOptions)
{
#if NET5_0_OR_GREATER
ret.Options.Set(new HttpRequestOptionsKey<object>(p.Key), p.Value);
#else
ret.Properties.Add(p);
#endif
}
}

foreach (var property in propertiesToAdd)
{
#if NET6_0_OR_GREATER
Expand Down
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "6.3",
"version": "6.4",
"publicReleaseRefSpec": [
"^refs/heads/main$", // we release out of main
"^refs/heads/rel/v\\d+\\.\\d+" // we also release branches starting with vN.N
Expand Down

0 comments on commit c1516a8

Please sign in to comment.