Skip to content

Commit 37ceee7

Browse files
betacargrant
authored andcommitted
New section documentation on how to perform local PubSub testing (#76)
* Added new section in README.md to clarify how to do local testing of event functions * Line regarding #37 removed
1 parent 2c1245b commit 37ceee7

File tree

1 file changed

+73
-1
lines changed

1 file changed

+73
-1
lines changed

docs/README.md

+73-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ This directory contains advanced docs around the Functions Framework.
77
## TODO Docs
88

99
- TODO: Run Multiple Cloud Functions [#23](https://github.com/GoogleCloudPlatform/functions-framework-nodejs/issues/23)
10-
- TODO: Pub/Sub Trigger [#37](https://github.com/GoogleCloudPlatform/functions-framework-nodejs/issues/37)
1110
- TODO: Deploy to Cloud Run [#28](https://github.com/GoogleCloudPlatform/functions-framework-nodejs/pull/28)
1211

1312
## Debugging functions
@@ -41,3 +40,76 @@ URL: http://localhost:8080/
4140
```
4241

4342
You can now use an IDE or other tooling to add breakpoints, step through your code and debug your function.
43+
44+
## Local testing of cloud events
45+
The setup for cloud functions that accept events is very similar to the instructions in the quickstart, with the following adjustments:
46+
47+
In your package.json, add a signature type (in bold) to your start command:
48+
<pre>
49+
"scripts": {
50+
"start": "functions-framework --target=helloWorld <b>--signature-type=event"</b>
51+
}
52+
</pre>
53+
54+
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).
55+
56+
### Submitting POST request to simulating a pubsub message
57+
58+
Create mockPubsub.json file with the following contents:
59+
```json
60+
{
61+
"message": {
62+
"attributes": {
63+
"key": "value"
64+
},
65+
"data": "SGVsbG8gQ2xvdWQgUHViL1N1YiEgSGVyZSBpcyBteSBtZXNzYWdlIQ==",
66+
"messageId": "136969346945"
67+
},
68+
"subscription": "projects/myproject/subscriptions/mysubscription"
69+
}
70+
```
71+
72+
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:
73+
```sh
74+
curl -d "@mockPubsub.json" -X POST \
75+
-H "Ce-Type: true" \
76+
-H "Ce-Specversion: true" \
77+
-H "Ce-Source: true" \
78+
-H "Ce-Id: true" \
79+
http://localhost:8080
80+
```
81+
82+
> In order to simulate a Cloud Event, you need to add the ```Ce-*``` headers, along with a _truthy_ value, to the request.
83+
84+
### Using pubsub emulator
85+
86+
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.
87+
88+
The high level approach is to:
89+
1. Start the pubsub emulator
90+
2. Use the pubsub client library to create a subscription and set the pushEndpoint to http://localhost:8080.
91+
92+
After that, all notifications to the subscription topic will be pushed to your cloud function.
93+
94+
Sample script for creating subscription with pushEndpoint:
95+
96+
```js
97+
{ PubSub } require('@google-cloud/pubsub');
98+
99+
async function main() {
100+
const pubsub = new PubSub({
101+
apiEndpoint: 'localhost:8085', // Pubsub emulator endpoint
102+
projectId: 'myproject',
103+
});
104+
const topic = await pubsub.topic('my-topic');
105+
const [topicExists] = await topic.exists();
106+
if (!topicExists) {
107+
await topic.create();
108+
}
109+
const createSubscriptionResponse = await topic.createSubscription('my_subscription', {
110+
pushEndpoint: 'https://localhost:8080',
111+
});
112+
}
113+
114+
main();
115+
```

0 commit comments

Comments
 (0)