|
| 1 | + |
| 2 | +# HTTP + SSE Sample with Keploy |
| 3 | + |
| 4 | +This sample demonstrates how to test a Go application that serves both standard **HTTP** endpoints and **Server-Sent Events (SSE)** on different ports using [Keploy](https://keploy.io). |
| 5 | + |
| 6 | +It uses **Caddy** as a reverse proxy to route traffic to the appropriate ports based on the request path. |
| 7 | + |
| 8 | +## Prerequisites |
| 9 | + |
| 10 | +- [Go](https://go.dev/doc/install) (1.21 or later) |
| 11 | +- [Docker](https://docs.docker.com/engine/install/) (for running Caddy) |
| 12 | +- [Keploy](https://keploy.io/docs/server/installation/) |
| 13 | + |
| 14 | +## Architecture |
| 15 | + |
| 16 | +- **HTTP Server**: Listens on port `8000`. Handles API requests. |
| 17 | +- **SSE Server**: Listens on port `8047`. Handles real-time event streams. |
| 18 | +- **Caddy (Router)**: Listens on port `80`. Routes `/subscribe/*` to port `8047` and everything else to `8000`. |
| 19 | + |
| 20 | +## Setup |
| 21 | + |
| 22 | +### 1. Start the Caddy Router |
| 23 | +Run the following command to start Caddy in a Docker container. This will simulate a production-like routing environment where a gateway handles traffic distribution. |
| 24 | + |
| 25 | +```bash |
| 26 | +docker rm -f sse-router 2>/dev/null || true |
| 27 | +docker run --name sse-router --rm -p 80:80 \ |
| 28 | + --add-host=host.docker.internal:host-gateway \ |
| 29 | + -v "$PWD/Caddyfile.record:/etc/caddy/Caddyfile" \ |
| 30 | + caddy:2-alpine |
| 31 | +``` |
| 32 | + |
| 33 | +## Recording Test Cases |
| 34 | + |
| 35 | +Open a new terminal to run the application with Keploy recording enabled. |
| 36 | + |
| 37 | +### 1. Start the Application |
| 38 | +```bash |
| 39 | +keploy record |
| 40 | +``` |
| 41 | + |
| 42 | +### 2. Generate Traffic |
| 43 | +Send requests to the Caddy router (localhost:80). |
| 44 | + |
| 45 | +**Standard HTTP Request:** |
| 46 | +```bash |
| 47 | +# 1. Health Check |
| 48 | +curl -i "http://localhost:8000/health" |
| 49 | + |
| 50 | +# 2. Simple Hello |
| 51 | +curl -i "http://localhost:8000/api/hello" |
| 52 | + |
| 53 | +# 3. Echo (with message) |
| 54 | +curl -i "http://localhost:8000/api/echo?msg=Greetings" |
| 55 | + |
| 56 | +# 3b. Echo (empty/default message) |
| 57 | +curl -i "http://localhost:8000/api/echo" |
| 58 | + |
| 59 | +# 4. Add Numbers |
| 60 | +curl -i "http://localhost:8000/api/add?a=50&b=25" |
| 61 | + |
| 62 | +# 5. Get Current Time |
| 63 | +curl -i "http://localhost:8000/api/time" |
| 64 | + |
| 65 | +# 6. Get Resource by ID |
| 66 | +curl -i "http://localhost:8000/api/resource/user-123" |
| 67 | +``` |
| 68 | + |
| 69 | +**SSE Request (Student):** |
| 70 | +```bash |
| 71 | +curl -N -H "Accept: text/event-stream" \ |
| 72 | + "http://localhost/subscribe/student/events?doubtId=abc" |
| 73 | +``` |
| 74 | + |
| 75 | +**SSE Request (Teacher):** |
| 76 | +```bash |
| 77 | +curl -N -H "Accept: text/event-stream" \ |
| 78 | + "http://localhost/subscribe/teacher/events?teacherId=t-42" |
| 79 | +``` |
| 80 | + |
| 81 | +*Note: You can verify that traffic is being routed correctly by checking the "port" field in JSON responses or the server logs.* |
| 82 | + |
| 83 | +## Running Tests |
| 84 | + |
| 85 | +Once you have recorded the test cases, you can stop the recording (Ctrl+C) and run the tests. |
| 86 | + |
| 87 | +```bash |
| 88 | +keploy test |
| 89 | +``` |
| 90 | + |
| 91 | +Keploy will replay the recorded traffic and verify that the responses match the expected output, ensuring that routing and logic remain consistent. |
0 commit comments