Siim Mardus
06/20/2024, 8:01 AMfromProviderState
function. Relevant issue from a year ago, which seems like it has been abandoned https://github.com/pact-foundation/pact-js/issues/1088
When I use fromProviderState
with request path, the mock server matches any request.
• fromProviderState('/users/${userId}', '/users/1234')
◦ This should mean that provider-side uses userId
to construct /users/<real-user-id>
and consumer side should match /users/1234
Expected behaviour:
• When I make request to /users/random-stuff
• Then test should fail, as the server should expect /users/1234
Actual behaviour:
• When I make request to /users/random-stuff
• Then test succeeds and server gives a response 🤯
I also tried to use equal
(equality matcher), as according to the docs, "`equal` - Value that must be equal to the example. This is mainly used to reset the matching rules which cascade." This seemed like a perfect candidate for this matcher, as we are matching inside the fromProviderState
🤷 I assumed that maybe it checked that /users/1234
and /users/random-stuff
are both strings.
• I tried: fromProviderState('/users/${userId}', equal('/users/1234'))
, but this still allowed /users/random
to be used...
⚠️ IMO this issue is quite pressing, because of two things:
• Getting path variables from provider state is super common use case, so using fromProviderState
is necessary
• Consumer side should not allow any data, when fromProviderState
is used, as it allows breaking the consumer side without breaking the tests. Simplified example:
◦ Initial JS API: `const fetchUsers(id) => fetch(/users/${id}
)`
◦ Test: fromProviderState('/users/${userId}', '/users/1234')
◦ Dev changes JS API: `const fetchUsers(id) => fetch(/users/changes/${id}
)`, breaking the contract
◦ Test still passes
Open to all help and suggestions 🙇. Maybe there is a workaround to achieve the same results or it's already fixed somewhere. Thanks!Siim Mardus
06/20/2024, 9:59 AM...
headers: {Authorization: `Bearer ${token}`}
...
Test:
...
Authorization: fromProviderState(`Bearer ${token}`, 'Bearer aa-bb-cc')
...
Dev changes JS API:
...
headers: {Authorization: `Bearer-no-more ${token}`}
...
Test still passes...Matt (pactflow.io / pact-js / pact-go)
Getting path variables from provider state is super common use case, so usingI would argue it’s not that common, at least, there are other ways to achieve this use case. But that’s not really the point, the software should work whether or not the feature is commonly used or not.is necessaryfromProviderState
Matt (pactflow.io / pact-js / pact-go)
Siim Mardus
06/20/2024, 12:27 PMSiim Mardus
06/20/2024, 12:31 PMpath
, I used an UUID in consumer:
• `path: `/users/${userId}``
... and then passing the userId
in the given block.
• .given('user with id exists', {userId})
... this ensures the same data in consumer and provider. Just the source is consumer, not provider.
But the case for auth is still very valid, as we want to use a real token on the provider side and a mock one on the consumer side.Matt (pactflow.io / pact-js / pact-go)