This is a sample C# application that demonstrates the usage of Keploy with .NET and MongoDB.
- Install Keploy
curl -L https://github.com/keploy/keploy/releases/latest/download/keploy_linux_amd64.tar.gz -o keploy.tar.gz
tar -xzf keploy.tar.gz
sudo mv keploy /usr/local/bin/
sudo chmod +x /usr/local/bin/keploy
rm keploy.tar.gz
# Verify installation
keploy version- Setup application
git clone https://github.com/keploy/samples-csharp.git
cd samples-csharp/crud-app-mongodb# Download and install Microsoft repository configuration
wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
rm packages-microsoft-prod.deb
# Update package lists
sudo apt-get update
# Install .NET 8 Runtime and SDK
sudo apt-get install -y dotnet-runtime-8.0 aspnetcore-runtime-8.0 dotnet-sdk-8.0
# Verify installation
dotnet --version# Import MongoDB public GPG key
wget -qO - https://www.mongodb.org/static/pgp/server-7.0.asc | sudo apt-key add -
# Create list file for MongoDB
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
# Update package database
sudo apt-get update
# Install MongoDB
sudo apt-get install -y mongodb-org
# Start MongoDB
sudo systemctl start mongod
# Enable MongoDB to start on boot
sudo systemctl enable mongod
# Verify MongoDB is running
sudo systemctl status mongodUpdate appsettings.json with your MongoDB connection string (default configuration works with local MongoDB):
{
"MongoDBSettings": {
"ConnectionString": "mongodb://localhost:27017",
"DatabaseName": "UserDb",
"UsersCollectionName": "Users"
}
}sudo -E env "PATH=$PATH" keploy record -c "dotnet run --urls=http://0.0.0.0:5001" --proxy-port 8080 --path ./keploy-testsNote: Keploy requires elevated permissions (sudo) to use eBPF for intercepting network calls.
Now, since we have our application up and running, let's perform a few cURL requests (in a new terminal):
curl -X POST -H "Content-Type: application/json" -d '{"name":"Sarthak Shnygle","age":23}' http://localhost:5067/api/users
curl -X POST -H "Content-Type: application/json" -d '{"name":"Gourav Kumar","age":22}' http://localhost:5067/api/userscurl http://localhost:5067/api/users# Replace {id} with actual ID from POST response
curl http://localhost:5067/api/users/{id}# Replace {id} with actual ID from POST response
curl -X DELETE http://localhost:5067/api/users/{id}And voila, we have our test cases generated in the ./keploy-tests directory!
Press Ctrl+C in the terminal where Keploy is running to stop recording.
Now let's run Keploy in test mode:
sudo -E env "PATH=$PATH" keploy test \
-c "dotnet run --urls=http://0.0.0.0:5001" \
--path ./keploy-tests \
--delay 10The --delay 10 flag gives the .NET application 10 seconds to start before running tests.
Keploy will replay all recorded test cases and generate a test report in the keploy-tests/reports directory.
If you get an "address already in use" error:
# Kill existing dotnet processes
pkill -9 dotnetEnsure MongoDB is running:
sudo systemctl status mongod
# If not running, start it
sudo systemctl start mongodAlways use sudo -E env "PATH=$PATH" with Keploy commands to preserve environment variables and provide necessary permissions for eBPF.
GET /api/users- Get all usersPOST /api/users- Create a new userGET /api/users/{id}- Get user by IDPUT /api/users/{id}- Update user by IDDELETE /api/users/{id}- Delete user by ID