-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Read the ClientRequestId off initial request #11304
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
22 changes: 22 additions & 0 deletions
22
sdk/core/Azure.Core/src/Pipeline/Internal/ReadClientRequestIdPolicy.cs
This file contains hidden or 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,22 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| namespace Azure.Core.Pipeline | ||
| { | ||
| internal class ReadClientRequestIdPolicy : HttpPipelineSynchronousPolicy | ||
| { | ||
| protected ReadClientRequestIdPolicy() | ||
| { | ||
| } | ||
|
|
||
| public static ReadClientRequestIdPolicy Shared { get; } = new ReadClientRequestIdPolicy(); | ||
|
|
||
| public override void OnSendingRequest(HttpMessage message) | ||
| { | ||
| if (message.Request.Headers.TryGetValue(ClientRequestIdPolicy.ClientRequestIdHeader, out string? value)) | ||
| { | ||
| message.Request.ClientRequestId = value; | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ | |
| using System.Threading.Tasks; | ||
| using Azure.Core.Pipeline; | ||
| using Azure.Core.Testing; | ||
| using Moq; | ||
| using NUnit.Framework; | ||
|
|
||
| namespace Azure.Core.Tests | ||
|
|
@@ -77,6 +78,66 @@ public async Task VersionDoesntHaveCommitHash() | |
| Regex.Escape($" ({RuntimeInformation.FrameworkDescription}; {RuntimeInformation.OSDescription})"), userAgent); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task CustomClientRequestIdAvailableInPerCallPolicies() | ||
| { | ||
| var policy = new Mock<HttpPipelineSynchronousPolicy>(); | ||
| policy.CallBase = true; | ||
| policy.Setup(p => p.OnSendingRequest(It.IsAny<HttpMessage>())) | ||
| .Callback<HttpMessage>(message => | ||
| { | ||
| Assert.AreEqual("ExternalClientId",message.Request.ClientRequestId); | ||
| Assert.True(message.Request.TryGetHeader("x-ms-client-request-id", out string requestId)); | ||
| Assert.AreEqual("ExternalClientId", requestId); | ||
| }).Verifiable(); | ||
|
|
||
| var options = new TestOptions(); | ||
| options.Transport = new MockTransport(new MockResponse(200)); | ||
| options.AddPolicy(policy.Object, HttpPipelinePosition.PerCall); | ||
|
|
||
| var pipeline = HttpPipelineBuilder.Build(options); | ||
| using (Request request = pipeline.CreateRequest()) | ||
| { | ||
| request.Method = RequestMethod.Get; | ||
| request.Uri.Reset(new Uri("http://example.com")); | ||
| request.Headers.Add("x-ms-client-request-id", "ExternalClientId"); | ||
| await pipeline.SendRequestAsync(request, CancellationToken.None); | ||
| } | ||
|
|
||
| policy.Verify(); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task CustomClientRequestIdSetInPerCallPolicyAppliedAsAHeader() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry for causing this problem - but thank you for making the scenario work.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a good test I with I had before. |
||
| { | ||
| var policy = new Mock<HttpPipelineSynchronousPolicy>(); | ||
| policy.CallBase = true; | ||
| policy.Setup(p => p.OnSendingRequest(It.IsAny<HttpMessage>())) | ||
| .Callback<HttpMessage>(message => | ||
| { | ||
| message.Request.ClientRequestId = "MyPolicyClientId"; | ||
| }).Verifiable(); | ||
|
|
||
| var options = new TestOptions(); | ||
| var transport = new MockTransport(new MockResponse(200)); | ||
| options.Transport = transport; | ||
| options.AddPolicy(policy.Object, HttpPipelinePosition.PerCall); | ||
|
|
||
| var pipeline = HttpPipelineBuilder.Build(options); | ||
| using (Request request = pipeline.CreateRequest()) | ||
| { | ||
| request.Method = RequestMethod.Get; | ||
| request.Uri.Reset(new Uri("http://example.com")); | ||
| request.Headers.Add("x-ms-client-request-id", "ExternalClientId"); | ||
| await pipeline.SendRequestAsync(request, CancellationToken.None); | ||
| } | ||
|
|
||
| policy.Verify(); | ||
|
|
||
| Assert.True(transport.SingleRequest.Headers.TryGetValue("x-ms-client-request-id", out var value)); | ||
| Assert.AreEqual("MyPolicyClientId", value); | ||
| } | ||
|
|
||
| private class TestOptions : ClientOptions | ||
| { | ||
| public TestOptions() | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests policy ordering.