npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

raml-mocker

v0.2.7

Published

Node module to create random responses to requests based on RAML rest definition and json-schema.

Downloads

70

Readme

raml-mocker Build Status NPM version Dependency Status

Node module to create random responses to requests based on RAML rest definition.

Objective

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.

GETTING STARTED

  • Yo need to define a RAML file like this: definition.raml
  • Import the json-schema file or define it inside the raml file.

HOW TO USE RAML MOCKER

var ramlMocker = require('raml-mocker');
var options = {
    path: 'test/raml'
};
var callback = function (requests){
    console.log(requests);
};
ramlMocker.generate(options, callback);

Or defining a collection of files instead of a path:

var options = {
    files: ['definition1.raml', 'folder/definition2.raml']
};

If your properties in the schema need an alternative logic you should use format. For example the schema in the test schema or in schemaTest:

'foo': {
    'type': 'string',
    'format': 'foo'
}

And in your options provide format definitions:

var options = {
    path: 'test/raml',
    formats : {
        foo: function foo(Faker, schema) {
            return Faker.Name.firstName();
        }
    },
    parserOptions: {
      dereferenceSchemas: true
    }
};

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).

And the RESULT in your callback

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: [Function](code)
        /** Function by default to return the example (codes 2XX defined in the RAML). */
        example: [Function]
        /** The same as mockByCode but applied to examples */
        exampleByCode: [Function](code)
    }
]

If you have express and underscore or lodash in your project, you can use a callback like this to create the mocked requests:

var callback = function (requestsToMock){
    _.each(requestsToMock, function(reqToMock){
        app[reqToMock.method](reqToMock.uri, function(req,res){
            var code = 200;
            if (reqToMock.defaultCode) {
                code = reqToMock.defaultCode;
            }
            res.send(code ,reqToMock.mock());
        });
    });
};

Json-Schema Support Draft-04

References

This plugin supports internal references and also references to json files under the working folder of the node process running raml-mocker. For example:

{
    "$schema": "http://json-schema.org/schema",
    "type": "object",
    "properties": {
        "test": {
            "format": "timestamp",
            "$ref": "#/definitions/other"
        }
    },
    "required": ["test"],
    "definitions": {
        "other": {
            "type": "number"
        }
    }
}
// schemas/thisSchema.json
{
    "$schema": "http://json-schema.org/schema",
    "type": "object",
    "properties": {
        "test": {
            "format": "timestamp",
            "$ref": "schemas/otherSchema.json#"
        }
    },
    "required": ["test"],
}

// schemas/otherSchema.json
{
    "$schema": "http://json-schema.org/schema",
    "type": "object",
    "properties": {
        "test": {
            "format": "timestamp",
            "type": "integer"
        }
    },
}

Keywords support

  • enum : ✓ yes
  • type: ✓ yes
  • allOf: ✓ yes
  • anyOf: ✓ yes
  • oneOf: ✓ yes
  • not: NO (very difficult to generate a random mock from not json-schemas)

If you need an example please check the test schema

CONTRIBUTING

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 request. I'll be grateful.

Part of the schema.js code is based in json-schema-mock.

History Log

0.2.7
  • Adds support for PATCH request
  • Fixes an issue when parsing raml files
0.2.6
  • Adds parserOptions (e.g. dereferenceSchemas by default is true)
  • Adds support for baseUriParameters
0.2.5
  • Updates raml-js-parser dependency to the new version
0.2.4
  • The raml-js-parser is now a git+https dependency
0.2.3
  • Work-around to fix dependencies resolution with circular references
0.2.2
  • Dependency resolution by $ref now is delegated to raml-parser
0.2.1
  • Adding baseUri to resource paths.
  • Accepting all media types stated in RAML 0.8 spec
  • Upgrades dependencies
0.2.0
  • Upgrades dependencies
0.1.13
  • Fixes compatibility issues.
0.1.12
  • Fixes null responses crash in getResponsesByCode
0.1.11
  • JSON schema now supports references to external files.
  • Adds the possibility to get also the responses examples defined inside RAML files.
0.1.10
  • Allows upper case in the format name definition. Fixes #1
0.1.9
  • Adds support for uniqueItems schema keyword.
0.1.8
  • Adds the possibility to return only the http code without a json.
  • Fixes some issues.
  • Removes unneeded packages.
0.1.7
  • Adds support to multiple http codes per uri & method in raml files.
0.1.6
  • Fixes an error without showing it in the console.