Closed mario-martinez-se closed 5 months ago
Are you able to provide the test that generates that Pact file?
Moving this to Pact-JS, as the header name is being set incorrectly
Are you able to provide the test that generates that Pact file?
Sure, this is the test:
test(
'200 status for bookings count by userId',
async () => {
const expectedBody: any = bookingsCountResponseBody;
const interaction: V3Interaction = {
states: [
{
description: `A restClient exists`,
},
{
description: `A user exists`,
parameters: { email: 'contract@mail.com' },
},
],
uponReceiving: 'a request for bookings count',
withRequest: {
method: 'GET',
headers: {
'se-api-token': fromProviderState(`\${seApiToken}`, '15123-234234-234asd'),
'se-token': fromProviderState(`\${seToken}`, 'ABC123'),
},
path: fromProviderState(
`/v4/users/\${userId}/bookings/count`,
`/v4/users/1234/bookings/count`
),
},
willRespondWith: {
body: expectedBody,
headers: {
'Content-Type': 'application/json; charset=utf-8',
},
status: 200,
},
};
await provider.addInteraction(interaction);
await provider.executeTest(async mockServer => {
const service: MockClient = new MockClient(mockServer.url);
await service.getBookingsCount();
flashSalesApi['get'] = jest.fn<Promise<any>, [string]>(async () => bookingsCountMock);
const dataFromAPI: IBookingsCount = await flashSalesApi.getBookingsCount('7777777');
expect(bookingsCountMock).toEqual(
(reify(expectedBody) as unknown) as IFlashsalesBookingsCount
);
expect(dataFromAPI).toEqual(
bookingCountFromApi((reify(expectedBody) as unknown) as IFlashsalesBookingsCount)
);
});
},
timeout
);
And this is the generated Pact file:
{
"consumer": {
"name": "Consumer"
},
"interactions": [
{
"description": "a request for bookings count",
"providerStates": [
{
"name": "A restClient exists"
},
{
"name": "A user exists",
"params": {
"email": "contract@mail.com"
}
}
],
"request": {
"generators": {
"header": {
"$['se-api-token'][0]": {
"expression": "${seApiToken}",
"type": "ProviderState"
},
"$['se-token'][0]": {
"expression": "${seToken}",
"type": "ProviderState"
}
},
"path": {
"expression": "/v4/users/${userId}/bookings/count",
"type": "ProviderState"
}
},
"headers": {
"se-api-token": "15123-234234-234asd",
"se-token": "ABC123"
},
"matchingRules": {
"header": {
"$['se-api-token'][0]": {
"combine": "AND",
"matchers": [
{
"match": "type"
}
]
},
"$['se-token'][0]": {
"combine": "AND",
"matchers": [
{
"match": "type"
}
]
}
},
"path": {
"combine": "AND",
"matchers": [
{
"match": "type"
}
]
}
},
"method": "GET",
"path": "/v4/users/1234/bookings/count"
},
"response": {
"body": {
"cancelled": 1,
"past": 2,
"upcoming": 3
},
"headers": {
"Content-Type": "application/json; charset=utf-8"
},
"matchingRules": {
"body": {
"$.cancelled": {
"combine": "AND",
"matchers": [
{
"match": "integer"
}
]
},
"$.past": {
"combine": "AND",
"matchers": [
{
"match": "integer"
}
]
},
"$.upcoming": {
"combine": "AND",
"matchers": [
{
"match": "integer"
}
]
}
},
"header": {}
},
"status": 200
}
}
],
"metadata": {
"pact-js": {
"version": "11.0.2"
},
"pactRust": {
"ffi": "0.4.0",
"models": "1.0.4"
},
"pactSpecification": {
"version": "3.0.0"
}
},
"provider": {
"name": "Provider"
}
}
Also, from package.json
:
"@pact-foundation/pact": "^11.0.2",
This should be fixed in the latest pact-core
package. An update of that transitive dependency or fresh install of Pact should pull it in.
I have a pact that looks like (excerpt):
When I run the tests for the provider, the status change url gets called as expected and it returns the required values,
userId
andseToken
. These values are properly interpolated in the path, so my server receives a hit in the right url (/v4/users/${userId}/bookings/count
), but the headers are not properly set in the request, so in my server logs I see:If I modify my pact file to be