it seems that with the pact builder its not possib...
# pact-rust
p
it seems that with the pact builder its not possible to have a different request/response for the same path and and http method or i don’t know how to get it done I’ve tried to build a pact with 2 different sets of requests and their responses but only one of them make it into the pact file. Here are the pacts in 2 different tests
Copy code
let pact = PactBuilder::new("Consumer", "Provider")
    .interaction("looks for something that doesn't exist", "", |mut i| {
        i.request
            .post()
            .path("/search")
            .content_type("application/json")
            .json_body(like!(json!({"key": "i_dont_exist"})));
        i.response
            .content_type("application/json")
            .json_body(json!({"count": 0, "results": []}));
        i
    })
    .output_dir("pact_contracts")
    .start_mock_server(None);

let pact = PactBuilder::new("Consumer", "Provider")
    .interaction("looks for something that exists", "", |mut i| {
        i.request
            .post()
            .path("/search")
            .content_type("application/json")
            .json_body(like!(json!({"key": "i_exist"})));
        i.response
            .content_type("application/json")
            .json_body(json!({"count": 1, "results": [{"i_exist"}]}));
        i
    })
    .output_dir("pact_contracts")
    .start_mock_server(None);
Other pact implementations seemed to get around this by using something called “Upon Receiving” but i can’t find this anywhere in the rust docs. How am I able to achieve this ?
u
“Upon Receiving” sets the description for the interaction, which is the string parameter to the
interaction
function, which you have set correctly. Make sure you are not deleting the contents of the "pact_contracts" directory between the tests. That is the most common reason for this type of problem.
p
I’ve not delete the file
Are there any documented examples of what it would look like to generate a pact with multiple interactions or a pacts with different sets of payload/response pairs to the same endpoint
u
p
Something strange is happening here. I clone the repository and run the tests locally and they still fail.
u
Hmm, are you running on OSX?
p
yes
u
Ok, let me see if I can replicate it on an OSX machine
p
when i do some crude debugging in the
<http://pact_builder.rs|pact_builder.rs>
Copy code
pub fn push_interaction(&mut self, interaction: &dyn Interaction) -> &mut Self {
        trace!("Adding interaction {:?}", interaction);
        println!("current pact{:?}", self);
        println!("adding interaction {:?}", interaction);
        self.pact.add_interaction(interaction).unwrap();
        println!("new pact{:?}", self);
        self
    }
just to print what is happening with the pactbuilder. it looks like the interaction isn’t being saved
Copy code
current pactPactBuilder { pact: RequestResponsePact { consumer: Consumer { name: "test_two_interactions_consumer" }, provider: Provider { name: "test_two_interactions_provider" }, interactions: [], metadata: {"pactRust": {"consumer": "0.10.1", "models": "1.0.0"}, "pactSpecification": {"version": "3.0.0"}}, specification_version: V3 }, output_dir: None }

adding interaction RequestResponseInteraction { id: None, description: "looks for something that doesn't exist", provider_states: [], request: Request { method: "POST", path: "/", query: None, headers: Some({"Content-Type": ["application/json"]}), body: Present(b"{\"key\":\"i_dont_exist\"}", Some(ContentType { main_type: "application", sub_type: "json", attributes: {}, suffix: None }), None), matching_rules: MatchingRules { rules: {BODY: MatchingRuleCategory { name: BODY, rules: {DocPath { path_tokens: [Root], expr: "$" }: RuleList { rules: [Type], rule_logic: And, cascaded: false }} }, PATH: MatchingRuleCategory { name: PATH, rules: {} }, HEADER: MatchingRuleCategory { name: HEADER, rules: {} }} }, generators: Generators { categories: {} } }, response: Response { status: 200, headers: Some({"Content-Type": ["application/json"]}), body: Present(b"{\"count\":0,\"results\":[]}", Some(ContentType { main_type: "application", sub_type: "json", attributes: {}, suffix: None }), None), matching_rules: MatchingRules { rules: {HEADER: MatchingRuleCategory { name: HEADER, rules: {} }, BODY: MatchingRuleCategory { name: BODY, rules: {} }} }, generators: Generators { categories: {} } } }

