Giter VIP home page Giter VIP logo

oaichecker's Introduction

license Build Status GoDoc codecov GolangCI Go Report Card

OpenAPI Checker

Run you server tests and at the same time check if you tests match with you OpenAPI specs.

Why?

As a developper I often forget to change the specs after a server modification and so my API documentation is always deprecated after 2 commits. In order to fix that I have written a little lib which takes you tests HTTP request/responses an matches them against your API specs. If you request doesn't matchs the specs, you request will fail and so you test also fail.

How?

This lib permits you to generate an http.RoundTripper which can be easily integrated inside you http.Client from you already existing tests.

Example

import (
	"github.com/stretchr/testify/assert"
	"github.com/Peltoche/oaichecker"
)

var client http.Client

func init() {
	// Loads the specs file.
	//
	// It contains only a the specs for the endpoint 'GET /pets'.
	specs, err := oaichecker.NewSpecsFromFile("./dataset/petstore_minimal.json")
	if err != nil {
		panic(err)
	}

	// Create a client which the custom transport
	client = http.Client{
		Transport: oaichecker.NewTransport(specs),
	}
}

func Test_Posting_a_valid_pet(t *testing.T) {
	// Make the requests as usual.
	//
	// In this case the request is valid but the specs are not followed because
	// the endpoint 'POST /v2/pet' is not defined inside the specs, only 'GET /pets'.
	_, err := client.Post("http://petstore.swagger.io/v2/pet", "application/json", strings.NewReader(`{
	"name": "doggie",
	"photoUrls": ["some-url"]}`))

	// This assert should success but as the specs are not followed, `req` is
	// nil and `err` contains the following message:
	//
	// "Post http://petstore.swagger.io/v2/pet: operation not defined inside the specs"
	assert.NoError(t, err)
}

oaichecker's People

Contributors

peltoche avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

icodein

oaichecker's Issues

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.