diff --git a/README.md b/README.md index 21f5fc38..a28db2e2 100644 --- a/README.md +++ b/README.md @@ -13,24 +13,30 @@ **Note** :- Issue Creation is disabled on this Repository, please visit [here](https://github.com/keploy/keploy/issues/new/choose) to submit Issue. # Keploy Go Samples -This repo contains the sample for [Keploy's](https://keploy.io)🐰 with Golang. Please feel free to contribute if you'd like submit a sample for another use-case or library. +This repo contains the sample for [Keploy's](https://keploy.io)🐰 with Golang. Please feel free to contribute if you'd like submit a sample for another use-case or library. ## Go Sample Apps with Keploy -1. [Echo-SQL](https://github.com/keploy/samples-go/tree/main/echo-sql) -2. [Fasthttp-Postgress](https://github.com/sarthaksarthak9/samples-go/tree/main/fasthttp-postgres) -3. [Gin-Mongo](https://github.com/keploy/samples-go/tree/main/gin-mongo) -4. [Gin-Redis](https://github.com/sarthaksarthak9/samples-go/tree/main/gin-redis) -5. [GraphQL-SQL](https://github.com/keploy/samples-go/tree/main/graphql-sql) -6. [Mux-SQL](https://github.com/keploy/samples-go/tree/main/mux-sql) -7. [S3-Keploy](https://github.com/sarthaksarthak9/samples-go/tree/main/S3-Keploy) -8. [SSE-Svelte](https://github.com/sarthaksarthak9/samples-go/tree/main/sse-svelte) -9. [Users-Profile](https://github.com/keploy/samples-go/tree/main/users-profile) +1. [Echo-SQL](https://github.com/keploy/samples-go/tree/main/echo-sql) : A sample URL Shortener app to test Keploy integration capabilities using Echo and PostgreSQL. +2. [Fasthttp-Postgress](https://github.com/sarthaksarthak9/samples-go/tree/main/fasthttp-postgres) : A sample application that get, create, update, and delete the data of a user in the database +3. [Gin-Mongo](https://github.com/keploy/samples-go/tree/main/gin-mongo) : A sample URL Shortener app to test Keploy integration capabilities using Gin and MongoDB. +4. [Gin-Redis](https://github.com/sarthaksarthak9/samples-go/tree/main/gin-redis) : A sample User Authentication system to test Keploy integration capabilities using Gin and Redis. +5. [GraphQL-SQL](https://github.com/keploy/samples-go/tree/main/graphql-sql) : A sample app to test Keploy integration capabilities using Go Chi, GraphQL and PostgreSQL. +6. [Mux-SQL](https://github.com/keploy/samples-go/tree/main/mux-sql) : A sample URL Shortener app to test Keploy integration capabilities using Mux and PostgreSQL +7. [S3-Keploy](https://github.com/sarthaksarthak9/samples-go/tree/main/S3-Keploy) : A simple CRUD application to showcase Keploy integration capabilities using Go-Fiber and S3. +8. [SSE-Svelte](https://github.com/sarthaksarthak9/samples-go/tree/main/sse-svelte) : A sample app to test Keploy integration capabilities with realtime subscriptions such as SSE. +9. [Users-Profile](https://github.com/keploy/samples-go/tree/main/users-profile) : A sample application that get, create, update, and delete the data of a user in the database (MongoDB for this application). +10. [Echo-MySQL](https://github.com/keploy/samples-go/tree/main/echo-mysql) : A simple golang based URL Shortner. +11. [Mux-MySQL](https://github.com/keploy/samples-go/tree/main/mux-mysql) : This is a simple url shortener written in Golang and MySQL +12. [Mux-Elasticsearch](https://github.com/keploy/samples-go/tree/main/mux-elasticsearch) : This is a sample Go project to show the CRUD operations of golang with elasticsearch and mux. +13. [Go-JWT](https://github.com/keploy/samples-go/tree/main/go-jwt) : A sample app with CRUD operations using Go and JWT. +14. [Go-Twilio](https://github.com/keploy/samples-go/tree/main/go-twilio) : A sample application that sends an SMS using Twilio API. ## Community Support ❀️ ### πŸ€” Questions? + Reach out to us. We're here to help! [![Slack](https://img.shields.io/badge/Slack-4A154B?style=for-the-badge&logo=slack&logoColor=white)](https://join.slack.com/t/keploy/shared_invite/zt-12rfbvc01-o54cOG0X1G6eVJTuI_orSA) @@ -39,6 +45,7 @@ Reach out to us. We're here to help! [![Twitter](https://img.shields.io/badge/Twitter-%231DA1F2.svg?style=for-the-badge&logo=Twitter&logoColor=white)](https://twitter.com/Keployio) ### πŸ’– Let's Build Together! + Whether you're a newbie coder or a wizard πŸ§™β€β™€οΈ, your perspective is golden. Take a peek at our: πŸ“œ [Contribution Guidelines](https://github.com/keploy/keploy/blob/main/CONTRIBUTING.md) diff --git a/echo-mysql/README.md b/echo-mysql/README.md index 68239889..c511974e 100644 --- a/echo-mysql/README.md +++ b/echo-mysql/README.md @@ -1,86 +1,94 @@ -# echo-mysql -A simple golang based url shortner +# Echo-MySQL +A simple Go-based URL shortener application. -# Requirments to run -1. Golang [How to install Golang](https://go.dev/doc/install) -2. Docker [How to install Docker?](https://docs.docker.com/engine/install/) +# Prerequisites +1. Golang: [Install Golang](https://go.dev/doc/install) +2. Docker: [Install Docker](https://docs.docker.com/engine/install/) # Setting up the project -Run the following commands to clone the repository and download the necessary Go modules +Clone the repository and download the necessary Go modules by running the following commands: -``` bash -git clone https://github.com/keploy/samples-go.git && cd samples-go/echo-mysql -go mod download +```bash + git clone https://github.com/keploy/samples-go.git && cd samples-go/echo-mysql + go mod download ``` - # Running app ## Let's start the MySql Instance -``` bash -sudo docker run --name mysql-container -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=uss -p 3306:3306 --rm mysql:latest +Use Docker to run a MySQL container: + +```bash + sudo docker run --name mysql-container -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=uss -p 3306:3306 --rm mysql:latest ``` -## Build the application -``` bash - go build -o echo-mysql . - ``` +## Build the application + +Compile the application with: + +```bash + go build -o echo-mysql . +``` # Capture the Testcases -``` bash -sudo -E env PATH=$PATH oss record -c "./echo-mysql" +To capture test cases, run the application in record mode: + +```bash + sudo -E env PATH=$PATH oss record -c "./echo-mysql" ``` To generate testcases we just need to make some API calls. You can use Postman, Hoppscotch, or simply curl - 1. Root Endpoint: + ```bash --> curl -X GET http://localhost:9090/ + curl -X GET http://localhost:9090/ ``` - 2. Health Check: + ```bash --> curl -X GET http://localhost:9090/healthcheck + curl -X GET http://localhost:9090/healthcheck ``` - 3. Short URL: + ```bash --> curl -X POST http://localhost:9090/shorten -H "Content-Type: application/json" -d '{"url": "https://github.com"}' + curl -X POST http://localhost:9090/shorten -H "Content-Type: application/json" -d '{"url": "https://github.com"}' ``` - 4. Resolve short code: ```bash --> curl -X GET http://localhost:9090/resolve/4KepjkTT + curl -X GET http://localhost:9090/resolve/4KepjkTT ``` -Now both these API calls were captured as a testcase and should be visible on the Keploy CLI. You should be seeing an app named keploy folder with the test cases we just captured and data mocks created. +Captured API calls will appear as test cases in the Keploy CLI. A new directory named keploy will contain the recorded test cases and data mocks. + +## Example Screenshot ![alt text](https://github.com/Hermione2408/samples-go/blob/app/echo-mysql/img/keploy_record.png?raw=true) # Run the captured testcases -Now that we have our testcase captured, run the test file. +Once the test cases are captured, you can run them with the following command: ```bash -sudo -E env PATH=$PATH oss test -c "./echo-mysql" --delay 20 + sudo -E env PATH=$PATH oss test -c "./echo-mysql" --delay 20 ``` -So no need to setup dependencies like MySQL, web-go locally or write mocks for your testing. +## Explanation -oss test runs the test cases captured in the previous step. It replays the captured API calls against the application to verify its behavior. +- The oss test command replays captured API calls to verify application behavior. +- Dependencies like MySQL are mocked during testing, so no additional setup is required. -The application thinks it's talking to MySQL πŸ˜„ +The application will behave as if it is connected to MySQL, enabling seamless testing without manual mocks. -We will get output something like this: +## Example Output -![alt text](https://github.com/Hermione2408/samples-go/blob/app/echo-mysql/img/keploy_test.png?raw=true) \ No newline at end of file +![alt text](https://github.com/Hermione2408/samples-go/blob/app/echo-mysql/img/keploy_test.png?raw=true) diff --git a/echo-sql/README.md b/echo-sql/README.md index 968c8292..deb989bb 100644 --- a/echo-sql/README.md +++ b/echo-sql/README.md @@ -1,43 +1,45 @@ # URL Shortener -A sample url shortener app to test Keploy integration capabilities using [Echo](https://echo.labstack.com/) and PostgreSQL. +A sample url shortener app to test Keploy integration capabilities using [Echo](https://echo.labstack.com/) and PostgreSQL. -## Installation Setup +## Installation and Setup + +### 1. Clone the Repository ```bash git clone https://github.com/keploy/samples-go.git && cd samples-go/echo-sql go mod download ``` -## Installation Keploy +### 2. Install Keploy -Install keploy via one-click:- +Install Keploy with a one-liner command: ```sh curl --silent -O -L https://keploy.io/install.sh && source install.sh ``` -### Start Postgres Instance +## Setting up PostgreSQL + +### Start Postgres Instance -Using the docker-compose file we will start our postgres instance ( use `podman-compose` if you are using RHEL based distribution ):- +Start the PostgreSQL instance using the provided docker-compose file (use podman-compose for RHEL-based distributions): ```bash -# 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 +If you encounter an error about the keploy-network, create the Docker network manually: ```bash docker network create keploy-network ``` - ### Update the Host > **Since we have setup our sample-app natively, we need to update the Postgres host on line 27, in `main.go`, from `postgresDb` to `localhost`.** -### Capture the testcases +## Capture the testcases Now, we will create the binary of our application:- @@ -53,8 +55,7 @@ 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. - -#### Generate testcases +## Generate testcases To generate testcases we just need to make some API calls. You can use [Postman](https://www.postman.com/), [Hoppscotch](https://hoppscotch.io/), or simply `curl` @@ -78,9 +79,10 @@ this will return the shortened url. The ts would automatically be ignored during } ``` -### Redirect to original URL from shortened URL +### Redirect to original URL from shortened URL 1. By using Curl Command + ```bash curl --request GET \ --url http://localhost:8082/GuwHCgoQ @@ -102,7 +104,6 @@ Now that we have our testcase captured, we will add `ts` to noise field in `test ![EliminateNoise](https://github.com/aswinbennyofficial/samples-go/assets/110408942/2b50d994-3418-4f7b-9f95-5bc1acd8ecf9) - Now let's run the test mode (in the echo-sql directory, not the Keploy directory). ```shell diff --git a/gin-mongo/README.md b/gin-mongo/README.md index 7eb3a44b..2cceefa8 100644 --- a/gin-mongo/README.md +++ b/gin-mongo/README.md @@ -1,15 +1,17 @@ ## Introduction -A sample url shortener app to test Keploy integration capabilities using [Gin](https://gin-gonic.com/) and [mongoDB](https://www.mongodb.com/). +This is a sample URL shortener app built using Gin and MongoDB. It demonstrates Keploy's integration capabilities for automated API testing and mocking. ## Setup URL shortener +### Clone the repository + ```bash git clone https://github.com/keploy/samples-go.git && cd samples-go/gin-mongo go mod download ``` -## Installation +### Install Keploy ```bash curl --silent -O -L https://keploy.io/install.sh && source install.sh @@ -19,24 +21,25 @@ curl --silent -O -L https://keploy.io/install.sh && source install.sh Keploy can be used on Linux, Windows and MacOS through [Docker](https://docs.docker.com/engine/install/). -> Note: To run Keploy on MacOS through [Docker](https://docs.docker.com/desktop/release-notes/#4252) the version must be ```4.25.2``` or above. +> Note: To run Keploy on MacOS through [Docker](https://docs.docker.com/desktop/release-notes/#4252) the version must be `4.25.2` or above. +## Start MongoDB Instance -### Let's start the MongoDB Instance Using the docker-compose file we will start our mongodb instance:- ```bash sudo docker run -p 27017:27017 -d --network keploy-network --name mongoDb mongo ``` -Now, we will create the docker image of our application:- +## Build and Run the App +Now, we will create the docker image of our application:- ```bash docker build -t gin-app:1.0 . ``` -### Capture the Testcases +## Capture the Testcases ```shell keploy record -c "docker run -p 8080:8080 --name MongoApp --network keploy-network gin-app:1.0" @@ -51,11 +54,11 @@ curl --request POST \ --url http://localhost:8080/url \ --header 'content-type: application/json' \ --data '{ - "url": "https://google.com" + "url": "https://keploy.io" }' ``` -this will return the shortened url. +this will return the shortened url. ```json { @@ -75,7 +78,7 @@ or by querying through the browser `http://localhost:8080/Lhr4BWAi` _Now, let's Now both these API calls were captured as a testcase and should be visible on the Keploy CLI. You should be seeing an app named `keploy folder` with the test cases we just captured and data mocks created. -### Run the captured testcases +## Run the captured testcases Now that we have our testcase captured, run the test file. @@ -107,7 +110,7 @@ This time all the test cases will pass. ## Run app Natively on local machine -#### Let's start the MongoDB Instance +### Let's start the MongoDB Instance Spin up your mongo container using @@ -200,4 +203,4 @@ sudo -E keploy test -c "./test-app-url-shortener" --goCoverage --delay 10 This time all the test cases will pass. -![testruns](./img/testrun-coverage.png?raw=true "Recent testruns") \ No newline at end of file +![testruns](./img/testrun-coverage.png?raw=true "Recent testruns") diff --git a/go-jwt/README.md b/go-jwt/README.md index 86d27767..88487748 100644 --- a/go-jwt/README.md +++ b/go-jwt/README.md @@ -1,5 +1,6 @@ # Go-JWT -A sample app + +A sample app with CRUD operations using Go and JWT. ## Installation Setup @@ -9,6 +10,7 @@ go mod download ``` ## Installation Keploy + Install keploy via one-click:- ```sh @@ -48,26 +50,29 @@ curl --request GET \ --url http://localhost:8000/health \ --header 'Accept: */*' \ --header 'Host: localhost:8000' \ - --header 'User-Agent: curl/7.81.0' + --header 'User-Agent: curl/7.81.0' ``` -this will return the response. + +this will return the response. + ``` {"status": "healthy"} ``` 2. Fetch the Products + ```bash curl --request GET \ --url http://localhost:8000/generate-token \ --header 'Host: localhost:8000' \ --header 'User-Agent: curl/7.81.0' \ - --header 'Accept: */*' + --header 'Accept: */*' ``` we will get output: ```json -{"token":""} +{ "token": "" } ``` 3. Fetch a single product @@ -77,15 +82,16 @@ curl --request GET \ --url http://localhost:8000/check-token?token= \ --header 'Accept: */*' \ --header 'Host: localhost:8000' \ - --header 'User-Agent: curl/7.81.0' + --header 'User-Agent: curl/7.81.0' ``` we will get output:- + ```json -{"username" : "example_user"} +{ "username": "example_user" } ``` -Now, since these API calls were captured as editable testcases and written to ``keploy/tests folder``. The keploy directory would also have `mocks` files that contains all the outputs. +Now, since these API calls were captured as editable testcases and written to `keploy/tests folder`. The keploy directory would also have `mocks` files that contains all the outputs. ![Testcase](./img/testcase.png?raw=true) @@ -100,5 +106,3 @@ sudo -E keploy test -c "./go-jwt" --delay 10 Once done, you can see the Test Runs on the Keploy server, like this: ![Testrun](./img/testrun.png?raw=true) - - diff --git a/mux-mysql/readme.md b/mux-mysql/readme.md index 2dafc2de..c985b646 100644 --- a/mux-mysql/readme.md +++ b/mux-mysql/readme.md @@ -1,189 +1,186 @@ # Introduction -This is a simple url shortener written in Golang and MySQL -# Requirments to run -1. Golang [How to install Golang](https://go.dev/doc/install) -2. Docker [How to install Docker?](https://docs.docker.com/engine/install/) +This is a simple URL shortener application written in **Golang** and **MySQL**. -# Setting up the project -Run the following commands to setup the project +## Requirements -``` bash +To run this application, ensure the following are installed: + +1. **Golang** – [How to install Golang](https://go.dev/doc/install) +2. **Docker** – [How to install Docker](https://docs.docker.com/engine/install) + +--- + +## Setting Up the Project + +Clone the repository and install dependencies: + +```bash git clone https://github.com/keploy/samples-go.git && cd samples-go/mux-mysql go mod download ``` -# Installing Keploy - -``` bash -curl -O https://raw.githubusercontent.com/keploy/keploy/main/keploy.sh && source keploy.sh +# Installing Keploy + +Install Keploy with the following command: + +```bash +curl --silent -O -L https://keploy.io/install.sh && source install.sh ``` # Running the Application + ### Method 1 : Using natively installed Keploy + 1. Spin up the mysql docker container - - ``` bash - docker run -p 3306:3306 --rm --name mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:latest - ``` - -2. The MySQL Database takes some time to be ready after spinning up the container, you can wait a minute or just run the following command to monitor the logs - ``` bash - watch -n 1 docker logs mysql - ``` - -3. Once we get the following message in the logs we are ready to start our golang server + ```bash + docker run -p 3306:3306 --rm --name mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:latest + ``` + +2. Wait for MySQL to initialize: + Use the following command to monitor MySQL logs and confirm it’s ready: + +```bash +watch -n 1 docker logs mysql +``` + +Once the logs display the following message, MySQL is ready: + +```bash +[System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete +``` + +3. Build the Golang application: + Set up the database connection string and build the executable: + +```bash +export ConnectionString="root:my-secret-pw@tcp(localhost:3306)/mysql" +go build -o main -cover +``` + +> If you encounter an error like bad connection, it indicates MySQL is not ready yet. Wait for MySQL logs to confirm readiness (Step 2). - ``` bash - 2024-01-19 13:53:43+00:00 [Note] [Entrypoint]: Stopping temporary server - 2024-01-19T13:53:43.854798Z 10 [System] [MY-013172] [Server] Received SHUTDOWN from user root. Shutting down mysqld (Version: 8.2.0). - 2024-01-19T13:54:00.148602Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.2.0) MySQL Community Server - GPL. - ``` - -4. Run the following command to build the executable out of our golang code - - ``` bash - export ConnectionString="root:my-secret-pw@tcp(localhost:3306)/mysql" - - go build -o main -cover - ``` - - If you get some error like this after running `./main`, - - ``` bash - [mysql] 2024/01/19 21:28:04 packets.go:37: unexpected EOF - [mysql] 2024/01/19 21:28:04 packets.go:37: unexpected EOF - [mysql] 2024/01/19 21:28:04 packets.go:37: unexpected EOF - 2024/01/19 21:28:04 Couldnt create store driver: bad connection - ``` - - This means that the mysql db is not ready yet, watch out for logs in **Step 2** and wait for 20-30s - - -5. To capture test cases for our application use the following command - - ``` bash +4. Capture test cases with Keploy: + Start recording API calls by running: + + ```bash keploy record -c "./main" - ``` + ``` + ![Running Keploy](https://github.com/heyyakash/samples-go/assets/85030597/05644fa2-64eb-4cd2-8c08-0c1d0690528f) - - -6. To generate test cases, we can simply use cURL or Postman. - - ``` bash - curl -X POST localhost:8080/create \ - -d '{"link":"https://google.com"}' - ``` - - This should return the following output - - ``` json - { - "message":"Converted", - "link":"http://localhost:8080/link/1", - "status":true - } - - ``` - -7. To get all the links, run - - ``` bash - curl localhost:8080/all - ``` - -8. To get redirected to the real url, just use the shortened link in browser or run - - ``` bash - curl localhost:8080/links/1 - ``` - -9. All these test cases are captured by Keploy - -![Screenshot from 2024-01-19 20-18-44](https://github.com/heyyakash/samples-go/assets/85030597/5b43b17b-81ef-453f-b843-cb9a7619d157) +5. Generate test cases: + Use `curl` or Postman to make API requests. -#### Running the test cases -1. Just one simple command, run + ```bash + curl -X POST localhost:8080/create \ + -d '{"link":"https://google.com"}' + ``` + + This should return the following output - ``` bash - keploy test -c "./main" --goCoverage + ```json + { + "message": "Converted", + "link": "http://localhost:8080/link/1", + "status": true + } ``` - -![Screenshot from 2024-01-19 20-21-49](https://github.com/heyyakash/samples-go/assets/85030597/8167df44-14ec-4037-a768-5e19f8a81826) -3. Done.. +6. To get all the links, run + ```bash + curl localhost:8080/all + ``` + +7. To get redirected to the real url, just use the shortened link in browser or run + + ```bash + curl localhost:8080/links/1 + ``` + +8. All these test cases are captured by Keploy + +![Screenshot from 2024-01-19 20-18-44](https://github.com/heyyakash/samples-go/assets/85030597/5b43b17b-81ef-453f-b843-cb9a7619d157) + +#### Run captured test cases: + +Use the following command to execute tests and measure code coverage: + +```bash + keploy test -c "./main" --goCoverage +``` + +![Screenshot from 2024-01-19 20-21-49](https://github.com/heyyakash/samples-go/assets/85030597/8167df44-14ec-4037-a768-5e19f8a81826) ## Method 2 : Using Dockerized version + 1. Spin up the mysql docker container - ``` bash + ```bash docker run -p 3306:3306 --rm --name mysql --network keploy-network -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:latest - ``` - + ``` + 2. The MySQL Database takes some time to be ready after spinning up the container, you can wait a minute or just run the following command to monitor the logs - ``` bash + ```bash watch -n 1 docker logs mysql - ``` - + ``` + 3. Once we get the following message in the logs we are ready to start our golang server - ``` bash - 2024-01-19 13:53:43+00:00 [Note] [Entrypoint]: Stopping temporary server - 2024-01-19T13:53:43.854798Z 10 [System] [MY-013172] [Server] Received SHUTDOWN from user root. Shutting down mysqld (Version: 8.2.0). - 2024-01-19T13:54:00.148602Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.2.0) MySQL Community Server - GPL. - ``` - + ```bash + 2024-01-19 13:53:43+00:00 [Note] [Entrypoint]: Stopping temporary server + 2024-01-19T13:53:43.854798Z 10 [System] [MY-013172] [Server] Received SHUTDOWN from user root. Shutting down mysqld (Version: 8.2.0). + 2024-01-19T13:54:00.148602Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.2.0) MySQL Community Server - GPL. + ``` + 4. Build the docker image of the application - ``` bash + ```bash docker build -t url-short . - ``` - + ``` + 5. To start the server with keploy, run - ``` bash - keploy record -c "docker run -p 8080:8080 --name urlshort --rm --network keploy-network url-short:latest" - ``` - There you go, keploy has started capturing incomming requests + ```bash + keploy record -c "docker run -p 8080:8080 --name urlshort --rm --network keploy-network url-short:latest" + ``` - ![Running Keploy](https://github.com/heyyakash/samples-go/assets/85030597/2b4f3c04-4631-4f9a-b317-7fdb6db87879) - -7. Make some API Requests: - - - ``` bash - curl -X POST localhost:8080/create \ - -d '{"link":"https://google.com"}' - ``` - - This should return the following output + There you go, keploy has started capturing incomming requests - ``` json - { - "message":"Converted", - "link":"http://localhost:8080/link/1", - "status":true - } - - ``` - - To get all the links, run + ![Running Keploy](https://github.com/heyyakash/samples-go/assets/85030597/2b4f3c04-4631-4f9a-b317-7fdb6db87879) - ``` bash - curl localhost:8080/all - ``` +6. Make some API Requests: - - ![Screenshot from 2024-01-19 20-36-09](https://github.com/heyyakash/samples-go/assets/85030597/eb17602d-c3cd-43e1-bf64-a55af62902f2) + ```bash + curl -X POST localhost:8080/create \ + -d '{"link":"https://google.com"}' + ``` + + This should return the following output -8. To run the tests, run + ```json + { + "message": "Converted", + "link": "http://localhost:8080/link/1", + "status": true + } + ``` - ``` bash - keploy test -c "docker run -p 8080:8080 --name urlshort --rm --network keploy-network url-short:latest" - ``` + To get all the links, run - ![Screenshot from 2024-01-19 20-38-08](https://github.com/heyyakash/samples-go/assets/85030597/472cab5e-9687-4fc5-bd57-3c52f56feedf) + ```bash + curl localhost:8080/all + ``` + ![Screenshot from 2024-01-19 20-36-09](https://github.com/heyyakash/samples-go/assets/85030597/eb17602d-c3cd-43e1-bf64-a55af62902f2) + +7. To run the tests, run + + ```bash + keploy test -c "docker run -p 8080:8080 --name urlshort --rm --network keploy-network url-short:latest" + ``` + ![Screenshot from 2024-01-19 20-38-08](https://github.com/heyyakash/samples-go/assets/85030597/472cab5e-9687-4fc5-bd57-3c52f56feedf) diff --git a/mux-sql/README.md b/mux-sql/README.md index 6639b1a7..341731f5 100644 --- a/mux-sql/README.md +++ b/mux-sql/README.md @@ -1,23 +1,27 @@ -# Product Catelog -A sample url shortener app to test Keploy integration capabilities +# Product Catalog + +A sample URL shortener application to test Keploy's integration capabilities. ## Installation Setup +Clone the repository and install dependencies: + ```bash git clone https://github.com/keploy/samples-go.git && cd samples-go/mux-sql go mod download ``` ## Installation Keploy + Install keploy via one-click:- ```sh curl --silent -O -L https://keploy.io/install.sh && source install.sh ``` -### Start Postgres Instance +## Start Postgres Instance -Using the docker-compose file we will start our postgres instance:- +Use the provided `docker-compose` file to start a PostgreSQL instance: ```bash docker-compose up -d postgres @@ -27,15 +31,19 @@ docker-compose up -d postgres > **Since we have setup our sample-app natively set the host to `localhost` on line 10.** -### Capture the Testcases +## Capture the Testcases -Now, we will create the binary of our application:- +### Build the Application Binary + +Generate the binary for the application: ```zsh go build -cover ``` -Once we have our binary file ready,this command will start the recording of API calls using ebpf:- +### Record API Calls + +Use the Keploy `record` command to start capturing API calls via `eBPF`: ```shell sudo -E keploy record -c "./test-app-product-catelog" @@ -54,11 +62,13 @@ curl --request POST \ --url http://localhost:8010/product \ --header 'content-type: application/json' \ --data '{ - "name":"Bubbles", + "name":"Bubbles", "price": 123 }' ``` -this will return the response. + +this will return the response. + ``` { "id": 1, @@ -68,6 +78,7 @@ this will return the response. ``` 2. Fetch the Products + ```bash curl --request GET \ --url http://localhost:8010/products @@ -76,21 +87,24 @@ curl --request GET \ we will get output: ```json -[{"id":1,"name":"Bubbles","price":123},{"id":2,"name":"Bubbles","price":123}] +[ + { "id": 1, "name": "Bubbles", "price": 123 }, + { "id": 2, "name": "Bubbles", "price": 123 } +] ``` 3. Fetch a single product -```sh +````sh curl --request GET \ --url http://localhost:8010/product/1 we will get output:- ```json {"id":1,"name":"Bubbles","price":123} -``` +```` -Now, since these API calls were captured as editable testcases and written to ``keploy/tests folder``. The keploy directory would also have `mocks` files that contains all the outputs of postgres operations. +Now, since these API calls were captured as editable testcases and written to `keploy/tests folder`. The keploy directory would also have `mocks` files that contains all the outputs of postgres operations. ![Testcase](./img/testcase.png?raw=true) @@ -115,18 +129,19 @@ With the three API calls that we made, we got around `55.6%` of coverage ## Create Unit Testcase with Keploy ### Prequiste + AI model API_KEY to use: - OpenAI's GPT-4o. - Alternative LLMs via [litellm](https://github.com/BerriAI/litellm?tab=readme-ov-file#quick-start-proxy---cli). -### Setup +### Setup Dependencies -Download Cobertura formatted coverage report dependencies. +Install dependencies for generating Cobertura-formatted coverage reports: ```bash go install github.com/axw/gocov/gocov@v1.1.0 -go install github.com/AlekSi/gocov-xml@v1.1.0 +go install github.com/AlekSi/gocov-xml@v1.1.0 ``` Get API key from [OpenAI](https://platform.openai.com/) or API Key from other LLM @@ -137,11 +152,12 @@ export API_KEY= ### Generate Unit tests -Let's check the current code coverage of out application : - +Let's check the current code coverage of out application : - ```bash go test -v ./... -coverprofile=coverage.out && gocov convert coverage.out | gocov-xml > coverage.xml ``` + We got around 44.1% of code coverage. ![Go Test](./img/mux-utg.png?raw=true) @@ -154,4 +170,4 @@ keploy gen --sourceFilePath="app.go" --testFilePath="app_test.go" --testCommand= With the above command, Keploy will generate new testcases in the our `app_test.go` can increase code coverage upto 70%. -![Keploy Mux UTG](./img/mux-utg-codecov.png?raw=true) \ No newline at end of file +![Keploy Mux UTG](./img/mux-utg-codecov.png?raw=true)