Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating and Fixing the Sample Apps (W.I.P.) #124

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
76 changes: 42 additions & 34 deletions echo-mysql/README.md
Original file line number Diff line number Diff line change
@@ -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)
![alt text](https://github.com/Hermione2408/samples-go/blob/app/echo-mysql/img/keploy_test.png?raw=true)
29 changes: 15 additions & 14 deletions echo-sql/README.md
Original file line number Diff line number Diff line change
@@ -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:-

Expand All @@ -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`

Expand All @@ -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
Expand All @@ -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
Expand Down
25 changes: 14 additions & 11 deletions gin-mongo/README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"
Expand All @@ -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
{
Expand All @@ -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.

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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")
![testruns](./img/testrun-coverage.png?raw=true "Recent testruns")
Loading