vienna.js - Automatic testing of (RESTful) API documentation
-
Upload
rouven-wessling -
Category
Software
-
view
186 -
download
2
Transcript of vienna.js - Automatic testing of (RESTful) API documentation
![Page 1: vienna.js - Automatic testing of (RESTful) API documentation](https://reader034.fdocuments.net/reader034/viewer/2022051318/58714db01a28ab55588b7273/html5/thumbnails/1.jpg)
Automatic testing of(RESTful) API documentation
vienna.js Meetup, June 2016
By Rouven Weßling ( ) Ecosystem Developer / Developer Evangelist, Contentful
@RouvenWessling
photo credit: by Matthias Ripp Vienna night view (license)
![Page 2: vienna.js - Automatic testing of (RESTful) API documentation](https://reader034.fdocuments.net/reader034/viewer/2022051318/58714db01a28ab55588b7273/html5/thumbnails/2.jpg)
A content management developer platform with an API at its core.
![Page 3: vienna.js - Automatic testing of (RESTful) API documentation](https://reader034.fdocuments.net/reader034/viewer/2022051318/58714db01a28ab55588b7273/html5/thumbnails/3.jpg)
Content Deliver API Preview API Images API (Not RESTful)
Content Management API
![Page 4: vienna.js - Automatic testing of (RESTful) API documentation](https://reader034.fdocuments.net/reader034/viewer/2022051318/58714db01a28ab55588b7273/html5/thumbnails/4.jpg)
![Page 5: vienna.js - Automatic testing of (RESTful) API documentation](https://reader034.fdocuments.net/reader034/viewer/2022051318/58714db01a28ab55588b7273/html5/thumbnails/5.jpg)
![Page 6: vienna.js - Automatic testing of (RESTful) API documentation](https://reader034.fdocuments.net/reader034/viewer/2022051318/58714db01a28ab55588b7273/html5/thumbnails/6.jpg)
api blueprintA powerful high-level API description language for web APIs.
![Page 7: vienna.js - Automatic testing of (RESTful) API documentation](https://reader034.fdocuments.net/reader034/viewer/2022051318/58714db01a28ab55588b7273/html5/thumbnails/7.jpg)
FORMAT: 1A HOST: http://polls.apiblueprint.org/
# Polls API
Polls is a simple API allowing consumers to view polls and vote in them.
![Page 8: vienna.js - Automatic testing of (RESTful) API documentation](https://reader034.fdocuments.net/reader034/viewer/2022051318/58714db01a28ab55588b7273/html5/thumbnails/8.jpg)
## Questions Collection [/questions]
### List All Questions [GET]
+ Response 200 (application/json) [ { "question": "Favourite programming language?", "published_at": "2015-08-05T08:40:51.620Z", "choices": [ { "choice": "Swift", "votes": 2048 }, { "choice": "Python", "votes": 1024 }, { "choice": "Objective-C", "votes": 512 }, { "choice": "Ruby", "votes": 256 } ] } ]
![Page 9: vienna.js - Automatic testing of (RESTful) API documentation](https://reader034.fdocuments.net/reader034/viewer/2022051318/58714db01a28ab55588b7273/html5/thumbnails/9.jpg)
### Create a New Question [POST]
You may create your own question using this action. It takes a JSON object containing a question and a collection of answers in the form 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", "votes": 0 }, {
![Page 10: vienna.js - Automatic testing of (RESTful) API documentation](https://reader034.fdocuments.net/reader034/viewer/2022051318/58714db01a28ab55588b7273/html5/thumbnails/10.jpg)
## Content type [/spaces/{space_id}/content_types/{content_type_id}?access_token={access_token}]
+ Parameters + space_id: cfexampleapi (required, string) - ID of the space in form of a string + access_token: b4c0n73n7fu1 (required, string) - A *production* Content Delivery API key. + content_type_id: cat (required, string) - ID of the content type in form of a string
![Page 11: vienna.js - Automatic testing of (RESTful) API documentation](https://reader034.fdocuments.net/reader034/viewer/2022051318/58714db01a28ab55588b7273/html5/thumbnails/11.jpg)
![Page 12: vienna.js - Automatic testing of (RESTful) API documentation](https://reader034.fdocuments.net/reader034/viewer/2022051318/58714db01a28ab55588b7273/html5/thumbnails/12.jpg)
DREDDNo more outdated API documentation.
![Page 13: vienna.js - Automatic testing of (RESTful) API documentation](https://reader034.fdocuments.net/reader034/viewer/2022051318/58714db01a28ab55588b7273/html5/thumbnails/13.jpg)
Testing read-onlynode_modules/.bin/dredd cma.apib https://api.contentful.com \ -m GET
![Page 14: vienna.js - Automatic testing of (RESTful) API documentation](https://reader034.fdocuments.net/reader034/viewer/2022051318/58714db01a28ab55588b7273/html5/thumbnails/14.jpg)
![Page 15: vienna.js - Automatic testing of (RESTful) API documentation](https://reader034.fdocuments.net/reader034/viewer/2022051318/58714db01a28ab55588b7273/html5/thumbnails/15.jpg)
![Page 16: vienna.js - Automatic testing of (RESTful) API documentation](https://reader034.fdocuments.net/reader034/viewer/2022051318/58714db01a28ab55588b7273/html5/thumbnails/16.jpg)
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 isvalidated
after called after some specific HTTP transaction regardless its result
afterEach called after each HTTP transaction
afterAll called after whole test run
![Page 17: vienna.js - Automatic testing of (RESTful) API documentation](https://reader034.fdocuments.net/reader034/viewer/2022051318/58714db01a28ab55588b7273/html5/thumbnails/17.jpg)
Hooksnode_modules/.bin/dredd cma.apib https://api.contentful.com \ --hookfiles=./test-hooks.js \ -m GET
![Page 18: vienna.js - Automatic testing of (RESTful) API documentation](https://reader034.fdocuments.net/reader034/viewer/2022051318/58714db01a28ab55588b7273/html5/thumbnails/18.jpg)
Skipping Testsvar hooks = require('hooks');
hooks.before( "Webhook calls > Webhook call details > Get the webhook call details", function (transaction) { transaction.skip = true; });
![Page 19: vienna.js - Automatic testing of (RESTful) API documentation](https://reader034.fdocuments.net/reader034/viewer/2022051318/58714db01a28ab55588b7273/html5/thumbnails/19.jpg)
Rate limitingvar hooks = require('hooks'); const DELAY = 1000/6.0;
hooks.afterEach(function(transaction, done) { setTimeout(done, DELAY); });
![Page 20: vienna.js - Automatic testing of (RESTful) API documentation](https://reader034.fdocuments.net/reader034/viewer/2022051318/58714db01a28ab55588b7273/html5/thumbnails/20.jpg)
Changing request datavar hooks = require('hooks');
hooks.beforeEach(function (transaction, done) { transaction.fullPath = transaction.fullPath.replace('fp91oelsziea', 'gbkxklvmolc1');
done(); });
![Page 21: vienna.js - Automatic testing of (RESTful) API documentation](https://reader034.fdocuments.net/reader034/viewer/2022051318/58714db01a28ab55588b7273/html5/thumbnails/21.jpg)
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 22: vienna.js - Automatic testing of (RESTful) API documentation](https://reader034.fdocuments.net/reader034/viewer/2022051318/58714db01a28ab55588b7273/html5/thumbnails/22.jpg)
Security �
![Page 23: vienna.js - Automatic testing of (RESTful) API documentation](https://reader034.fdocuments.net/reader034/viewer/2022051318/58714db01a28ab55588b7273/html5/thumbnails/23.jpg)
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 24: vienna.js - Automatic testing of (RESTful) API documentation](https://reader034.fdocuments.net/reader034/viewer/2022051318/58714db01a28ab55588b7273/html5/thumbnails/24.jpg)
Slides will be available on Slideshare: http://www.slideshare.net/rwessling