Node module to create random responses to requests based on RAML rest definition and json-schema.
Node module to create random responses to requests based on RAML rest definition.
The goal of this plugin is to provide automatic mocked responses that honor your defined RAML REST contracts. These contracts are evaluated and the plugin provides functions to generate random responses.
The RAML files should contain HTTP responses for each request you want to mock, application/json and a valid json schema.
var ramlMocker = require'raml-mocker';var options =path: 'test/raml';varconsole.logrequests;;ramlMockergenerateoptions callback;
Or defining a collection of files instead of a path:
var options =files: 'definition1.raml' 'folder/definition2.raml';
'foo':'type': 'string''format': 'foo'
And in your options provide format definitions:
var options =path: 'test/raml'formats :foo:return FakerNamefirstName;;
As you could see the random generation is based on faker, so the function to generate a specific format receives the faker object and the schema. You can use Faker for implementing the logic (check the documentation).
This generated request will return an array of objects like this:
/** URI of the request to mock */uri: '/test/:id/objectDef'/** Method of the request (get, post, ...) */method: 'get'/** Function by default to return the mock (codes 2XX defined in the RAML). */mock: Function/** If you don't define a 2XX code or want to use randomly other code responses. You can use this function* Just use instead of mock(); -> mockByCode(418);*/mockByCode: Functioncode/** Function by default to return the example (codes 2XX defined in the RAML). */example: Function/** The same as mockByCode but applied to examples */exampleByCode: Functioncode
var_eachrequestsToMockappreqToMockmethodreqToMockurivar code = 200;if reqToMockdefaultCodecode = reqToMockdefaultCode;ressendcode reqToMockmock;;;;
This plugin supports internal references and also references to json files under the working folder of the node process running raml-mocker. For example:
// schemas/thisSchema.json// schemas/otherSchema.json
If you need an example please check the test schema
If you have any improvement please open an issue. If you want to collaborate do not hesitate to request it or do a fork + pull requests. I'll be grateful.
Part of the schema.js code is based in json-schema-mock.