|
| 1 | +//! Builder for application/x-www-form-urlencoded bodies |
| 2 | +
|
| 3 | +use bytes::Bytes; |
| 4 | +use itertools::Itertools; |
| 5 | +use pact_models::content_types::ContentType; |
| 6 | +use pact_models::path_exp::DocPath; |
| 7 | + |
| 8 | +use crate::engine::{build_matching_rule_node, ExecutionPlanNode, NodeValue}; |
| 9 | +use crate::engine::bodies::PlanBodyBuilder; |
| 10 | +use crate::engine::context::PlanMatchingContext; |
| 11 | + |
| 12 | +/// Plan builder for application/x-www-form-urlencoded bodies |
| 13 | +#[derive(Clone, Debug)] |
| 14 | +pub struct FormUrlencodedPlanBuilder; |
| 15 | + |
| 16 | +impl FormUrlencodedPlanBuilder { |
| 17 | + /// Create a new instance |
| 18 | + pub fn new() -> Self { |
| 19 | + FormUrlencodedPlanBuilder {} |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +impl PlanBodyBuilder for FormUrlencodedPlanBuilder { |
| 24 | + fn supports_type(&self, content_type: &ContentType) -> bool { |
| 25 | + content_type.base_type() == "application/x-www-form-urlencoded" |
| 26 | + } |
| 27 | + |
| 28 | + fn build_plan(&self, content: &Bytes, context: &PlanMatchingContext) -> anyhow::Result<ExecutionPlanNode> { |
| 29 | + let expected_form: Vec<(String, String)> = serde_urlencoded::from_bytes(content) |
| 30 | + .map_err(|e| anyhow::anyhow!("Failed to parse form-urlencoded body: {}", e))?; |
| 31 | + |
| 32 | + let mut params: std::collections::HashMap<String, Vec<String>> = std::collections::HashMap::new(); |
| 33 | + for (key, value) in expected_form { |
| 34 | + params.entry(key).or_default().push(value); |
| 35 | + } |
| 36 | + |
| 37 | + let mut body_node = ExecutionPlanNode::action("tee"); |
| 38 | + body_node.add( |
| 39 | + ExecutionPlanNode::action("form:parse") |
| 40 | + .add(ExecutionPlanNode::resolve_value(DocPath::new_unwrap("$.body"))) |
| 41 | + ); |
| 42 | + |
| 43 | + let root_path = DocPath::root(); |
| 44 | + let mut root_node = ExecutionPlanNode::container(&root_path); |
| 45 | + |
| 46 | + let keys = params.keys().cloned().sorted().collect_vec(); |
| 47 | + |
| 48 | + if !params.is_empty() { |
| 49 | + for key in &keys { |
| 50 | + let values = params.get(key).unwrap(); |
| 51 | + let key_path = root_path.join(key); |
| 52 | + |
| 53 | + let expected_value = if values.len() == 1 { |
| 54 | + NodeValue::STRING(values[0].clone()) |
| 55 | + } else { |
| 56 | + NodeValue::SLIST(values.clone()) |
| 57 | + }; |
| 58 | + |
| 59 | + let mut item_node = ExecutionPlanNode::container(&key_path); |
| 60 | + let mut presence_check = ExecutionPlanNode::action("if"); |
| 61 | + presence_check.add( |
| 62 | + ExecutionPlanNode::action("check:exists") |
| 63 | + .add(ExecutionPlanNode::resolve_current_value(&key_path)) |
| 64 | + ); |
| 65 | + |
| 66 | + if context.matcher_is_defined(&key_path) { |
| 67 | + let matchers = context.select_best_matcher(&key_path); |
| 68 | + item_node.add(ExecutionPlanNode::annotation( |
| 69 | + format!("{} {}", key, matchers.generate_description(true)) |
| 70 | + )); |
| 71 | + presence_check.add(build_matching_rule_node( |
| 72 | + &ExecutionPlanNode::value_node(expected_value), |
| 73 | + &ExecutionPlanNode::resolve_current_value(&key_path), |
| 74 | + &matchers, |
| 75 | + true, |
| 76 | + context.config.show_types_in_errors |
| 77 | + )); |
| 78 | + } else { |
| 79 | + item_node.add(ExecutionPlanNode::annotation(format!("{}={}", key, expected_value))); |
| 80 | + presence_check.add( |
| 81 | + ExecutionPlanNode::action("match:equality") |
| 82 | + .add(ExecutionPlanNode::value_node(expected_value)) |
| 83 | + .add(ExecutionPlanNode::resolve_current_value(&key_path)) |
| 84 | + .add(ExecutionPlanNode::value_node(NodeValue::NULL)) |
| 85 | + .add(ExecutionPlanNode::value_node(context.config.show_types_in_errors)) |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + item_node.add(presence_check); |
| 90 | + root_node.add(item_node); |
| 91 | + } |
| 92 | + |
| 93 | + root_node.add( |
| 94 | + ExecutionPlanNode::action("expect:entries") |
| 95 | + .add(ExecutionPlanNode::value_node(NodeValue::SLIST(keys.clone()))) |
| 96 | + .add(ExecutionPlanNode::resolve_current_value(&root_path)) |
| 97 | + .add( |
| 98 | + ExecutionPlanNode::action("join") |
| 99 | + .add(ExecutionPlanNode::value_node("The following expected form parameters were missing: ")) |
| 100 | + .add( |
| 101 | + ExecutionPlanNode::action("join-with") |
| 102 | + .add(ExecutionPlanNode::value_node(", ")) |
| 103 | + .add( |
| 104 | + ExecutionPlanNode::splat() |
| 105 | + .add(ExecutionPlanNode::action("apply")) |
| 106 | + ) |
| 107 | + ) |
| 108 | + ) |
| 109 | + ); |
| 110 | + |
| 111 | + if !context.config.allow_unexpected_entries { |
| 112 | + root_node.add( |
| 113 | + ExecutionPlanNode::action("expect:only-entries") |
| 114 | + .add(ExecutionPlanNode::value_node(NodeValue::SLIST(keys.clone()))) |
| 115 | + .add(ExecutionPlanNode::resolve_current_value(&root_path)) |
| 116 | + .add( |
| 117 | + ExecutionPlanNode::action("join") |
| 118 | + .add(ExecutionPlanNode::value_node("The following form parameters were not expected: ")) |
| 119 | + .add( |
| 120 | + ExecutionPlanNode::action("join-with") |
| 121 | + .add(ExecutionPlanNode::value_node(", ")) |
| 122 | + .add( |
| 123 | + ExecutionPlanNode::splat() |
| 124 | + .add(ExecutionPlanNode::action("apply")) |
| 125 | + ) |
| 126 | + ) |
| 127 | + ) |
| 128 | + ); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + body_node.add(root_node); |
| 133 | + Ok(body_node) |
| 134 | + } |
| 135 | +} |
0 commit comments