@@ -182,230 +182,3 @@ Always use `sudo -E env "PATH=$PATH"` with Keploy commands to preserve environme
182182- [ .NET 8 Documentation] ( https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-8 )
183183- [ MongoDB .NET Driver] ( https://www.mongodb.com/docs/drivers/csharp/ )
184184
185- # CRUD app using MongoDB
186-
187- 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.
188-
189- ---
190-
191- ## 📦 Tech Stack
192-
193- - [ .NET 8 Web API] ( https://dotnet.microsoft.com/ )
194- - [ MongoDB] ( https://www.mongodb.com/ )
195- - [ Keploy] ( https://keploy.io ) – API testing and mocking tool
196-
197- ---
198-
199- ## 🚀 Getting Started
200-
201- ### 1. Clone the Repository
202-
203- ``` bash
204- git clone https://github.com/Pranav5255/samples-mongodb.git
205- cd samples-mongodb
206- ```
207-
208- ### 2. Install Prerequisites
209-
210- #### Install .NET 8 SDK and Runtime
211-
212- ``` bash
213- # Download and install Microsoft repository configuration
214- wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
215- sudo dpkg -i packages-microsoft-prod.deb
216- rm packages-microsoft-prod.deb
217-
218- # Update package lists
219- sudo apt-get update
220-
221- # Install .NET 8 Runtime and SDK
222- sudo apt-get install -y dotnet-runtime-8.0 aspnetcore-runtime-8.0 dotnet-sdk-8.0
223-
224- # Verify installation
225- dotnet --version
226- ```
227-
228- #### Install MongoDB
229-
230- ``` bash
231- # Import MongoDB public GPG key
232- wget -qO - https://www.mongodb.org/static/pgp/server-7.0.asc | sudo apt-key add -
233-
234- # Create list file for MongoDB
235- 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
236-
237- # Update package database
238- sudo apt-get update
239-
240- # Install MongoDB
241- sudo apt-get install -y mongodb-org
242-
243- # Start MongoDB
244- sudo systemctl start mongod
245-
246- # Enable MongoDB to start on boot
247- sudo systemctl enable mongod
248-
249- # Verify MongoDB is running
250- sudo systemctl status mongod
251- ```
252-
253- ### 3. Configure MongoDB
254-
255- Update ` appsettings.json ` with your MongoDB connection string:
256-
257- ``` json
258- {
259- "MongoDBSettings" : {
260- "ConnectionString" : " mongodb://localhost:27017" ,
261- "DatabaseName" : " UserDb" ,
262- "UsersCollectionName" : " Users"
263- }
264- }
265- ```
266-
267- ### 4. Run the .NET App
268-
269- ``` bash
270- dotnet run
271- ```
272-
273- App runs by default at:
274-
275- ```
276- http://localhost:5067
277- ```
278-
279- You can test endpoints like:
280-
281- ``` http
282- GET /users
283- POST /users
284- GET /users/{id}
285- PUT /users/{id}
286- DELETE /users/{id}
287- ```
288-
289- #### Test with cURL
290-
291- ``` bash
292- # Create a user
293- curl -X POST " http://localhost:5067/users" \
294- -H " Content-Type: application/json" \
295- -d ' {"name":"Pranav","age":24}'
296-
297- # Get all users
298- curl http://localhost:5067/users
299- ```
300-
301- ---
302-
303- ## 🐾 Generate Test Cases with Keploy
304-
305- ### 1. Install Keploy
306-
307- ``` bash
308- # Download Keploy binary
309- curl -L https://github.com/keploy/keploy/releases/latest/download/keploy_linux_amd64.tar.gz -o keploy.tar.gz
310-
311- # Extract it
312- tar -xzf keploy.tar.gz
313-
314- # Move to /usr/local/bin
315- sudo mv keploy /usr/local/bin/
316-
317- # Make it executable
318- sudo chmod +x /usr/local/bin/keploy
319-
320- # Clean up
321- rm keploy.tar.gz
322-
323- # Verify installation
324- keploy version
325- ```
326-
327- ### 2. Record API Calls
328-
329- ** Important:** Keploy requires elevated permissions to use eBPF for intercepting network calls. You must run it with ` sudo ` :
330-
331- ``` bash
332- sudo -E env " PATH=$PATH " keploy record -c " dotnet run" --proxy-port 8080 --path ./keploy-tests
333- ```
334-
335- The application will start and Keploy will begin recording. Keep this terminal running.
336-
337- ### 3. Replay Tests
338- After Recording:
339- ``` bash
340- keploy.exe test --path ./keploy-tests
341- ```
342-
343- ### 3. Generate Traffic
344-
345- Open a ** new terminal** and send traffic to your application:
346-
347- ``` bash
348- # Create a user
349- curl -X POST " http://localhost:5067/users" \
350- -H " Content-Type: application/json" \
351- -d ' {"name":"John","age":30}'
352-
353- # Get all users
354- curl http://localhost:5067/users
355-
356- # Get user by ID (replace {id} with actual ID from response)
357- curl http://localhost:5067/users/{id}
358- ```
359-
360- Keploy will automatically capture these requests and responses as test cases in the ` ./keploy-tests ` directory.
361-
362- ### 4. Stop Recording
363-
364- Press ` Ctrl+C ` in the terminal where Keploy is running to stop recording.
365-
366- ### 5. Replay Tests
367-
368- After recording, replay the captured test cases:
369-
370- ``` bash
371- sudo -E env " PATH=$PATH " keploy test -c " dotnet run" --path ./keploy-tests --delay 10
372- ```
373-
374- The ` --delay 10 ` flag gives the .NET application 10 seconds to start before running tests.
375-
376- ---
377-
378- ## 📝 Notes
379-
380- - ** 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.
381-
382- - ** Port Already in Use** : If you get an "address already in use" error, kill any existing dotnet processes:
383- ``` bash
384- pkill -9 dotnet
385- ```
386-
387- - ** MongoDB Connection** : Ensure MongoDB is running before starting the application:
388- ``` bash
389- sudo systemctl status mongod
390- ```
391-
392- ---
393-
394- ## 🔗 Resources
395-
396- - [ Keploy Documentation] ( https://keploy.io/docs )
397- - [ .NET 8 Documentation] ( https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-8 )
398- - [ MongoDB .NET Driver] ( https://www.mongodb.com/docs/drivers/csharp/ )
399-
400- ---
401-
402- ## 🤝 Contributing
403-
404- Contributions are welcome! Please feel free to submit a Pull Request.
405-
406- ---
407-
408- ## 📄 License
409-
410- This project is licensed under the MIT License.
411-
0 commit comments