11# CRUD app using MongoDB
22
3- This repository contains a sample .NET 7 Web API that uses MongoDB for CRUD operations and integrates with [ Keploy] ( https://keploy.io ) to auto-generate and run test cases by recording real API traffic.
3+ This repository contains a sample .NET 8 Web API that uses MongoDB for CRUD operations and integrates with [ Keploy] ( https://keploy.io ) to auto-generate and run test cases by recording real API traffic.
44
55---
66
@@ -16,57 +16,205 @@ This repository contains a sample .NET 7 Web API that uses MongoDB for CRUD oper
1616
1717### 1. Clone the Repository
1818
19- ``` bash
19+ ``` bash
2020git clone https://github.com/Pranav5255/samples-mongodb.git
2121cd samples-mongodb
2222```
2323
24- ### 2. Configure MongoDB
25- Update ` appsettings.json ` with your MongoDB connection string:
24+ ### 2. Install Prerequisites
2625
27- ``` json
26+ #### Install .NET 8 SDK and Runtime
27+
28+ ``` bash
29+ # Download and install Microsoft repository configuration
30+ wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
31+ sudo dpkg -i packages-microsoft-prod.deb
32+ rm packages-microsoft-prod.deb
33+
34+ # Update package lists
35+ sudo apt-get update
36+
37+ # Install .NET 8 Runtime and SDK
38+ sudo apt-get install -y dotnet-runtime-8.0 aspnetcore-runtime-8.0 dotnet-sdk-8.0
39+
40+ # Verify installation
41+ dotnet --version
42+ ```
43+
44+ #### Install MongoDB
45+
46+ ``` bash
47+ # Import MongoDB public GPG key
48+ wget -qO - https://www.mongodb.org/static/pgp/server-7.0.asc | sudo apt-key add -
49+
50+ # Create list file for MongoDB
51+ echo " deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
52+
53+ # Update package database
54+ sudo apt-get update
55+
56+ # Install MongoDB
57+ sudo apt-get install -y mongodb-org
58+
59+ # Start MongoDB
60+ sudo systemctl start mongod
61+
62+ # Enable MongoDB to start on boot
63+ sudo systemctl enable mongod
64+
65+ # Verify MongoDB is running
66+ sudo systemctl status mongod
67+ ```
68+
69+ ### 3. Configure MongoDB
70+
71+ Update ` appsettings.json ` with your MongoDB connection string:
72+
73+ ``` json
2874{
29- "ConnectionString" : " mongodb://localhost:27017" ,
30- "DatabaseName" : " UserDb" ,
31- "UsersCollectionName" : " Users"
75+ "MongoDBSettings" : {
76+ "ConnectionString" : " mongodb://localhost:27017" ,
77+ "DatabaseName" : " UserDb" ,
78+ "UsersCollectionName" : " Users"
79+ }
3280}
3381```
34- ### 3. Run the .NET App
35- ``` bash
82+
83+ ### 4. Run the .NET App
84+
85+ ``` bash
3686dotnet run
3787```
3888
3989App runs by default at:
40- ``` arduino
90+
91+ ```
4192http://localhost:5067
4293```
4394
4495You can test endpoints like:
45- ``` http
96+
97+ ``` http
4698GET /users
4799POST /users
48100GET /users/{id}
101+ PUT /users/{id}
102+ DELETE /users/{id}
103+ ```
104+
105+ #### Test with cURL
106+
107+ ``` bash
108+ # Create a user
109+ curl -X POST " http://localhost:5067/users" \
110+ -H " Content-Type: application/json" \
111+ -d ' {"name":"Pranav","age":24}'
112+
113+ # Get all users
114+ curl http://localhost:5067/users
49115```
50116
117+ ---
118+
51119## 🐾 Generate Test Cases with Keploy
52- ### 1. Download Keploy
53- ``` powershell
54- curl -O -L https://keploy.io/install.sh && source install.sh
120+
121+ ### 1. Install Keploy
122+
123+ ``` bash
124+ # Download Keploy binary
125+ curl -L https://github.com/keploy/keploy/releases/latest/download/keploy_linux_amd64.tar.gz -o keploy.tar.gz
126+
127+ # Extract it
128+ tar -xzf keploy.tar.gz
129+
130+ # Move to /usr/local/bin
131+ sudo mv keploy /usr/local/bin/
132+
133+ # Make it executable
134+ sudo chmod +x /usr/local/bin/keploy
135+
136+ # Clean up
137+ rm keploy.tar.gz
138+
139+ # Verify installation
140+ keploy version
55141```
56142
57143### 2. Record API Calls
58- ``` bash
59- keploy.exe record --proxy-port 8080 --app-port 5067 --path ./keploy-tests
144+
145+ ** Important:** Keploy requires elevated permissions to use eBPF for intercepting network calls. You must run it with ` sudo ` :
146+
147+ ``` bash
148+ sudo -E env " PATH=$PATH " keploy record -c " dotnet run" --proxy-port 8080 --path ./keploy-tests
60149```
61150
62- Now send traffic to:
63- ``` bash
64- http://localhost:8080/users
151+ The application will start and Keploy will begin recording. Keep this terminal running.
152+
153+ ### 3. Generate Traffic
154+
155+ Open a ** new terminal** and send traffic to your application:
156+
157+ ``` bash
158+ # Create a user
159+ curl -X POST " http://localhost:5067/users" \
160+ -H " Content-Type: application/json" \
161+ -d ' {"name":"John","age":30}'
162+
163+ # Get all users
164+ curl http://localhost:5067/users
165+
166+ # Get user by ID (replace {id} with actual ID from response)
167+ curl http://localhost:5067/users/{id}
65168```
66- Use Postman, Swagger or cURL to generate traffic.
67169
68- ### 3. Replay Tests
69- After Recording:
70- ``` bash
71- keploy.exe test --path ./keploy-tests
170+ Keploy will automatically capture these requests and responses as test cases in the ` ./keploy-tests ` directory.
171+
172+ ### 4. Stop Recording
173+
174+ Press ` Ctrl+C ` in the terminal where Keploy is running to stop recording.
175+
176+ ### 5. Replay Tests
177+
178+ After recording, replay the captured test cases:
179+
180+ ``` bash
181+ sudo -E env " PATH=$PATH " keploy test -c " dotnet run" --path ./keploy-tests --delay 10
72182```
183+
184+ The ` --delay 10 ` flag gives the .NET application 10 seconds to start before running tests.
185+
186+ ---
187+
188+ ## 📝 Notes
189+
190+ - ** Sudo Required** : Keploy uses eBPF (extended Berkeley Packet Filter) to intercept network traffic, which requires root privileges. Always run ` keploy ` commands with ` sudo -E env "PATH=$PATH" ` to preserve your environment variables.
191+
192+ - ** Port Already in Use** : If you get an "address already in use" error, kill any existing dotnet processes:
193+ ``` bash
194+ pkill -9 dotnet
195+ ```
196+
197+ - ** MongoDB Connection** : Ensure MongoDB is running before starting the application:
198+ ``` bash
199+ sudo systemctl status mongod
200+ ```
201+
202+ ---
203+
204+ ## 🔗 Resources
205+
206+ - [ Keploy Documentation] ( https://keploy.io/docs )
207+ - [ .NET 8 Documentation] ( https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-8 )
208+ - [ MongoDB .NET Driver] ( https://www.mongodb.com/docs/drivers/csharp/ )
209+
210+ ---
211+
212+ ## 🤝 Contributing
213+
214+ Contributions are welcome! Please feel free to submit a Pull Request.
215+
216+ ---
217+
218+ ## 📄 License
219+
220+ This project is licensed under the MIT License.
0 commit comments