|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +use async_trait::async_trait; |
| 5 | +use azure_core::{ |
| 6 | + credentials::TokenCredential, |
| 7 | + http::{ |
| 8 | + headers::Headers, |
| 9 | + policies::{Policy, PolicyResult}, |
| 10 | + Context, HttpClient, Method, RawResponse, Request, StatusCode, TransportOptions, |
| 11 | + }, |
| 12 | +}; |
| 13 | +use azure_core_test::{credentials::MockCredential, http::MockHttpClient}; |
| 14 | +use azure_security_keyvault_secrets::{SecretClient, SecretClientOptions}; |
| 15 | +use futures::FutureExt; |
| 16 | +use std::sync::Arc; |
| 17 | + |
| 18 | +// Define a policy that will remove the User-Agent header. |
| 19 | +#[derive(Debug)] |
| 20 | +struct RemoveUserAgent; |
| 21 | + |
| 22 | +#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] |
| 23 | +#[cfg_attr(not(target_arch = "wasm32"), async_trait)] |
| 24 | +impl Policy for RemoveUserAgent { |
| 25 | + async fn send( |
| 26 | + &self, |
| 27 | + ctx: &Context, |
| 28 | + request: &mut Request, |
| 29 | + next: &[Arc<dyn Policy>], |
| 30 | + ) -> PolicyResult { |
| 31 | + let headers = request.headers_mut(); |
| 32 | + |
| 33 | + // Note: HTTP headers are case-insensitive but client-added headers are normalized to lowercase. |
| 34 | + headers.remove("user-agent"); |
| 35 | + |
| 36 | + next[0].send(ctx, request, &next[1..]).await |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +async fn test_remove_user_agent() -> Result<(), Box<dyn std::error::Error>> { |
| 41 | + // Policies are created in an Arc to be generally shared. |
| 42 | + let remove_user_agent = Arc::new(RemoveUserAgent); |
| 43 | + |
| 44 | + // Construct client options with your policy that runs after the built-in per-call UserAgentPolicy. |
| 45 | + let mut options = SecretClientOptions::default(); |
| 46 | + options |
| 47 | + .client_options |
| 48 | + .per_call_policies |
| 49 | + .push(remove_user_agent); |
| 50 | + |
| 51 | + // Ignore: this is only set up for testing. |
| 52 | + // You normally would create credentials from `azure_identity` and |
| 53 | + // use the default transport in production. |
| 54 | + let (credential, transport) = setup()?; |
| 55 | + options.client_options.transport = Some(TransportOptions::new(transport)); |
| 56 | + |
| 57 | + // Construct the client with these options and a shared credential. |
| 58 | + let client = SecretClient::new( |
| 59 | + "https://my-vault.vault.azure.net", |
| 60 | + credential.clone(), |
| 61 | + Some(options), |
| 62 | + )?; |
| 63 | + |
| 64 | + // We'll fetch a secret and let the mock client assert the User-Agent header was removed. |
| 65 | + let secret = client |
| 66 | + .get_secret("my-secret", "", None) |
| 67 | + .await? |
| 68 | + .into_body() |
| 69 | + .await?; |
| 70 | + assert_eq!(secret.value.as_deref(), Some("secret-value")); |
| 71 | + |
| 72 | + Ok(()) |
| 73 | +} |
| 74 | + |
| 75 | +// ----- BEGIN TEST SETUP ----- |
| 76 | +#[tokio::test] |
| 77 | +async fn test_core_remove_user_agent() -> Result<(), Box<dyn std::error::Error>> { |
| 78 | + test_remove_user_agent().await |
| 79 | +} |
| 80 | + |
| 81 | +#[tokio::main] |
| 82 | +async fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 83 | + test_remove_user_agent().await |
| 84 | +} |
| 85 | + |
| 86 | +#[allow(clippy::type_complexity)] |
| 87 | +fn setup() -> Result<(Arc<dyn TokenCredential>, Arc<dyn HttpClient>), Box<dyn std::error::Error>> { |
| 88 | + let client = MockHttpClient::new(|request| { |
| 89 | + async move { |
| 90 | + assert!(request.url().path().starts_with("/secrets/my-secret")); |
| 91 | + assert_eq!(*request.method(), Method::Get); |
| 92 | + assert!( |
| 93 | + !request |
| 94 | + .headers() |
| 95 | + .iter() |
| 96 | + .any(|(name, _)| name.as_str().eq_ignore_ascii_case("user-agent")), |
| 97 | + "user-agent header should be absent" |
| 98 | + ); |
| 99 | + Ok(RawResponse::from_bytes( |
| 100 | + StatusCode::Ok, |
| 101 | + Headers::new(), |
| 102 | + r#"{"value":"secret-value"}"#, |
| 103 | + )) |
| 104 | + } |
| 105 | + .boxed() |
| 106 | + }); |
| 107 | + |
| 108 | + Ok((MockCredential::new()?, Arc::new(client))) |
| 109 | +} |
| 110 | +// ----- END TEST SETUP ----- |
0 commit comments