-
Notifications
You must be signed in to change notification settings - Fork 2.2k
AzConfig: User provided Customized Headers Policy #6544
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 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
17e4f28
draft
c1e3a5d
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-java …
5b39b9e
revert back to no changes
163cfcb
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-java …
1b813e3
add AddHeadersFromContextPolicy and docs
fc6b98b
Merge branch 'CustomRequestID' of https://github.com/mssfang/azure-sd…
e8c7dd5
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-java …
3952d40
revert back version
b8f15c5
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-java …
60648db
add test
42365bb
Merge branch 'CustomRequestID' of https://github.com/mssfang/azure-sd…
ed75576
use import library
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
43 changes: 43 additions & 0 deletions
43
...ion/src/main/java/com/azure/data/appconfiguration/implementation/CustomHeadersPolicy.java
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,43 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.azure.data.appconfiguration.implementation; | ||
|
|
||
| import com.azure.core.http.HttpHeader; | ||
| import com.azure.core.http.HttpHeaders; | ||
| import com.azure.core.http.HttpPipelineCallContext; | ||
| import com.azure.core.http.HttpPipelineNextPolicy; | ||
| import com.azure.core.http.HttpResponse; | ||
| import com.azure.core.http.policy.HttpPipelinePolicy; | ||
| import reactor.core.publisher.Mono; | ||
|
|
||
| import java.util.Objects; | ||
| import java.util.function.Supplier; | ||
|
|
||
| public class CustomHeadersPolicy implements HttpPipelinePolicy { | ||
|
mssfang marked this conversation as resolved.
Outdated
mssfang marked this conversation as resolved.
Outdated
|
||
| private final Supplier<HttpHeaders> customHeaderSupplier; | ||
|
|
||
| public CustomHeadersPolicy(Supplier<HttpHeaders> customHeaderSupplier) { | ||
|
mssfang marked this conversation as resolved.
Outdated
|
||
| this.customHeaderSupplier = Objects.requireNonNull(customHeaderSupplier, "Custom header cannot be null"); | ||
| } | ||
|
|
||
| /** | ||
| * Adds the customized headers to authenticate a request to Azure App Configuration service. | ||
| * | ||
| * @param context The request context | ||
| * @param next The next HTTP pipeline policy to process the {@code context's} request after this policy | ||
| * completes. | ||
| * @return A {@link Mono} representing the HTTP response that will arrive asynchronously. | ||
| */ | ||
| @Override | ||
| public Mono<HttpResponse> process(HttpPipelineCallContext context, HttpPipelineNextPolicy next) { | ||
| final HttpHeaders httpHeaders = customHeaderSupplier.get(); | ||
|
|
||
| if (Objects.nonNull(httpHeaders) && httpHeaders.getSize() > 0) { | ||
|
alzimmermsft marked this conversation as resolved.
Outdated
|
||
| for (HttpHeader header : httpHeaders) { | ||
| context.getHttpRequest().getHeaders().put(header.getName(), header.getValue()); | ||
|
mssfang marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
| return next.process(); | ||
| } | ||
| } | ||
133 changes: 133 additions & 0 deletions
133
...appconfiguration/src/test/java/com/azure/data/appconfiguration/CustomizedHeadersTest.java
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,133 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.azure.data.appconfiguration; | ||
|
|
||
| import com.azure.core.http.HttpHeaders; | ||
| import com.azure.core.http.HttpMethod; | ||
| import com.azure.core.http.HttpPipeline; | ||
| import com.azure.core.http.HttpPipelineBuilder; | ||
| import com.azure.core.http.HttpRequest; | ||
| import com.azure.core.http.HttpResponse; | ||
| import com.azure.core.http.policy.FixedDelay; | ||
| import com.azure.core.http.policy.RetryPolicy; | ||
| import com.azure.core.test.http.NoOpHttpClient; | ||
| import com.azure.data.appconfiguration.implementation.CustomHeadersPolicy; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
| import reactor.core.publisher.Flux; | ||
| import reactor.core.publisher.Mono; | ||
|
|
||
| import java.net.URL; | ||
| import java.nio.ByteBuffer; | ||
| import java.nio.charset.Charset; | ||
| import java.time.Duration; | ||
| import java.time.temporal.ChronoUnit; | ||
| import java.util.UUID; | ||
| import java.util.function.Supplier; | ||
|
|
||
| public class CustomizedHeadersTest { | ||
|
|
||
| private static final String REQUEST_ID_HEADER = "x-ms-client-request-id"; | ||
| private static final String CORRELATION_ID_HEADER = "x-ms-correlation-request-id"; | ||
| private static final String CORRELATION_CONTEXT_HEADER = "correlation-context"; | ||
|
mssfang marked this conversation as resolved.
Outdated
|
||
|
|
||
|
mssfang marked this conversation as resolved.
Outdated
|
||
| private final HttpResponse mockResponse = new HttpResponse(null) { | ||
| @Override | ||
| public int getStatusCode() { | ||
| return 500; | ||
| } | ||
| @Override | ||
| public String getHeaderValue(String name) { | ||
| return null; | ||
| } | ||
| @Override | ||
| public HttpHeaders getHeaders() { | ||
| return new HttpHeaders(); | ||
| } | ||
| @Override | ||
| public Mono<byte[]> getBodyAsByteArray() { | ||
| return Mono.empty(); | ||
| } | ||
| @Override | ||
| public Flux<ByteBuffer> getBody() { | ||
| return Flux.empty(); | ||
| } | ||
| @Override | ||
| public Mono<String> getBodyAsString() { | ||
| return Mono.empty(); | ||
| } | ||
| @Override | ||
| public Mono<String> getBodyAsString(Charset charset) { | ||
| return Mono.empty(); | ||
| } | ||
| }; | ||
|
mssfang marked this conversation as resolved.
Outdated
|
||
|
|
||
|
|
||
| @Test | ||
| public void newRequestIdForEachCall() throws Exception { | ||
|
mssfang marked this conversation as resolved.
Outdated
|
||
|
|
||
| String customRequestIdHeaderName = "x-ms-client-custom-request-id"; | ||
| String clientProvidedRequestId = UUID.randomUUID().toString(); | ||
|
|
||
| Supplier<HttpHeaders> requestIdSupplier = () -> | ||
| new HttpHeaders().put(customRequestIdHeaderName, clientProvidedRequestId); | ||
|
mssfang marked this conversation as resolved.
Outdated
|
||
|
|
||
| HttpPipeline pipeline = new HttpPipelineBuilder() | ||
| .httpClient(new NoOpHttpClient() { | ||
| String firstRequestId = null; | ||
| @Override | ||
| public Mono<HttpResponse> send(HttpRequest request) { | ||
| if (firstRequestId != null) { | ||
| String newRequestId = request.getHeaders().getValue(REQUEST_ID_HEADER); | ||
| Assertions.assertNotNull(newRequestId); | ||
| Assertions.assertNotEquals(newRequestId, firstRequestId); | ||
| } | ||
| firstRequestId = request.getHeaders().getValue(REQUEST_ID_HEADER); | ||
| if (firstRequestId == null) { | ||
| Assertions.fail(); | ||
| } | ||
| return Mono.just(mockResponse); | ||
| } | ||
| }) | ||
| .policies(new CustomHeadersPolicy(requestIdSupplier)) | ||
| .build(); | ||
| pipeline.send(new HttpRequest(HttpMethod.GET, new URL("http://localhost/"))).block(); | ||
| pipeline.send(new HttpRequest(HttpMethod.GET, new URL("http://localhost/"))).block(); | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| public void clientProvidedRequestIdForRetry() throws Exception { | ||
|
|
||
| String customRequestIdHeaderName = "x-ms-client-custom-request-id"; | ||
| String clientProvidedRequestId = UUID.randomUUID().toString(); | ||
|
|
||
| Supplier<HttpHeaders> requestIdSupplier = () -> | ||
| new HttpHeaders().put(customRequestIdHeaderName, clientProvidedRequestId); | ||
| final HttpPipeline pipeline = new HttpPipelineBuilder() | ||
| .httpClient(new NoOpHttpClient() { | ||
| String firstRequestId = null; | ||
|
|
||
| @Override | ||
| public Mono<HttpResponse> send(HttpRequest request) { | ||
| if (firstRequestId != null) { | ||
| String newRequestId = request.getHeaders().getValue(customRequestIdHeaderName); | ||
| Assertions.assertNotNull(newRequestId); | ||
| Assertions.assertEquals(newRequestId, firstRequestId); | ||
| Assertions.assertEquals(newRequestId, clientProvidedRequestId); | ||
| } | ||
| firstRequestId = request.getHeaders().getValue(customRequestIdHeaderName); | ||
| if (firstRequestId == null) { | ||
| Assertions.fail(); | ||
| } | ||
| return Mono.just(mockResponse); | ||
| } | ||
| }) | ||
| .policies(new CustomHeadersPolicy(requestIdSupplier), | ||
| new RetryPolicy(new FixedDelay(1, Duration.of(0, ChronoUnit.SECONDS)))) | ||
| .build(); | ||
|
|
||
| pipeline.send(new HttpRequest(HttpMethod.GET, new URL("http://localhost/"))).block(); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.