Skip to content
Asserting data against OpenAPI docs.
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
_examples/echo Present an example of package using Echo Framework May 18, 2019
fixtures Load document from Uri Oct 19, 2018
.gitignore Initial Commit Oct 18, 2018
.travis.yml Initial Commit Oct 18, 2018
CONTRIBUTING.md Update the package name May 11, 2019
LICENSE Initial Commit Oct 18, 2018
Makefile Remove the coverage out file May 12, 2019
README.md
assert.go Assert http response May 12, 2019
assert_test.go Assert http response May 12, 2019
assertions.go Assert http response May 12, 2019
assertions_test.go Assert http response May 12, 2019
base_test.go
doc.go Improve README file May 18, 2019
document.go
go.mod Update the package name May 11, 2019
go.sum Assert request headers Oct 25, 2018
swagger.go Change the media types segment May 12, 2019
swagger_test.go Change the media types segment May 12, 2019

README.md

OpenAPI - Assert

Build Status Codecov branch GoDoc Go Report Card License

Description

openapi-assert is a Go package that provides a affordable way to validate http requests and responses data throught OpenAPI Schema Specification (Swagger) and the project was inspired by PHP Swagger Assertions. It has the following features:

  • Assert request and response media types
  • Assert request and response headers
  • Assert request query strings
  • Assert request and response body.
  • Assert the entire http request and response object.

Requirements

OpenAPI Assert requires Go 1.11 or later.

Instalation

Use go get.

$ go get github.com/faabiosr/openapi-assert

Then import the package into your own code:

import "github.com/faabiosr/openapi-assert"

Usage

The package provides methods that allow you to assert raw data using swagger files.

See it in action:

package main

import (
    assert "github.com/faabiosr/openapi-assert"
    "log"
    "net/http"
)

func main() {
    doc, err := assert.LoadFromURI("http://petstore.swagger.io/v2/swagger.json")

    if err != nil {
        log.Fatal(err)
    }

    log.Println(
        assert.RequestMediaType("text/html", doc, "/pet", http.MethodPost),
    )
}

If you want to assert data many times, it is recommended to create instance of assert:

package main

import (
    assert "github.com/faabiosr/openapi-assert"
    "log"
    "net/http"
)

func main() {
    doc, err := assert.LoadFromURI("http://petstore.swagger.io/v2/swagger.json")

    if err != nil {
        log.Fatal(err)
    }

    assert := assert.New(doc)

    log.Println(
        assert.RequestMediaType("text/html", "/pet", http.MethodPost),
    )

    log.Println(
        assert.RequestMediaType("image/gif", "/v2/pet", http.MethodPost),
    )
}

Asserting http request object using the swagger schema file:

package main

import (
	"fmt"
	assert "github.com/faabiosr/openapi-assert"
	"log"
	"net/http"
)

func main() {
	doc, err := assert.LoadFromURI("http://petstore.swagger.io/v2/swagger.json")

	if err != nil {
		log.Fatal(err)
	}

	assert := assert.New(doc)

	http.HandleFunc("/v2/pet", func(w http.ResponseWriter, r *http.Request) {
		err := assert.Request(r)

		fmt.Fprint(w, err)
	})

	log.Fatal(
		http.ListenAndServe("127.0.0.1:9000", nil),
	)
}

Asserting http request object using the swagger schema file:

package main

import (
	assert "github.com/faabiosr/openapi-assert"
	"log"
	"net/http"
)

func main() {
	doc, err := assert.LoadFromURI("http://petstore.swagger.io/v2/swagger.json")

	if err != nil {
		log.Fatal(err)
	}

	assert := assert.New(doc)

	res, err := http.Get("https://petstore.swagger.io/v2/pet/111111422")

	if err != nil {
		log.Fatal(err)
	}

	log.Println(assert.Response(res))
}

Examples

Development

Requirements

  • Install Go

Makefile

# Clean up
$ make clean

# Download project dependencies
$ make configure

# Run tests and generates html coverage file
$ make cover

# Format all go files
$ make fmt

# Run tests
$make test

License

This project is released under the MIT licence. See LICENSE for more details.

You can’t perform that action at this time.