-
Notifications
You must be signed in to change notification settings - Fork 763
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add methods allowing retrieval of HttpRequestMessage from ResilienceC…
…ontext (#5460) Fixes #4957 - Adds extension methods allowing retrieval of the request message from resilience context - Replaces usage of ResilienceKeys.RequestMessage variable with corresponding Get/Set methods
- Loading branch information
1 parent
06e66b7
commit 18002be
Showing
10 changed files
with
158 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...raries/Microsoft.Extensions.Http.Resilience/Resilience/HttpResilienceContextExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// 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.Diagnostics.CodeAnalysis; | ||
using System.Net.Http; | ||
using Microsoft.Extensions.Http.Resilience.Internal; | ||
using Microsoft.Shared.DiagnosticIds; | ||
using Microsoft.Shared.Diagnostics; | ||
using Polly; | ||
|
||
namespace Polly; | ||
|
||
/// <summary> | ||
/// Provides utility methods for working with <see cref="ResilienceContext"/>. | ||
/// </summary> | ||
[Experimental(diagnosticId: DiagnosticIds.Experiments.Resilience, UrlFormat = DiagnosticIds.UrlFormat)] | ||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...osoft.Extensions.Http.Resilience.Tests/Resilience/HttpResilienceContextExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// 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()); | ||
} | ||
|
||
[Fact] | ||
public void GetRequestMessage_RequestMessageIsNull_ReturnsNull() | ||
{ | ||
var context = ResilienceContextPool.Shared.Get(); | ||
context.Properties.Set(ResilienceKeys.RequestMessage, null); | ||
|
||
Assert.Null(context.GetRequestMessage()); | ||
} | ||
|
||
[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()); | ||
} | ||
|
||
[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); | ||
} | ||
|
||
[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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters