Skip to content

Commit

Permalink
Fixes dotnet#5105
Browse files Browse the repository at this point in the history
Add support for HttpRequestMessage objects containing StreamContent to
the AddStandardHedgingHandler() resilience API.

This change does not update any public API contracts. It updates
internal and private API contracts only.

This is a small commit to resolve comments made on the PR
  • Loading branch information
Adam Hammond committed Apr 18, 2024
1 parent 60accf3 commit 576fca5
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,12 @@ public static IStandardHedgingHandlerBuilder AddStandardHedgingHandler(this IHtt
Throw.InvalidOperationException("Request message snapshot is not attached to the resilience context.");
}
// if a routing strategy has been configured but it does not return the next route, then no more routes
// are availabe, stop hedging
// if a routing strategy has been configured, get the next route from the routing strategy
Uri? route;
if (args.PrimaryContext.Properties.TryGetValue(ResilienceKeys.RoutingStrategy, out var routingPipeline))
{
// if a routing strategy has been configured but it does not return the next route, then no more routes
// are availabe, stop hedging
if (!routingPipeline.TryGetNextRoute(out route))
{
return null;
Expand All @@ -121,7 +122,7 @@ public static IStandardHedgingHandlerBuilder AddStandardHedgingHandler(this IHtt
if (route != null)
{
// replace the RequestUri of the request per the routing strategy
// replace the Host on the RequestUri of the request per the routing strategy
requestMessage.RequestUri = requestMessage.RequestUri!.ReplaceHost(route);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ public static async Task<RequestMessageSnapshot> CreateAsync(HttpRequestMessage
[System.Diagnostics.CodeAnalysis.SuppressMessage("Resilience", "EA0014:The async method doesn't support cancellation", Justification = "Past the point of no cancellation.")]
public async Task<HttpRequestMessage> CreateRequestMessageAsync()
{
if (IsReset())
if (!IsInitialized())
{
throw new InvalidOperationException($"{nameof(CreateRequestMessageAsync)}() cannot be called on a snapshot object that has been reset and has not been initialized");
throw new InvalidOperationException($"{nameof(CreateRequestMessageAsync)}() cannot be called on a snapshot object that has been reset and/or has not been initialized");
}

var clone = new HttpRequestMessage(_method!, _requestUri)
var clone = new HttpRequestMessage(_method!, _requestUri?.OriginalString)
{
Version = _version!
};
Expand Down Expand Up @@ -135,9 +135,9 @@ private static async Task<(HttpContent? content, HttpContent? clonedContent)> Cl
return (content, clonedContent);
}

private bool IsReset()
private bool IsInitialized()
{
return _method == null;
return _method != null;
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Resilience", "EA0014:The async method doesn't support cancellation", Justification = "Past the point of no cancellation.")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public async Task SendAsync_EnsureSnapshotAttached()
}

[Fact]
public void ExecuteAsync_requestMessageNotFound_Throws()
public void ExecuteAsync_RequestMessageNotFound_Throws()
{
var strategy = Create();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,15 @@ public async Task CreateSnapshotAsync_OriginalMessageChanged_SnapshotReturnsOrig
[Fact]
public async Task CreateRequestMessageAsync_SnapshotIsReset_ThrowsException()
{
_requestMessage!.Method = HttpMethod.Get;
Assert.Null(_requestMessage.Content);
using var stream = new MemoryStream();
using var streamWriter = new StreamWriter(stream);
await streamWriter.WriteAsync("some stream content").ConfigureAwait(false);
await streamWriter.FlushAsync().ConfigureAwait(false);
stream.Position = 0;
_requestMessage!.Content = new StreamContent(stream);
AddRequestHeaders(_requestMessage);
AddRequestOptions(_requestMessage);
AddContentHeaders(_requestMessage!.Content);
using RequestMessageSnapshot snapshot = await RequestMessageSnapshot.CreateAsync(_requestMessage).ConfigureAwait(false);
((IResettable)snapshot).TryReset();
_ = await Assert.ThrowsAsync<InvalidOperationException>(snapshot.CreateRequestMessageAsync);
Expand Down

0 comments on commit 576fca5

Please sign in to comment.