Skip to content

Commit

Permalink
Merge branch 'master' into feat/v2-mock-server
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed Jul 9, 2024
2 parents 5613804 + b52007e commit 41ed972
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
5 changes: 3 additions & 2 deletions rust/pact_ffi/src/mock_server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ pub extern fn pactffi_get_tls_ca_certificate() -> *mut c_char {
#[no_mangle]
#[tracing::instrument(level = "trace")]
#[allow(clippy::not_unsafe_ptr_arg_deref)]
#[deprecated(note = "This function is deprecated and replaced with `pactffi_create_mock_server_for_transport`")]
pub extern fn pactffi_create_mock_server_for_pact(pact: PactHandle, addr_str: *const c_char, tls: bool) -> i32 {
let result = catch_unwind(|| {
let addr_c_str = unsafe {
Expand Down Expand Up @@ -340,7 +341,7 @@ ffi_fn! {
transport_config: *const c_char
) -> i32 {
let addr = safe_str!(addr);
let transport = safe_str!(transport);
let transport = optional_str(transport).unwrap_or_else(|| "http".to_string());

let transport_config = match optional_str(transport_config).map(|config| str::parse::<Value>(config.as_str())) {
None => Ok(None),
Expand All @@ -366,7 +367,7 @@ ffi_fn! {
};

match start_mock_server_for_transport(Uuid::new_v4().to_string(),
inner.pact.boxed(), socket_addr, transport, config) {
inner.pact.boxed(), socket_addr, transport.as_str(), config) {
Ok(ms_port) => {
inner.mock_server_started = true;
ms_port
Expand Down
28 changes: 15 additions & 13 deletions rust/pact_ffi/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::env;
use std::{env, thread};
use std::ffi::{CStr, CString};
use std::fs::File;
use std::io::Read;
use std::path::PathBuf;
use std::ptr::null;
use std::time::Duration;

use bytes::Bytes;
use expectest::prelude::*;
Expand All @@ -30,6 +31,7 @@ use pact_ffi::mock_server::{
pactffi_mock_server_mismatches,
pactffi_write_pact_file,
pactffi_mock_server_logs,
pactffi_create_mock_server_for_transport
};
#[allow(deprecated)]
use pact_ffi::mock_server::handles::{
Expand Down Expand Up @@ -398,7 +400,6 @@ fn add_text_comment() {
}

#[test_log::test]
#[allow(deprecated)]
fn http_consumer_feature_test() {
let consumer_name = CString::new("http-consumer").unwrap();
let provider_name = CString::new("http-provider").unwrap();
Expand All @@ -414,7 +415,7 @@ fn http_consumer_feature_test() {
let query_param_matcher = CString::new("{\"value\":\"bar\",\"pact:matcher:type\":\"regex\", \"regex\":\"(bar|baz|bat)\"}").unwrap();
let request_body_with_matchers = CString::new("{\"id\": {\"value\":1,\"pact:matcher:type\":\"type\"}}").unwrap();
let response_body_with_matchers = CString::new("{\"created\": {\"value\":\"maybe\",\"pact:matcher:type\":\"regex\", \"regex\":\"(yes|no|maybe)\"}}").unwrap();
let address = CString::new("127.0.0.1:0").unwrap();
let address = CString::new("127.0.0.1").unwrap();
let description = CString::new("a request to test the FFI interface").unwrap();
let method = CString::new("POST").unwrap();
let query = CString::new("foo").unwrap();
Expand All @@ -435,8 +436,8 @@ fn http_consumer_feature_test() {
pactffi_with_header(interaction.clone(), InteractionPart::Response, special_header.as_ptr(), 0, value_header_with_matcher.as_ptr());
pactffi_with_body(interaction.clone(), InteractionPart::Response, header.as_ptr(), response_body_with_matchers.as_ptr());
pactffi_response_status(interaction.clone(), 200);
let port = pactffi_create_mock_server_for_pact(pact_handle.clone(), address.as_ptr(), false);

let port = pactffi_create_mock_server_for_transport(pact_handle.clone(), address.as_ptr(), 0, null(), null());
expect!(port).to(be_greater_than(0));

// Mock server has started, we can't now modify the pact
Expand All @@ -461,6 +462,7 @@ fn http_consumer_feature_test() {
}
};

thread::sleep(Duration::from_millis(100)); // Give mock server some time to update events
let mismatches = unsafe {
CStr::from_ptr(pactffi_mock_server_mismatches(port)).to_string_lossy().into_owned()
};
Expand Down Expand Up @@ -665,7 +667,7 @@ fn pactffi_with_binary_file_feature_test(specification: PactSpecification, expec

let content_type = CString::new("image/gif").unwrap();
let path = CString::new("/upload").unwrap();
let address = CString::new("127.0.0.1:0").unwrap();
let address = CString::new("127.0.0.1").unwrap();
let description = CString::new("a request to test the FFI interface").unwrap();
let method = CString::new("POST").unwrap();

Expand All @@ -684,8 +686,7 @@ fn pactffi_with_binary_file_feature_test(specification: PactSpecification, expec
// will respond with...
pactffi_response_status(interaction.clone(), 201);

let port = pactffi_create_mock_server_for_pact(pact_handle.clone(), address.as_ptr(), false);

let port = pactffi_create_mock_server_for_transport(pact_handle.clone(), address.as_ptr(), 0, null(), null());
expect!(port).to(be_greater_than(0));

let client = Client::default();
Expand All @@ -705,6 +706,7 @@ fn pactffi_with_binary_file_feature_test(specification: PactSpecification, expec

pactffi_write_pact_file(port, file_path.as_ptr(), true);

thread::sleep(Duration::from_millis(100)); // Give mock server some time to update events
let mismatches = unsafe {
CStr::from_ptr(pactffi_mock_server_mismatches(port)).to_string_lossy().into_owned()
};
Expand Down Expand Up @@ -773,7 +775,6 @@ fn test_missing_plugin() {

// Issue #299
#[test_log::test]
#[allow(deprecated)]
fn each_value_matcher() {
let consumer_name = CString::new("each_value_matcher-consumer").unwrap();
let provider_name = CString::new("each_value_matcher-provider").unwrap();
Expand All @@ -796,15 +797,15 @@ fn each_value_matcher() {
]
});
let body = CString::new(json.to_string()).unwrap();
let address = CString::new("127.0.0.1:0").unwrap();
let address = CString::new("127.0.0.1").unwrap();
let method = CString::new("PUT").unwrap();

pactffi_upon_receiving(interaction.clone(), description.as_ptr());
pactffi_with_request(interaction.clone(), method.as_ptr(), path.as_ptr());
pactffi_with_body(interaction.clone(), InteractionPart::Request, content_type.as_ptr(), body.as_ptr());
pactffi_response_status(interaction.clone(), 200);

let port = pactffi_create_mock_server_for_pact(pact_handle.clone(), address.as_ptr(), false);
let port = pactffi_create_mock_server_for_transport(pact_handle.clone(), address.as_ptr(), 0, null(), null());

expect!(port).to(be_greater_than(0));

Expand All @@ -823,6 +824,7 @@ fn each_value_matcher() {
}
};

thread::sleep(Duration::from_millis(100)); // Give mock server some time to update events
let mismatches = unsafe {
CStr::from_ptr(pactffi_mock_server_mismatches(port)).to_string_lossy().into_owned()
};
Expand All @@ -838,7 +840,6 @@ fn each_value_matcher() {

// Issue #301
#[test_log::test]
#[allow(deprecated)]
fn each_key_matcher() {
let consumer_name = CString::new("each_key_matcher-consumer").unwrap();
let provider_name = CString::new("each_key_matcher-provider").unwrap();
Expand All @@ -862,15 +863,15 @@ fn each_key_matcher() {
]
});
let body = CString::new(json.to_string()).unwrap();
let address = CString::new("127.0.0.1:0").unwrap();
let address = CString::new("127.0.0.1").unwrap();
let method = CString::new("PUT").unwrap();

pactffi_upon_receiving(interaction.clone(), description.as_ptr());
pactffi_with_request(interaction.clone(), method.as_ptr(), path.as_ptr());
pactffi_with_body(interaction.clone(), InteractionPart::Request, content_type.as_ptr(), body.as_ptr());
pactffi_response_status(interaction.clone(), 200);

let port = pactffi_create_mock_server_for_pact(pact_handle.clone(), address.as_ptr(), false);
let port = pactffi_create_mock_server_for_transport(pact_handle.clone(), address.as_ptr(), 0, null(), null());

expect!(port).to(be_greater_than(0));

Expand All @@ -880,6 +881,7 @@ fn each_key_matcher() {
.body(r#"{"1": "foo","not valid": 1,"key": "value","key2": "value"}"#)
.send();

thread::sleep(Duration::from_millis(100)); // Give mock server some time to update events
let mismatches = unsafe {
CStr::from_ptr(pactffi_mock_server_mismatches(port)).to_string_lossy().into_owned()
};
Expand Down

0 comments on commit 41ed972

Please sign in to comment.