Skip to content

Commit

Permalink
Fix HttpRequestMethod.Properties is obsolete warning
Browse files Browse the repository at this point in the history
Add a small extension that has conditional compilation based on
target framework to access .Options or .Properties accordingly.
  • Loading branch information
damianh committed Nov 20, 2024
1 parent 8cd67d7 commit ed05aee
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ public async Task Request_without_body_should_have_correct_format()

var headers = httpRequest.Headers;
headers.Count().Should().Be(2);


}

[Fact]
Expand All @@ -56,7 +54,7 @@ public async Task Http_request_should_have_correct_format()
};

request.Headers.Add("custom", "custom");
request.Properties.Add("custom", "custom");
request.GetProperties().Add("custom", "custom");

var _ = await client.RequestDeviceAuthorizationAsync(request);

Expand All @@ -69,7 +67,7 @@ public async Task Http_request_should_have_correct_format()
headers.Count().Should().Be(3);
headers.Should().Contain(h => h.Key == "custom" && h.Value.First() == "custom");

var properties = httpRequest.Properties;
var properties = httpRequest.GetProperties();
properties.Count.Should().Be(1);

var prop = properties.First();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public async Task Http_request_should_have_correct_format()
};

request.Headers.Add("custom", "custom");
request.Properties.Add("custom", "custom");
request.GetProperties().Add("custom", "custom");

var response = await client.GetDiscoveryDocumentAsync(request);

Expand All @@ -61,7 +61,7 @@ public async Task Http_request_should_have_correct_format()
headers.Count().Should().Be(2);
headers.Should().Contain(h => h.Key == "custom" && h.Value.First() == "custom");

var properties = httpRequest.Properties;
var properties = httpRequest.GetProperties();
properties.Count.Should().Be(1);

var prop = properties.First();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public async Task Http_request_should_have_correct_format()
};

request.Headers.Add("custom", "custom");
request.Properties.Add("custom", "custom");
request.GetProperties().Add("custom", "custom");

Check warning on line 33 in identity-model/test/IdentityModel.Tests/HttpClientExtensions/DynamicClientRegistrationTests.cs

View workflow job for this annotation

GitHub Actions / Build

'HttpRequestMessage.Properties' is obsolete: 'HttpRequestMessage.Properties has been deprecated. Use Options instead.'

var response = await client.RegisterClientAsync(request);

Expand All @@ -44,7 +44,7 @@ public async Task Http_request_should_have_correct_format()
headers.Count().Should().Be(2);
headers.Should().Contain(h => h.Key == "custom" && h.Value.First() == "custom");

var properties = httpRequest.Properties;
var properties = httpRequest.GetProperties();

Check warning on line 47 in identity-model/test/IdentityModel.Tests/HttpClientExtensions/DynamicClientRegistrationTests.cs

View workflow job for this annotation

GitHub Actions / Build

'HttpRequestMessage.Properties' is obsolete: 'HttpRequestMessage.Properties has been deprecated. Use Options instead.'
properties.Count.Should().Be(1);

var prop = properties.First();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#if NETFRAMEWORK
using System.Net.Http;
#endif

namespace Duende.IdentityModel.HttpClientExtensions;

internal static class HttpRequestMethodExtensions
{
public static IDictionary<string, object> GetProperties(this HttpRequestMessage requestMessage)
{
#if NETFRAMEWORK
return requestMessage.Properties;
#else
return (IDictionary<string, object>)requestMessage.Options;
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public async Task Http_request_should_have_correct_format()
};

request.Headers.Add("custom", "custom");
request.Properties.Add("custom", "custom");
request.GetProperties().Add("custom", "custom");

Check warning on line 49 in identity-model/test/IdentityModel.Tests/HttpClientExtensions/JsonWebKeyExtensionsTests.cs

View workflow job for this annotation

GitHub Actions / Build

'HttpRequestMessage.Properties' is obsolete: 'HttpRequestMessage.Properties has been deprecated. Use Options instead.'

var response = await client.GetJsonWebKeySetAsync(request);

Expand All @@ -60,7 +60,7 @@ public async Task Http_request_should_have_correct_format()
headers.Count().Should().Be(2);
headers.Should().Contain(h => h.Key == "custom" && h.Value.First() == "custom");

var properties = httpRequest.Properties;
var properties = httpRequest.GetProperties();

Check warning on line 63 in identity-model/test/IdentityModel.Tests/HttpClientExtensions/JsonWebKeyExtensionsTests.cs

View workflow job for this annotation

GitHub Actions / Build

'HttpRequestMessage.Properties' is obsolete: 'HttpRequestMessage.Properties has been deprecated. Use Options instead.'
properties.Count.Should().Be(1);

var prop = properties.First();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public async Task Http_request_should_have_correct_format()
State = "5678"
};
request.Headers.Add("custom", "custom");
request.Properties.Add("custom", "custom");
request.GetProperties().Add("custom", "custom");

Check warning on line 41 in identity-model/test/IdentityModel.Tests/HttpClientExtensions/PushedAuthorizationTests.cs

View workflow job for this annotation

GitHub Actions / Build

'HttpRequestMessage.Properties' is obsolete: 'HttpRequestMessage.Properties has been deprecated. Use Options instead.'

