Skip to content

Commit

Permalink
Fixes dotnet#4957
Browse files Browse the repository at this point in the history
Adds extension methods allowing retrieval of the request message from resilience context
  • Loading branch information
iliar-turdushev committed Sep 30, 2024
1 parent 2b6bc38 commit 5631d0d
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Microsoft.Extensions.Http.Resilience.Internal;

internal static class ResilienceKeys
{
public static readonly ResiliencePropertyKey<HttpRequestMessage> RequestMessage = new("Resilience.Http.RequestMessage");
public static readonly ResiliencePropertyKey<HttpRequestMessage?> RequestMessage = new("Resilience.Http.RequestMessage");

public static readonly ResiliencePropertyKey<RequestRoutingStrategy> RoutingStrategy = new("Resilience.Http.RequestRoutingStrategy");

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Net.Http;
using Microsoft.Extensions.Http.Resilience.Internal;
using Microsoft.Shared.Diagnostics;
using Polly;

namespace Polly;

/// <summary>
/// Provides utility methods for working with <see cref="ResilienceContext"/>.
/// </summary>
public static class HttpResilienceContextExtensions
{
/// <summary>
/// Gets the request message from the <see cref="ResilienceContext"/>.
/// </summary>
/// <param name="context">The resilience context.</param>
/// <returns>
/// The request message.
/// If the request message is not present in the <see cref="ResilienceContext"/> the method returns <see langword="null"/>.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="context"/> is <see langword="null"/>.</exception>
public static HttpRequestMessage? GetRequestMessage(this ResilienceContext context)
{
_ = Throw.IfNull(context);
return context.Properties.GetValue(ResilienceKeys.RequestMessage, default);
}

/// <summary>
/// Sets the request message on the <see cref="ResilienceContext"/>.
/// </summary>
/// <param name="context">The resilience context.</param>
/// <param name="requestMessage">The request message.</param>
/// <exception cref="ArgumentNullException"><paramref name="context"/> is <see langword="null"/>.</exception>
public static void SetRequestMessage(this ResilienceContext context, HttpRequestMessage? requestMessage)
{
_ = Throw.IfNull(context);
context.Properties.Set(ResilienceKeys.RequestMessage, requestMessage);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Net.Http;
using Microsoft.Extensions.Http.Resilience.Internal;
using Polly;
using Xunit;

namespace Microsoft.Extensions.Http.Resilience.Test.Resilience;

public class HttpResilienceContextExtensionsTests
{
[Fact]
public void GetRequestMessage_ResilienceContextIsNull_Throws()
{
ResilienceContext context = null!;
Assert.Throws<ArgumentNullException>(context.GetRequestMessage);
}

[Fact]
public void GetRequestMessage_RequestMessageIsMissing_ReturnsNull()
{
var context = ResilienceContextPool.Shared.Get();

Assert.Null(context.GetRequestMessage());

ResilienceContextPool.Shared.Return(context);
}

[Fact]
public void GetRequestMessage_RequestMessageIsNull_ReturnsNull()
{
var context = ResilienceContextPool.Shared.Get();
context.Properties.Set(ResilienceKeys.RequestMessage, null);

Assert.Null(context.GetRequestMessage());

ResilienceContextPool.Shared.Return(context);
}

[Fact]
public void GetRequestMessage_RequestMessageIsPresent_ReturnsRequestMessage()
{
var context = ResilienceContextPool.Shared.Get();
using var request = new HttpRequestMessage();
context.Properties.Set(ResilienceKeys.RequestMessage, request);

Assert.Same(request, context.GetRequestMessage());

ResilienceContextPool.Shared.Return(context);
}

[Fact]
public void SetRequestMessage_ResilienceContextIsNull_Throws()
{
ResilienceContext context = null!;
using var request = new HttpRequestMessage();
Assert.Throws<ArgumentNullException>(() => context.SetRequestMessage(request));
}

[Fact]
public void SetRequestMessage_RequestMessageIsNull_SetsNullRequestMessage()
{
var context = ResilienceContextPool.Shared.Get();
context.SetRequestMessage(null);

Assert.True(context.Properties.TryGetValue(ResilienceKeys.RequestMessage, out HttpRequestMessage? request));
Assert.Null(request);

ResilienceContextPool.Shared.Return(context);
}

[Fact]
public void SetRequestMessage_RequestMessageIsNotNull_SetsRequestMessage()
{
var context = ResilienceContextPool.Shared.Get();
using var request = new HttpRequestMessage();
context.SetRequestMessage(request);

Assert.True(context.Properties.TryGetValue(ResilienceKeys.RequestMessage, out HttpRequestMessage? actualRequest));
Assert.Same(request, actualRequest);

ResilienceContextPool.Shared.Return(context);
}
}

0 comments on commit 5631d0d

Please sign in to comment.