Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic date in request #495

Closed
onno-amsterdam opened this issue Mar 26, 2024 · 3 comments
Closed

Dynamic date in request #495

onno-amsterdam opened this issue Mar 26, 2024 · 3 comments

Comments

@onno-amsterdam
Copy link

Hi Pact-net,
I have a question I hope you can help with.

In my contract I have this Post request body:

{
"startDate": "2024-04-01"
}

The provider is expecting and only accepting a date in the future. Is there a way to make sure the date in the contract is always in the future? Of course I can work with a date that's far in the future but still that's not as robust as I would hope.

Hope you can help & thank you!

@adamrodger
Copy link
Contributor

Pact is about testing the "shape" of the data rather than any business logic. That means testing that the date is a string and matches a particular format (e.g. via checking against a regex).

The canonical example is writing a contract for a service which allows you to reset your password. Typically passwords have certain requirements such as "must contain a capital letter" etc.

The reason you don't test the logic is that the logic can change over time but the contract won't. The password will always be a string, but what's considered a valid password will change. The contract should therefore only ever confirm that the password is a string.

You use things like provider states and stubs on the provider end to configure the provider to respond appropriately. For example you may use a provider state of "given the password is valid" which configures the provider to accept the next submitted password, or "given the password is invalid" to reject it (e.g. by returning a HTTP 400 response).

So yeah, that's what you need to do here - use provider states to configure the provider to accept the date in one interaction and reject it in another interaction so you can verify the response in both cases.

@adamrodger adamrodger closed this as not planned Won't fix, can't repro, duplicate, stale Mar 26, 2024
@onno-amsterdam
Copy link
Author

onno-amsterdam commented Mar 26, 2024 via email

@mefellows
Copy link
Member

Adam - in the spec there is a ProviderState generator that allows the provider to replace a dynamic value like this during verification:

matcher Spec Version example configuration description
ProviderState V4 {"expression": "/api/user/${id}", type": "ProviderState"} Generates a value that is looked up from the provider state context using the given expression

It's designed for this very purpose.

It is technically supported by the V3 spec (via the extendable generators feature), and is documented in the V4 spec here: https://github.com/pact-foundation/pact-specification/tree/version-4?tab=readme-ov-file#supported-generators

The compatibility suite test case is here: https://github.com/pact-foundation/pact-compatibility-suite/blob/4a3c411cd5ae23b1491b9e89619ed1bbc7558df1/features/V3/http_generators.feature#L6

This blog post elaborates on the use case: https://pactflow.io/blog/injecting-values-from-provider-states/.

I would suggest we re-open this as a feature enhancement for the V4 implementation, or create a separate tracking issue for the features in V4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants