Skip to content

Latest commit

 

History

History
282 lines (194 loc) · 4.95 KB

File metadata and controls

282 lines (194 loc) · 4.95 KB
id samples-rust-mongodb
title Rust + MongoDB
sidebar_label Rust + MongoDB
description This sample app demonstrates how to use Rust with MongoDB and Keploy to automatically record and replay API test cases.
tags
rust
quickstart
samples
examples
tutorial
mongodb
keploy
keyword
Rust
MongoDB
Keploy
API Test Generator
Auto Testcase Generation

import EnterpriseInstallReminder from '@site/src/components/EnterpriseInstallReminder'; import SectionDivider from '@site/src/components/SectionDivider'; import ProductTier from '@site/src/components/ProductTier';

Using Docker Compose 🐳. {#using-docker-compose}

A simple Notes REST API built with Rust and MongoDB, showcasing Keploy’s record & replay capabilities without writing manual test cases.


Clone the Sample Application

git clone https://github.com/Gagan202005/Keploy-Rust-Mongodb-QuickStart.git
cd Keploy-Rust-Mongodb-QuickStart

Rust (Cargo)

Rust is required to build and run the application.

Install Rust using rustup:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source "$HOME/.cargo/env"

Verify installation:

rustc --version
cargo --version

Lights, Camera, Record!

Start MongoDB and Mongo Express using Docker Compose:

docker compose up -d mongo mongo-express

Build & Run the Application 🛠️

docker compose up -d --build app

Verify the application:

curl http://localhost:8000/

Expected response:

Rust + MongoDB + Keploy Quickstart is running!

Capture Test Cases

Start recording with Keploy:

keploy record -c "docker compose up --build app" --container-name keploy-rust-app

Now make some API calls (via curl, Postman, or Hoppscotch).

1️⃣ Create a Note

curl -X POST http://localhost:8000/notes \
  -H "Content-Type: application/json" \
  -d '{"text":"hello keploy"}'

Response:

{
  "text": "hello keploy"
}

2️⃣ Fetch All Notes

curl http://localhost:8000/notes

Response:

{
  "text": "hello keploy"
}

We will get output something like this:

Keploy Record Mode

Keploy automatically records:

  • HTTP test cases

  • MongoDB mocks

Recorded data is stored at:

keploy/test-set-0/tests
keploy/test-set-0/mocks

Run Tests ▶️

Replay the recorded test cases:

keploy test -c "docker compose up --build app" --delay 10 --container-name keploy-rust-app

We will get output something like this:

Keploy Replay Mode

The --delay flag allows the application time to start before replay begins.

# Running Locally on Linux / WSL 🐧 {#running-app-locally} A simple Notes REST API built with Rust and MongoDB, showcasing Keploy’s record & replay capabilities without writing manual test cases.

In local mode:

  • MongoDB runs in Docker

  • Rust application runs using cargo run

Clone the Sample Application

git clone https://github.com/Gagan202005/Keploy-Rust-Mongodb-QuickStart.git
cd Keploy-Rust-Mongodb-QuickStart

Rust (Cargo)

Rust is required to build and run the application locally.

Install Rust using rustup:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source "$HOME/.cargo/env"

Verify installation:

rustc --version
cargo --version

Lights, Camera, Record!

Start MongoDB

docker compose up -d mongo mongo-express

Run the Application

cargo run

You should see:

Server running at http://localhost:8000

Record Tests (Local)

Start recording:

keploy record -c "cargo run"

Now make some API calls (via curl, Postman, or Hoppscotch).

1️⃣ Create a Note

curl -X POST http://localhost:8000/notes \
  -H "Content-Type: application/json" \
  -d '{"text":"hello keploy"}'

Response:

{
  "text": "hello keploy"
}

2️⃣ Fetch All Notes

curl http://localhost:8000/notes

Response:

{
  "text": "hello keploy"
}

We will get output something like this:

Keploy Replay Mode

Stop recording:

  • Ctrl + C

Replay Tests (Local)

keploy test -c "cargo run" --delay 10

We will get output something like this:

Keploy Replay Mode

Wrapping It Up

You’ve successfully:

  • Built a Rust + MongoDB REST API

  • Recorded real API traffic using Keploy

  • Replayed deterministic tests without hitting the database

Feel free to explore further by modifying:

  • test-x.yml

  • mocks.yml

Happy Testing with Keploy 🐰✨