var response = await client.PushAuthorizationAsync(request);

Expand All @@ -52,7 +52,7 @@ public async Task Http_request_should_have_correct_format()
headers.Count().Should().Be(3);
headers.Should().Contain(h => h.Key == "custom" && h.Value.First() == "custom");

var properties = httpRequest.Properties;
var properties = httpRequest.GetProperties();

Check warning on line 55 in identity-model/test/IdentityModel.Tests/HttpClientExtensions/PushedAuthorizationTests.cs

View workflow job for this annotation

GitHub Actions / Build

'HttpRequestMessage.Properties' is obsolete: 'HttpRequestMessage.Properties has been deprecated. Use Options instead.'
properties.Count.Should().Be(1);

var prop = properties.First();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public async Task Http_request_should_have_correct_format()
};

request.Headers.Add("custom", "custom");
request.Properties.Add("custom", "custom");
request.GetProperties().Add("custom", "custom");

Check warning on line 31 in identity-model/test/IdentityModel.Tests/HttpClientExtensions/TokenIntrospectionTests.cs

View workflow job for this annotation

GitHub Actions / Build

'HttpRequestMessage.Properties' is obsolete: 'HttpRequestMessage.Properties has been deprecated. Use Options instead.'

_ = await client.IntrospectTokenAsync(request);

Expand All @@ -42,7 +42,7 @@ public async Task Http_request_should_have_correct_format()
["Accept"] = new[] { "application/json" },
["custom"] = new[] { "custom" },
});
httpRequest.Properties.Should().BeEquivalentTo(new Dictionary<string, string>
httpRequest.GetProperties().Should().BeEquivalentTo(new Dictionary<string, string>

Check warning on line 45 in identity-model/test/IdentityModel.Tests/HttpClientExtensions/TokenIntrospectionTests.cs

View workflow job for this annotation

GitHub Actions / Build

'HttpRequestMessage.Properties' is obsolete: 'HttpRequestMessage.Properties has been deprecated. Use Options instead.'
{
["custom"] = "custom",
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public async Task Http_request_should_have_correct_format()
};

request.Headers.Add("custom", "custom");
request.Properties.Add("custom", "custom");
request.GetProperties().Add("custom", "custom");

var _ = await client.RequestTokenAsync(request);
var httpRequest = handler.Request;
Expand All @@ -56,7 +56,7 @@ public async Task Http_request_should_have_correct_format()
headers.Count().Should().Be(3);
headers.Should().Contain(h => h.Key == "custom" && h.Value.First() == "custom");

var properties = httpRequest.Properties;
var properties = httpRequest.GetProperties();
properties.Count.Should().Be(1);

var prop = properties.First();
Expand Down Expand Up @@ -159,13 +159,13 @@ public async Task Additional_request_properties_should_be_propagated()
Scope = "scope"
};

request.Properties.Add("foo", "bar");
request.GetProperties().Add("foo", "bar");

var response = await _client.RequestClientCredentialsTokenAsync(request);

response.IsError.Should().BeFalse();

var properties = _handler.Request.Properties;
var properties = _handler.Request.GetProperties();
var foo = properties.First().Value as string;
foo.Should().NotBeNull();
foo.Should().Be("bar");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public async Task Http_request_should_have_correct_format()
};

request.Headers.Add("custom", "custom");
request.Properties.Add("custom", "custom");
request.GetProperties().Add("custom", "custom");

var response = await client.RevokeTokenAsync(request);

Expand All @@ -41,7 +41,7 @@ public async Task Http_request_should_have_correct_format()
headers.Count().Should().Be(2);
headers.Should().Contain(h => h.Key == "custom" && h.Value.First() == "custom");

var properties = httpRequest.Properties;
var properties = httpRequest.GetProperties();
properties.Count.Should().Be(1);

var prop = properties.First();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public async Task Http_request_should_have_correct_format()
};

request.Headers.Add("custom", "custom");
request.Properties.Add("custom", "custom");
request.GetProperties().Add("custom", "custom");

Check warning on line 48 in identity-model/test/IdentityModel.Tests/HttpClientExtensions/UserInfoExtensionsTests.cs

View workflow job for this annotation

GitHub Actions / Build

'HttpRequestMessage.Properties' is obsolete: 'HttpRequestMessage.Properties has been deprecated. Use Options instead.'

var response = await client.GetUserInfoAsync(request);

Expand All @@ -60,7 +60,7 @@ public async Task Http_request_should_have_correct_format()
headers.Should().Contain(h => h.Key == "custom" && h.Value.First() == "custom");
headers.Should().Contain(h => h.Key == "Authorization" && h.Value.First() == "Bearer token");

var properties = httpRequest.Properties;
var properties = httpRequest.GetProperties();

Check warning on line 63 in identity-model/test/IdentityModel.Tests/HttpClientExtensions/UserInfoExtensionsTests.cs

View workflow job for this annotation

GitHub Actions / Build

'HttpRequestMessage.Properties' is obsolete: 'HttpRequestMessage.Properties has been deprecated. Use Options instead.'
properties.Count.Should().Be(1);

var prop = properties.First();
Expand Down

0 comments on commit ed05aee

Please sign in to comment.