Paul Ologeh
11/14/2022, 12:50 AMlet 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 ?uglyog
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.Paul Ologeh
11/15/2022, 9:37 PMPaul Ologeh
11/15/2022, 9:39 PMuglyog
uglyog
Paul Ologeh
11/22/2022, 6:56 PMuglyog
Paul Ologeh
11/22/2022, 9:52 PMuglyog
Paul Ologeh
11/22/2022, 9:55 PM<http://pact_builder.rs|pact_builder.rs>
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
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 }
Paul Ologeh
11/22/2022, 9:59 PMpact-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 ☁️
uglyog
uglyog
RUST_LOG=trace cargo test test_two_interactions
uglyog