API Testing in your CI Pipeline

[email protected] www.container-solutions.com Jason Smith

Senior Engineer

Container Solutions

@jasonrichardsmi @containersoluti The Three Things

● Specifications

● Testing your API

● Mocking your API

www.container-solutions.com [email protected] The Three Things

● Specifications

● Testing your API

● Mocking your API

www.container-solutions.com [email protected] 19 Nov 1863

www.container-solutions.com [email protected] Now

www.container-solutions.com [email protected] IDL (Interface Description Language)

J. R. Nestor W. A. Wulf . A. Lamb

August 1981 Carnegie-Mellon University Computer Science Department

www.container-solutions.com [email protected] IDL (Interface Description Language)

In order to work together harmoniously, ... programs ... must have a precise and compatible definition of the data structures they use to communicate with each other.

www.container-solutions.com [email protected] IDL (Interface Description Language)

● Precision

● Representation Independence

● Language Independence

● Maintainability

● Communication Form

www.container-solutions.com [email protected] SOAP

WSDL

www.container-solutions.com [email protected] REST

Tony Tam recounts the history of Swagger and the Open API Initiative

www.container-solutions.com [email protected] REST

Not standardized...

www.container-solutions.com [email protected] OpenAPI Specification

Formerly Swagger

Open API Initiative()

www.container-solutions.com [email protected] Other Major Players

API Blueprint

RAML

www.container-solutions.com [email protected] YAML!

www.container-solutions.com [email protected] So many tools sswagger-js lua-Spore ::Plugin::Swagger2 bootprint-swagger swagger-jack swagger-ui NSwag -swagger Dredd hapi-swagger swagger-tools Swashbuckle Swagger-PHP ember-swagger-ui Swagger Validation swagger-editor Swashbuckle.AspNetCore SwaggerAssertions generator-openapi-repo swagger-node-client swagger-core AutoRest -swaggerize-fastroute-library intellij-swagger ratify swagger-parser SwaggerProvider SwaggerGen linter-swagger swagger-express swagger-codegen NSwag Jane OpenAPI ReDoc NelmioApiDocBundle swagger-node QSwag gossi/swagger swagger-commander Restler validator-badge SwaggerWcf calcinai/strut swagger-confluence swagger-assert ring-swagger Nancy.Swagger falsy swagger-diff Swaggervel compojure- a127 pyramid-swagger swagger.ed -rest-swagger fnhouse-swagger api-spec-converter -restplus SwagDefGen django-tastypie-swagger pedestal-swagger blueoak-server pyswagger vscode-apielements flask-restful-swagger swagger1st express-openapi flex ember-swagger-ui swagger-docs Friboo fleek-parser Flasgger commandcar source2swagger swagger-sdk fleek-response flask-swagger SwagGen swagger_engine cbSwagger fleek-router bravado swagger-mock-validator swaggerize fleek-validator bravado-core Spectacle spray-swagger cowboy-swagger fury-adapter-swagger swagger-spec-validator octohipster Api-doc PhoenixSwagger generator-swaggerize swagger-py-codegen swagger-docs-cfml gform-admin go-swagger got-swag Connexion swagger swagger-cli-client goa hapi-swaggered pecan-swagger go-restful Swagger2Postman grpc-gateway hippie-swagger swagger-parser Sashay swagger2 oas-nodegen swagger-tester beego servant-swagger pokemock swagger-aggregator Swagger spec generator assertj-swagger sails-swagger swagger-stub restapidoc binder-swagger- serverless_swagger -swagger swaggydoc dropwizard-swagger swagger-jsdoc prance swagger4spring-web elide swagger-mongodb Apivore swagger-jaxrs-doclet jooby-swagger swagger-tools grape-swagger swaggerj4 restlet-framework swaggerize-express swagger-blocks swaggerapi springfox swaggerize-hapi swagger_engine swagger-validator swagger-codegen-maven-plugin swagmock swagger-ajax-client swagger2markup Swagger Express Middleware rspec-rails-swagger swagger--client swagger2markup-gradle-plugin Swagger Parser rswag swagger-client-generator swagger-maven-plugin test2doc.js sbt-swagger-codegen swagger-validate swagger-codegen-gradle-plugin koa-joi-swagger play-swagger ServiceStack swagger-request-validator Raisin Fintrospect fubumvc-swagger Swagger Parser Swagger2 SwagGen dotswaggen Jester - Jolie rEST routER Swagger2::Client .guru Swagger Framework

www.container-solutions.com [email protected] The Repo

https://github.com/ContainerSolutions/DreddDemo

www.container-solutions.com [email protected] + =

https://github.com/ContainerSolutions/DreddDemo www.container-solutions.com [email protected] The API

Swagger Editor

Swagger UI

https://github.com/ContainerSolutions/DreddDemo www.container-solutions.com [email protected] Simple Server

Go (Gorilla Mux)

Special Handlers

https://github.com/ContainerSolutions/DreddDemo www.container-solutions.com [email protected] Special Handlers

authMiddleware

NopeHandler

https://github.com/ContainerSolutions/DreddDemo www.container-solutions.com [email protected] The Three Things

● Specifications

● Testing your API

● Mocking your API

www.container-solutions.com [email protected] Dredd

https://github.com/apiaryio/dredd

“Dredd reads your API description and step by step validates whether your API implementation replies with responses as they are described in the documentation.”

www.container-solutions.com [email protected] Dredd dry-run: null hookfiles: dredd/hooks.js language: nodejs sandbox: false server: ./main dredd.yml server-wait: 2 init: false custom: {} names: false only: [] reporter: [] output: [] header: [] ...

www.container-solutions.com [email protected] Dredd Hooks

● POST > /items Requires Auth ● GET > /nope returns error

www.container-solutions.com [email protected] Dredd

$dredd

www.container-solutions.com [email protected] www.container-solutions.com [email protected] Makefile

builddredd: @docker build -t dreddtest -f dredd/DockerDredd .

dreddtest: builddredd @docker run\ -e "DEMOSHASALT=test_salt"\ dreddtest

www.container-solutions.com [email protected] Run the test

$make dreddtest

www.container-solutions.com [email protected] Circle-CI

machine: services: - docker test: override: - make dreddtest

www.container-solutions.com [email protected] Travis-CI

language: go go: 1.6 services: - docker script: - make dreddtest

www.container-solutions.com [email protected] Results

https://travis-ci.org/ContainerSolutions/DreddDemo

https://circleci.com/gh/ContainerSolutions/DreddDemo

https://semaphoreci.com/jasonrichardsmith/dredddemo

http://54.72.105.103:8080/job/DreddDemo/

www.container-solutions.com [email protected] The Three Things

● Specifications

● Testing your API

● Mocking your API

www.container-solutions.com [email protected] Mocking your api

www.container-solutions.com [email protected] Pokemock

https://github.com/mobilcom-debitel/pokemock

Drakov https://github.com/Aconex/drakov

www.container-solutions.com [email protected] www.container-solutions.com [email protected] Pokemock

FROM node:7-alpine

RUN install -g pokemock

COPY apispec/spec.yml . COPY pokemock/runpokemock.sh . ENTRYPOINT /bin/sh runpokemock.sh

www.container-solutions.com [email protected] Let’s make it public

https://sloppy.io/

www.container-solutions.com [email protected] Those tests

Badges

www.container-solutions.com [email protected] What you get from the start

● A formal contract

● API testing

● A mock for consumer testing

www.container-solutions.com [email protected] Thank You! Jason Smith

Senior Engineer

Container Solutions

@jasonrichardsmi