GithubHelp home page GithubHelp logo

ridicule's Introduction

ridicule

hapi plugin providing mocks for restful and querystring based APIs


NOTICE:

This repository has been archived and is not supported.

No Maintenance Intended


NOTICE: SUPPORT FOR THIS PROJECT HAS ENDED

This projected was owned and maintained by Walmart. This project has reached its end of life and Walmart no longer supports this project.

We will no longer be monitoring the issues for this project or reviewing pull requests. You are free to continue using this project under the license terms or forks of this project at your own risk. This project is no longer subject to Walmart's bug bounty program or other security monitoring.

Actions you can take

We recommend you take the following action:

  • Review any configuration files used for build automation and make appropriate updates to remove or replace this project
  • Notify other members of your team and/or organization of this change
  • Notify your security team to help you evaluate alternative options

Forking and transition of ownership

For security reasons, Walmart does not transfer the ownership of our primary repos on Github or other platforms to other individuals/organizations. Further, we do not transfer ownership of packages for public package management systems.

If you would like to fork this package and continue development, you should choose a new name for the project and create your own packages, build automation, etc.

Please review the licensing terms of this project, which continue to be in effect even after decommission.##curved-carousel

A simple way to create an infinitely scrollable carousel, with optional curvature.

setup

The following options are available for configuration.

apiPrefix
A string or array of strings representing the base of the routes that should be mocked
Defaults to /api

mocksDir
Directory containing the mock files.
Defaults to __dirname + './mocks', where __dirname is this plugin

mocksAdminPath
A relative URL that the admin page for this plugin can be found.
Defaults to /admin/mocks

mocksAdminServerLabels
An array of labels used during hapi's plugin registration
Defaults to ['admin']

enabled
Wether or not the mocks are enabled.
Defaults to false

enableForceCookiePage
A boolean representing wether or not to register the cookie setter page. This page gives a simple button to turn on or off the always_ridicule cookie, which allows for a client to opt-in to forcing mock responses, regardless of server settings. Useful for novice testers or devices where bookmarklet creaton is cumbersome. Defaults to false

forceCookiePath
A relative URL that the cookie setter page can be found at.
Defaults to /cookie

forceCookieValue
A string representing the value of the always_ridicule cookie. Allows for servers to have a publically available opt-in mocking method without having the specific necessary cookie publically known. expires value is 30 minutes from the time it is set.
Defaults to "true"

how it works

Inside of your mocksDir, create a ridicule.js file that exposes an array hapi routes in it's module.exports.

module.exports = [{
    method: 'GET',
    path: '/hello',
    handler: function(request, reply) {

        reply('hello world');
    }
},{
    method: 'POST',
    path: '/goodbye',
    handler: function(request, reply) {

        reply('thanks for all the fish');
    }
}]

At the start of the server, all configured routes are registered with a random 40 character prefix. (e.g. the route /foo is mocked out at /(uuid)/foo)

When the mock server is enabled, all incoming requests are matched against the configured apiPrefix. When a match is found, the request is internally forwarded to the mock route handler.

You have the ability to bailout on mocking out the request in a handler (useful for when you are mocking out only a subset of a querystring based system, since hapi matches on paths only). Simply require rididcule in the file, and then call ridicule.bailout(req, res), where req and res are the request and response objects inside of the handler.

configuring your mocks

The most common way of mocking out requests is just to return the data of a json file within the reply() interface of your route handler.

    var data = require('./mockFile.json');

    var route = {
        method: '*',
        path: '/mockPath',
        config: {
            handler: function (request, reply) {
                return reply(data);
            }
        }
    };

However, you have the ability to add additional metadata to the response, such as custom headers and HTTP status codes. You just need to wrap up your existing json file inside of another object, setting the original to payload, and add a ridiculeSettings field.

so this request

{
    "foo": "bar"
}

becomes

{
    "ridiculeSettings": {
        "statusCode": 418,
        "headers": { "Content-Type": "x-stream" }
    }
    payload: {
        "foo": "bar"
    }
}

Worth noting that since this is added at the very end of the hapi's request flow, As a result, since payload has to be inlined in the mock file, and is therefore limited to only support string and JSON objects, and not streams or buffers.

enabling your mocks

There are three ways to enable/disable mocks.

  1. The hapi configuration setting

    You can change the enabled opton in your setup to true. This enables it at start, but can changed in the future by..

  2. Toggling the admin page button

    You can turn mocks on or off at any time by visting the path configured under mocksAdminPath. By default this is /admin/mocks. Note that this is registered behind a hapi label (configurable in your setup, but ['admin'] by default). This allows you to ship ridicule in your production site, and have the toggle switch exist on a port that isn't publicly accessible.

  3. A cookie on the client

    If the cookie always_ridicule is set with the value from the configured forceCookieValue (default value is the string "true", e.g. always_ridicule="true") on the client, then the configured mocks will always be served (until it is deleted, of course). In, addition, always_ridicule=false will force the client to never be mocked out, regardless of the server's current settings.

querystrings

Since hapi matches routes only on paths (and not any querystring parameters), you need to be able to match more than one query against a path. For that, ridicule provides ridicule.validate method.

validation

