Closed NikZak closed 1 year ago
/jira ticket
👋 Thanks, Jira [PACT-530] ticket created.
The issue is that you have specified the PactSpecification as V4, but you are using the V3 model classes to read and write the Pact file. While this is an issue (it is probably converting the formats and losing information), setting the spec version to V3 does not have this issue.
There are already functions you can use that can read and write the Pact file correctly, taking into account the format. See https://docs.rs/pact_models/1.0.3/pact_models/pact/fn.read_pact.html and https://docs.rs/pact_models/1.0.3/pact_models/pact/fn.write_pact.html.
Example:
let pact_path = PathBuf::from("pact.json");
write_pact(pact.boxed(), &pact_path, PactSpecification::V3, true).unwrap();
// read pact from file
let pact_from_file = read_pact(&pact_path).unwrap().as_request_response_pact().unwrap();
println!("Pact just from file: {pact_from_file:?}");
Or if you want to use V4 format:
let pact_path = PathBuf::from("pact.json");
write_pact(pact.boxed(), &pact_path, PactSpecification::V4, true).unwrap();
// read pact from file
let pact_from_file = read_pact(&pact_path).unwrap().as_v4_pact().unwrap();
println!("Pact just from file: {pact_from_file:?}");
Thank you for your prompt response! It works! Do you recommend using V3 or V4 pact specification? And could you point me to the docs describing differences between those? Or briefly describe it here
Do I understand correctly that RequestResponsePact structure can only be used for V3 and for V4 the structure is V4Pact?
RequestResponsePact has an attribute PactSpecification which can take V4 as value. If it can't then it should probably be written somewhere or checks added. RequestResponsePact
You can also make some attributes of RequestResponsePact private then ppl would not be able to directly fill them as I did. Only through methods
All good suggestions.
V4 is newer and not as well supported as V3, but supports more types of workflows (like gRPC and Websockets). It depends on how you want to share your Pact files. From when you are doing, V3 will work fine for you. You can read up on the differences at https://github.com/pact-foundation/pact-specification/tree/version-3 and https://github.com/pact-foundation/pact-specification/tree/version-4
Eventually I did this repo to solve the initial problem https://github.com/NikZak/pact-proxy-rs
Some context first (not relevant). I want to make testing of my applications faster so I want to exclude fetching data from web during testing. The way to do it is fetch data first time, save the response and then if there is saved request/response than match against the saved data. I decided to go with PACT as it seems that you are solving similar problem and have done a lot of work about how to save and read the http requests/response.
Problem. I get the response from the web. Make a pact object. Save it. Then read it and it is not same in some important aspects. E.g 1) There are added backslashes for escaped characters 2) The request query is not saved.
Here is an excerpt of a minimal reproducible example:
And here is the output:
The added backslashes is a bigger nuisance than missing query string.
Probably I am using wrong api, so leaving it for the experts
The whole minimal reproducible example is a bit long so I placed it here. It is not compiling as you need to install dependencies: