|
| 1 | +## Local Testing of Cloud Events |
| 2 | + |
| 3 | +The setup for cloud functions that accept events is very similar to the instructions in the quickstart, with the following adjustments: |
| 4 | + |
| 5 | +In your `package.json`, add a signature type (in bold) to your start command: |
| 6 | + |
| 7 | +<pre> |
| 8 | + "scripts": { |
| 9 | + "start": "functions-framework --target=helloWorld <b>--signature-type=event"</b> |
| 10 | + } |
| 11 | +</pre> |
| 12 | + |
| 13 | +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). |
| 14 | + |
| 15 | +### Submitting POST Request to Simulate Pub/Sub messages |
| 16 | + |
| 17 | +Create a `mockPubsub.json` file with the following contents: |
| 18 | + |
| 19 | +```json |
| 20 | +{ |
| 21 | + "message": { |
| 22 | + "attributes": { |
| 23 | + "key": "value" |
| 24 | + }, |
| 25 | + "data": "SGVsbG8gQ2xvdWQgUHViL1N1YiEgSGVyZSBpcyBteSBtZXNzYWdlIQ==", |
| 26 | + "messageId": "136969346945" |
| 27 | + }, |
| 28 | + "subscription": "projects/myproject/subscriptions/mysubscription" |
| 29 | +} |
| 30 | +``` |
| 31 | + |
| 32 | +The file can be in any folder on your computer. From the terminal, go to the directory where `mockPubsub.json` is located, and run the following command (assuming your cloud function is hosted locally on port 8080): |
| 33 | + |
| 34 | +```sh |
| 35 | +curl -d "@mockPubsub.json" \ |
| 36 | + -X POST \ |
| 37 | + -H "Ce-Type: true" \ |
| 38 | + -H "Ce-Specversion: true" \ |
| 39 | + -H "Ce-Source: true" \ |
| 40 | + -H "Ce-Id: true" \ |
| 41 | + http://localhost:8080 |
| 42 | +``` |
| 43 | + |
| 44 | +### Using the Pub/Sub emulator |
| 45 | + |
| 46 | +Another way to test your cloud function Pub/Sub endpoint is to use the [Pub/Sub Emulator](https://cloud.google.com/pubsub/docs/emulator). This allows you to use the Pub/Sub notification from another service to trigger your cloud function. |
| 47 | + |
| 48 | +The high level approach is to: |
| 49 | +1. Start the Pub/Sub Emulator |
| 50 | +2. Use the Pub/Sub client library to create a subscription and set the `pushEndpoint` to `http://localhost:8080`. |
| 51 | + |
| 52 | +After setup, all notifications to the subscription topic will be pushed to your cloud function. |
| 53 | + |
| 54 | +Here is a sample script for creating subscription with a `pushEndpoint`: |
| 55 | + |
| 56 | + ```js |
| 57 | +const { PubSub } = require('@google-cloud/pubsub'); |
| 58 | + |
| 59 | +async function main() { |
| 60 | + const apiEndpoint = 'localhost:8085'; |
| 61 | + console.log(`Listening to the Pub/Sub emulator event at: ${apiEndpoint}`); |
| 62 | + const pubsub = new PubSub({ |
| 63 | + apiEndpoint, // Pub/Sub emulator endpoint |
| 64 | + projectId: 'myproject', |
| 65 | + }); |
| 66 | + const topic = await pubsub.topic('my-topic'); |
| 67 | + const [topicExists] = await topic.exists(); |
| 68 | + if (!topicExists) { |
| 69 | + await topic.create(); |
| 70 | + } |
| 71 | + const createSubscriptionResponse = await topic.createSubscription('my_subscription', { |
| 72 | + pushEndpoint: 'https://localhost:8080', |
| 73 | + }); |
| 74 | +} |
| 75 | + |
| 76 | +main(); |
| 77 | + ``` |
0 commit comments