Skip to content

Commit

Permalink
fix: Update compatibility-suite after fix for #484
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed Jan 30, 2025
1 parent a7f2c06 commit 2c51fb1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
38 changes: 38 additions & 0 deletions compatibility-suite/tests/v3_steps/http_matching.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::fs::File;
use std::io::BufReader;

Expand All @@ -12,6 +13,7 @@ use pact_models::interaction::Interaction;
use pact_models::matchingrules::matchers_from_json;
use pact_models::pact::Pact;
use pact_models::prelude::RequestResponseInteraction;
use pact_models::query_strings::parse_query_string;
use pact_models::request::Request;
use pact_models::sync_pact::RequestResponsePact;
use regex::Regex;
Expand Down Expand Up @@ -58,6 +60,17 @@ fn an_expected_request_configured_with_the_following(world: &mut V3World, step:
setup_body(body, &mut world.expected_request, data.get("content type").map(|ct| ct.as_str()));
}

if let Some(query) = data.get("query") {
world.expected_request.query = parse_query_string(query);
}

if let Some(headers) = data.get("headers") {
if !headers.is_empty() {
let headers = parse_headers(headers);
world.expected_request.headers = Some(headers);
}
}

if let Some(value) = data.get("matching rules") {
let json: Value = if value.starts_with("JSON:") {
serde_json::from_str(value.strip_prefix("JSON:").unwrap_or(value).trim()).unwrap()
Expand All @@ -75,6 +88,18 @@ fn an_expected_request_configured_with_the_following(world: &mut V3World, step:
}
}

fn parse_headers(headers: &str) -> HashMap<String, Vec<String>> {
headers.split(",")
.map(|header| {
let key_value = header.strip_prefix("'").unwrap_or(header)
.strip_suffix("'").unwrap_or(header)
.splitn(2, ":")
.map(|v| v.trim())
.collect::<Vec<_>>();
(key_value[0].to_string(), parse_header(key_value[0], key_value[1]))
}).collect()
}

#[given(expr = "a request is received with the following:")]
fn a_request_is_received_with_the_following(world: &mut V3World, step: &Step) {
let mut request = Request::default();
Expand All @@ -90,6 +115,17 @@ fn a_request_is_received_with_the_following(world: &mut V3World, step: &Step) {
if let Some(body) = data.get("body") {
setup_body(body, &mut request, data.get("content type").map(|ct| ct.as_str()));
}

if let Some(query) = data.get("query") {
request.query = parse_query_string(query);
}

if let Some(headers) = data.get("headers") {
if !headers.is_empty() {
let headers = parse_headers(headers);
request.headers = Some(headers);
}
}
}
world.received_requests.push(request);
}
Expand All @@ -104,6 +140,8 @@ fn the_following_requests_are_received(world: &mut V3World, step: &Step) {
if let Some(field) = headers.get(index) {
match field.as_str() {
"body" => setup_body(value, &mut request, None),
"query" => request.query = parse_query_string(value),
"headers" => request.headers = Some(parse_headers(value)),
_ => {}
}
}
Expand Down
6 changes: 3 additions & 3 deletions compatibility-suite/tests/v4_steps/http_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ async fn a_provider_is_started_that_returns_the_response_from_interaction_with_t
for (index, value) in table.rows.get(1).unwrap().iter().enumerate() {
if let Some(field) = headers.get(index) {
match field.as_str() {
"status" => interaction.response.status = value.parse().unwrap(),
"headers" => {
"status" | "response" => interaction.response.status = value.parse().unwrap(),
"headers" | "response headers" => {
let headers = interaction.response.headers_mut();
let headers_to_add = value.split(",")
.map(|header| {
Expand All @@ -116,7 +116,7 @@ async fn a_provider_is_started_that_returns_the_response_from_interaction_with_t
}
}
},
"body" => {
"body" | "response body" => {
setup_body(value, &mut interaction.response, None);
},
_ => {}
Expand Down

0 comments on commit 2c51fb1

Please sign in to comment.