SlideShare a Scribd company logo
API Contract testing
Ryan M Harrison August 23, 2019
More testing, why!?...just let me code
2
Aside: If you need some help
with the math, let me know, but
that should be enough to get
you started! Huh? No, I don't
need to read your thesis, I can
imagine roughly what it says.
API specification languages
3
OAS (Open API Spec)
AKA: Swagger
API Blueprint RAML
RESTful API Modeling
Language
API specification languages
4
#%RAML 1.0
title: Pet Shop
uses:
NewLibrary:
NewLibrary.raml
version: v1
baseUri: /shop
types:
Mammal:
type:
NewLibrary.Pet
Bird:
type:
NewLibrary.Pet
properties:
wingLength:
number
swagger: "2.0",
info: {…},
host: "petstore.swagger.io",
basePath: "/v2",
tags: […],
schemes: […],
paths: [{
/pet: {
post: {
tags: {
summary "Add a new
pet to the store",
description ""},
operationId:
{"addPet"}
}]
}]
FORMAT: 1A
# Pets
Petstore!
## Pets Collection [/pets]
### List All Pets [GET]
+ Response 200
(application/json)
{
"name": "Buckbeak",
"species": "Hippogriff",
}
API specification languages
5
Open API Spec API Blueprint RAML
Sponsor Open API Initiative
(SmartBear)
Apiary
(Oracle)
RAML Workgroup
(Mulesoft)
Format JSON Markdown
(apib)
YAML
Schema JSON Schema MSON
License Apache 2.0 MIT Apache 2.0
API Gateway
support
AWS API Gateway
Apigee (Google)
3scale (Rehat)
Contracts drive tooling
6
Supported by automated tooling
- Contract → application
controllers
- Contract → End-point testing
- Contract → Documentation
- Contract → SDK stubs
- Contract → Access control
{
"swagger":"2.0",
"host":"petstore.swagger.io",
"/pet/{petId}":{
"get":{
"operationId":"getPetById",
"parameters":[ ],
"responses":{ }
}
}
}
Example: Interactive Docs
7
widdershins
swagger-ui
{
"swagger":"2.0",
"host":"petstore.swagger.io",
"/pet/{petId}":{
"get":{
"operationId":"getPetById",
"parameters":[ ],
"responses":{ }
}
}
}
Tooling
8
Open API Spec
Authoring / Editor swagger-editor
API docs swagger-ui,
widdershins,
spectacle, redoc
Testing dredd
Mock server prism
SDK stubs swagger-codegen
Contract->Controller swagger-node
Testing > Contract
● Authoring / Editor
● Interactive API docs
● Testing
○ Contract^
■ Producer-side (server-side)
■ Consumer-side (client-side)
○ Behavior
● Mock server
● API Stubbing
● Contract-to-Controller integration
● Spec conversion
9
Testing > Contract
● Shared understanding >> Business logic
● Contract is the “source of truth”
10
DEMO: Dredd w/ amida-auth-service
11
Follow along: https://github.com/amida-tech/amida-auth-
microservice/tree/devreview-20190823-dredd
Gotchas
● Must manage state in hooks
○ Or work-around using `--sorted`
● Dredd still doesn’t support Java hooks^
● Assume 1:1 Request:Response, i.e. multi-response request not
fully supported^
● Incompatibility between OAS Schema Objects and JSON
Schema
Dredd gotchas
12
● Must use hooks for auth flows
● Must manage state in hooks
○ Or quick-and-dirty work-around using `--sorted`
● Dredd still doesn’t support Java hooks^
● Assume 1:1 Request:Response, i.e. multi-response request not
fully supported^
● Some incompatibility between OAS Schema Objects and JSON
Schema, i.e. $ref be your bane
Testing
● Authoring / Editor
● Interactive API docs
● Testing
○ Contract^
■ Producer-side (server-side)
■ Consumer-side (client-side)
○ Behavior
● Mock server
● API Stubbing
● Contract-to-Controller integration
● Spec conversion
13
Testing > Contract > Consumer-side
14
Producer-sideConsumer-side
Testing > Behavioral
15
● karate (Java)
● Behave (Python)
● RESTinstance (JS)
● apickli (JS)
Scenario: Create user, view list, delete user
Given url_users
And request {username: <>, password: <>}
When method post
Given url_users
When method get
Given url_users
And request {id: response.id}
When method delete
Ponderings
● Binding API contract to controllers, e.g. swagger-node?
● Where to include validation rules?
○ Door #1: Unit tests ⇒ No examples for API consumer
○ Door #2: Contract ⇒ Bloated contract
○ Door #3: Behavior ⇒ Unclear “user flow”
● Postman collections vs API contracts
○ Postman doesn’t seem to support comparison to
example^
○ Cannot export from Postman to any API Spec^
● Transformers: Good open source one?
○ Proprietary: https://www.apimatic.io/transformer/
16
Binding contract to controller
17
getPetById('/pet/:PetId',
(req, res, next) => {
validate(PetId)
myDB.get(new Resource('/pet', petId
(err, pet) => {
if(err) next(err)
res.json(pet.data)
})
})
{
"swagger":"2.0",
"host":"petstore.swagger.io",
"/pet/{petId}":{
"get":{
"operationId":"getPetById",
"parameters":[ ],
"responses":{ }
}
}
}
middleware: swagger-node
Questions?

More Related Content

What's hot (20)

PPT
Java Servlets
BG Java EE Course
 
PPTX
Linear transformation and application
shreyansp
 
PDF
Graph theory in network system
Manikanta satyala
 
PDF
Python Basics | Python Tutorial | Edureka
Edureka!
 
PPTX
2. R-basics, Vectors, Arrays, Matrices, Factors
krishna singh
 
PDF
Inner product spaces
EasyStudy3
 
PDF
Relation Hasse diagram
Rachana Pathak
 
PPT
Chapter 5 Graphs (1).ppt
ishan743441
 
PPTX
graph theory
ganith2k13
 
PPT
01 xml document structure
Baskarkncet
 
PPTX
Systems Of Differential Equations
JDagenais
 
PDF
Functions in discrete mathematics
Rachana Pathak
 
PPT
Switch statements in Java
Jin Castor
 
PDF
Datatypes in python
eShikshak
 
PPTX
recurrence relations
Anurag Cheela
 
PPTX
CMSC 56 | Lecture 15: Closures of Relations
allyn joy calcaben
 
PDF
Function arguments In Python
Amit Upadhyay
 
PPTX
Applications of graph theory
NilaNila16
 
PPTX
Application of partial derivatives with two variables
Sagar Patel
 
PDF
Python Tutorial | Python Tutorial for Beginners | Python Training | Edureka
Edureka!
 
Java Servlets
BG Java EE Course
 
Linear transformation and application
shreyansp
 
Graph theory in network system
Manikanta satyala
 
Python Basics | Python Tutorial | Edureka
Edureka!
 
2. R-basics, Vectors, Arrays, Matrices, Factors
krishna singh
 
Inner product spaces
EasyStudy3
 
Relation Hasse diagram
Rachana Pathak
 
Chapter 5 Graphs (1).ppt
ishan743441
 
graph theory
ganith2k13
 
01 xml document structure
Baskarkncet
 
Systems Of Differential Equations
JDagenais
 
Functions in discrete mathematics
Rachana Pathak
 
Switch statements in Java
Jin Castor
 
Datatypes in python
eShikshak
 
recurrence relations
Anurag Cheela
 
CMSC 56 | Lecture 15: Closures of Relations
allyn joy calcaben
 
Function arguments In Python
Amit Upadhyay
 
Applications of graph theory
NilaNila16
 
Application of partial derivatives with two variables
Sagar Patel
 
Python Tutorial | Python Tutorial for Beginners | Python Training | Edureka
Edureka!
 

Similar to 2019-08-23 API contract testing with Dredd (20)

PDF
JHipster Beyond CRUD - JHipster Conf' 2019
Intesys
 
PDF
Zen and the Art of REST API documentation - MuCon London 2015
Steve Judd
 
PDF
LF_APIStrat17_Your API Spec is a Contract, So Test It Like One!
LF_APIStrat
 
PDF
Test-Driven Documentation for your REST(ful) service
Jeroen Reijn
 
PPTX
Another API-Blueprint, RAML and Swagger Comparison
SmartBear
 
PDF
Building APIs with the OpenApi Spec
Pedro J. Molina
 
PPTX
SVQdotNET: Building APIs with OpenApi
Juan Luis Guerrero Minero
 
PPTX
Contract driven development
Stephen Erdman
 
PDF
Always up to date, testable and maintainable documentation with OpenAPI
GOG.com dev team
 
PPTX
Swagger - make your API accessible
Victor Trakhtenberg
 
PDF
Jcon 2017 How to use Swagger to develop REST applications
johannes_fiala
 
PDF
Testing swagger contracts without contract based testing
Алексей Стягайло
 
PPTX
API Description Languages: Which is the Right One for Me?
Akana
 
PPTX
API Description Languages: Which is the Right One for Me?
Akana
 
PPTX
How to generate a rest application - DevFest Vienna 2016
johannes_fiala
 
PPTX
Model-driven Round-trip Engineering of REST APIs
Jordi Cabot
 
PDF
apidays LIVE New York - API Code First vs Design First by Phil Sturgeon
apidays
 
PDF
Designing APIs with OpenAPI Spec
Adam Paxton
 
PDF
Generating docs from APIs
jamiehannaford
 
PDF
APIdays Paris 2019 - OpenAPI and AsyncAPI specifications as contracts by Mehd...
apidays
 
JHipster Beyond CRUD - JHipster Conf' 2019
Intesys
 
Zen and the Art of REST API documentation - MuCon London 2015
Steve Judd
 
LF_APIStrat17_Your API Spec is a Contract, So Test It Like One!
LF_APIStrat
 
Test-Driven Documentation for your REST(ful) service
Jeroen Reijn
 
Another API-Blueprint, RAML and Swagger Comparison
SmartBear
 
Building APIs with the OpenApi Spec
Pedro J. Molina
 
SVQdotNET: Building APIs with OpenApi
Juan Luis Guerrero Minero
 
Contract driven development
Stephen Erdman
 
Always up to date, testable and maintainable documentation with OpenAPI
GOG.com dev team
 
Swagger - make your API accessible
Victor Trakhtenberg
 
Jcon 2017 How to use Swagger to develop REST applications
johannes_fiala
 
Testing swagger contracts without contract based testing
Алексей Стягайло
 
API Description Languages: Which is the Right One for Me?
Akana
 
API Description Languages: Which is the Right One for Me?
Akana
 
How to generate a rest application - DevFest Vienna 2016
johannes_fiala
 
Model-driven Round-trip Engineering of REST APIs
Jordi Cabot
 
apidays LIVE New York - API Code First vs Design First by Phil Sturgeon
apidays
 
Designing APIs with OpenAPI Spec
Adam Paxton
 
Generating docs from APIs
jamiehannaford
 
APIdays Paris 2019 - OpenAPI and AsyncAPI specifications as contracts by Mehd...
apidays
 
Ad

More from Ryan M Harrison (8)

PPTX
2020-11-13 Anatomy of a FHIR Implementation Guide
Ryan M Harrison
 
PDF
2021 12-03 TOGAF for Developers
Ryan M Harrison
 
PDF
Positioning yourself for success in technical careers
Ryan M Harrison
 
PPTX
2019-01-24 Sequelize ORM (Object Relational Mapper): models, migrations, oh my
Ryan M Harrison
 
PDF
Breaking a monolith: In-place refactoring with service-oriented architecture ...
Ryan M Harrison
 
PDF
End-To-End Asymmetric Encryption of Biomedical Data In-Transit and At-Rest
Ryan M Harrison
 
PDF
Nest v. Flat with EmberData
Ryan M Harrison
 
PDF
DEF CON 24: Reverse engineering biomedical equipment for fun and open science
Ryan M Harrison
 
2020-11-13 Anatomy of a FHIR Implementation Guide
Ryan M Harrison
 
2021 12-03 TOGAF for Developers
Ryan M Harrison
 
Positioning yourself for success in technical careers
Ryan M Harrison
 
2019-01-24 Sequelize ORM (Object Relational Mapper): models, migrations, oh my
Ryan M Harrison
 
Breaking a monolith: In-place refactoring with service-oriented architecture ...
Ryan M Harrison
 
End-To-End Asymmetric Encryption of Biomedical Data In-Transit and At-Rest
Ryan M Harrison
 
Nest v. Flat with EmberData
Ryan M Harrison
 
DEF CON 24: Reverse engineering biomedical equipment for fun and open science
Ryan M Harrison
 
Ad

Recently uploaded (20)

PPTX
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PDF
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
PPTX
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
PDF
Technical-Careers-Roadmap-in-Software-Market.pdf
Hussein Ali
 
PPTX
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PPTX
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
PPTX
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
PDF
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
PDF
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
PDF
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
PPTX
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
PDF
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
PDF
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PPTX
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
PPTX
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
AOMEI Partition Assistant Crack 10.8.2 + WinPE Free Downlaod New Version 2025
bashirkhan333g
 
PDF
NEW-Viral>Wondershare Filmora 14.5.18.12900 Crack Free
sherryg1122g
 
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
Technical-Careers-Roadmap-in-Software-Market.pdf
Hussein Ali
 
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
AOMEI Partition Assistant Crack 10.8.2 + WinPE Free Downlaod New Version 2025
bashirkhan333g
 
NEW-Viral>Wondershare Filmora 14.5.18.12900 Crack Free
sherryg1122g
 

2019-08-23 API contract testing with Dredd

  • 1. API Contract testing Ryan M Harrison August 23, 2019
  • 2. More testing, why!?...just let me code 2 Aside: If you need some help with the math, let me know, but that should be enough to get you started! Huh? No, I don't need to read your thesis, I can imagine roughly what it says.
  • 3. API specification languages 3 OAS (Open API Spec) AKA: Swagger API Blueprint RAML RESTful API Modeling Language
  • 4. API specification languages 4 #%RAML 1.0 title: Pet Shop uses: NewLibrary: NewLibrary.raml version: v1 baseUri: /shop types: Mammal: type: NewLibrary.Pet Bird: type: NewLibrary.Pet properties: wingLength: number swagger: "2.0", info: {…}, host: "petstore.swagger.io", basePath: "/v2", tags: […], schemes: […], paths: [{ /pet: { post: { tags: { summary "Add a new pet to the store", description ""}, operationId: {"addPet"} }] }] FORMAT: 1A # Pets Petstore! ## Pets Collection [/pets] ### List All Pets [GET] + Response 200 (application/json) { "name": "Buckbeak", "species": "Hippogriff", }
  • 5. API specification languages 5 Open API Spec API Blueprint RAML Sponsor Open API Initiative (SmartBear) Apiary (Oracle) RAML Workgroup (Mulesoft) Format JSON Markdown (apib) YAML Schema JSON Schema MSON License Apache 2.0 MIT Apache 2.0 API Gateway support AWS API Gateway Apigee (Google) 3scale (Rehat)
  • 6. Contracts drive tooling 6 Supported by automated tooling - Contract → application controllers - Contract → End-point testing - Contract → Documentation - Contract → SDK stubs - Contract → Access control { "swagger":"2.0", "host":"petstore.swagger.io", "/pet/{petId}":{ "get":{ "operationId":"getPetById", "parameters":[ ], "responses":{ } } } }
  • 8. Tooling 8 Open API Spec Authoring / Editor swagger-editor API docs swagger-ui, widdershins, spectacle, redoc Testing dredd Mock server prism SDK stubs swagger-codegen Contract->Controller swagger-node
  • 9. Testing > Contract ● Authoring / Editor ● Interactive API docs ● Testing ○ Contract^ ■ Producer-side (server-side) ■ Consumer-side (client-side) ○ Behavior ● Mock server ● API Stubbing ● Contract-to-Controller integration ● Spec conversion 9
  • 10. Testing > Contract ● Shared understanding >> Business logic ● Contract is the “source of truth” 10
  • 11. DEMO: Dredd w/ amida-auth-service 11 Follow along: https://github.com/amida-tech/amida-auth- microservice/tree/devreview-20190823-dredd Gotchas ● Must manage state in hooks ○ Or work-around using `--sorted` ● Dredd still doesn’t support Java hooks^ ● Assume 1:1 Request:Response, i.e. multi-response request not fully supported^ ● Incompatibility between OAS Schema Objects and JSON Schema
  • 12. Dredd gotchas 12 ● Must use hooks for auth flows ● Must manage state in hooks ○ Or quick-and-dirty work-around using `--sorted` ● Dredd still doesn’t support Java hooks^ ● Assume 1:1 Request:Response, i.e. multi-response request not fully supported^ ● Some incompatibility between OAS Schema Objects and JSON Schema, i.e. $ref be your bane
  • 13. Testing ● Authoring / Editor ● Interactive API docs ● Testing ○ Contract^ ■ Producer-side (server-side) ■ Consumer-side (client-side) ○ Behavior ● Mock server ● API Stubbing ● Contract-to-Controller integration ● Spec conversion 13
  • 14. Testing > Contract > Consumer-side 14 Producer-sideConsumer-side
  • 15. Testing > Behavioral 15 ● karate (Java) ● Behave (Python) ● RESTinstance (JS) ● apickli (JS) Scenario: Create user, view list, delete user Given url_users And request {username: <>, password: <>} When method post Given url_users When method get Given url_users And request {id: response.id} When method delete
  • 16. Ponderings ● Binding API contract to controllers, e.g. swagger-node? ● Where to include validation rules? ○ Door #1: Unit tests ⇒ No examples for API consumer ○ Door #2: Contract ⇒ Bloated contract ○ Door #3: Behavior ⇒ Unclear “user flow” ● Postman collections vs API contracts ○ Postman doesn’t seem to support comparison to example^ ○ Cannot export from Postman to any API Spec^ ● Transformers: Good open source one? ○ Proprietary: https://www.apimatic.io/transformer/ 16
  • 17. Binding contract to controller 17 getPetById('/pet/:PetId', (req, res, next) => { validate(PetId) myDB.get(new Resource('/pet', petId (err, pet) => { if(err) next(err) res.json(pet.data) }) }) { "swagger":"2.0", "host":"petstore.swagger.io", "/pet/{petId}":{ "get":{ "operationId":"getPetById", "parameters":[ ], "responses":{ } } } } middleware: swagger-node