A sample url shortener app to test Keploy integration capabilities using Echo and PostgreSQL.
git clone https://github.com/keploy/samples-go.git && cd samples-go/echo-sql
go mod download
Install keploy via one-click:-
curl --silent -O -L https://keploy.io/install.sh && source install.sh
Using the docker-compose file we will start our postgres instance ( use podman-compose
if you are using RHEL based distribution ):-
# Start Postgres
docker-compose up -d postgres
If there is an error saying keploy-network could not be found. Use the following command to create the docker network
docker network create keploy-network
Since we have setup our sample-app natively, we need to update the Postgres host on line 27, in
main.go
, frompostgresDb
tolocalhost
.
Now, we will create the binary of our application:-
go build -cover
Once we have our binary file ready,this command will start the recording of API calls using ebpf:-
sudo -E keploy record -c "./echo-psql-url-shortener"
Make API Calls using Hoppscotch, Postman or cURL command. Keploy with capture those calls to generate the test-suites containing testcases and data mocks.
To generate testcases we just need to make some API calls. You can use Postman, Hoppscotch, or simply curl
curl --request POST \
--url http:https://localhost:8082/url \
--header 'content-type: application/json' \
--data '{
"url": "https://github.com"
}'
this will return the shortened url. The ts would automatically be ignored during testing because it'll always be different.
{
"ts": 1647802058801841100,
"url": "http:https://localhost:8082/GuwHCgoQ"
}
- By using Curl Command
curl --request GET \
--url http:https://localhost:8082/GuwHCgoQ
- Or by querying through the browser
http:https://localhost:8082/GuwHCgoQ
Now both these API calls were captured as editable testcases and written to keploy/tests
folder. The keploy directory would also have mocks
file that contains all the outputs of postgres operations. Here's what the folder structure look like:
Now, let's see the magic! ✨💫
Now that we have our testcase captured, we will add ts
to noise field in test-*.yaml
files.
1. On line 32 we will add "body.ts: []
" under the "header.Date: []
".
Now let's run the test mode (in the echo-sql directory, not the Keploy directory).
sudo -E keploy test -c "./echo-psql-url-shortener" --delay 10 --goCoverage
output should look like
So no need to setup fake database/apis like Postgres or write mocks for them. Keploy automatically mocks them and, The application thinks it's talking to Postgres 😄