Git Product home page Git Product logo

httpmock.js's Introduction

httpmock.js

httpmock.js is a server that serves mostly static content. Its configuration is specified by an external file in a DSL.

The goal is to allow easy specification of mock servers for client testing.

It began as a node.js port of restmock (https://github.com/tildedave/restmock).

Installing

Install with npm:

npm install https://github.com/tildedave/httpmock.js/tarball/0.0.1

Run with node:

node ./node_modules/httpmock/httpmock.js --config mock.js

Usage

Specify the server's behavior in a configuration file (config.js) and then start the server:

node httpmock.js --config config.js

Example config.js

routes(
  route("Hello, world!",
        request(uri("/hello")),
        response(text("Hello, world!"))),
  route("Can retrieve all the kittens",
        request(uri("/kittens"),
                method.GET),
        response(text("Some adorable kittens!"))),
  route("Can't make a new kitten",
        request(uri("/kittens"),
                method.POST),
        response(status(422))),
  route("Can update a kitten",
        request(uri("kittens/([0-9]+)"),
                method.PUT),
        response(status(422))),
  route("Can check a kitten's async status",
        request(uri("kittenjobs/([0-9]+)"),
               method.GET),
        response(time(1, json({ done : false })),
                 time(2, json({ done : false })),
                 otherwise(json({ done : true})))))

Customizing the Server

Because the server configuration is JavaScript, it is possible to script dynamic behavior into an otherwise static server.

Matchers

Functions inside a request determine if an incoming HTTP request matches the given route.

Some built-in matchers are uri and method.

Custom matcher objects should have a function matches that returns true or false.

Here is an example of a custom matcher that will match a request's accept header.

var accepts = function(acceptsType) {
  return {
    matches : function (req) {
      var accept = req.headers.accept;
      if (!accept) {
        return false;
      }

      return accept.indexOf(acceptsType) != -1;
    }
  };
}

routes(
  route("JSON data", 
    request(uri("/data"),
            accepts("application/json")),
    response(json({ success: true }))),
  route("other kinds of data",
    request(uri("/data")),
    response(text("succeeded!"))));

Requirements

httpmock.js depends on optimist for parsing command-line arguments.

npm install optimist

httpmock.js's People

Contributors

tildedave avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

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.