ridicule.validate is a function that takes a request and reply pair, then a validation object, consisting a validator object, and a callback function.

The validator object is a plain object, where each key represents the key of the querystring, and the value is either a string, RegExp, or function that matches the value of the corresponding value in the querystring.

For example, the following querystring

?category=Music&genre=ska&year=2012

would match the following validation

...
  handler: function(request, reply) {
      ridicule.validate(request, reply, {
          validator: {
              category: 'Music',
              genre: function(value) {
                return value === 'ska' || value === 'rocksteady'
              },
              year: /^\d{4}$/
          },
          callback: function(request, reply) {
              reply({'all': 'good'});
          }
      })
  }
...

of course you can easily chain multiple checks

      var queriesToCheck = [{
        validator: {
            category: 'Music',
            genre:'ska',
            year: /^\d{4}$/
        },
        callback: function(request, reply) {
            reply().file('./authMock.json');
        }
      },{
        validator: {
            foo: 'bar',
            baz: 'biz'
        },
        callback: function(request, reply) {
            reply({'wordsAre': 'hard'});
        }
      }];

      queriesToCheck.some(function(route) {
        return ridicule.validate(request, reply, route);
      });

bailout

You may run into an issue where you want to mock out a subset of your requests at an endpoint, but not all of them. In this case, ridicule provides a handy helper function, ridicule.bailout.

all you need to do is add it to the end of your query checks, passing through the handlers request and reply interfaces.

      var matched = queriesToCheck.some(function(route) {
          return ridicule.validate(request, reply, route);
      });

      if (!matched) {
          ridicule.bailout(request, reply);
      }

An alternative to this is setting the validator object on your final query to true. This will automatically match the supplied query, and can act as a catchall

      var queriesToCheck = [{
        validator: {
            category: 'Music',
            genre:'ska',
            year: /^\d{4}$/
        },
        callback: function(request, reply) {
            reply().file('./authMock.json');
        }
      },{
        validator: true,
        callback: function(request, reply) {
            reply().file('./catchall.json');
        }
      }];

      queriesToCheck.some(function(route) {
        return ridicule.validate(request, reply, route);
      });

ridicule's People

Contributors

cwstege avatar kpdecker avatar mmonto7 avatar patrickkettner avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ridicule's Issues

Hapi vision plugin dependency

In Hapi 9+, the vision plugin (along with many others) are no longer included by default. The error is "TypeError: plugin.views is not a function". I tried to load the vision plugin first which gets past the views error, but it continues to fail:

server.register(require("vision"))
    .then(function onVisSuccess() {
        server.register({
            register: require("ridicule"),
            options: {
                apiPrefix: "/myApi",
                mocksDir: "mocks",
                enabled: true
            }
        }).then(function onRidSuccess() {
            //Do something
        }, function onRidError(err) {
            console.log("Failed to load ridicule plugin " + JSON.stringify(err));
        })
    })
    .catch(function onCatch(err) {
        throw err;
    });

Ultimately the ridicule plugin should state its dependencies using server.dependency:
http://stackoverflow.com/questions/32187168/hapi-js-load-plugins-in-order

What is the short-term fix to get the current version of ridicule to work with Hapi >9, and what's the long-term schedule for updating the plugin?

Create upstream diff tool

Create a tool that allows the stored mocks to be compared to the upstream mocks. This would allow maintainers to detect skew between the mock data and upstream services.

Ex: http://localhost/mockAdmin/diff?host=api.github.com

Bonus points: allowing flags in the mock data files stating which fields are expected to change on subsequent requests, i.e. ignore the path foo.bar.timestamp when diffing.

Hapi v17 Support

Overview

If you are not aware yet, Hapi v17 is making the transition from callbacks to async/await, as well as deprecating some other rarely used functionality. This is a breaking change that may make your plugin no longer compatible with the Hapi API.

Changelog

Draft release notes can be found here: hapijs/hapi#3658

Target Release

The target release date for v17 is the beginning of November.

Tasks

  • Reply to this to acknowledge that you are actively maintaining this module and are willing to update it
  • Update plugin to be fully async/await compatible using the v17 branch from Hapi for testing

    Possible dev flow for updating

    • Clone Hapi
    • npm link within the Hapi repo
    • npm link hapi within your plugin repo
    • Your plugin will now be using v17 of Hapi branch for tests
  • Release new major version of your plugin on npm. Please use a major version increment as this will be a breaking change and it would be terrible for it to sneak into current versions of Hapi.

Notes

  • Support is being dropped for all versions of Node <8.0.0.
  • Hapi v16 will continue to be supported for as long as there exists a Node LTS actively being supported that is not compatible with v17.
  • Targeted release date is November 1st, 2017. Please try to have your plugin updated before then.
  • This issue is being opened because your plugin is listed on the official hapi website

Add "secure" auth mode

Rather than using constants for auth, we should allow for a custom configurable cookie value to provide some basic filtering of the cookie content, outside of the open source content "true".

Add easy enable endpoint

Add an endpoint that can be hit to enable the cookie for a specific user this should:

  1. Have a configurable path
  2. Only be enabled if the path is set
  3. Provide a 1 request implementation unless the custom cookie value is enabled (#8), in which case this should take a query param that is that value.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.