João Luiz Vieira
06/21/2022, 10:56 PM"@pact-foundation/pact": "^9.17.3"
This is my contract. It is already in the pact broker.
{
"method": "POST",
"path": "/files",
"headers": {
"Content-Type": "application/json"
},
"body": {
"clientId": "2454ad95-0352-4494-9a40-6afb333959e4",
"fileContent": "xxxxofAAABAE=",
}
}
In the provider side, I just created a test and added routes. Until here, everything is ok.
The weird part is that when the verification happens with new Verifier(options).verifyProvider()
, it calls the business logic code without the body. The other properties (baseUrl, method), they are passed to the the code, but not the body.
<http://router.post|router.post>('/', (req, res, next) => {
console.log("req.baseUrl", req.baseUrl)
console.log("req.path", req.path)
console.log("req.method", req.method)
console.log("req.body", req.body)
})
The code above, generate this logs:
req.baseUrl /files
req.path /
req.method POST
req.body undefined
Not sure why the body is now being sent to the endpoint being tested. I have done other contract tests, but all without request body and I never notice this before.
Anyone has any ideas of what I could be doing wrong?João Luiz Vieira
06/21/2022, 11:38 PM"jest-pact": "^0.9.4"
João Luiz Vieira
06/22/2022, 12:10 AMconst { Verifier } = require('@pact-foundation/pact')
const express = require('express')
const pactBrokerUrl = "<https://pact-brokers.tools.XX.XX>"
const providerBaseUrl = '<http://localhost:3000/>'
const app = express()
const server = app.listen(3000, () => {
console.log("Service Listening")
})
<http://app.post|app.post>('/files', (req, res, next) => {
console.log("req.baseUrl", req.baseUrl)
console.log("req.path", req.path)
console.log("req.method", req.method)
console.log("req.body", req.body)
throw Error("only a test")
})
const options = {
provider: "file-service",
providerBaseUrl,
pactBrokerUrl,
logLevel: "debug",
consumerVersionTags: ["merge-requests/5984"],
};
describe("Pact Verification", () => {
test("validates the expectations", () => {
return new Verifier(options).verifyProvider()
.finally(() => { server.close() })
})
})
João Luiz Vieira
06/22/2022, 12:10 AM"test:pactProvider": "jest -c jest.contracts.provider.config.js --runInBand"
Timothy Jones
06/22/2022, 12:39 AMTimothy Jones
06/22/2022, 12:40 AMTimothy Jones
06/22/2022, 12:42 AMTimothy Jones
06/22/2022, 12:42 AMMatt (pactflow.io / pact-js / pact-go)
Matt (pactflow.io / pact-js / pact-go)
Matt (pactflow.io / pact-js / pact-go)
express.json()
middleware to automatically parse the body and make it available on the req
object:
var express = require('express')
var app = express()
app.use(express.json()) // for parsing application/json
app.use(express.urlencoded({ extended: true })) // for parsing application/x-www-form-urlencoded
<http://app.post|app.post>('/profile', function (req, res, next) {
console.log(req.body)
res.json(req.body)
})
Timothy Jones
06/22/2022, 12:47 AMTimothy Jones
06/22/2022, 12:47 AMMatt (pactflow.io / pact-js / pact-go)
Matt (pactflow.io / pact-js / pact-go)
Timothy Jones
06/22/2022, 12:55 AMMatt (pactflow.io / pact-js / pact-go)
Timothy Jones
06/22/2022, 12:57 AMTimothy Jones
06/22/2022, 12:58 AMJoão Luiz Vieira
06/22/2022, 1:48 AMapp.use(express.json())
middleware.
Thank you so much @Matt (pactflow.io / pact-js / pact-go) and @Timothy Jones for the help!Timothy Jones
06/22/2022, 1:54 AMMatt (pactflow.io / pact-js / pact-go)
Matt (pactflow.io / pact-js / pact-go)
João Luiz Vieira
06/22/2022, 2:28 AM