-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Communication: Add TokenCredentialAddHostHeaderPolicy for TokenCreden…
…tial Requests (#24442) * Communication: Add TokenCredentialAdditionalHeaderPolicy for CallingServerClientBuilder * Add TokenCredentialAddHostHeaderPolicyTests * Fixing comment * Use URL class to get hostname * Fix style errors Co-authored-by: Melissa Neubert <[email protected]>
- Loading branch information
1 parent
ad3586a
commit e0987f9
Showing
3 changed files
with
173 additions
and
6 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
38 changes: 38 additions & 0 deletions
38
...c/main/java/com/azure/communication/callingserver/TokenCredentialAddHostHeaderPolicy.java
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,38 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.communication.callingserver; | ||
|
||
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; | ||
|
||
/** | ||
* HttpPipelinePolicy to append request host headers for CallingServerClient TokenCredential requests | ||
*/ | ||
public final class TokenCredentialAddHostHeaderPolicy implements HttpPipelinePolicy { | ||
private static final String X_MS_HOST_HEADER = "x-ms-host"; | ||
private final String xMsHostValue; | ||
|
||
/** | ||
* Created with a non-null resourceHostName value | ||
* @param resourceHostName Host name of the ACS resource | ||
*/ | ||
public TokenCredentialAddHostHeaderPolicy(String resourceHostName) { | ||
Objects.requireNonNull(resourceHostName, "'resourceHostName' cannot be a null value."); | ||
xMsHostValue = resourceHostName; | ||
} | ||
|
||
@Override | ||
public Mono<HttpResponse> process(HttpPipelineCallContext context, HttpPipelineNextPolicy next) { | ||
if (!xMsHostValue.isEmpty()) { | ||
context.getHttpRequest().setHeader(X_MS_HOST_HEADER, xMsHostValue); | ||
} | ||
return next.process(); | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
...t/java/com/azure/communication/callingserver/TokenCredentialAddHostHeaderPolicyTests.java
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,110 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.communication.callingserver; | ||
|
||
import com.azure.core.http.HttpClient; | ||
import com.azure.core.http.HttpPipeline; | ||
import com.azure.core.http.HttpPipelineBuilder; | ||
import com.azure.core.http.HttpMethod; | ||
import com.azure.core.http.HttpRequest; | ||
import com.azure.core.http.HttpResponse; | ||
import com.azure.core.http.policy.HttpPipelinePolicy; | ||
import reactor.core.publisher.Mono; | ||
import reactor.test.StepVerifier; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
|
||
public class TokenCredentialAddHostHeaderPolicyTests { | ||
|
||
private class NoOpHttpClient implements HttpClient { | ||
@Override | ||
public Mono<HttpResponse> send(HttpRequest request) { | ||
return Mono.empty(); // NOP | ||
} | ||
} | ||
|
||
private static final String HOST_NAME = "host.communication.azure.com"; | ||
|
||
private final HttpPipelinePolicy verifyHeadersPolicy = (context, next) -> { | ||
HttpRequest request = context.getHttpRequest(); | ||
String hostHeaderValue = request.getHeaders().getValue("x-ms-host"); | ||
assertEquals(HOST_NAME, hostHeaderValue); | ||
return next.process(); | ||
}; | ||
|
||
@Test | ||
public void getRequestTest() throws MalformedURLException { | ||
final TokenCredentialAddHostHeaderPolicy clientPolicy = new TokenCredentialAddHostHeaderPolicy(HOST_NAME); | ||
|
||
final HttpPipeline pipeline = new HttpPipelineBuilder() | ||
.httpClient(new NoOpHttpClient()) | ||
.policies(clientPolicy, verifyHeadersPolicy) | ||
.build(); | ||
|
||
HttpRequest request = new HttpRequest(HttpMethod.GET, new URL("https://localhost?id=b93a5ef4-f622-44d8-a80b-ff983122554e")); | ||
StepVerifier.create(pipeline.send(request)) | ||
.verifyComplete(); | ||
} | ||
|
||
@Test | ||
public void postRequestTest() throws MalformedURLException { | ||
final TokenCredentialAddHostHeaderPolicy clientPolicy = new TokenCredentialAddHostHeaderPolicy(HOST_NAME); | ||
|
||
final HttpPipeline pipeline = new HttpPipelineBuilder() | ||
.httpClient(new NoOpHttpClient()) | ||
.policies(clientPolicy, verifyHeadersPolicy) | ||
.build(); | ||
|
||
HttpRequest request = new HttpRequest(HttpMethod.POST, new URL("https://localhost?id=b93a5ef4-f622-44d8-a80b-ff983122554e")); | ||
StepVerifier.create(pipeline.send(request)) | ||
.verifyComplete(); | ||
} | ||
|
||
@Test | ||
public void patchRequestTest() throws MalformedURLException { | ||
final TokenCredentialAddHostHeaderPolicy clientPolicy = new TokenCredentialAddHostHeaderPolicy(HOST_NAME); | ||
|
||
final HttpPipeline pipeline = new HttpPipelineBuilder() | ||
.httpClient(new NoOpHttpClient()) | ||
.policies(clientPolicy, verifyHeadersPolicy) | ||
.build(); | ||
|
||
HttpRequest request = new HttpRequest(HttpMethod.PATCH, new URL("https://localhost?id=b93a5ef4-f622-44d8-a80b-ff983122554e")); | ||
StepVerifier.create(pipeline.send(request)) | ||
.verifyComplete(); | ||
} | ||
|
||
|
||
@Test | ||
public void putRequestTest() throws MalformedURLException { | ||
final TokenCredentialAddHostHeaderPolicy clientPolicy = new TokenCredentialAddHostHeaderPolicy(HOST_NAME); | ||
|
||
final HttpPipeline pipeline = new HttpPipelineBuilder() | ||
.httpClient(new NoOpHttpClient()) | ||
.policies(clientPolicy, verifyHeadersPolicy) | ||
.build(); | ||
|
||
HttpRequest request = new HttpRequest(HttpMethod.PUT, new URL("https://localhost?id=b93a5ef4-f622-44d8-a80b-ff983122554e")); | ||
StepVerifier.create(pipeline.send(request)) | ||
.verifyComplete(); | ||
} | ||
|
||
@Test | ||
public void deleteRequestTest() throws MalformedURLException { | ||
final TokenCredentialAddHostHeaderPolicy clientPolicy = new TokenCredentialAddHostHeaderPolicy(HOST_NAME); | ||
|
||
final HttpPipeline pipeline = new HttpPipelineBuilder() | ||
.httpClient(new NoOpHttpClient()) | ||
.policies(clientPolicy, verifyHeadersPolicy) | ||
.build(); | ||
|
||
HttpRequest request = new HttpRequest(HttpMethod.DELETE, new URL("https://localhost?id=b93a5ef4-f622-44d8-a80b-ff983122554e")); | ||
StepVerifier.create(pipeline.send(request)) | ||
.verifyComplete(); | ||
} | ||
} |