|
| 1 | +--- |
| 2 | +id: samples-fasthttp |
| 3 | +title: Sample CRUD App (Golang) |
| 4 | +sidebar_label: FastHttp + Postgres |
| 5 | +description: The following sample app showcases how to use FastHttp framework and the Keploy Platform. |
| 6 | +tags: |
| 7 | + - go |
| 8 | + - quickstart |
| 9 | + - samples |
| 10 | + - examples |
| 11 | + - tutorial |
| 12 | + - postgres |
| 13 | + - fasthttp |
| 14 | +keyword: |
| 15 | + - FastHttp Framework |
| 16 | + - Postgres Mock |
| 17 | + - Golang |
| 18 | + - API Test generator |
| 19 | + - Auto Testcase generation |
| 20 | +--- |
| 21 | + |
| 22 | +## Introduction |
| 23 | + |
| 24 | +🪄 Dive into the world of CRUD applications and see how seamlessly Keploy integrates with [FastHttp](https://github.com/valyala/fasthttp) and [Postgres](https://www.postgresql.org/). Buckle up, it's gonna be a fun ride! 🎢 |
| 25 | + |
| 26 | +import InstallationGuide from '../concepts/installation.md' |
| 27 | + |
| 28 | +<InstallationGuide/> |
| 29 | + |
| 30 | +## Clone the sample CRUD application 🧪 |
| 31 | + |
| 32 | +```bash |
| 33 | +git clone https://github.com/keploy/samples-go.git && cd samples-go/fasthttp-postgres |
| 34 | +go mod download |
| 35 | +``` |
| 36 | + |
| 37 | +## Installation 📥 |
| 38 | + |
| 39 | +There are 2 ways you can run this sample application. |
| 40 | + |
| 41 | +- [Using Docker compose: running application as well as Postgres on Docker container](#using-docker-compose-) |
| 42 | +- [Using Docker container for Postgres and running application locally](#running-app-locally-on-linuxwsl-) |
| 43 | + |
| 44 | +## Using Docker Compose 🐳 |
| 45 | + |
| 46 | +We will be using Docker Compose to run the application as well as Postgres on Docker container. |
| 47 | + |
| 48 | +### Lights, Camera, Record! 🎥 |
| 49 | + |
| 50 | +Fire up the application and Postgres instance with Keploy. Keep an eye on the two key flags: |
| 51 | +`-c`: Command to run the app (e.g., `docker compose up`). |
| 52 | + |
| 53 | +`--container-name`: The container name in the `docker-compose.yml` for traffic interception. |
| 54 | + |
| 55 | +```bash |
| 56 | +keploy record -c "docker compose up" --container-name "fasthttpPostgresApp" |
| 57 | +``` |
| 58 | + |
| 59 | +Getting logs like this? Perfect! 👌 |
| 60 | + |
| 61 | + |
| 62 | +🔥 Challenge time! Generate some test cases. How? Just **make some API calls**. Postman, Hoppscotch or even curl - take your pick! |
| 63 | + |
| 64 | +Let's create some users and books: |
| 65 | + |
| 66 | +#### Post Requests |
| 67 | + |
| 68 | +```bash |
| 69 | +curl -X POST -H "Content-Type: application/json" -d '{"name":"Author Name"}' http://localhost:8080/authors |
| 70 | +curl -X POST -H "Content-Type: application/json" -d '{"title":"Book Title","author_id":1}' http://localhost:8080/books |
| 71 | +``` |
| 72 | + |
| 73 | +#### Get Request |
| 74 | + |
| 75 | +```bash |
| 76 | +curl -i http://localhost:8080/books |
| 77 | +``` |
| 78 | + |
| 79 | +🎉 Woohoo! With simple API calls, you've crafted test cases with mocks! Dive into the Keploy directory and feast your eyes on the newly minted `test-1.yml` and `mocks.yml`. |
| 80 | + |
| 81 | +Here's a peek of what you get: |
| 82 | + |
| 83 | +```yaml |
| 84 | +version: api.keploy.io/v1beta1 |
| 85 | +kind: Http |
| 86 | +name: test-1 |
| 87 | +spec: |
| 88 | + metadata: {} |
| 89 | + req: |
| 90 | + method: POST |
| 91 | + proto_major: 1 |
| 92 | + proto_minor: 1 |
| 93 | + url: http://localhost:8080/authors |
| 94 | + header: |
| 95 | + Accept: "*/*" |
| 96 | + Content-Length: "22" |
| 97 | + Content-Type: application/json |
| 98 | + Host: localhost:8080 |
| 99 | + User-Agent: curl/7.88.1 |
| 100 | + body: '{"name":"Author Name"}' |
| 101 | + timestamp: 2024-06-24T13:05:47.732915734+05:30 |
| 102 | + resp: |
| 103 | + status_code: 201 |
| 104 | + header: |
| 105 | + Content-Length: "0" |
| 106 | + Date: Mon, 24 Jun 2024 07:35:47 GMT |
| 107 | + Server: Server |
| 108 | + body: "" |
| 109 | + status_message: Created |
| 110 | + proto_major: 0 |
| 111 | + proto_minor: 0 |
| 112 | + timestamp: 2024-06-24T13:05:49.810554677+05:30 |
| 113 | + objects: [] |
| 114 | + assertions: |
| 115 | + noise: |
| 116 | + header.Date: [] |
| 117 | + created: 1719214549 |
| 118 | +curl: |- |
| 119 | + curl --request POST \ |
| 120 | + --url http://localhost:8080/authors \ |
| 121 | + --header 'Host: localhost:8080' \ |
| 122 | + --header 'User-Agent: curl/7.88.1' \ |
| 123 | + --header 'Accept: */*' \ |
| 124 | + --header 'Content-Type: application/json' \ |
| 125 | + --data '{"name":"Author Name"}' |
| 126 | +``` |
| 127 | +
|
| 128 | +This is how the generated **mock.yml** will look like: |
| 129 | +
|
| 130 | +```yaml |
| 131 | +version: api.keploy.io/v1beta1 |
| 132 | +kind: Postgres |
| 133 | +name: mock-0 |
| 134 | +spec: |
| 135 | + metadata: |
| 136 | + type: config |
| 137 | + postgresrequests: |
| 138 | + - identifier: StartupRequest |
| 139 | + length: 96 |
| 140 | + payload: AAAAYAADAAB1c2VyAHBvc3RncmVzAGNsaWVudF9lbmNvZGluZwBVVEY4AGV4dHJhX2Zsb2F0X2RpZ2l0cwAyAGRhdGFiYXNlAGRiAGRhdGVzdHlsZQBJU08sIE1EWQAA |
| 141 | + startup_message: |
| 142 | + protocolversion: 196608 |
| 143 | + parameters: |
| 144 | + client_encoding: UTF8 |
| 145 | + database: db |
| 146 | + datestyle: ISO, MDY |
| 147 | + extra_float_digits: "2" |
| 148 | + user: postgres |
| 149 | + auth_type: 0 |
| 150 | + postgresresponses: |
| 151 | + - header: [R] |
| 152 | + identifier: ServerResponse |
| 153 | + length: 96 |
| 154 | + authentication_md5_password: |
| 155 | + salt: [200, 42, 157, 175] |
| 156 | + msg_type: 82 |
| 157 | + auth_type: 5 |
| 158 | + reqtimestampmock: 2024-06-24T13:05:47.736932812+05:30 |
| 159 | + restimestampmock: 2024-06-24T13:05:47.74668502+05:30 |
| 160 | +connectionId: "0" |
| 161 | +``` |
| 162 | +
|
| 163 | +_Time to perform more API magic!_ |
| 164 | +
|
| 165 | +#### Get All Books |
| 166 | +
|
| 167 | +```bash |
| 168 | +curl -i http://localhost:8080/books |
| 169 | +``` |
| 170 | + |
| 171 | +Or just type `http://localhost:8080/books` in your browser. Your choice! |
| 172 | + |
| 173 | +Spotted the new test and mock files in your project? High five! 🙌 |
| 174 | + |
| 175 | +### Run Tests 🏃♀️ |
| 176 | + |
| 177 | +Time to put things to the test 🧪 |
| 178 | + |
| 179 | +```bash |
| 180 | +keploy test -c "docker compose up" --container-name "fasthttpPostgresApp" --delay 10 |
| 181 | +``` |
| 182 | + |
| 183 | +> The `--delay` flag? Oh, that's just giving your app a little breather (in seconds) before the test cases come knocking. |
| 184 | +
|
| 185 | +Your results should be looking like this: |
| 186 | + |
| 187 | + |
| 188 | + |
| 189 | +Did you spot that the ts (timestamp) is showing some differences? Yep, time has a way of doing that! 🕰️ |
| 190 | + |
| 191 | +Final thoughts? Dive deeper! Try different API calls, tweak the DB response in the `mocks.yml`, or fiddle with the request or response in `test-x.yml`. Run the tests again and see the magic unfold! ✨👩💻👨💻✨ |
| 192 | + |
| 193 | +### Wrapping it up 🎉 |
| 194 | + |
| 195 | +Congrats on the journey so far! You've seen Keploy's power, flexed your coding muscles, and had a bit of fun too! Now, go out there and keep exploring, innovating, and creating! Remember, with the right tools and a sprinkle of fun, anything's possible. 😊🚀 |
| 196 | + |
| 197 | +Happy coding! ✨👩💻👨💻✨ |
| 198 | + |
| 199 | +--- |
| 200 | + |
| 201 | +## Running App Locally on Linux/WSL 🐧 |
| 202 | + |
| 203 | +We'll be running our sample application right on Linux, but just to make things a tad more thrilling, we'll have the database (Postgres) chill on Docker. Ready? Let's get the party started! 🎉 |
| 204 | + |
| 205 | +If you are using WSL on Windows then use below to start WSL in the user's home directory: |
| 206 | + |
| 207 | +```bash |
| 208 | +wsl ~ |
| 209 | +``` |
| 210 | + |
| 211 | +First things first, update the Postgres URL to `localhost:5432` on **line 21** of our trusty `main.go` file. |
| 212 | + |
| 213 | +### 🍃 Kickstart Postgres |
| 214 | + |
| 215 | +Let's breathe life into your Postgres container. A simple spell should do the trick: |
| 216 | + |
| 217 | +```bash |
| 218 | +docker compose up postgres |
| 219 | +``` |
| 220 | + |
| 221 | +### 📼 Recording Time! |
| 222 | + |
| 223 | +Ready, set, record! Here's how: |
| 224 | + |
| 225 | +```bash |
| 226 | +go build -cover |
| 227 | +keploy record -c "./app" |
| 228 | + |
| 229 | +``` |
| 230 | + |
| 231 | +Keep an eye out for the `-c` flag! It's the command charm to run the app. Whether you're using `go run main.go` or the binary path like `./app`, it's your call. |
| 232 | +If you're seeing logs that resemble the ones below, you're on the right track: |
| 233 | + |
| 234 | + |
| 235 | + |
| 236 | +Alright! With the app alive and kicking, let's weave some test cases. Making some API calls! Postman, Hoppscotch, |
| 237 | + |
| 238 | +or even the classic curl - take your pick! |
| 239 | + |
| 240 | +Time to create some users and books: |
| 241 | + |
| 242 | +#### Post Requests |
| 243 | + |
| 244 | +```bash |
| 245 | +curl -X POST -H "Content-Type: application/json" -d '{"name":"Author Name"}' http://localhost:8080/authors |
| 246 | +curl -X POST -H "Content-Type: application/json" -d '{"title":"Book Title","author_id":1}' http://localhost:8080/books |
| 247 | +``` |
| 248 | + |
| 249 | +#### Get Request |
| 250 | + |
| 251 | +```bash |
| 252 | +curl -i http://localhost:8080/books |
| 253 | +``` |
| 254 | + |
| 255 | +🎉 Look at you go! With a few simple API calls, you've crafted test cases with mocks! Peek into the Keploy directory and behold the freshly minted `test-1.yml` and `mocks.yml`. |
| 256 | + |
| 257 | +### 🏃♀️ Run the Tests! |
| 258 | + |
| 259 | +Time to put it all to the test: |
| 260 | + |
| 261 | +```bash |
| 262 | +keploy test -c "./app" --delay 5 |
| 263 | +``` |
| 264 | + |
| 265 | +> That `--delay` flag? Just a little pause (in seconds) to let your app catch its breath before the test cases start rolling in. |
| 266 | +
|
| 267 | +When all is said and done, your test results should look a little something like this: |
| 268 | + |
| 269 | + |
| 270 | + |
| 271 | +Final thoughts? Dive deeper! Try different API calls, tweak the DB response in the `mocks.yml`, or fiddle with the request or response in `test-x.yml`. Run the tests again and see the magic unfold! ✨👩💻👨💻✨ |
| 272 | + |
| 273 | +### Wrapping it up 🎉 |
| 274 | + |
| 275 | +Congrats on the journey so far! You've seen Keploy's power, flexed your coding muscles, and had a bit of fun too! Now, go out there and keep exploring, innovating, and creating! Remember, with the right tools and a sprinkle of fun, anything's possible. 😊🚀 |
| 276 | + |
| 277 | +Happy coding! ✨👩💻👨💻✨ |
| 278 | + |
| 279 | +Hope this helps you out, if you still have any questions, reach out to us . |
| 280 | + |
| 281 | +import GetSupport from '../concepts/support.md' |
| 282 | + |
| 283 | +<GetSupport/> |
0 commit comments