This guide shows you how to use the Functions Framework for local testing with:
- CloudEvents
- CloudEvents Conformance Testing
Install @openfunction/functions-framework
and list it as a dependency in your package.json
:
npm install --save @openfunction/functions-framework
Create an index.js
file and declare your function:
const functions = require('@openfunction/functions-framework');
functions.cloudEvent('helloCloudEvents', (cloudevent) => {
console.log(cloudevent.specversion);
console.log(cloudevent.type);
console.log(cloudevent.source);
console.log(cloudevent.subject);
console.log(cloudevent.id);
console.log(cloudevent.time);
console.log(cloudevent.datacontenttype);
});
Add a package.json
script to start the Functions Framework and pass the name of your function as the target
.
{
"scripts": {
"start": "functions-framework --target=helloCloudEvents"
}
}
Start the Functions Framework:
npm start
Your function will be serving at http://localhost:8080/
, however,
it is no longer accessible via HTTP GET
requests from the browser.
cloudevents send http://localhost:8080 --specver--id abc-123 --source cloudevents.conformance.tool --type foo.bar