Mock Server
Examples
Petstore Example

Petstore Example

Pestore is the most popular example, that was used for API demenstration. In this example we will implement several simple API for pet operation:

  1. Add a Pet - HTTP POST /pet
  2. Update a Pet - HTTP PUT /pet
  3. Get the Pet detail - HTTP GET /pet/{petId}
  4. Delete a Pet - HTTP GET /pet/{petId}
  5. Find Pet by Status - HTTP GET /pet/findByStatus
  6. List all pets - HTTP GET /pet

to demenstrate how to import external javascript files, we put all the pet related callback functions in a seperate file ./routes/pet.js, and define all the API in main.js.

Here is the main.js, it defines all the API:

//
// a simple petstore mock server exampe (all routes should be defined in main.js)
//
 
// import request handlers
var pet = require("./routes/pet.js")
 
// define the routes by using external functions
mock.define("/pet", "POST", pet.addPet);
mock.define("/pet", "PUT", pet.upatePet);
mock.define("/pet/{petId}", "GET", pet.findByPetId);
mock.define("/pet/{petId}", "DELETE", pet.deletePet);
 
mock.define("/pet", "GET", pet.listPets);
mock.define("/pet/findbystatus", "GET", pet.findByStatus);
 
// you can define route by inlne functions directly 
mock.define("/hello/world", "GET", function (req, res) { 
  var users = [ 
    { username: "hello", email: "hello@gmail.com" },
    { username: "world", email: "world@gmail.com" }
  ]
 
  return res.json(users)
 }); 
 
// an api to clean state
 mock.define("/clean", "GET", function (req, res) { 
  state.pets = [];
  return res.json(state)
 }); 



Here is the routes/pet.js, it defines all the API Callback:

// pet state
state.pets = state.pets || []
 
// list all pets
// curl http://980be62vfygro77jw.mock.apigit.com/pet
exports.listPets = function (req, res) {
	res.send(200, state.pets);
};
 
// generate unique Pet ID 
function uniqueId() {
	return Math.floor((1 + Math.random()) * 0x10000)
		.toString(16)
		.substring(1);
}
 
// add a pet
// curl -X POST http://980be62vfygro77jw.mock.apigit.com/pet -H 'Content-Type: application/json' -d '{"name":"pet2","status":"available"}'
exports.addPet = function (req, res) {
	var pet = {
		id: uniqueId(), // assign unique ID
		name: req.body.name,
		status: req.body.status
	};
 
	state.pets.push(pet);
 
	res.send(200, pet);
};
 
// update a pet 
// curl -X PUT http://980be62vfygro77jw.mock.apigit.com/pet -H 'Content-Type: application/json' -d '{"id":"3", "name":"pet3","status":"sold"}'
exports.upatePet = function (req, res) {
	var pet = req.body;
	var pets = state.pets || [];
 
	for (var i = 0; i < pets.length; i++) {
		if (pets[i].id == pet.id) {
			pets[i] = pet;
			break;
		}
	}
 
	state.pets = pets;
 
	res.send(200, pet);
};
 
// find the Pet By ID
// curl  -X "DELETE" http://980be62vfygro77jw.mock.apigit.com/pet/2
exports.findByPetId = function (req, res) {
	var pet = null;
 
	req.check('petId', 'Invalid parameter').notEmpty();
	if (req.validationErrors()) {
		return res.json(400, req.validationErrorsJson());
	}
 
	for (var i = 0; i < state.pets.length; i++) {
		if (state.pets[i].id == req.params.petId) {
			pet = state.pets[i];
			break;
		}
	}
 
	res.send(200, pet);
};
 
// delete pet by ID
// curl  -X "DELETE" http://980be62vfygro77jw.mock.apigit.com/pet/2
exports.deletePet = function (req, res) {
	var pet = null;
	var pets = state.pets || [];
 
	req.check('petId', 'Invalid parameter').notEmpty();
	if (req.validationErrors()) {
		return res.json(400, req.validationErrorsJson());
	}
 
	for (var i = 0; i < pets.length; i++) {
		if (pets[i].id == req.params.petId) {
			pet = pets[i];
			pets.splice(i, 1);
			break;
		}
	}
 
	res.send(200, pet);
};
 
 
// list pets by status
// curl http://980be62vfygro77jw.mock.apigit.com/pet/findbystatus?status=active
exports.findByStatus = function (req, res) {
	var pets = [];
 
	for (var i = 0; i < state.pets.length; i++) {
		if (state.pets[i].status == req.query.status) {
			pets.push(state.pets[i]);
		}
	}
 
	res.send(200, pets);
};