pact-js
pact-js copied to clipboard
CORS option does not seem to function properly since V10.x.x
Software versions
Please provide at least OS and version of pact-js
- OS: Ubuntu 20.04.5 LTS
- Consumer Pact library: "@pact-foundation/pact": "^10.1.3"
- Node Version: v16.13.0
Issue Checklist
Please confirm the following:
- [x] I have upgraded to the latest
- [x] I have the read the FAQs in the Readme
- [x] I have triple checked, that there are no unhandled promises in my code and have read the section on intermittent test failures
- [x] I have set my log level to debug and attached a log file showing the complete request/response cycle
- [ ] For bonus points and virtual high fives, I have created a reproduceable git repository (see below) to illustrate the problem
Expected behaviour
Request matching on PUT request with the cors option set to true
Actual behaviour
Network error. As seen in the debug logs. An attempt is made to match the actual request "OPTIONS" to the expected request "PUT". Which results in a failed assertion.
Steps to reproduce
const provider = new PactV3({
port: 8082,
log: path.resolve(process.cwd(), "logs", "pact.log"),
dir: path.resolve(process.cwd(), "pacts"),
cors: true,
consumer: "application",
provider: "applicationAPI",
logLevel: "debug",
});
describe("serviceUnderTest", () => {
it("should validate PUT request", async () => {
await provider.addInteraction({
state: "update",
uponReceiving: "update",
withRequest: {
method: "PUT",
path: "/resource/1",
headers: {
"Content-Type": "application/json"
},
body: {}
}
});
await provider.executeTest(async () => {
await updateResource(requestDTO); // this is an axios put request
});
});
}
Relevant log files
+1 - I also have the same issue on DELETE calls when setting the flag cors: true
in the PactV3 options.
Identical for me, too, to the POST method
@mefellows Looks like the cors
option is never passed down to the core - and even if it was, the core doesn't pass it on to the binaries.
Yep. I had a look last week when this first was raised. I don't believe it's an option the core accepts, but has this on by default. We should remove the option from the interface altogether actually (but now .
@uglyog is my understanding right that CORS should be enabled by default for the call to pactffi_create_mock_server_for_pact
(see https://github.com/pact-foundation/pact-reference/blob/master/rust/pact_ffi/src/mock_server/mod.rs#L217)?
If it’s enabled by default, I don’t think it’s working :/
This is a complete aside, but state: "update"
isn't really how state is meant to be used - it's supposed to be used to describe the precondition for the interaction. Something like state: "resource with id=1 exists"
would be more idiomatic.
(of course, you may already know this and the state may just be there for your example).
Handling CORS pre-flight requests won't be enabled by default. It needs to be enabled when the mock server is started, but the option is not exposed via FFI.
@mefellows the new pactffi_create_mock_server_for_transport
function does take a config option which can include enabling CORS support.
Ok, it looks like it depends on which function you are using to start the mock server. pactffi_create_mock_server_for_pact
does enable CORS support, and I am assuming that is the one Pact-JS is using.
It does, see here: https://github.com/pact-foundation/pact-js-core/blob/master/src/consumer/index.ts
However, from the logs shared above it appears that the CORS support in the FFI is not working
Guys, can you please tell me if there are any updates on this?
Hi Maksym, apologies we haven't had a chance to look into this issue yet. My suspicion is that there is a bug in the FFI core.
It seems to be set for the CLI verifier but we need to trace how it gets set via the pact_ffi
package.
Just had a few minutes to look into this, quick update. It seems the CORS is enabled and is responding to the OPTIONS
requests:
...Responding to CORS pre-flight request
This code seems to be executing as expected.
Could you please share trace
level logs - that will let us see what HTTP headers actually responded with.
I suspect it's just some additional response headers missing or invalid to address this.
Hi,
The referenced server mock code doesn't return the expected headers when cors: true. If the below would be implemented this issue will be resolved. We are blocked from updating to 10.x.x because of this issue.
Based on jsdom xhr-utils headers source Line: 71,78,92
Headers we expect the mock server to return for OPTIONS request: Access-Control-Allow-Origin: * Access-Control-Allow-Credentials: true Access-Control-Allow-Headers: *
Headers we expect the mock server to return for the request after the OPTIONS request: Access-Control-Allow-Origin: * Access-Control-Allow-Credentials: true
We hope this helps resolving the issue, Best regards
This block should be returning headers on the OPTIONS call, so perhaps it's just the subsequent calls it's missing?
I'd appreciate seeing the trace logs for the OP though, because it's seemingly failing on the initial OPTIONS call.
This block should be returning headers on the OPTIONS call, so perhaps it's just the subsequent calls it's missing?
I'd appreciate seeing the trace logs for the OP though, because it's seemingly failing on the initial OPTIONS call.
Access-Control-Allow-Credentials is not present in that repository, so it will not fly anyway in our case. We didn't investigate that much, we only know that adding these headers manually in the xhr-utils code make v10.x.x work as expected.
When cors: true we expect the mock server / pact framework to handle OPTIONS requests for us. We have no problem with cors in our own server code, we know how to handle CORS related headers.
If talking about our test cases — all of our contract tests started failing after upgrading from the 9 to 10 version of the pact package.
"devDependencies": {
"@pact-foundation/pact": "^9.18.1" -> "^10.1.4",
"@pact-foundation/pact-node": "^10.17.2",
"@testing-library/jest-dom": "^5.16.3",
"@testing-library/react": "^11.2.6",
...
}
Update of the pact package I make with the following command:
npm i -S @pact-foundation/pact@latest --force
Below are logs, which are absolutely identical for each contract test file.
FAIL .../__tests__/TestService.contract.test.ts
● Console
console.error
Error: Cross origin http://localhost forbidden
at dispatchError (...\node_modules\jsdom\lib\jsdom\living\xhr\xhr-utils.js:63:19)
at validCORSHeaders (...\node_modules\jsdom\lib\jsdom\living\xhr\xhr-utils.js:75:5)
at validCORSPreflightHeaders (...\node_modules\jsdom\lib\jsdom\living\xhr\xhr-utils.js:89:8)
at Request.<anonymous> (...\node_modules\jsdom\lib\jsdom\living\xhr\xhr-utils.js:374:12)
at Request.emit (node:events:390:28)
at Request._processResponse (...\node_modules\jsdom\lib\jsdom\living\helpers\http-request.js:228:12)
at ClientRequest.<anonymous> (...\node_modules\jsdom\lib\jsdom\living\helpers\http-request.js:101:12)
at Object.onceWrapper (node:events:510:26)
at ClientRequest.emit (node:events:390:28)
at HTTPParser.parserOnIncomingClient [as onIncoming] (node:_http_client:623:27)
at HTTPParser.parserOnHeadersComplete (node:_http_common:128:17)
at Socket.socketOnData (node:_http_client:487:22)
at Socket.emit (node:events:390:28)
at addChunk (node:internal/streams/readable:315:12)
at readableAddChunk (node:internal/streams/readable:289:9)
at Socket.Readable.push (node:internal/streams/readable:228:10)
at TCP.onStreamRead (node:internal/stream_base_commons:199:23) undefined
at VirtualConsole.<anonymous> (node_modules/jsdom/lib/jsdom/virtual-console.js:29:45)
at dispatchError (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:66:53)
at validCORSHeaders (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:75:5)
at validCORSPreflightHeaders (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:89:8)
at Request.<anonymous> (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:374:12)
at Request._processResponse (node_modules/jsdom/lib/jsdom/living/helpers/http-request.js:228:12)
at ClientRequest.<anonymous> (node_modules/jsdom/lib/jsdom/living/helpers/http-request.js:101:12)
console.error
at Pact.Object.<anonymous>.Pact.verify (node_modules/@pact-foundation/src/httpPact/index.ts:211:15)
at Object.<anonymous> (node_modules/jest-pact/dist/pactWith.js:12:45)
console.error
Pact verification failed!
at Pact.Object.<anonymous>.Pact.verify (node_modules/@pact-foundation/src/httpPact/index.ts:212:15)
at Object.<anonymous> (node_modules/jest-pact/dist/pactWith.js:12:45)
console.error
Test failed for the following reasons:
Mock server failed with the following mismatches:
0) The following request was expected but not received:
Method: GET
Path: /keys/00ce3a6e-c350-4c17-a58a-da120c9cb63b
at Pact.Object.<anonymous>.Pact.verify (node_modules/@pact-foundation/src/httpPact/index.ts:213:15)
at Object.<anonymous> (node_modules/jest-pact/dist/pactWith.js:12:45)
● Pact between flow and test-keys › with 40000 ms timeout for Pact › Test API - Keys › returns a 401 unauthorized
expect(received).toBe(expected) // Object.is equality
Expected: 401
Received: undefined
3 | export const checkIfHttpError = (e: Error, expectedStatusCode: number) => {
4 | expect(axios.isAxiosError(e)).toBe(true);
> 5 | expect((e as AxiosError).response?.status).toBe(expectedStatusCode);
| ^
6 | };
7 |
at checkIfHttpError (../test/testsMatchers.ts:5:48)
at Object.<anonymous> (.../__tests__/TestService.contract.test.ts:54:17)
● Pact between flow and test-keys › with 40000 ms timeout for Pact › Test API - Keys › returns a 401 unauthorized
Pact verification failed - expected interactions did not match actual.
at new VerificationError (node_modules/@pact-foundation/pact/src/errors/verificationError.js:21:42)
at Pact.Object.<anonymous>.Pact.verify (node_modules/@pact-foundation/src/httpPact/index.ts:217:13)
at Object.<anonymous> (node_modules/jest-pact/dist/pactWith.js:12:45)
I temporarily removed all *.contract.test.ts
files, making changes that prevented compiling (removed specific type for willRespondWith.body
), but only in one test class (that I'm reverted), and started getting more expanded logs.
RUNS .../__tests__/TestService.contract.test.ts
2022-10-27T17:11:04.168338Z WARN ThreadId(02) pact_models::content_types: Failed to parse '{"value":"application/json; charset=utf-8","regex":"application\\/json; *charset=utf-8","pact:matcher:type":"regex"}' as a content type: mime parse error: an invalid token was
encountered, 7B at position 0
FAIL .../__tests__/TestService.contract.test.tson/json; charset=utf-8","regex":"application\\/json; *charset=utf-8","pact:matcher:type":"regex"}' as a content type: mime parse error: an invalid token was
● Console
console.error
Error: Cross origin http://localhost forbidden
at dispatchError (...\node_modules\jsdom\lib\jsdom\living\xhr\xhr-utils.js:63:19)
at validCORSHeaders (...\node_modules\jsdom\lib\jsdom\living\xhr\xhr-utils.js:75:5)
at validCORSPreflightHeaders (...\node_modules\jsdom\lib\jsdom\living\xhr\xhr-utils.js:89:8)
at Request.<anonymous> (...\node_modules\jsdom\lib\jsdom\living\xhr\xhr-utils.js:374:12)
at Request.emit (node:events:390:28)
at Request._processResponse (...\node_modules\jsdom\lib\jsdom\living\helpers\http-request.js:228:12)
at ClientRequest.<anonymous> (...\node_modules\jsdom\lib\jsdom\living\helpers\http-request.js:101:12)
at Object.onceWrapper (node:events:510:26)
at ClientRequest.emit (node:events:390:28)
at HTTPParser.parserOnIncomingClient [as onIncoming] (node:_http_client:623:27) undefined
at VirtualConsole.<anonymous> (node_modules/jsdom/lib/jsdom/virtual-console.js:29:45)
at dispatchError (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:66:53)
at validCORSHeaders (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:75:5)
at validCORSPreflightHeaders (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:89:8)
at Request.<anonymous> (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:374:12)
at Request._processResponse (node_modules/jsdom/lib/jsdom/living/helpers/http-request.js:228:12)
console.error
at Pact.Object.<anonymous>.Pact.verify (node_modules/@pact-foundation/src/httpPact/index.ts:211:15)
at Object.<anonymous> (node_modules/jest-pact/dist/pactWith.js:12:45)
console.error
Pact verification failed!
at Pact.Object.<anonymous>.Pact.verify (node_modules/@pact-foundation/src/httpPact/index.ts:212:15)
at Object.<anonymous> (node_modules/jest-pact/dist/pactWith.js:12:45)
console.error
Test failed for the following reasons:
Mock server failed with the following mismatches:
0) The following request was expected but not received:
Method: GET
Path: /keys/00ce3a6e-c350-4c17-a58a-da120c9cb63b
at Pact.Object.<anonymous>.Pact.verify (node_modules/@pact-foundation/src/httpPact/index.ts:213:15)
at Object.<anonymous> (node_modules/jest-pact/dist/pactWith.js:12:45)
console.error
Error: Cross origin http://localhost forbidden
at dispatchError (...\node_modules\jsdom\lib\jsdom\living\xhr\xhr-utils.js:63:19)
at validCORSHeaders (...\node_modules\jsdom\lib\jsdom\living\xhr\xhr-utils.js:75:5)
at validCORSPreflightHeaders (...\node_modules\jsdom\lib\jsdom\living\xhr\xhr-utils.js:89:8)
at Request.<anonymous> (...\node_modules\jsdom\lib\jsdom\living\xhr\xhr-utils.js:374:12)
at Request.emit (node:events:390:28)
at Request._processResponse (...\node_modules\jsdom\lib\jsdom\living\helpers\http-request.js:228:12)
at ClientRequest.<anonymous> (...\node_modules\jsdom\lib\jsdom\living\helpers\http-request.js:101:12)
at Object.onceWrapper (node:events:510:26)
at ClientRequest.emit (node:events:390:28)
at HTTPParser.parserOnIncomingClient [as onIncoming] (node:_http_client:623:27) undefined
at VirtualConsole.<anonymous> (node_modules/jsdom/lib/jsdom/virtual-console.js:29:45)
at dispatchError (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:66:53)
at validCORSHeaders (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:75:5)
at validCORSPreflightHeaders (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:89:8)
at Request.<anonymous> (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:374:12)
at Request._processResponse (node_modules/jsdom/lib/jsdom/living/helpers/http-request.js:228:12)
console.error
at Pact.Object.<anonymous>.Pact.verify (node_modules/@pact-foundation/src/httpPact/index.ts:211:15)
at Object.<anonymous> (node_modules/jest-pact/dist/pactWith.js:12:45)
console.error
Pact verification failed!
at Pact.Object.<anonymous>.Pact.verify (node_modules/@pact-foundation/src/httpPact/index.ts:212:15)
at Object.<anonymous> (node_modules/jest-pact/dist/pactWith.js:12:45)
console.error
Test failed for the following reasons:
Mock server failed with the following mismatches:
0) The following request was expected but not received:
Method: GET
Path: /keys/00ce3a6e-c350-4c17-a58a-da120c9cb63b
Headers:
Authorization: Bearer testToken
at Pact.Object.<anonymous>.Pact.verify (node_modules/@pact-foundation/src/httpPact/index.ts:213:15)
at Object.<anonymous> (node_modules/jest-pact/dist/pactWith.js:12:45)
console.error
Error: Cross origin http://localhost forbidden
at dispatchError (...\node_modules\jsdom\lib\jsdom\living\xhr\xhr-utils.js:63:19)
at validCORSHeaders (...\node_modules\jsdom\lib\jsdom\living\xhr\xhr-utils.js:75:5)
at validCORSPreflightHeaders (...\node_modules\jsdom\lib\jsdom\living\xhr\xhr-utils.js:89:8)
at Request.<anonymous> (...\node_modules\jsdom\lib\jsdom\living\xhr\xhr-utils.js:374:12)
at Request.emit (node:events:390:28)
at Request._processResponse (...\node_modules\jsdom\lib\jsdom\living\helpers\http-request.js:228:12)
at ClientRequest.<anonymous> (...\node_modules\jsdom\lib\jsdom\living\helpers\http-request.js:101:12)
at Object.onceWrapper (node:events:510:26)
at ClientRequest.emit (node:events:390:28)
at HTTPParser.parserOnIncomingClient [as onIncoming] (node:_http_client:623:27) undefined
at VirtualConsole.<anonymous> (node_modules/jsdom/lib/jsdom/virtual-console.js:29:45)
at dispatchError (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:66:53)
at validCORSHeaders (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:75:5)
at validCORSPreflightHeaders (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:89:8)
at Request.<anonymous> (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:374:12)
at Request._processResponse (node_modules/jsdom/lib/jsdom/living/helpers/http-request.js:228:12)
console.error
at Pact.Object.<anonymous>.Pact.verify (node_modules/@pact-foundation/src/httpPact/index.ts:211:15)
at Object.<anonymous> (node_modules/jest-pact/dist/pactWith.js:12:45)
console.error
Pact verification failed!
at Pact.Object.<anonymous>.Pact.verify (node_modules/@pact-foundation/src/httpPact/index.ts:212:15)
at Object.<anonymous> (node_modules/jest-pact/dist/pactWith.js:12:45)
console.error
Test failed for the following reasons:
Mock server failed with the following mismatches:
0) The following request was expected but not received:
Method: POST
Path: /keys
at Pact.Object.<anonymous>.Pact.verify (node_modules/@pact-foundation/src/httpPact/index.ts:213:15)
at Object.<anonymous> (node_modules/jest-pact/dist/pactWith.js:12:45)
console.error
Error: Cross origin http://localhost forbidden
at dispatchError (...\node_modules\jsdom\lib\jsdom\living\xhr\xhr-utils.js:63:19)
at validCORSHeaders (...\node_modules\jsdom\lib\jsdom\living\xhr\xhr-utils.js:75:5)
at validCORSPreflightHeaders (...\node_modules\jsdom\lib\jsdom\living\xhr\xhr-utils.js:89:8)
at Request.<anonymous> (...\node_modules\jsdom\lib\jsdom\living\xhr\xhr-utils.js:374:12)
at Request.emit (node:events:390:28)
at Request._processResponse (...\node_modules\jsdom\lib\jsdom\living\helpers\http-request.js:228:12)
at ClientRequest.<anonymous> (...\node_modules\jsdom\lib\jsdom\living\helpers\http-request.js:101:12)
at Object.onceWrapper (node:events:510:26)
at ClientRequest.emit (node:events:390:28)
at HTTPParser.parserOnIncomingClient [as onIncoming] (node:_http_client:623:27) undefined
at VirtualConsole.<anonymous> (node_modules/jsdom/lib/jsdom/virtual-console.js:29:45)
at dispatchError (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:66:53)
at validCORSHeaders (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:75:5)
at validCORSPreflightHeaders (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:89:8)
at Request.<anonymous> (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:374:12)
at Request._processResponse (node_modules/jsdom/lib/jsdom/living/helpers/http-request.js:228:12)
console.error
at Pact.Object.<anonymous>.Pact.verify (node_modules/@pact-foundation/src/httpPact/index.ts:211:15)
at Object.<anonymous> (node_modules/jest-pact/dist/pactWith.js:12:45)
console.error
Pact verification failed!
at Pact.Object.<anonymous>.Pact.verify (node_modules/@pact-foundation/src/httpPact/index.ts:212:15)
at Object.<anonymous> (node_modules/jest-pact/dist/pactWith.js:12:45)
console.error
Test failed for the following reasons:
Mock server failed with the following mismatches:
0) The following request was expected but not received:
Method: POST
Path: /keys
Headers:
Accept: application/json, text/plain, */*
Authorization: Bearer testToken
Content-Type: application/json
Body: {"key":"string","key... (69 length)
at Pact.Object.<anonymous>.Pact.verify (node_modules/@pact-foundation/src/httpPact/index.ts:213:15)
at Object.<anonymous> (node_modules/jest-pact/dist/pactWith.js:12:45)
console.error
Error: Cross origin http://localhost forbidden
at dispatchError (...\node_modules\jsdom\lib\jsdom\living\xhr\xhr-utils.js:63:19)
at validCORSHeaders (...\node_modules\jsdom\lib\jsdom\living\xhr\xhr-utils.js:75:5)
at validCORSPreflightHeaders (...\node_modules\jsdom\lib\jsdom\living\xhr\xhr-utils.js:89:8)
at Request.<anonymous> (...\node_modules\jsdom\lib\jsdom\living\xhr\xhr-utils.js:374:12)
at Request.emit (node:events:390:28)
at Request._processResponse (...\node_modules\jsdom\lib\jsdom\living\helpers\http-request.js:228:12)
at ClientRequest.<anonymous> (...\node_modules\jsdom\lib\jsdom\living\helpers\http-request.js:101:12)
at Object.onceWrapper (node:events:510:26)
at ClientRequest.emit (node:events:390:28)
at HTTPParser.parserOnIncomingClient [as onIncoming] (node:_http_client:623:27) undefined
at VirtualConsole.<anonymous> (node_modules/jsdom/lib/jsdom/virtual-console.js:29:45)
at dispatchError (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:66:53)
at validCORSHeaders (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:75:5)
at validCORSPreflightHeaders (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:89:8)
at Request.<anonymous> (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:374:12)
at Request._processResponse (node_modules/jsdom/lib/jsdom/living/helpers/http-request.js:228:12)
console.error
at Pact.Object.<anonymous>.Pact.verify (node_modules/@pact-foundation/src/httpPact/index.ts:211:15)
at Object.<anonymous> (node_modules/jest-pact/dist/pactWith.js:12:45)
console.error
Pact verification failed!
at Pact.Object.<anonymous>.Pact.verify (node_modules/@pact-foundation/src/httpPact/index.ts:212:15)
at Object.<anonymous> (node_modules/jest-pact/dist/pactWith.js:12:45)
console.error
Test failed for the following reasons:
Mock server failed with the following mismatches:
0) The following request was expected but not received:
Method: POST
Path: /keys
Headers:
Accept: application/json, text/plain, */*
Authorization: Bearer testToken
Content-Type: application/json
Body: {"key":"","key_name"... (69 length)
at Pact.Object.<anonymous>.Pact.verify (node_modules/@pact-foundation/src/httpPact/index.ts:213:15)
at Object.<anonymous> (node_modules/jest-pact/dist/pactWith.js:12:45)
console.error
Error: Cross origin http://localhost forbidden
at dispatchError (...\node_modules\jsdom\lib\jsdom\living\xhr\xhr-utils.js:63:19)
at validCORSHeaders (...\node_modules\jsdom\lib\jsdom\living\xhr\xhr-utils.js:75:5)
at validCORSPreflightHeaders (...\node_modules\jsdom\lib\jsdom\living\xhr\xhr-utils.js:89:8)
at Request.<anonymous> (...\node_modules\jsdom\lib\jsdom\living\xhr\xhr-utils.js:374:12)
at Request.emit (node:events:390:28)
at Request._processResponse (...\node_modules\jsdom\lib\jsdom\living\helpers\http-request.js:228:12)
at ClientRequest.<anonymous> (...\node_modules\jsdom\lib\jsdom\living\helpers\http-request.js:101:12)
at Object.onceWrapper (node:events:510:26)
at ClientRequest.emit (node:events:390:28)
at HTTPParser.parserOnIncomingClient [as onIncoming] (node:_http_client:623:27) undefined
at VirtualConsole.<anonymous> (node_modules/jsdom/lib/jsdom/virtual-console.js:29:45)
at dispatchError (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:66:53)
at validCORSHeaders (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:75:5)
at validCORSPreflightHeaders (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:89:8)
at Request.<anonymous> (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:374:12)
at Request._processResponse (node_modules/jsdom/lib/jsdom/living/helpers/http-request.js:228:12)
console.error
at Pact.Object.<anonymous>.Pact.verify (node_modules/@pact-foundation/src/httpPact/index.ts:211:15)
at Object.<anonymous> (node_modules/jest-pact/dist/pactWith.js:12:45)
console.error
Pact verification failed!
at Pact.Object.<anonymous>.Pact.verify (node_modules/@pact-foundation/src/httpPact/index.ts:212:15)
at Object.<anonymous> (node_modules/jest-pact/dist/pactWith.js:12:45)
console.error
Test failed for the following reasons:
Mock server failed with the following mismatches:
0) The following request was expected but not received:
Method: POST
Path: /keys
Headers:
Accept: application/json, text/plain, */*
Authorization: Bearer testToken
Content-Type: application/json
Body: {"key":"key","key_na... (56 length)
at Pact.Object.<anonymous>.Pact.verify (node_modules/@pact-foundation/src/httpPact/index.ts:213:15)
at Object.<anonymous> (node_modules/jest-pact/dist/pactWith.js:12:45)
console.error
Error: Cross origin http://localhost forbidden
at dispatchError (...\node_modules\jsdom\lib\jsdom\living\xhr\xhr-utils.js:63:19)
at validCORSHeaders (...\node_modules\jsdom\lib\jsdom\living\xhr\xhr-utils.js:75:5)
at validCORSPreflightHeaders (...\node_modules\jsdom\lib\jsdom\living\xhr\xhr-utils.js:89:8)
at Request.<anonymous> (...\node_modules\jsdom\lib\jsdom\living\xhr\xhr-utils.js:374:12)
at Request.emit (node:events:390:28)
at Request._processResponse (...\node_modules\jsdom\lib\jsdom\living\helpers\http-request.js:228:12)
at ClientRequest.<anonymous> (...\node_modules\jsdom\lib\jsdom\living\helpers\http-request.js:101:12)
at Object.onceWrapper (node:events:510:26)
at ClientRequest.emit (node:events:390:28)
at HTTPParser.parserOnIncomingClient [as onIncoming] (node:_http_client:623:27) undefined
at VirtualConsole.<anonymous> (node_modules/jsdom/lib/jsdom/virtual-console.js:29:45)
at dispatchError (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:66:53)
at validCORSHeaders (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:75:5)
at validCORSPreflightHeaders (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:89:8)
at Request.<anonymous> (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:374:12)
at Request._processResponse (node_modules/jsdom/lib/jsdom/living/helpers/http-request.js:228:12)
console.error
at Pact.Object.<anonymous>.Pact.verify (node_modules/@pact-foundation/src/httpPact/index.ts:211:15)
at Object.<anonymous> (node_modules/jest-pact/dist/pactWith.js:12:45)
console.error
Pact verification failed!
at Pact.Object.<anonymous>.Pact.verify (node_modules/@pact-foundation/src/httpPact/index.ts:212:15)
at Object.<anonymous> (node_modules/jest-pact/dist/pactWith.js:12:45)
console.error
Test failed for the following reasons:
Mock server failed with the following mismatches:
0) The following request was expected but not received:
Method: POST
Path: /keys
Headers:
Accept: application/json, text/plain, */*
Authorization: Bearer testToken
Content-Type: application/json
Body: {"key":"LS0tLS1CRUdJ... (1676 length)
at Pact.Object.<anonymous>.Pact.verify (node_modules/@pact-foundation/src/httpPact/index.ts:213:15)
at Object.<anonymous> (node_modules/jest-pact/dist/pactWith.js:12:45)
● Pact between flow and test-keys › with 40000 ms timeout for Pact › test API - Keys › get key by id › and the user is not authenticated › returns a 401 unauthorized
expect(received).toBe(expected) // Object.is equality
Expected: 401
Received: undefined
3 | export const checkIfHttpError = (e: Error, expectedStatusCode: number) => {
4 | expect(axios.isAxiosError(e)).toBe(true);
> 5 | expect((e as AxiosError).response?.status).toBe(expectedStatusCode);
| ^
6 | };
7 |
at checkIfHttpError (src/infrastructure/test/testsMatchers.ts:5:48)
at Object.<anonymous> (.../__tests__/TestService.contract.test.ts:89:25)
● Pact between flow and Test-keys › with 40000 ms timeout for Pact › Test API - Keys › get key by id › and the user is not authenticated › returns a 401 unauthorized
Pact verification failed - expected interactions did not match actual.
at new VerificationError (node_modules/@pact-foundation/pact/src/errors/verificationError.js:21:42)
at Pact.Object.<anonymous>.Pact.verify (node_modules/@pact-foundation/src/httpPact/index.ts:217:13)
at Object.<anonymous> (node_modules/jest-pact/dist/pactWith.js:12:45)
● Pact between flow and Test-keys › with 40000 ms timeout for Pact › Test API - Keys › get key by id › successfully returns a key › retrieve a key by id
Network Error
at createError (node_modules/axios/lib/core/createError.js:16:15)
at XMLHttpRequest.handleError (node_modules/axios/lib/adapters/xhr.js:117:14)
at XMLHttpRequest.invokeTheCallbackFunction (node_modules/jsdom/lib/jsdom/living/generated/EventHandlerNonNull.js:18:28)
at XMLHttpRequest.<anonymous> (node_modules/jsdom/lib/jsdom/living/helpers/create-event-accessor.js:35:32)
at innerInvokeEventListeners (node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:338:25)
at invokeEventListeners (node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:274:3)
at XMLHttpRequestImpl._dispatch (node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:221:9)
at fireAnEvent (node_modules/jsdom/lib/jsdom/living/helpers/events.js:18:36)
at requestErrorSteps (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:131:3)
at dispatchError (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:60:3)
at validCORSHeaders (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:75:5)
at validCORSPreflightHeaders (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:89:8)
at Request.<anonymous> (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:374:12)
at Request._processResponse (node_modules/jsdom/lib/jsdom/living/helpers/http-request.js:228:12)
at ClientRequest.<anonymous> (node_modules/jsdom/lib/jsdom/living/helpers/http-request.js:101:12)
● Pact between flow and Test-keys › with 40000 ms timeout for Pact › Test API - Keys › get key by id › successfully returns a key › retrieve a key by id
Pact verification failed - expected interactions did not match actual.
at new VerificationError (node_modules/@pact-foundation/pact/src/errors/verificationError.js:21:42)
at Pact.Object.<anonymous>.Pact.verify (node_modules/@pact-foundation/src/httpPact/index.ts:217:13)
at Object.<anonymous> (node_modules/jest-pact/dist/pactWith.js:12:45)
● Pact between flow and Test-keys › with 40000 ms timeout for Pact › Test API - Keys › create a key › and the user is not authenticated › returns a 401 unauthorized
expect(received).toBe(expected) // Object.is equality
Expected: 401
Received: undefined
3 | export const checkIfHttpError = (e: Error, expectedStatusCode: number) => {
4 | expect(axios.isAxiosError(e)).toBe(true);
> 5 | expect((e as AxiosError).response?.status).toBe(expectedStatusCode);
| ^
6 | };
7 |
at checkIfHttpError (src/infrastructure/test/testsMatchers.ts:5:48)
at Object.<anonymous> (.../__tests__/TestService.contract.test.ts:165:25)
● Pact between flow and Test-keys › with 40000 ms timeout for Pact › Test API - Keys › create a key › and the user is not authenticated › returns a 401 unauthorized
Pact verification failed - expected interactions did not match actual.
at new VerificationError (node_modules/@pact-foundation/pact/src/errors/verificationError.js:21:42)
at Pact.Object.<anonymous>.Pact.verify (node_modules/@pact-foundation/src/httpPact/index.ts:217:13)
at Object.<anonymous> (node_modules/jest-pact/dist/pactWith.js:12:45)
● Pact between flow and Test-keys › with 40000 ms timeout for Pact › Test API - Keys › create a key › request without name › returns a 400
expect(received).toBeInstanceOf(expected)
Expected constructor: ValidationException
Received constructor: Error
208 | });
209 | } catch (e) {
> 210 | expect(e).toBeInstanceOf(ValidationException);
| ^
211 | const validationError = e as ValidationException;
212 | expect(validationError.message).toStrictEqual(
213 | invalidResponse.error.message,
at Object.<anonymous> (.../__tests__/TestService.contract.test.ts:210:35)
● Pact between flow and Test-keys › with 40000 ms timeout for Pact › Test API - Keys › create a key › request without name › returns a 400
Pact verification failed - expected interactions did not match actual.
at new VerificationError (node_modules/@pact-foundation/pact/src/errors/verificationError.js:21:42)
at Pact.Object.<anonymous>.Pact.verify (node_modules/@pact-foundation/src/httpPact/index.ts:217:13)
at Object.<anonymous> (node_modules/jest-pact/dist/pactWith.js:12:45)
● Pact between flow and Test-keys › with 40000 ms timeout for Pact › Test API - Keys › create a key › request without name › returns a 400
expect.assertions(3)
Expected three assertions to be called but received one assertion call.
199 |
200 | it("returns a 400", async () => {
> 201 | expect.assertions(3);
| ^
202 | try {
203 | await keyService.create({
204 | KeyValue: "Value",
at Object.<anonymous> (.../__tests__/TestService.contract.test.ts:201:28)
● Pact between flow and Test-keys › with 40000 ms timeout for Pact › Test API - Keys › create a key › request without key value › returns a 400
expect(received).toBeInstanceOf(expected)
Expected constructor: ValidationException
Received constructor: Error
260 | });
261 | } catch (error) {
> 262 | expect(error).toBeInstanceOf(ValidationException);
| ^
263 | const validationError = error as ValidationException;
264 | expect(validationError.message).toStrictEqual(
265 | invalidResponse.error.message,
at Object.<anonymous> (.../__tests__/TestService.contract.test.ts:262:39)
● Pact between flow and Test-keys › with 40000 ms timeout for Pact › Test API - Keys › create a key › request without key value › returns a 400
Pact verification failed - expected interactions did not match actual.
at new VerificationError (node_modules/@pact-foundation/pact/src/errors/verificationError.js:21:42)
at Pact.Object.<anonymous>.Pact.verify (node_modules/@pact-foundation/src/httpPact/index.ts:217:13)
at Object.<anonymous> (node_modules/jest-pact/dist/pactWith.js:12:45)
● Pact between flow and Test-keys › with 40000 ms timeout for Pact › Test API - Keys › create a key › request without key value › returns a 400
expect.assertions(3)
Expected three assertions to be called but received one assertion call.
251 |
252 | it("returns a 400", async () => {
> 253 | expect.assertions(3);
| ^
254 | try {
255 | await keyService.create({
256 | KeyValue: "",
at Object.<anonymous> (.../__tests__/TestService.contract.test.ts:253:28)
● Pact between flow and Test-keys › with 40000 ms timeout for Pact › Test API - Keys › create a key › request creation existing key › returns a 409
expect(received).toBeInstanceOf(expected)
Expected constructor: TestKeyConflictException
Received constructor: Error
310 | });
311 | } catch (error) {
> 312 | expect(error).toBeInstanceOf(TestKeyConflictException);
| ^
313 | const conflictError = error as TestKeyConflictException;
314 | expect(conflictError.message).toStrictEqual(
315 | conflictResponse.message,
at Object.<anonymous> (.../__tests__/TestService.contract.test.ts:312:39)
● Pact between flow and Test-keys › with 40000 ms timeout for Pact › Test API - Keys › create a key › request creation existing key › returns a 409
Pact verification failed - expected interactions did not match actual.
at new VerificationError (node_modules/@pact-foundation/pact/src/errors/verificationError.js:21:42)
at Pact.Object.<anonymous>.Pact.verify (node_modules/@pact-foundation/src/httpPact/index.ts:217:13)
at Object.<anonymous> (node_modules/jest-pact/dist/pactWith.js:12:45)
● Pact between flow and Test-keys › with 40000 ms timeout for Pact › Test API - Keys › create a key › request creation existing key › returns a 409
expect.assertions(2)
Expected two assertions to be called but received one assertion call.
301 |
302 | it("returns a 409", async () => {
> 303 | expect.assertions(2);
| ^
304 | try {
305 | await keyService.create({
306 | KeyValue: "key",
at Object.<anonymous> (.../__tests__/TestService.contract.test.ts:303:28)
● Pact between flow and Test-keys › with 40000 ms timeout for Pact › Test API - Keys › create a key › valid data › success
Network Error
at createError (node_modules/axios/lib/core/createError.js:16:15)
at XMLHttpRequest.handleError (node_modules/axios/lib/adapters/xhr.js:117:14)
at XMLHttpRequest.invokeTheCallbackFunction (node_modules/jsdom/lib/jsdom/living/generated/EventHandlerNonNull.js:18:28)
at XMLHttpRequest.<anonymous> (node_modules/jsdom/lib/jsdom/living/helpers/create-event-accessor.js:35:32)
at innerInvokeEventListeners (node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:338:25)
at invokeEventListeners (node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:274:3)
at XMLHttpRequestImpl._dispatch (node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:221:9)
at fireAnEvent (node_modules/jsdom/lib/jsdom/living/helpers/events.js:18:36)
at requestErrorSteps (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:131:3)
at dispatchError (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:60:3)
at validCORSHeaders (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:75:5)
at validCORSPreflightHeaders (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:89:8)
at Request.<anonymous> (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:374:12)
at Request._processResponse (node_modules/jsdom/lib/jsdom/living/helpers/http-request.js:228:12)
at ClientRequest.<anonymous> (node_modules/jsdom/lib/jsdom/living/helpers/http-request.js:101:12)
● Pact between flow and Test-keys › with 40000 ms timeout for Pact › Test API - Keys › create a key › valid data › success
Pact verification failed - expected interactions did not match actual.
at new VerificationError (node_modules/@pact-foundation/pact/src/errors/verificationError.js:21:42)
at Pact.Object.<anonymous>.Pact.verify (node_modules/@pact-foundation/src/httpPact/index.ts:217:13)
at Object.<anonymous> (node_modules/jest-pact/dist/pactWith.js:12:45)
In addition, I want to say that I see as Pact failure with something “panic” because they cannot work with mock-server
In addition, I want to say that I see as Pact failure with something “panic” because they cannot work with mock-server
If you look carefully in the stacktrace you can see that the issue is like described before: "node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js"
To test that this work you can manually go to the file xhr-utils.js in the file system and insert the following:
Add these 3 lines between line 88 and 89 in node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js (i.e. on first line inside function validCORSPreflightHeaders):
response.headers["access-control-allow-origin"] = "*";
response.headers["access-control-allow-credentials"] = "true";
response.headers["access-control-allow-headers"] = "*";
Add these 2 lines between line 70 and 71 in node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js (i.e. on first line inside function validCORSHeaders):
response.headers["access-control-allow-origin"] = "*";
response.headers["access-control-allow-credentials"] = "true";
Your tests should now work even if the mock server is currently broken in v10.x.x given that the tests did work in v9.18.1
@gillsoftab It looks like a crutch, and even more - I didn't want to change something in the node_modules folder. Maybe there is any case to fix that another way (by upgrading someone's library)?
I don’t think he’s suggesting it as a workaround, I think he’s using the change to illustrate that the problem is in the pact mock server in v10
Oh, I understand, sorry. Then, I will try to do that and reply to you back after the changes
After making the changes that @gillsoftab mentioned, I started getting another error
RUNS .../__tests__/TestService.contract.test.ts
RUNS .../__tests__/TestService.contract.test.ts
RUNS .../__tests__/TestService.contract.test.ts
2022-10-28T07:04:44.701196Z WARN ThreadId(02) pact_models::content_types: Failed to parse '{"value":"application/json; charset=utf-8","regex":"application\\/json; *charset=utf-8","pact:matcher:type":"regex"}' as a content type: mime parse error: an invalid token was
encountered, 7B at position 0
FAIL .../__tests__/TestService.contract.test.tson/json; charset=utf-8","regex":"application\\/json; *charset=utf-8","pact:matcher:type":"regex"}' as a content type: mime parse error: an invalid token was
● Console
console.error
at Pact.Object.<anonymous>.Pact.verify (node_modules/@pact-foundation/src/httpPact/index.ts:211:15)
at Object.<anonymous> (node_modules/jest-pact/dist/pactWith.js:12:45)
console.error
Pact verification failed!
at Pact.Object.<anonymous>.Pact.verify (node_modules/@pact-foundation/src/httpPact/index.ts:212:15)
at Object.<anonymous> (node_modules/jest-pact/dist/pactWith.js:12:45)
....
Something changed in the v10 matches logic
TS2322: Type '{ x1: Matcher<string>; x2: Matcher<string>; x3: Matcher<string>; x4: Matcher<string>; x5: Matcher<boolean>; x6: Matcher<...>; type: RegexMatcher<...>; }'
is not assignable to type 'AnyTemplate'.
Types of property 'x6' are incompatible.
Type 'Matcher<Date>' is not assignable to type 'string | number | boolean | JsonArray | JsonMap | Matcher<AnyTemplate> | ArrayMatcher<AnyTemplate> | TemplateMap | ArrayTemplate'.
Type 'Matcher<Date>' is not assignable to type 'Matcher<AnyTemplate>'.
Type 'Date' is not assignable to type 'AnyTemplate'.
Type 'Date' is not assignable to type 'TemplateMap'.
Index signature for type 'string' is missing in type 'Date'.
Yeah, there are two conflicts from v9:
- We cannot use types in v10 for
willRespondWith.body
, only variables withany
type - In
any
type forwillRespondWith.body
- we cannot uselike(new Date("2022-..."))
matcher
The type change is unrelated to the topic of this issue- you can’t have a Date in json
After debugging the headers coming back, it looks like for some reason the library is detecting a trailing /
on the header it was using to determine the access-control-allow-origin
header.
This PR should address that: https://github.com/pact-foundation/pact-reference/pull/225
Awesome! As I can see, today, Matt's pull request was merged. Please inform us what the next steps are. Adapting implementation of reference Pact on Ruby to JavaScript language, and after that, we can update to the version of the pact-js
library?
Also, I don't fully understand what we should do with changes in willRespondWith.body
. For example, don't use new Date()
even if we used that in our business logic in our UI and do not use a specific type for that parameter value (use :any
)?
We'll update this issue once the code has been released and made available to this library - that's for us to do.
Also, I don't fully understand what we should do with changes in willRespondWith.body. For example, don't use new Date() even if we used that in our business logic in our UI and do not use a specific type for that parameter value (use :any)?
Date is not a valid JSON type (https://www.json.org/json-en.html). If you want to represent it in Pact, it must be one of those types.
Pact can only represent what the wire protocol itself can represent. In fact, this is desirable, because this is how we ensure we make it clear how the message will be communicated, regardless of the specific internal representations (and types) of data various applications have - in this case, it could be represented as a string or a number (I don't know what you use now, that's a question for you to discuss with your team).
If you changed your internal representation, the Pact tests will prevent you from using the wrong type over the wire.
I think this should be fixed by https://github.com/pact-foundation/pact-reference/pull/225, which should be in the latest Pact JS. Could you please confirm and re-open if still an issue for you?
Hi, Matt! I want to confirm that everything worked fine with the latest Pact library - 10.4.0
. By the way, we should remove some of the contract tests written in a "strict mode"