This post is about code-based mock servers, available on Team and Enterprise plans. If you only need answers from your OpenAPI document, a spec-based mock server needs no script at all.
If you have created your own specification within your repository, congratulations! You can now automatically generate the Mock Server script. To do so, simply navigate to "Mock Scripts" within your repository, enter edit mode and click on "Generate from Spec".
Generate Mock Server script from API spec
Select an API specification and an operation, and the route script is generated for you.
Mock Server script according to API spec
Thanks to the powerful automatic Mock Server tool, you will receive several scripts related to the API specification. However, it is important to note that only a few lines of code are required to deploy the Mock Server.
//this script should be in apigit-mock/main.js
mock.define("/", "GET", function (req, res) {
res.status(200).send("welcome to apigit mock server");
});
Within the "main.js" file, you will find a variety of route functions that are related to the API specification. You are free to define additional functions within this file for the purpose of further testing.
With Dynamic MockServer, developers can create multiple scenarios for different endpoints, each with different response data, headers, and status codes. This allows developers to test their applications in various scenarios and conditions, ensuring that they work as expected in different situations.
Deploying a Dynamic Mock Server on the Apigit platform is a straightforward process. By defining an array list and adding, updating, or searching within it, you can receive responses based on the requests that you have sent previously.
//all routes should be defined in main.js
const { v4: uuidv4 } = require("uuid");
// create a user
mock.define("/user", "POST", function (req, res) {
var user = req.body;
if (user.username === "") {
res.status(405).send("Invalid input");
return;
}
user.id = uuidv4(); // generate uniqueID for the new user
state.users = state.users || []; // global state
state.users.push(user);
res.status(200).send(user);
});
// find user by name
mock.define("/user/{username}", "GET", function (req, res) {
var users = state.users || [];
var user = null;
for (var i = 0; i < users.length; i++) {
if (users[i].username == req.params.username) {
user = users[i];
break;
}
}
if (user === null) {
res.status(404).send("User not found");
return;
}
res.status(200).send(user);
});
To ensure that your script remains organized and easy to understand, it is recommended that you define all routes within the "main.js" file. However, if you prefer to keep your code more modular, you can create a new route file by clicking on the "+" icon and adding your callback function to the new file. The file structure will appear as follows:
//main.js
var pet = require("./routes/pet.js")
// define the routes callback in seperated files
// pet
mock.define("/pet", "PUT", pet.updatePet); // update an existing pet
And define the callback function in another file.
//./routes/pet.js
exports.updatePet = function (req, res) {
var pets = state.pets || [];
var pet = req.body;
var i = 0;
for (i = 0; i < pets.length; i++) {
if (pets[i].id == pet.id) {
pets[i] = pet;
state.pets = pets;
break;
}
}
if (i === pets.length) { // menas not found the pet
res.status(404).send('Pet not found');
return;
}
res.status(200).send(pet);
};
For full example, you can visit Github.
To use the Mock Server that you have created, you must first publish it: click Publish, choose Publish MockServer and select Code-based. Once it has been published, you can find the server address in the repository's Publish Center.
You can keep exploring the mock server script by visiting Automatic generation of Ready-to-Use Mockservers or Mock Server API Definition.
Publish a mock server from your OpenAPI documents without writing code. It validates requests, answers from your examples and schemas, and works on every plan.
APIGit has two types of mock server. Here is how they differ, when each one fits, and how to run both from one repository.
Spec-based mock servers validate each request against your OpenAPI document, return clear problem+json errors, and let clients pick a response with the Prefer header.
Create a free workspace, add an OpenAPI file and publish your first API document and mock server in minutes.