|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package com.azure.spring.cloud.autoconfigure.keyvault.environment; |
| 5 | + |
| 6 | +import com.azure.core.http.policy.HttpLogDetailLevel; |
| 7 | +import com.azure.security.keyvault.secrets.SecretClient; |
| 8 | +import com.azure.spring.cloud.autoconfigure.implementation.keyvault.secrets.properties.AzureKeyVaultSecretProperties; |
| 9 | +import com.azure.spring.cloud.core.implementation.util.AzureSpringIdentifier; |
| 10 | +import com.azure.spring.cloud.core.provider.RetryOptionsProvider; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 13 | +import org.junit.jupiter.api.parallel.Isolated; |
| 14 | +import org.springframework.boot.test.system.CapturedOutput; |
| 15 | +import org.springframework.boot.test.system.OutputCaptureExtension; |
| 16 | + |
| 17 | +import java.time.Duration; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 20 | + |
| 21 | +@Isolated("Run this by itself as it captures System.out") |
| 22 | +@ExtendWith(OutputCaptureExtension.class) |
| 23 | +class KeyVaultSecretPropertySourceUserAgentTests { |
| 24 | + |
| 25 | + @Test |
| 26 | + public void userAgentTest(CapturedOutput output) { |
| 27 | + AzureKeyVaultSecretProperties properties = new AzureKeyVaultSecretProperties(); |
| 28 | + properties.setEndpoint("https://sample-propertysource.vault.azure.net/"); |
| 29 | + properties.getClient().getLogging().setLevel(HttpLogDetailLevel.HEADERS); |
| 30 | + properties.getClient().getLogging().getAllowedHeaderNames().add("User-Agent"); |
| 31 | + properties.getRetry().setMode(RetryOptionsProvider.RetryMode.FIXED); |
| 32 | + properties.getRetry().getFixed().setDelay(Duration.ofSeconds(1)); |
| 33 | + properties.getRetry().getFixed().setMaxRetries(0); |
| 34 | + |
| 35 | + KeyVaultEnvironmentPostProcessor environmentPostProcessor = new KeyVaultEnvironmentPostProcessor(); |
| 36 | + SecretClient secretClient = environmentPostProcessor.buildSecretClient(properties); |
| 37 | + try { |
| 38 | + secretClient.getSecret("property-source-name1"); |
| 39 | + } catch (Exception exception) { |
| 40 | + // Eat it because we just want the log. |
| 41 | + } |
| 42 | + String allOutput = output.getAll(); |
| 43 | + String format1 = String.format("User-Agent:%s", AzureSpringIdentifier.AZURE_SPRING_KEY_VAULT_SECRETS); |
| 44 | + String format2 = String.format("\"User-Agent\":\"%s", AzureSpringIdentifier.AZURE_SPRING_KEY_VAULT_SECRETS); |
| 45 | + assertTrue(allOutput.contains(format1) || allOutput.contains(format2)); |
| 46 | + } |
| 47 | +} |
0 commit comments