Skip to content

Commit 74bd453

Browse files
author
Ronald Holshausen
committed
feat: add support for custom headers with the verifier FFI calls #182
1 parent 12a7b78 commit 74bd453

File tree

7 files changed

+39
-10
lines changed

7 files changed

+39
-10
lines changed

rust/Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/pact_ffi/src/mock_server/bodies.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ pub fn process_object(
109109
Value::Object(obj.iter()
110110
.filter(|(key, _)| !key.starts_with("pact:"))
111111
.map(|(key, val)| {
112-
let mut item_path = path.join(key);
112+
let item_path = path.join(key);
113113
(key.clone(), match val {
114114
Value::Object(ref map) => process_object(map, matching_rules, generators, item_path, false, skip_matchers),
115115
Value::Array(ref array) => process_array(array, matching_rules, generators, item_path, false, skip_matchers),

rust/pact_ffi/src/verifier/handle.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
//! Handle interface to creating a verifier
22
3+
use std::collections::HashMap;
34
use std::sync::Arc;
45
use itertools::Itertools;
6+
use libc::clone;
57

68
use log::debug;
79
use pact_models::prelude::HttpAuth;
@@ -204,11 +206,8 @@ impl VerifierHandle {
204206
disable_ssl_verification: bool,
205207
request_timeout: u64
206208
) {
207-
self.verification_options = VerificationOptions {
208-
request_filter: None::<Arc<NullRequestFilterExecutor>>,
209-
disable_ssl_verification,
210-
request_timeout
211-
}
209+
self.verification_options.disable_ssl_verification = disable_ssl_verification;
210+
self.verification_options.request_timeout = request_timeout;
212211
}
213212

214213
/// Update the details used when publishing results
@@ -295,6 +294,11 @@ impl VerifierHandle {
295294
pub fn set_output(&mut self, out: &str) {
296295
self.verifier_output.output = out.split('\n').map(|s| s.to_string()).collect();
297296
}
297+
298+
/// Add a custom header to be included in the call to the provider
299+
pub fn add_custom_header(&mut self, header_name: &str, header_value: &str) {
300+
self.verification_options.custom_headers.insert(header_name.to_string(), header_value.to_string());
301+
}
298302
}
299303

300304
impl Default for VerifierHandle {

rust/pact_ffi/src/verifier/mod.rs

+20
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,26 @@ ffi_fn! {
310310
}
311311
}
312312

313+
ffi_fn! {
314+
/// Adds a custom header to be added to the requests made to the provider.
315+
///
316+
/// # Safety
317+
///
318+
/// The header name and value must point to a valid NULL terminated string and must contain
319+
/// valid UTF-8.
320+
fn pactffi_verifier_add_custom_header(
321+
handle: *mut handle::VerifierHandle,
322+
header_name: *const c_char,
323+
header_value: *const c_char
324+
) {
325+
let handle = as_mut!(handle);
326+
let header_name = safe_str!(header_name);
327+
let header_value = safe_str!(header_value);
328+
329+
handle.add_custom_header(header_name, header_value);
330+
}
331+
}
332+
313333
ffi_fn! {
314334
/// Adds a Pact file as a source to verify.
315335
///

rust/pact_ffi/src/verifier/verifier.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,8 @@ async fn handle_matches(matches: &clap::ArgMatches<'_>) -> Result<(), i32> {
222222
request_filter: None::<Arc<NullRequestFilterExecutor>>,
223223
disable_ssl_verification: matches.is_present("disable-ssl-verification"),
224224
request_timeout: matches.value_of("request-timeout")
225-
.map(|t| t.parse::<u64>().unwrap_or(5000)).unwrap_or(5000)
225+
.map(|t| t.parse::<u64>().unwrap_or(5000)).unwrap_or(5000),
226+
custom_headers: Default::default()
226227
};
227228

228229
let publish_options = if matches.is_present("publish") {

rust/pact_verifier/src/lib.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -562,14 +562,17 @@ pub struct VerificationOptions<F> where F: RequestFilterExecutor {
562562
pub disable_ssl_verification: bool,
563563
/// Timeout in ms for verification requests and state callbacks
564564
pub request_timeout: u64,
565+
/// Custom headers to be added to the requests to the provider
566+
pub custom_headers: HashMap<String, String>
565567
}
566568

567569
impl <F: RequestFilterExecutor> Default for VerificationOptions<F> {
568570
fn default() -> Self {
569571
VerificationOptions {
570572
request_filter: None,
571573
disable_ssl_verification: false,
572-
request_timeout: 5000
574+
request_timeout: 5000,
575+
custom_headers: Default::default()
573576
}
574577
}
575578
}

rust/pact_verifier_cli/src/main.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ async fn handle_matches(matches: &clap::ArgMatches<'_>) -> Result<(), i32> {
334334
disable_ssl_verification: matches.is_present("disable-ssl-verification"),
335335
request_timeout: matches.value_of("request-timeout")
336336
.map(|t| t.parse::<u64>().unwrap_or(5000)).unwrap_or(5000),
337+
custom_headers: Default::default()
337338
};
338339

339340
let publish_options = if matches.is_present("publish") {
@@ -364,7 +365,7 @@ async fn handle_matches(matches: &clap::ArgMatches<'_>) -> Result<(), i32> {
364365
test_framework: "pact_verifier_cli".to_string(),
365366
app_name: "pact_verifier_cli".to_string(),
366367
app_version: env!("CARGO_PKG_VERSION").to_string()
367-
})
368+
}),
368369
).await
369370
.map_err(|err| {
370371
error!("Verification failed with error: {}", err);

0 commit comments

Comments
 (0)