|
| 1 | + |
| 2 | +# Express Redis Sample |
| 3 | + |
| 4 | +## Overview |
| 5 | + |
| 6 | +This API manages books record, built with Express and Redis. It provides routes to add, update, delete, view books, view books by ID, and change task authors. |
| 7 | + |
| 8 | +## Installation |
| 9 | + |
| 10 | +### Prerequisites |
| 11 | + |
| 12 | +Ensure you have the following installed: |
| 13 | + |
| 14 | +- Docker |
| 15 | +- Node.js and npm |
| 16 | +- Keploy CLI |
| 17 | + |
| 18 | +# Installing Keploy |
| 19 | + |
| 20 | +Let's get started by setting up the Keploy alias with this command: |
| 21 | + |
| 22 | +```sh |
| 23 | +curl -O https://raw.githubusercontent.com/keploy/keploy/main/keploy.sh && source keploy.sh |
| 24 | +``` |
| 25 | + |
| 26 | +Clone the repository and move to express-redis folder |
| 27 | + |
| 28 | +```bash |
| 29 | +git clone https://github.com/keploy/samples-typescript.git |
| 30 | +cd samples-typescript/express-redis |
| 31 | +``` |
| 32 | +## Installing Locally |
| 33 | + |
| 34 | +### Install the dependencies |
| 35 | +```bash |
| 36 | +npm install |
| 37 | +``` |
| 38 | + |
| 39 | +### Start Redis-server Container |
| 40 | +```bash |
| 41 | +docker run -d --name redis-stack-server -p 6379:6379 redis/redis-stack-server:latest |
| 42 | +``` |
| 43 | + |
| 44 | +### Load the Samples and build server: |
| 45 | + |
| 46 | +In case of running application natively, we will have to change the host to localhost in index.ts |
| 47 | + |
| 48 | +```bash |
| 49 | +const redisClient = new Redis({ |
| 50 | +``` |
| 51 | +
|
| 52 | +If running server on docker |
| 53 | +
|
| 54 | +```bash |
| 55 | +host: 'redis', |
| 56 | +``` |
| 57 | +
|
| 58 | +If running server locally |
| 59 | +
|
| 60 | +```bash |
| 61 | +host: 'localhost' |
| 62 | +port: 6379 |
| 63 | +``` |
| 64 | +
|
| 65 | +### Build the application: |
| 66 | +```bash |
| 67 | +npm install |
| 68 | +npm run build |
| 69 | +``` |
| 70 | +
|
| 71 | +### Start the application: |
| 72 | +```bash |
| 73 | +npm run start |
| 74 | +``` |
| 75 | +
|
| 76 | +
|
| 77 | +> redis-ts@1.0.0 start |
| 78 | +> node dist/index.js |
| 79 | +
|
| 80 | +
|
| 81 | +```bash |
| 82 | +Server is running on http://localhost:3000 |
| 83 | +Connected to Redis server |
| 84 | +Sample books loaded into Redis |
| 85 | +``` |
| 86 | +
|
| 87 | +Some sample data is automatically created in redis. |
| 88 | +
|
| 89 | +
|
| 90 | +## Using Docker |
| 91 | +
|
| 92 | +Make sure you have docker installed. |
| 93 | +
|
| 94 | +```bash |
| 95 | +git clone https://github.com/keploy/samples-typescript.git |
| 96 | +cd samples-typescript/express-redis |
| 97 | +docker compose up |
| 98 | +``` |
| 99 | +
|
| 100 | +### Generate Test Cases |
| 101 | +
|
| 102 | +For server running locally. |
| 103 | +```bash |
| 104 | +keploy record -c "npm run start" |
| 105 | +``` |
| 106 | +
|
| 107 | +For the docker container. |
| 108 | +
|
| 109 | +```bash |
| 110 | +keploy record -c "docker compose up" --container-name "redis-express" -n "keploy-network" |
| 111 | +``` |
| 112 | +
|
| 113 | +The above command will start recording the API calls made to the application and will generate a test case in the `testcases/` directory. |
| 114 | +
|
| 115 | +> 💡 You can use Postman ,CURL or any other API testing tool to test the API calls. |
| 116 | +
|
| 117 | +### Interact with Application |
| 118 | +
|
| 119 | +Use Postman, CURL, or any other API testing tool to hit your API routes. This interaction will be recorded by Keploy. |
| 120 | +
|
| 121 | +Some curl commands to easily test the application are |
| 122 | +
|
| 123 | +1. "http://localhost:3000/books" to retrieve the data of the books |
| 124 | +
|
| 125 | +```bash |
| 126 | +curl --location 'http://localhost:3000/books' |
| 127 | +``` |
| 128 | +
|
| 129 | +2. "http://localhost:3000/books/:id" to retrieve the data of a specific book |
| 130 | +
|
| 131 | +```bash |
| 132 | +curl --location 'http://localhost:3000/books/2' |
| 133 | +``` |
| 134 | +
|
| 135 | +3. "http://localhost:3000/publish" to publish a book |
| 136 | +
|
| 137 | +```bash |
| 138 | +curl --location 'http://localhost:3000/publish' --header 'Content-Type: application/json' --data '{ |
| 139 | + "id":"6", |
| 140 | + "title":"Harry Potter and the Prisoner of Azkaban", |
| 141 | + "author":"J.K. Rowling" |
| 142 | +}' |
| 143 | +``` |
| 144 | +
|
| 145 | +4. "http://localhost:3000/books/:id" to delete a book |
| 146 | +
|
| 147 | +```bash |
| 148 | +curl --location --request DELETE 'http://localhost:3000/books/1' |
| 149 | +``` |
| 150 | +
|
| 151 | +5. "http://localhost:3000/books/:id" to update a book |
| 152 | +
|
| 153 | +```bash |
| 154 | +curl --location --request PUT 'http://localhost:3000/books/6' --header 'Content-Type: application/json' --data '{ |
| 155 | + "title":"Harry Potter and the Half-Blood Prince", |
| 156 | + "author":"J.K. Rowling" |
| 157 | +}' |
| 158 | +``` |
| 159 | +
|
| 160 | +Observe test run results: |
| 161 | +Voila!! Our test cases have passed 🌟 |
| 162 | +
|
| 163 | +### Test the Application using Keploy |
| 164 | +
|
| 165 | +For server running locally. |
| 166 | +```bash |
| 167 | +keploy test -c "npm run start" |
| 168 | +``` |
| 169 | +
|
| 170 | +For the docker container. |
| 171 | +
|
| 172 | +```bash |
| 173 | +keploy test -c "docker compose up" --container-name "redis-express" -n "keploy-network" |
| 174 | +``` |
| 175 | +
|
| 176 | +Keploy will replay the recorded interactions and validate the responses against the expected results. |
| 177 | +
|
| 178 | +Voila! 🎉 You have successfully tested the application using Keploy. Keploy also generates coverage reports for the test-suites. |
0 commit comments