Skip to content

Latest commit

 

History

History
311 lines (215 loc) · 7.53 KB

File metadata and controls

311 lines (215 loc) · 7.53 KB
id perl-mongo
title Sample Perl-MongoDB URL Shortener App
sidebar_label Perl + MongoDB
description The following sample app showcases how to use the Perl framework and the Keploy Platform.
tags
perl
quickstart
samples
examples
tutorial
perl-framework
mongodb
keyword
Perl Framework
MongoDB
API Test generator
Auto case generation

import Link from '@docusaurus/Link' import InstallReminder from '@site/src/components/InstallReminder'; import SectionDivider from '@site/src/components/SectionDivider';

How to run the sample application Using Docker Compose 🐳

🪄 In this guide, you will set up a Perl-based URL Shortener using MongoDB and learn how Keploy enables automated API testing through record and replay modes. 🎢

Get Started! 🎬

Clone the repository and move to relevant folder

git clone https://github.com/Akshat005Chaudhary/keploy-perl-mongodb-quickstart.git
cd keploy-perl-mongodb-quickstart

We will be using Docker compose to run the application as well as Mongo on Docker container.

Lights, Camera, Record! 🎥

Fire up the application and mongoDB instance with Keploy. Keep an eye on the two key flags: -c: Command to run the app (e.g., docker compose up).

--container-name: The container name in the docker-compose.yml for traffic interception.

keploy record -c "docker compose up" --container-name=perl-app

alt text

🔥 Challenge time! Generate some test cases. How? Just make some API calls. Postman, Hoppscotch or even curl - take your pick!

Let's generate the testcases.

Make API Calls using Postman or cURL command. Keploy with capture those calls to generate the test-suites containing testcases and data mocks.

1. POST /shorten

curl -X POST http://localhost:5000/shorten \
  -H "Content-Type: application/json" \
  -d '{"url":"https://keploy.io"}'

Here's a peek of what you get:

{
  "code": "QWERTY",
  "shortUrl": "http://localhost:5000QWERTY"
}

🎉 Woohoo! With a simple API call, you've crafted a test case with a mock! Dive into the Keploy directory and feast your eyes on the newly minted test-1.yml and mocks.yml

Time to perform more API magic! Follow the breadcrumbs... or Make more API Calls

2. Redirect Call

Suppose the received code is QWERTY

curl -v http://localhost:5000/QWERTY

This is return 302 status code and redirect to the original url if code is correct.

HTTP/1.1 302 Found
Location: https://keploy.io

3. Check Stats

curl http://localhost:5000/stats/QWERTY

This will return the stats of the short url.

{
  "clicks": 1,
  "code": "QWERTY",
  "createdAt": "2026-01-30T06:47:03Z",
  "originalUrl": "https://keploy.io"
}

4. Negative Case

curl http://localhost:5000/XXXXXX

This is give error:

{
  "error": "Short URL code not found"
}

And once you are done, you can stop the recording and give yourself a pat on the back! With that simple spell, you've conjured up a test case with a mock! Explore the keploy directory and you'll discover your handiwork in tests directory and mocks.yml.

Want to see if everything works as expected?

Run Tests

Time to put things to the test 🧪

keploy test -c "docker compose up" --container-name=perl-app --delay 10

alt text

The --delay flag? Oh, that's just giving your app a little breather (in seconds) before the test cases come knocking.

Wrapping it up 🎉

Congrats on the journey so far! You've seen Keploy's power, flexed your coding muscles, and had a bit of fun too! Now, go out there and keep exploring, innovating, and creating! Remember, with the right tools and a sprinkle of fun, anything's possible.😊🚀

Happy coding! ✨👩‍💻👨‍💻✨

Running App Locally on Linux/WSL/macOS 🐧

🪄 In this guide, you will set up a Perl-based URL Shortener using MongoDB and learn how Keploy enables automated API testing through record and replay modes. 🎢

Get Started! 🎬

Clone the repository and move to relevant folder

git clone https://github.com/Akshat005Chaudhary/keploy-perl-mongodb-quickstart.git
cd keploy-perl-mongodb-quickstart

Make sure you have Perl installed 🐪, Docker set up 🐳, and Keploy installed 🐰 before proceeding.

We'll be running our sample application right on Linux, but just to make things a tad more thrilling, we'll have the database (MongoDB) chill on Docker. Ready? Let's get the party started!🎉

Install cpanm

Ubuntu:

sudo apt update
sudo apt install cpanminus -y

MacOS:

brew install cpanminus

Windows:

cpan App::cpanminus

Install Dependencies

cpanm --installdeps .

Start the MongoDB container

docker compose up -d mongo

Confirm Server behavior

perl app.pl daemon -l http://localhost:5000

Should say:

Web application available at http://localhost:5000

📼 Roll the Tape - Recording Time!

Ready, set, record! Here's how:

keploy record -c "perl app.pl daemon -l http://localhost:5000"

alt text

Keep an eye out for the -c flag! It's the command charm to run the app.

Alright, magician! With the app alive and kicking, let's weave some test cases. The spell? Making some API calls! Postman, Hoppscotch, or the classic curl - pick your wand.

Let's generate the testcases.

Make API Calls using Postman or cURL command. Keploy with capture those calls to generate the test-suites containing testcases and data mocks.

1. POST /shorten

curl -X POST http://localhost:5000/shorten \
  -H "Content-Type: application/json" \
  -d '{"url":"https://keploy.io"}'

Here's a peek of what you get:

{
  "code": "QWERTY",
  "shortUrl": "http://localhost:5000QWERTY"
}

🎉 Woohoo! With a simple API call, you've crafted a test case with a mock! Dive into the Keploy directory and feast your eyes on the newly minted test-1.yml and mocks.yml

Time to perform more API magic! Follow the breadcrumbs... or Make more API Calls

2. Redirect Call

Suppose the received code is QWERTY

curl -v http://localhost:5000/QWERTY

This is return 302 status code and redirect to the original url if code is correct.

HTTP/1.1 302 Found
Location: https://keploy.io

3. Check Stats

curl http://localhost:5000/stats/QWERTY

This will return the stats of the short url.

{
  "clicks": 1,
  "code": "QWERTY",
  "createdAt": "2026-01-30T06:47:03Z",
  "originalUrl": "https://keploy.io"
}

4. Negative Case

curl http://localhost:5000/XXXXXX

This is give error:

{
  "error": "Short URL code not found"
}

And once you are done, you can stop the recording and give yourself a pat on the back! With that simple spell, you've conjured up a test case with a mock! Explore the keploy directory and you'll discover your handiwork in tests directory and mocks.yml.

Want to see if everything works as expected?

Run Tests

Time to put things to the test 🧪

keploy test -c "perl app.pl daemon -l http://localhost:5000"

alt text

Wrapping it up 🎉

Congrats on the journey so far! You've seen Keploy's power, flexed your coding muscles, and had a bit of fun too! Now, go out there and keep exploring, innovating, and creating! Remember, with the right tools and a sprinkle of fun, anything's possible.😊🚀