|
| 1 | +# Firebase Emulator Testing Guide |
| 2 | + |
| 3 | +This guide explains how to run the Firebase emulator locally and test your Firebase functions using cURL. |
| 4 | + |
| 5 | +## Running the Firebase Emulator |
| 6 | + |
| 7 | +1. Navigate to your project root directory: |
| 8 | + ```bash |
| 9 | + cd /Users/pengxiao/workspaces/zenuml/web-sequence |
| 10 | + ``` |
| 11 | + |
| 12 | +2. Start the Firebase emulator: |
| 13 | + ```bash |
| 14 | + firebase emulators:start |
| 15 | + ``` |
| 16 | + |
| 17 | + This will start the emulator and display the available endpoints in the terminal. |
| 18 | + |
| 19 | +## Accessing the Emulator UI |
| 20 | + |
| 21 | +The Firebase Emulator Suite includes a web-based UI that provides a dashboard for all your emulated services: |
| 22 | + |
| 23 | +1. When you start the emulators, look for a message like: |
| 24 | + ``` |
| 25 | + ✔ emulators: All emulators ready! It is now safe to connect. |
| 26 | + i emulators: View Emulator UI at http://localhost:4000/ |
| 27 | + ``` |
| 28 | + |
| 29 | +2. Open the provided URL in your browser (typically http://localhost:4000) |
| 30 | + |
| 31 | +3. The UI provides: |
| 32 | + - A list of all your Cloud Functions endpoints |
| 33 | + - The ability to view logs for each function |
| 34 | + - A way to test functions directly from the UI |
| 35 | + - Firestore database viewer (if enabled) |
| 36 | + - Authentication emulator (if enabled) |
| 37 | + |
| 38 | +This UI makes it easier to debug and test your Firebase functions without having to use cURL commands for everything. |
| 39 | + |
| 40 | +## Testing Firebase Functions with cURL |
| 41 | + |
| 42 | +### Testing the Info Endpoint |
| 43 | + |
| 44 | +To verify that the emulator is running correctly, test the `/info` endpoint: |
| 45 | + |
| 46 | +```bash |
| 47 | +curl -X POST \ |
| 48 | + http://localhost:5000/info \ |
| 49 | + -H 'Content-Type: application/json' \ |
| 50 | + -d '{"event": {"event": "testEvent", "category": "test"}, "userId": "test-user"}' |
| 51 | +``` |
| 52 | + |
| 53 | +You should receive a response like: |
| 54 | +``` |
| 55 | +Hello from web-sequence-local! |
| 56 | +``` |
| 57 | + |
| 58 | +### Testing the Track Endpoint |
| 59 | + |
| 60 | +To test the `/track` endpoint: |
| 61 | + |
| 62 | +```bash |
| 63 | +curl -X POST \ |
| 64 | + http://localhost:5000/track \ |
| 65 | + -H 'Content-Type: application/json' \ |
| 66 | + -d '{"event": {"event": "testEvent", "category": "test"}, "userId": "test-user"}' |
| 67 | +``` |
| 68 | + |
| 69 | +You should receive a response like: |
| 70 | +``` |
| 71 | +Event tracked successfully |
| 72 | +``` |
| 73 | + |
| 74 | +### Testing Different Payload Formats |
| 75 | + |
| 76 | +The track endpoint supports two different payload formats: |
| 77 | + |
| 78 | +1. Object format (recommended): |
| 79 | + ```bash |
| 80 | + curl -X POST \ |
| 81 | + http://localhost:5002/track \ |
| 82 | + -H 'Content-Type: application/json' \ |
| 83 | + -d '{"event": {"event": "testEvent", "category": "test"}, "userId": "test-user"}' |
| 84 | + ``` |
| 85 | + |
| 86 | +2. String format (legacy): |
| 87 | + ```bash |
| 88 | + curl -X POST \ |
| 89 | + http://localhost:5002/track \ |
| 90 | + -H 'Content-Type: application/json' \ |
| 91 | + -d '{"event": "testEvent", "category": "test", "userId": "test-user"}' |
| 92 | + ``` |
| 93 | + |
| 94 | +## Monitoring Logs |
| 95 | + |
| 96 | +While the emulator is running, you can monitor the logs in the terminal. These logs will show: |
| 97 | + |
| 98 | +1. Incoming requests |
| 99 | +2. Function execution details |
| 100 | +3. Any errors or warnings |
| 101 | +4. Mixpanel tracking information |
| 102 | + |
| 103 | +## Troubleshooting |
| 104 | + |
| 105 | +### Functions Emulator Shows as "OFF" in the UI |
| 106 | + |
| 107 | +If the Functions emulator shows as "OFF" in the Emulator UI: |
| 108 | + |
| 109 | +1. Make sure your `firebase.json` file has the proper emulators configuration: |
| 110 | + ```json |
| 111 | + "emulators": { |
| 112 | + "functions": { |
| 113 | + "port": 5002 |
| 114 | + }, |
| 115 | + "hosting": { |
| 116 | + "port": 5000 |
| 117 | + }, |
| 118 | + "ui": { |
| 119 | + "enabled": true, |
| 120 | + "port": 4000 |
| 121 | + } |
| 122 | + } |
| 123 | + ``` |
| 124 | + |
| 125 | +2. Try starting the emulator with the explicit `--only` flag: |
| 126 | + ```bash |
| 127 | + firebase emulators:start --only functions |
| 128 | + ``` |
| 129 | + |
| 130 | +3. Check the terminal output for any error messages related to the Functions emulator. |
| 131 | + |
| 132 | +### Timeout Issues with the Track Endpoint |
| 133 | + |
| 134 | +If you encounter a timeout issue with the `/track` endpoint: |
| 135 | + |
| 136 | +1. Check the emulator logs for any errors |
| 137 | +2. Verify that the function is responding immediately without waiting for Mixpanel |
| 138 | +3. Make sure the payload format is correct |
| 139 | + |
| 140 | +## Notes on URL Structure |
| 141 | + |
| 142 | +- Direct function URLs: `http://localhost:5002/{functionName}` |
| 143 | +- If you've added rewrite rules in firebase.json, you can also use: `http://localhost:5000/{path}` |
| 144 | + |
| 145 | +Remember to stop the emulator when you're done testing by pressing `Ctrl+C` in the terminal. |
0 commit comments