Skip to content

Commit

Permalink
feat: Update message builders to support adding message metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed May 23, 2023
1 parent c3a8826 commit 6aac93b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
2 changes: 1 addition & 1 deletion rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions rust/pact_consumer/src/builders/message_builder.rs
Expand Up @@ -72,6 +72,15 @@ impl MessageInteractionBuilder {
self
}

/// Adds a key/value pair to the message metadata. The key can be anything that is convertible
/// into a string, and the value must be conveyable into a JSON value.
pub fn metadata<S: Into<String>, J: Into<Value>>(&mut self, key: S, value: J) -> &mut Self {
let metadata = self.message_contents.metadata
.get_or_insert_with(|| hashmap!{});
metadata.insert(key.into(), value.into());
self
}

/// The interaction we've built (in V4 format).
pub fn build(&self) -> AsynchronousMessage {
debug!("Building V4 AsynchronousMessage interaction: {:?}", self);
Expand Down Expand Up @@ -238,3 +247,26 @@ impl MessageInteractionBuilder {
self
}
}

#[cfg(test)]
mod tests {
use expectest::prelude::*;
use maplit::hashmap;
use serde_json::json;

use crate::builders::MessageInteractionBuilder;

#[test]
fn supports_setting_metadata_values() {
let message = MessageInteractionBuilder::new("test")
.metadata("a", "a")
.metadata("b", json!("b"))
.metadata("c", vec![1, 2, 3])
.build();
expect!(message.contents.metadata).to(be_equal_to(hashmap! {
"a".to_string() => json!("a"),
"b".to_string() => json!("b"),
"c".to_string() => json!([1, 2, 3])
}));
}
}
32 changes: 32 additions & 0 deletions rust/pact_consumer/src/builders/sync_message_builder.rs
Expand Up @@ -70,6 +70,15 @@ impl SyncMessageInteractionBuilder {
self
}

/// Adds a key/value pair to the message request metadata. The key can be anything that is
/// convertible into a string, and the value must be conveyable into a JSON value.
pub fn request_metadata<S: Into<String>, J: Into<Value>>(&mut self, key: S, value: J) -> &mut Self {
let metadata = self.request_contents.metadata
.get_or_insert_with(|| hashmap!{});
metadata.insert(key.into(), value.into());
self
}

/// The interaction we've built (in V4 format).
pub fn build(&self) -> SynchronousMessage {
debug!("Building V4 SynchronousMessages interaction: {:?}", self);
Expand Down Expand Up @@ -315,3 +324,26 @@ impl SyncMessageInteractionBuilder {
self
}
}

#[cfg(test)]
mod tests {
use expectest::prelude::*;
use maplit::hashmap;
use serde_json::json;

use crate::builders::SyncMessageInteractionBuilder;

#[test]
fn supports_setting_metadata_values() {
let message = SyncMessageInteractionBuilder::new("test")
.request_metadata("a", "a")
.request_metadata("b", json!("b"))
.request_metadata("c", vec![1, 2, 3])
.build();
expect!(message.request.metadata).to(be_equal_to(hashmap! {
"a".to_string() => json!("a"),
"b".to_string() => json!("b"),
"c".to_string() => json!([1, 2, 3])
}));
}
}

0 comments on commit 6aac93b

Please sign in to comment.