Giter VIP home page Giter VIP logo

dredd's Introduction

Dredd โ€” HTTP API Validation Tool

Build Status Dependency Status devDependency Status Coverage Status

NPM

Dredd is a command-line tool for validating API documentation written in API Blueprint format against its backend implementation. With Dredd you can easily plug your API documentation into the Continous Integration system like Travis CI or Jenkins and have API documentation up-to-date, all the time. Dredd uses the Gavel for judging if a particular API response is valid or if is not. If you are curious about how decisions are made, please refer to Gavel's behavior specification.

Dredd API Blueprint testing tool

Installation

Node.js and NPM is required.

$ npm install -g dredd

Get Started Testing API Documentation and backend

Create a new documentation file in API Blueprint format in blueprint.md

# GET /
+ Response 200 (application/json; charset=utf-8)

      {"message": "Hello World!"}

Let's create a backend for example in Express.js. To install it:

$ npm install express

Create file with backend application in app.js:

var express = require('express');
var app = express();

app.get('/', function (req, res) {
  res.json({message: 'Hello World!'});
})

var server = app.listen(3000);

Run the API application on background:

$ node app.js &

Finally, run Dredd for validation:

$ dredd blueprint.md http://localhost:3000

Celebrate! Your API is in sync with your documentation:

info: Beginning Dredd testing...
warn: Parser warning:  (10) message-body asset is expected to be a pre-formatted code block, every of its line indented by exactly 8 spaces or 2 tabs 62:30
pass: GET / duration: 12ms
complete: 1 passing, 0 failing, 0 errors, 0 skipped
complete: Tests took 19ms

See dredd-example repo for real-life example in continous integration.

Writing validatable blueprints

If you are using URI templates in your blueprint, you have to provide example values in the blueprint's URI parameter syntax to provide values for each URI parameter substitution. Every resource in the blueprint defined by URI template without specifying example values is not validatable, it's considered as an ambigous transaction and skipped. In case of any ambigous transaction Dredd will throw a warning and let you know which parameter example value is not defined in the blueprint.

Hooks

If you want to execute some code before and after each request, Dredd can be configured to use hookfiles to do basic setup/teardown between each validation (specified with the --hookfiles flag). Hookfiles can be in javascript or coffeescript, and must import the hook methods.

Requests are identified by their name, which is derived from the structure of the blueprint. You can print a list of the generated names with --names.

Example

Get Names:

$ dredd single_get.md http://machines.apiary.io --names
info: Machines > Machines collection > Get Machines

Write a hookfile:

{before, after} = require 'hooks'

before "Machines > Machines collection > Get Machines", (transaction) ->
  console.log "before"

after "Machines > Machines collection > Get Machines", (transaction) ->
  console.log "after"

Skipping a validation with hooks:

before "Machines > Machines collection > Get Machines", (transaction) ->
  transaction.skip = true

Run validation:

dredd single_get.md http://machines.apiary.io --hookfiles=*_hooks.*

Dredd also supports callbacks before and after all tests:

{beforeAll, afterAll} = require 'hooks'

beforeAll (done) ->
  # do setup
  done()

afterAll (done) ->
  # do teardown
  done()

If beforeAll and afterAll are called multiple times, the callbacks are executed serially in the order they were called.

Command Line Options

$ dredd --help
Usage:
  dredd <path to blueprint> <api_endpoint> [OPTIONS]

Example:
  dredd ./apiary.md http://localhost:3000 --dry-run

Options:
  --dry-run, -y        Do not run any real HTTP transaction, only parse
                       blueprint and compile transactions.       [default: null]
  --hookfiles, -f      Specifes a pattern to match files with before/after
                       hooks for running tests                   [default: null]
  --names, -n          Only list names of requests (for use in a hookfile). No
                       requests are made.                       [default: false]
  --reporter, -r       Output additional report format. This option can be used
                       multiple times to add multiple reporters. Options:
                       junit, nyan, dot, markdown, html.
                                                                   [default: []]
  --output, -o         Specifies output file when using additional file-based
                       reporter. This option can be used multiple times if
                       multiple file-based reporters are used.
                                                                   [default: []]
  --header, -h         Extra header to include in every request. This option
                       can be used multiple times to add multiple headers.
                                                                   [default: []]
  --sorted, -s         Sorts requests in a sensible way so that objects are not
                       modified before they are created. Order: CONNECT,
                       OPTIONS, POST, GET, HEAD, PUT, PATCH, DELETE, TRACE.
                                                                [default: false]
  --user, -u           Basic Auth credentials in the form username:password.
                                                                 [default: null]
  --inline-errors, -e  Determines whether failures and errors are displayed as
                       they occur (true) or agregated and displayed at the end
                       (false).
                                                                [default: false]
  --details, -d        Determines whether request/response details are included
                       in passing validations.
                                                                [default: false]
  --method, -m         Restrict tests to a particular HTTP method (GET, PUT,
                       POST, DELETE, PATCH). This option can be used multiple
                       times to allow multiple methods.
                                                                   [default: []]
  --color, -c          Determines whether console output should include colors.
                                                                 [default: true]
  --level, -l          The level of logging to output. Options: silly, debug,
                       verbose, info, warn, error.
                                                               [default: "info"]
  --timestamp, -t      Determines whether console output should include
                       timestamps.
                                                                [default: false]
  --help               Show usage information.
  --version            Show version number.

Additionally, boolean flags can be negated by prefixing no-, for example: --no-color --no-inline-errors.

Contribution

Any contribution is more then welcome! Let's start with creating your own virtual development environment, then fork, write tests, write clean, readable code which communicate, use scripts/bdd, keep the test coverage and create a pull request. :)

Make sure to follow Dredd issues page.

To learn more about the future of API Blueprint & Testing visit apiaryio/api-blueprint#21.

dredd's People

Contributors

abtris avatar codefred avatar jonathanbp avatar zdne avatar eddieroger avatar kuba-kubula avatar ksarna avatar theodorton avatar frio avatar

Watchers

James Cloos 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.