26
Automatic testing of (RESTful) API documentation Nordic APIs Platform Summit, October 2016 By Rouven Weßling ( ) Ecosystem Developer / Developer Evangelist, Contentful @RouvenWessling photo credit: by Stockholm, Approaching Blue Hour Tobias Lindman (CC-BY)

Nordic APIs - Automatic Testing of (RESTful) API Documentation

Embed Size (px)

Citation preview

Page 1: Nordic APIs - Automatic Testing of (RESTful) API Documentation

Automatic testing of (RESTful)API documentation

Nordic APIs Platform Summit, October 2016

By Rouven Weßling ( )Ecosystem Developer / Developer Evangelist, Contentful

@RouvenWessling

photo credit: by Stockholm, Approaching Blue Hour Tobias Lindman (CC-BY)

Page 2: Nordic APIs - Automatic Testing of (RESTful) API Documentation

A content management developer platform with an API at its core.

Page 3: Nordic APIs - Automatic Testing of (RESTful) API Documentation
Page 4: Nordic APIs - Automatic Testing of (RESTful) API Documentation

What do you do?

Page 5: Nordic APIs - Automatic Testing of (RESTful) API Documentation

Powered by

Page 6: Nordic APIs - Automatic Testing of (RESTful) API Documentation

Documentation will be wrong

Page 7: Nordic APIs - Automatic Testing of (RESTful) API Documentation

Users hate that

Page 8: Nordic APIs - Automatic Testing of (RESTful) API Documentation

It's all in the spec

api blueprintA powerful high-level API description language for web APIs.

Page 9: Nordic APIs - Automatic Testing of (RESTful) API Documentation

## Questions Collection [/questions]

### Create a New Question [POST]

You may create your own question using this action. It takes a JSONobject containing a question and a collection of answers in theform of choices.

+ Request (application/json) { "question": "Favourite programming language?", "choices": [ "Swift", "Python", "Objective-C", "Ruby" ] }

+ Response 201 (application/json) + Headers Location: /questions/2 + Body { "question": "Favourite programming language?", "published_at": "2015-08-05T08:40:51.620Z", "choices": [ { "choice": "Swift",

Page 10: Nordic APIs - Automatic Testing of (RESTful) API Documentation

Let's get testing

DREDDNo more outdated API documentation.

Page 11: Nordic APIs - Automatic Testing of (RESTful) API Documentation

Testing read-onlynode_modules/.bin/dredd cma.apib https://api.contentful.com \ -m GET

Page 12: Nordic APIs - Automatic Testing of (RESTful) API Documentation
Page 13: Nordic APIs - Automatic Testing of (RESTful) API Documentation
Page 14: Nordic APIs - Automatic Testing of (RESTful) API Documentation

HooksbeforeAll called at the beginning of the whole test run

beforeEach called before each HTTP transaction

before called before some specific HTTP transaction

beforeEachValidation called before each HTTP transaction is validated

beforeValidation called before some specific HTTP transaction is validated

after called a�er some specific HTTP transaction regardless its result

afterEach called a�er each HTTP transaction

afterAll called a�er whole test run

Page 15: Nordic APIs - Automatic Testing of (RESTful) API Documentation

Hooksnode_modules/.bin/dredd cma.apib https://api.contentful.com \ --hookfiles=./test-hooks.js \ -m GET

Page 16: Nordic APIs - Automatic Testing of (RESTful) API Documentation

Skipping Testsvar hooks = require('hooks');

hooks.before( "Webhook calls > Webhook call details > Get the webhook call details", function (transaction) { transaction.skip = true; });

Page 17: Nordic APIs - Automatic Testing of (RESTful) API Documentation

Mutating datavar hooks = require('hooks');

hooks.before( "Entries > Delete an Entry", function (transaction) { client.createEntry(entry, "1234") });

Page 18: Nordic APIs - Automatic Testing of (RESTful) API Documentation

Rate limitingvar hooks = require('hooks');const DELAY = 1000/6.0;

hooks.afterEach(function(transaction, done) { setTimeout(done, DELAY);});

Page 19: Nordic APIs - Automatic Testing of (RESTful) API Documentation

Changing request datavar hooks = require('hooks');

hooks.beforeEach(function (transaction, done) { transaction.fullPath = transaction.fullPath.replace('fp91oelsziea', 'gbkxklvmolc1');

done();});

Page 20: Nordic APIs - Automatic Testing of (RESTful) API Documentation

Chai assertionsvar hooks = require('hooks');var assert = require('chai').assert;

hooks.afterEach(function(transaction, done) { if (!transaction.skip) { assert.match(transaction.real.headers['x-contentful-request-id'], /̂content-api:[a-zA-Z0-9]{22}$/ }

done();});

Page 21: Nordic APIs - Automatic Testing of (RESTful) API Documentation

Security �

Page 22: Nordic APIs - Automatic Testing of (RESTful) API Documentation

Censor private datavar hooks = require('hooks');const SECRET_HEADERS = ['Authorization'].map(header => header.toLowerCase());

hooks.afterEachValidation(function(transaction, done) { Object.keys(transaction.request.headers).forEach(function(key) { if (SECRET_HEADERS.indexOf(key.toLowerCase()) > -1) { transaction.request.headers[key] = "***HIDDEN SECRET DATA***"; } });

done();});

Page 23: Nordic APIs - Automatic Testing of (RESTful) API Documentation

CI the beast

Page 24: Nordic APIs - Automatic Testing of (RESTful) API Documentation
Page 25: Nordic APIs - Automatic Testing of (RESTful) API Documentation

ConclusionBase your documentation on an API specTest that specMake it part of your CIDon't substitute your integration tests

Page 26: Nordic APIs - Automatic Testing of (RESTful) API Documentation

Slides available on Slideshare: http://www.slideshare.net/rwessling/nordic-apis-automatic-testing-of-restful-api-documentation

Follow me on Twitter: @RouvenWessling