-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support integration json for form urlencoded
- Loading branch information
Showing
8 changed files
with
361 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
//! Form UrlEncoded matching support | ||
|
||
use serde_json::Value; | ||
use tracing::{debug, error, trace}; | ||
|
||
use pact_models::generators::Generators; | ||
use pact_models::matchingrules::MatchingRuleCategory; | ||
use pact_models::path_exp::DocPath; | ||
|
||
use crate::mock_server::bodies::process_json; | ||
|
||
/// Process a JSON body with embedded matching rules and generators | ||
pub fn process_form_urlencoded_json(body: String, matching_rules: &mut MatchingRuleCategory) -> String { | ||
trace!("process_form_urlencoded_json"); | ||
// @todo support generators in form_urlencoded_json, they are currently ignored due to the error 'Generators only support JSON and XML' | ||
let mut generators = Generators::default(); | ||
let json = process_json(body, matching_rules, &mut generators); | ||
debug!("form_urlencoded json: {json}"); | ||
let values: Value = serde_json::from_str(json.as_str()).unwrap(); | ||
debug!("form_urlencoded values: {values}"); | ||
let params = convert_json_value_to_query_params(values, matching_rules); | ||
debug!("form_urlencoded params: {:?}", params); | ||
serde_urlencoded::to_string(params).expect("could not serialize body to form urlencoded string") | ||
} | ||
|
||
type QueryParams = Vec<(String, Option<Value>)>; | ||
|
||
fn convert_json_value_to_query_params(value: Value, matching_rules: &mut MatchingRuleCategory) -> QueryParams { | ||
let mut params: QueryParams = vec![]; | ||
match value { | ||
Value::Object(map) => { | ||
for (key, val) in map.iter() { | ||
let path = &mut DocPath::root(); | ||
path.push_field(key); | ||
match val { | ||
Value::Null => { | ||
matching_rules.remove_rule(&path); | ||
error!("Value '{}' is not supported in form urlencoded. Matcher (if defined) is removed", val); | ||
}, | ||
Value::Bool(val) => { | ||
matching_rules.remove_rule(&path); | ||
error!("Value '{}' is not supported in form urlencoded. Matcher (if defined) is removed", val) | ||
}, | ||
Value::Number(_) => params.push((key.clone(), Some(val.clone()))), | ||
Value::String(_) => params.push((key.clone(), Some(val.clone()))), | ||
Value::Array(vec) => { | ||
for (index, val) in vec.iter().enumerate() { | ||
let path = &mut path.clone(); | ||
path.push_index(index); | ||
match val { | ||
Value::Null => { | ||
matching_rules.remove_rule(&path); | ||
error!("Value '{}' is not supported in form urlencoded. Matcher (if defined) is removed", val); | ||
}, | ||
Value::Bool(val) => { | ||
matching_rules.remove_rule(&path); | ||
error!("Value '{}' is not supported in form urlencoded. Matcher (if defined) is removed", val); | ||
}, | ||
Value::Number(_) => params.push((key.clone(), Some(val.clone()))), | ||
Value::String(_) => params.push((key.clone(), Some(val.clone()))), | ||
Value::Array(val) => { | ||
matching_rules.remove_rule(&path); | ||
error!("Value '{:?}' is not supported in form urlencoded. Matcher (if defined) is removed", val); | ||
}, | ||
Value::Object(val) => { | ||
matching_rules.remove_rule(&path); | ||
error!("Value '{:?}' is not supported in form urlencoded. Matcher (if defined) is removed", val); | ||
}, | ||
} | ||
} | ||
}, | ||
Value::Object(val) => { | ||
matching_rules.remove_rule(&path); | ||
error!("Value '{:?}' is not supported in form urlencoded. Matcher (if defined) is removed", val); | ||
}, | ||
} | ||
} | ||
}, | ||
_ => () | ||
} | ||
params | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use expectest::prelude::*; | ||
use rstest::rstest; | ||
use serde_json::json; | ||
|
||
use pact_models::matchingrules_list; | ||
use pact_models::matchingrules::{MatchingRule, MatchingRuleCategory}; | ||
use pact_models::matchingrules::expressions::{MatchingRuleDefinition, ValueType}; | ||
|
||
use super::*; | ||
|
||
#[rstest] | ||
#[case(json!({ "": "empty key" }), vec![("".to_string(), Some(json!("empty key")))])] | ||
#[case(json!({ "": ["first", "second", "third"] }), vec![("".to_string(), Some(json!("first"))), ("".to_string(), Some(json!("second"))), ("".to_string(), Some(json!("third")))])] | ||
#[case(json!({ "number_value": 123 }), vec![("number_value".to_string(), Some(json!(123)))])] | ||
#[case(json!({ "string_value": "hello world" }), vec![("string_value".to_string(), Some(json!("hello world")))])] | ||
#[case( | ||
json!({ "array_values": [null, 234, "example text", {"key": "value"}, ["value 1", "value 2"]] }), | ||
vec![ | ||
("array_values".to_string(), Some(json!(234))), | ||
("array_values".to_string(), Some(json!("example text"))), | ||
], | ||
)] | ||
#[case(json!({ "null_value": null }), vec![])] | ||
#[case(json!({ "false": false }), vec![])] | ||
#[case(json!({ "true": true }), vec![])] | ||
#[case(json!({ "array_of_null": [null] }), vec![])] | ||
#[case(json!({ "array_of_false": [false] }), vec![])] | ||
#[case(json!({ "array_of_true": [true] }), vec![])] | ||
#[case(json!({ "array_of_objects": [{ "key": "value" }] }), vec![])] | ||
#[case(json!({ "array_of_arrays": [["value 1", "value 2"]] }), vec![])] | ||
#[case(json!({ "object_value": { "key": "value" } }), vec![])] | ||
fn convert_json_value_to_query_params_test(#[case] json: Value, #[case] result: QueryParams) { | ||
let mut matching_rules = MatchingRuleCategory::empty("body"); | ||
expect!(convert_json_value_to_query_params(json, &mut matching_rules)).to(be_equal_to(result)); | ||
expect!(matching_rules).to(be_equal_to(matchingrules_list!{"body"; "$" => []})); | ||
} | ||
|
||
#[rstest] | ||
#[case(json!({ "": "empty key" }), "=empty+key", matchingrules_list!{"body"; "$" => []})] | ||
#[case(json!({ "": ["first", "second", "third"] }), "=first&=second&=third", matchingrules_list!{"body"; "$" => []})] | ||
#[case(json!({ "": { "pact:matcher:type": "includes", "value": "empty" } }), "", matchingrules_list!{"body"; "$" => []})] | ||
#[case(json!({ "number_value": -123.45 }), "number_value=-123.45".to_string(), matchingrules_list!{"body"; "$" => []})] | ||
#[case(json!({ "string_value": "hello world" }), "string_value=hello+world".to_string(), matchingrules_list!{"body"; "$" => []})] | ||
#[case( | ||
json!({ "array_values": [null, 234, "example text", {"key": "value"}, ["value 1", "value 2"]] }), | ||
"array_values=234&array_values=example+text".to_string(), | ||
matchingrules_list!{"body"; "$" => []} | ||
)] | ||
#[case(json!({ "null_value": null }), "".to_string(), matchingrules_list!{"body"; "$" => []})] | ||
#[case(json!({ "null_value_with_matcher": { "pact:matcher:type": "null" } }), "".to_string(), matchingrules_list!{"body"; "$" => []})] | ||
#[case( | ||
json!({ "number_value_with_matcher": { "pact:matcher:type": "number", "min": 0, "max": 10, "value": 123 } }), | ||
"number_value_with_matcher=123".to_string(), | ||
matchingrules_list!{"body"; "$.number_value_with_matcher" => [MatchingRule::Number]} | ||
)] | ||
#[case( | ||
json!({ "number_value_with_matcher_and_generator": { "pact:matcher:type": "number", "pact:generator:type": "RandomInt", "min": 0, "max": 10, "value": 123 } }), | ||
"number_value_with_matcher_and_generator=123".to_string(), | ||
matchingrules_list!{"body"; "$.number_value_with_matcher_and_generator" => [MatchingRule::Number]} | ||
)] | ||
// Missing value => null will be used => but it is not supported, so matcher is removed. | ||
#[case( | ||
json!({ "number_matcher_only": { "pact:matcher:type": "number", "min": 0, "max": 10 } }), | ||
"".to_string(), | ||
matchingrules_list!{"body"; "$" => []} | ||
)] | ||
#[case( | ||
json!({ "string_value_with_matcher_and_generator": { "pact:matcher:type": "type", "value": "some string", "pact:generator:type": "RandomString", "size": 15 } }), | ||
"string_value_with_matcher_and_generator=some+string".to_string(), | ||
matchingrules_list!{"body"; "$.string_value_with_matcher_and_generator" => [MatchingRule::Type]} | ||
)] | ||
#[case( | ||
json!({ "string_value_with_matcher": { "pact:matcher:type": "type", "value": "some string", "size": 15 } }), | ||
"string_value_with_matcher=some+string".to_string(), | ||
matchingrules_list!{"body"; "$.string_value_with_matcher" => [MatchingRule::Type]} | ||
)] | ||
#[case( | ||
json!({ "array_values_with_matcher": { "pact:matcher:type": "eachValue", "value": ["string value"], "rules": [{ "pact:matcher:type": "type", "value": "string" }] } }), | ||
"array_values_with_matcher=string+value".to_string(), | ||
matchingrules_list!{"body"; "$.array_values_with_matcher" => [MatchingRule::EachValue(MatchingRuleDefinition::new("[\"string value\"]".to_string(), ValueType::Unknown, MatchingRule::Type, None))]} | ||
)] | ||
#[case(json!({ "false": false }), "".to_string(), matchingrules_list!{"body"; "$" => []})] | ||
#[case(json!({ "true": true }), "".to_string(), matchingrules_list!{"body"; "$" => []})] | ||
#[case(json!({ "array_of_false": [false] }), "".to_string(), matchingrules_list!{"body"; "$" => []})] | ||
#[case(json!({ "array_of_true": [true] }), "".to_string(), matchingrules_list!{"body"; "$" => []})] | ||
#[case(json!({ "array_of_objects": [{ "key": "value" }] }), "".to_string(), matchingrules_list!{"body"; "$" => []})] | ||
#[case(json!({ "array_of_arrays": [["value 1", "value 2"]] }), "".to_string(), matchingrules_list!{"body"; "$" => []})] | ||
#[case(json!({ "object_value": { "key": "value" } }), "".to_string(), matchingrules_list!{"body"; "$" => []})] | ||
#[case(json!( | ||
{ "unsupported_value_with_matcher": { "pact:matcher:type": "boolean", "value": true } }), | ||
"".to_string(), | ||
matchingrules_list!{"body"; "$" => []} | ||
)] | ||
fn process_form_urlencoded_json_test(#[case] json: Value, #[case] result: String, #[case] expected_matching_rules: MatchingRuleCategory) { | ||
let mut matching_rules = MatchingRuleCategory::empty("body"); | ||
expect!(process_form_urlencoded_json(json.to_string(), &mut matching_rules)).to(be_equal_to(result)); | ||
expect!(matching_rules).to(be_equal_to(expected_matching_rules)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.