Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new section in README.md to clarify how to do local testing of … #40

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,73 @@ For more details on this signature type, check out the Google Cloud Functions
documentation on
[background functions](https://cloud.google.com/functions/docs/writing/background#cloud_pubsub_example).

## Local testing of cloud events
The setup for cloud functions that accept events is very similar to the instructions in the quickstart, with the following adjustments:

In your package.json, add a signature type (in bold) to your start command:
<pre>
"scripts": {
"start": "functions-framework --target=helloWorld <b>--signature-type=event"</b>
}
</pre>

Upon running ```sh npm start ```, you'll see the function is still being served at http://localhost:8080/. However it is no longer accessible via GET requests from the browser. Instead, send a POST request where the request body conforms to the API defined by [push subscriptions](https://cloud.google.com/pubsub/docs/push).

### Submitting POST request to simulating a pubsub message

Create mockPubsub.json file with the following contents:
```json
{
"message": {
"attributes": {
"key": "value"
},
"data": "SGVsbG8gQ2xvdWQgUHViL1N1YiEgSGVyZSBpcyBteSBtZXNzYWdlIQ==",
"messageId": "136969346945"
},
"subscription": "projects/myproject/subscriptions/mysubscription"
}
```

The file can be in any folder on your computer. From the terminal, goto the directory where ```mockPubsub.json``` is located, and run the following command assuming your cloud function is hosted locally on port 8080:
```sh
curl -d "@mockPubsub.json" -X POST http://localhost:8080
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I also suggest to add the CE-* headers, maybe with a truthy value, to the docs so the lib can know that the request is for a Cloud Event?

curl -d "@mockPubsub.json" -X POST -H "Ce-Type: true" -H "Ce-Specversion: true" -H "Ce-Source: true" -H "Ce-Id: true" http://localhost:8080

This would help with issues like #41, and also set the values correctly to the data and context arguments.

```

### Using pubsub emulator

Another way to test your cloud function pubsub endpoint is to use the [pubsub emulator](https://cloud.google.com/pubsub/docs/emulator). This allows you to use the pubsub notification from another service to trigger your cloud function.

The high level approach is to:
1. Start the pubsub emulator
2. Use the pubsub client library to create a subscription and set the pushEndpoint to http://localhost:8080.

After that, all notifications to the subscription topic will be pushed to your cloud function.

Sample script for creating subscription with pushEndpoint:

```js
{ PubSub } require('@google-cloud/pubsub');

async function main() {
const pubsub = new PubSub({
apiEndpoint: 'localhost:8085', // Pubsub emulator endpoint
projectId: 'myproject',
});
const topic = await pubsub.topic('my-topic');
const [topicExists] = await topic.exists();
if (!topicExists) {
await topic.create();
}
const createSubscriptionResponse = await topic.createSubscription('my_subscription', {
pushEndpoint: 'https://localhost:8080',
});
}

main();
```


# Contributing

Contributions to this library are welcome and encouraged. See
Expand Down