new pactPactBuilder { pact: RequestResponsePact { consumer: Consumer { name: "test_two_interactions_consumer" }, provider: Provider { name: "test_two_interactions_provider" }, interactions: [RequestResponseInteraction { id: None, description: "looks for something that doesn't exist", provider_states: [], request: Request { method: "POST", path: "/", query: None, headers: Some({"Content-Type": ["application/json"]}), body: Present(b"{\"key\":\"i_dont_exist\"}", Some(ContentType { main_type: "application", sub_type: "json", attributes: {}, suffix: None }), None), matching_rules: MatchingRules { rules: {BODY: MatchingRuleCategory { name: BODY, rules: {DocPath { path_tokens: [Root], expr: "$" }: RuleList { rules: [Type], rule_logic: And, cascaded: false }} }, PATH: MatchingRuleCategory { name: PATH, rules: {} }, HEADER: MatchingRuleCategory { name: HEADER, rules: {} }} }, generators: Generators { categories: {} } }, response: Response { status: 200, headers: Some({"Content-Type": ["application/json"]}), body: Present(b"{\"count\":0,\"results\":[]}", Some(ContentType { main_type: "application", sub_type: "json", attributes: {}, suffix: None }), None), matching_rules: MatchingRules { rules: {HEADER: MatchingRuleCategory { name: HEADER, rules: {} }, BODY: MatchingRuleCategory { name: BODY, rules: {} }} }, generators: Generators { categories: {} } } }], metadata: {"pactRust": {"consumer": "0.10.1", "models": "1.0.0"}, "pactSpecification": {"version": "3.0.0"}}, specification_version: V3 }, output_dir: None }
current pactPactBuilder { pact: RequestResponsePact { consumer: Consumer { name: "test_two_interactions_consumer" }, provider: Provider { name: "test_two_interactions_provider" }, interactions: [], metadata: {"pactRust": {"consumer": "0.10.1", "models": "1.0.0"}, "pactSpecification": {"version": "3.0.0"}}, specification_version: V3 }, output_dir: None }

adding interaction RequestResponseInteraction { id: None, description: "looks for something that exists", provider_states: [], request: Request { method: "POST", path: "/", query: None, headers: Some({"Content-Type": ["application/json"]}), body: Present(b"{\"key\":\"i_exist\"}", Some(ContentType { main_type: "application", sub_type: "json", attributes: {}, suffix: None }), None), matching_rules: MatchingRules { rules: {BODY: MatchingRuleCategory { name: BODY, rules: {DocPath { path_tokens: [Root], expr: "$" }: RuleList { rules: [Type], rule_logic: And, cascaded: false }} }, PATH: MatchingRuleCategory { name: PATH, rules: {} }, HEADER: MatchingRuleCategory { name: HEADER, rules: {} }} }, generators: Generators { categories: {} } }, response: Response { status: 200, headers: Some({"Content-Type": ["application/json"]}), body: Present(b"{\"count\":1,\"results\":[\"i_exist\"]}", Some(ContentType { main_type: "application", sub_type: "json", attributes: {}, suffix: None }), None), matching_rules: MatchingRules { rules: {BODY: MatchingRuleCategory { name: BODY, rules: {} }, HEADER: MatchingRuleCategory { name: HEADER, rules: {} }} }, generators: Generators { categories: {} } } }

new pactPactBuilder { pact: RequestResponsePact { consumer: Consumer { name: "test_two_interactions_consumer" }, provider: Provider { name: "test_two_interactions_provider" }, interactions: [RequestResponseInteraction { id: None, description: "looks for something that exists", provider_states: [], request: Request { method: "POST", path: "/", query: None, headers: Some({"Content-Type": ["application/json"]}), body: Present(b"{\"key\":\"i_exist\"}", Some(ContentType { main_type: "application", sub_type: "json", attributes: {}, suffix: None }), None), matching_rules: MatchingRules { rules: {BODY: MatchingRuleCategory { name: BODY, rules: {DocPath { path_tokens: [Root], expr: "$" }: RuleList { rules: [Type], rule_logic: And, cascaded: false }} }, PATH: MatchingRuleCategory { name: PATH, rules: {} }, HEADER: MatchingRuleCategory { name: HEADER, rules: {} }} }, generators: Generators { categories: {} } }, response: Response { status: 200, headers: Some({"Content-Type": ["application/json"]}), body: Present(b"{\"count\":1,\"results\":[\"i_exist\"]}", Some(ContentType { main_type: "application", sub_type: "json", attributes: {}, suffix: None }), None), matching_rules: MatchingRules { rules: {BODY: MatchingRuleCategory { name: BODY, rules: {} }, HEADER: MatchingRuleCategory { name: HEADER, rules: {} }} }, generators: Generators { categories: {} } } }], metadata: {"pactRust": {"consumer": "0.10.1", "models": "1.0.0"}, "pactSpecification": {"version": "3.0.0"}}, specification_version: V3 }, output_dir: None }
it’s probably a mac os specific issue. but it passed on your workflow here So i’m a bit confused. I’ve removed rust and installed it again and nothing. FYI
Copy code
pact-reference/rust on  master [!?] via 🦀 v1.65.0 on ☁️ 
❯ rustup -V
rustup 1.25.1 (bb60b1e89 2022-07-12)
info: This is the version for the rustup toolchain manager, not the rustc compiler.
info: The currently active `rustc` version is `rustc 1.65.0 (897e37553 2022-11-02)`

pact-reference/rust on  master [!?] via 🦀 v1.65.0 on ☁️  
❯ rustc --version
rustc 1.65.0 (897e37553 2022-11-02)

pact-reference/rust on  master [!?] via 🦀 v1.65.0 on ☁️
u
It works ok on OSX for me, so that might not be the issue
Can you run it with trace logging, and send those though
RUST_LOG=trace cargo test test_two_interactions
Maybe create a Github issue with the logs