How to create Scenarios?

Scenarios are registered with the function register() on the Stubr instance itself. register() takes a number of mandatory and optional attributes to configure them to the desired needs.

After all scenarios have been registered, Stubr is started by executing run() on your Stubr instance (e.g. stubr.run()).

🚧

Scenarios must be registered before mock server start

Only scenarios registered on Stubr before it was started with stubr.run() are considered. Scenarios cannot be added at runtime.

Example:
import Stubr from 'stubr';
import { Method } from 'stubr';

// instantiate Stubr
const stubr = new Stubr();

// register scenario
stubr.register({
	// scenarios can be grouped (optional)
	group: "My Group",
	// required
	name: "Scenario 1",
	// required
	route: "/my/first/route", 
	// GET, POST, PUT, PATCH, DELETE, OPTIONS, HEAD
	method: Method.GET,
	// headers, parameters and body are passed to provide context for validation
	validate: (requestHeaders, requestBody, requestParams) => {
		return requestHeaders.Authorization !== undefined; // disallow unauthorized requests
	},
  // http response code
	responseCode: 200,
	// optionally you can receive headers and body to construct dynamic response body based on request
	responseBody: (requestHeaders, requestBody, requestParams) => {
		data: "json response"
	}
});

// start Stubr
stubr.run();
const Stubr = require("stubr").default;

// instantiate Stubr
const stubr = new Stubr();

// register scenario
stubr.register({
	// scenarios can be grouped (optional)
	group: "My Group",
	// required
	name: "Scenario 1",
	// required
	route: "/my/first/route", 
	// GET, POST, PUT, PATCH, DELETE, OPTIONS, HEAD
	method: "GET",
	// headers, parameters and body are passed to provide context for validation
	validate: (requestHeaders, requestBody, requestParams) => {
		return requestHeaders.Authorization !== undefined; // disallow unauthorized requests
	},
  // http response code
	responseCode: 200,
	// optionally you can receive headers and body to construct dynamic response body based on request
	responseBody: (requestHeaders, requestBody, requestParams) => {
		data: "json response"
	}
});

// start Stubr
stubr.run();