Skip to content

Commit

Permalink
feat: add for custom headers to the HTTP client used by the verifier #…
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald Holshausen committed Feb 28, 2022
1 parent 74bd453 commit f52c362
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 18 deletions.
2 changes: 0 additions & 2 deletions rust/pact_ffi/src/verifier/handle.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
//! Handle interface to creating a verifier
use std::collections::HashMap;
use std::sync::Arc;
use itertools::Itertools;
use libc::clone;

use log::debug;
use pact_models::prelude::HttpAuth;
Expand Down
65 changes: 49 additions & 16 deletions rust/pact_verifier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,14 @@ use std::time::Duration;

use ansi_term::*;
use ansi_term::Colour::*;
use anyhow::anyhow;
use futures::prelude::*;
use futures::stream::StreamExt;
use http::{header, HeaderMap};
use http::header::HeaderName;
use itertools::Itertools;
use log::*;
use maplit::*;
use pact_plugin_driver::plugin_manager::{load_plugin, shutdown_plugins};
use regex::Regex;
use serde_json::Value;

pub use callback_executors::NullRequestFilterExecutor;
use callback_executors::RequestFilterExecutor;
use pact_matching::{match_response, Mismatch};
use pact_matching::logging::LOG_ID;
use pact_models::generators::GeneratorTestMode;
use pact_models::http_utils::HttpAuth;
use pact_models::interaction::Interaction;
Expand All @@ -34,16 +29,24 @@ use pact_models::pact::{load_pact_from_url, Pact, read_pact};
use pact_models::prelude::v4::SynchronousHttp;
use pact_models::provider_states::*;
use pact_models::v4::interaction::V4Interaction;
use pact_plugin_driver::plugin_manager::{load_plugin, shutdown_plugins};
use pact_plugin_driver::plugin_models::{PluginDependency, PluginDependencyType};
use regex::Regex;
use serde_json::Value;

pub use callback_executors::NullRequestFilterExecutor;
use callback_executors::RequestFilterExecutor;
use pact_matching::{match_response, Mismatch};
use pact_matching::logging::LOG_ID;
use pact_matching::metrics::{MetricEvent, send_metrics};

use crate::callback_executors::{ProviderStateError, ProviderStateExecutor};
use crate::messages::{process_message_result, verify_message_from_provider, verify_sync_message_from_provider};
use crate::metrics::VerificationMetrics;
use crate::pact_broker::{Link, PactVerificationContext, publish_verification_results, TestResult};
pub use crate::pact_broker::{ConsumerVersionSelector, PactsForVerificationRequest};
use crate::provider_client::make_provider_request;
use crate::request_response::process_request_response_result;
use pact_plugin_driver::plugin_models::{PluginDependency, PluginDependencyType};
use pact_matching::metrics::{MetricEvent, send_metrics};
use crate::metrics::VerificationMetrics;
use crate::verification_result::VerificationExecutionResult;

mod provider_client;
Expand Down Expand Up @@ -302,11 +305,21 @@ async fn verify_interaction<'a, F: RequestFilterExecutor, S: ProviderStateExecut
options: &VerificationOptions<F>,
provider_state_executor: &Arc<S>
) -> Result<Option<String>, MismatchResult> {
let client = Arc::new(reqwest::Client::builder()
.danger_accept_invalid_certs(options.disable_ssl_verification)
.timeout(Duration::from_millis(options.request_timeout))
.build()
.unwrap_or(reqwest::Client::new()));
let mut client_builder = reqwest::Client::builder()
.danger_accept_invalid_certs(options.disable_ssl_verification)
.timeout(Duration::from_millis(options.request_timeout));

if !options.custom_headers.is_empty() {
let headers = match setup_custom_headers(&options.custom_headers) {
Ok(headers) => headers,
Err(err) => {
return Err(MismatchResult::Error(err.to_string(), interaction.id()));
}
};
client_builder = client_builder.default_headers(headers);
}

let client = Arc::new(client_builder.build().unwrap_or(reqwest::Client::new()));

let mut provider_states_results = hashmap!{};
let sc_results = futures::stream::iter(
Expand Down Expand Up @@ -385,6 +398,26 @@ async fn verify_interaction<'a, F: RequestFilterExecutor, S: ProviderStateExecut
result
}

fn setup_custom_headers(custom_headers: &HashMap<String, String>) -> anyhow::Result<HeaderMap> {
let mut headers = header::HeaderMap::new();
for (key, value) in custom_headers {
let header_name = match HeaderName::try_from(key) {
Ok(name) => name,
Err(err) => {
return Err(anyhow!("Custom header '{key}' is invalid. Only ASCII characters (32-127) are permitted - {err}"));
}
};
let header_value = match header::HeaderValue::from_str(value.as_str()) {
Ok(value) => value,
Err(err) => {
return Err(anyhow!("Custom header '{key}' has an invalid value '{value}'. Only ASCII characters (32-127) are permitted - {err}"));
}
};
headers.append(header_name, header_value);
}
Ok(headers)
}

fn generate_display_for_result(
status: u16,
status_result: ANSIGenericString<str>,
Expand Down

0 comments on commit f52c362

Please sign in to comment.