From ed05aeefdaaf16a0213d3390635f2acbf3c3b0e2 Mon Sep 17 00:00:00 2001 From: Damian Hickey <57436+damianh@users.noreply.github.com> Date: Wed, 20 Nov 2024 11:31:53 +0100 Subject: [PATCH] Fix HttpRequestMethod.Properties is obsolete warning Add a small extension that has conditional compilation based on target framework to access .Options or .Properties accordingly. --- .../DeviceAuthorizationExtensionsTests.cs | 6 ++---- .../DiscoveryExtensionsTests.cs | 4 ++-- .../DynamicClientRegistrationTests.cs | 4 ++-- .../HttpRequestMethodExtensions.cs | 17 +++++++++++++++++ .../JsonWebKeyExtensionsTests.cs | 4 ++-- .../PushedAuthorizationTests.cs | 4 ++-- .../TokenIntrospectionTests.cs | 4 ++-- .../TokenRequestExtensionsRequestTests.cs | 8 ++++---- .../TokenRevocationExtensions.cs | 4 ++-- .../UserInfoExtensionsTests.cs | 4 ++-- 10 files changed, 37 insertions(+), 22 deletions(-) create mode 100644 identity-model/test/IdentityModel.Tests/HttpClientExtensions/HttpRequestMethodExtensions.cs diff --git a/identity-model/test/IdentityModel.Tests/HttpClientExtensions/DeviceAuthorizationExtensionsTests.cs b/identity-model/test/IdentityModel.Tests/HttpClientExtensions/DeviceAuthorizationExtensionsTests.cs index 9d72efd0..1445f535 100644 --- a/identity-model/test/IdentityModel.Tests/HttpClientExtensions/DeviceAuthorizationExtensionsTests.cs +++ b/identity-model/test/IdentityModel.Tests/HttpClientExtensions/DeviceAuthorizationExtensionsTests.cs @@ -39,8 +39,6 @@ public async Task Request_without_body_should_have_correct_format() var headers = httpRequest.Headers; headers.Count().Should().Be(2); - - } [Fact] @@ -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); @@ -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(); diff --git a/identity-model/test/IdentityModel.Tests/HttpClientExtensions/DiscoveryExtensionsTests.cs b/identity-model/test/IdentityModel.Tests/HttpClientExtensions/DiscoveryExtensionsTests.cs index b49707bc..71ecb6d5 100644 --- a/identity-model/test/IdentityModel.Tests/HttpClientExtensions/DiscoveryExtensionsTests.cs +++ b/identity-model/test/IdentityModel.Tests/HttpClientExtensions/DiscoveryExtensionsTests.cs @@ -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); @@ -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(); diff --git a/identity-model/test/IdentityModel.Tests/HttpClientExtensions/DynamicClientRegistrationTests.cs b/identity-model/test/IdentityModel.Tests/HttpClientExtensions/DynamicClientRegistrationTests.cs index 4e8f1f9e..7d9d78fc 100644 --- a/identity-model/test/IdentityModel.Tests/HttpClientExtensions/DynamicClientRegistrationTests.cs +++ b/identity-model/test/IdentityModel.Tests/HttpClientExtensions/DynamicClientRegistrationTests.cs @@ -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"); var response = await client.RegisterClientAsync(request); @@ -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(); properties.Count.Should().Be(1); var prop = properties.First(); diff --git a/identity-model/test/IdentityModel.Tests/HttpClientExtensions/HttpRequestMethodExtensions.cs b/identity-model/test/IdentityModel.Tests/HttpClientExtensions/HttpRequestMethodExtensions.cs new file mode 100644 index 00000000..35dbcba5 --- /dev/null +++ b/identity-model/test/IdentityModel.Tests/HttpClientExtensions/HttpRequestMethodExtensions.cs @@ -0,0 +1,17 @@ +#if NETFRAMEWORK +using System.Net.Http; +#endif + +namespace Duende.IdentityModel.HttpClientExtensions; + +internal static class HttpRequestMethodExtensions +{ + public static IDictionary GetProperties(this HttpRequestMessage requestMessage) + { +#if NETFRAMEWORK + return requestMessage.Properties; +#else + return (IDictionary)requestMessage.Options; +#endif + } +} \ No newline at end of file diff --git a/identity-model/test/IdentityModel.Tests/HttpClientExtensions/JsonWebKeyExtensionsTests.cs b/identity-model/test/IdentityModel.Tests/HttpClientExtensions/JsonWebKeyExtensionsTests.cs index e4822087..b3ec15fd 100644 --- a/identity-model/test/IdentityModel.Tests/HttpClientExtensions/JsonWebKeyExtensionsTests.cs +++ b/identity-model/test/IdentityModel.Tests/HttpClientExtensions/JsonWebKeyExtensionsTests.cs @@ -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"); var response = await client.GetJsonWebKeySetAsync(request); @@ -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(); properties.Count.Should().Be(1); var prop = properties.First(); diff --git a/identity-model/test/IdentityModel.Tests/HttpClientExtensions/PushedAuthorizationTests.cs b/identity-model/test/IdentityModel.Tests/HttpClientExtensions/PushedAuthorizationTests.cs index 720fa17b..89dfe2d0 100644 --- a/identity-model/test/IdentityModel.Tests/HttpClientExtensions/PushedAuthorizationTests.cs +++ b/identity-model/test/IdentityModel.Tests/HttpClientExtensions/PushedAuthorizationTests.cs @@ -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"); var response = await client.PushAuthorizationAsync(request); @@ -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(); properties.Count.Should().Be(1); var prop = properties.First(); diff --git a/identity-model/test/IdentityModel.Tests/HttpClientExtensions/TokenIntrospectionTests.cs b/identity-model/test/IdentityModel.Tests/HttpClientExtensions/TokenIntrospectionTests.cs index 6c3ff83e..c99b93db 100644 --- a/identity-model/test/IdentityModel.Tests/HttpClientExtensions/TokenIntrospectionTests.cs +++ b/identity-model/test/IdentityModel.Tests/HttpClientExtensions/TokenIntrospectionTests.cs @@ -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"); _ = await client.IntrospectTokenAsync(request); @@ -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 + httpRequest.GetProperties().Should().BeEquivalentTo(new Dictionary { ["custom"] = "custom", }); diff --git a/identity-model/test/IdentityModel.Tests/HttpClientExtensions/TokenRequestExtensionsRequestTests.cs b/identity-model/test/IdentityModel.Tests/HttpClientExtensions/TokenRequestExtensionsRequestTests.cs index c084d776..3f31a07f 100644 --- a/identity-model/test/IdentityModel.Tests/HttpClientExtensions/TokenRequestExtensionsRequestTests.cs +++ b/identity-model/test/IdentityModel.Tests/HttpClientExtensions/TokenRequestExtensionsRequestTests.cs @@ -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; @@ -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(); @@ -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"); diff --git a/identity-model/test/IdentityModel.Tests/HttpClientExtensions/TokenRevocationExtensions.cs b/identity-model/test/IdentityModel.Tests/HttpClientExtensions/TokenRevocationExtensions.cs index 9b5a486d..43e9a964 100644 --- a/identity-model/test/IdentityModel.Tests/HttpClientExtensions/TokenRevocationExtensions.cs +++ b/identity-model/test/IdentityModel.Tests/HttpClientExtensions/TokenRevocationExtensions.cs @@ -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); @@ -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(); diff --git a/identity-model/test/IdentityModel.Tests/HttpClientExtensions/UserInfoExtensionsTests.cs b/identity-model/test/IdentityModel.Tests/HttpClientExtensions/UserInfoExtensionsTests.cs index 60943e11..f030823d 100644 --- a/identity-model/test/IdentityModel.Tests/HttpClientExtensions/UserInfoExtensionsTests.cs +++ b/identity-model/test/IdentityModel.Tests/HttpClientExtensions/UserInfoExtensionsTests.cs @@ -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"); var response = await client.GetUserInfoAsync(request); @@ -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(); properties.Count.Should().Be(1); var prop = properties.First();