项目作者: faabiosr

项目描述 :
Asserting data against OpenAPI docs.
高级语言: Go
项目地址: git://github.com/faabiosr/openapi-assert.git
创建时间: 2018-10-18T00:58:25Z
项目社区:https://github.com/faabiosr/openapi-assert

开源协议:MIT License

下载


OpenAPI - Assert

Build Status
Codecov branch
Go Reference
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.

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

Then import the package into your own code:

  1. 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:

  1. package main
  2. import (
  3. "log"
  4. "net/http"
  5. assert "github.com/faabiosr/openapi-assert"
  6. )
  7. func main() {
  8. doc, err := assert.LoadFromURI("http://petstore.swagger.io/v2/swagger.json")
  9. if err != nil {
  10. log.Fatal(err)
  11. }
  12. assert := assert.New(doc)
  13. log.Println(
  14. assert.RequestMediaType("text/html", "/pet", http.MethodPost),
  15. )
  16. log.Println(
  17. assert.RequestMediaType("image/gif", "/v2/pet", http.MethodPost),
  18. )
  19. }

Asserting http request object using the swagger schema file:

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. assert "github.com/faabiosr/openapi-assert"
  7. )
  8. func main() {
  9. doc, err := assert.LoadFromURI("http://petstore.swagger.io/v2/swagger.json")
  10. if err != nil {
  11. log.Fatal(err)
  12. }
  13. assert := assert.New(doc)
  14. http.HandleFunc("/v2/pet", func(w http.ResponseWriter, r *http.Request) {
  15. err := assert.Request(r)
  16. fmt.Fprint(w, err)
  17. })
  18. log.Fatal(
  19. http.ListenAndServe("127.0.0.1:9000", nil),
  20. )
  21. }

Asserting http response object using the swagger schema file:

  1. package main
  2. import (
  3. "log"
  4. "net/http"
  5. assert "github.com/faabiosr/openapi-assert"
  6. )
  7. func main() {
  8. doc, err := assert.LoadFromURI("http://petstore.swagger.io/v2/swagger.json")
  9. if err != nil {
  10. log.Fatal(err)
  11. }
  12. assert := assert.New(doc)
  13. res, err := http.Get("https://petstore.swagger.io/v2/pet/111111422")
  14. if err != nil {
  15. log.Fatal(err)
  16. }
  17. log.Println(assert.Response(res))
  18. }

Examples

Development

Requirements

Makefile

  1. # Clean up
  2. $ make clean
  3. # Download project dependencies
  4. $ make configure
  5. # Run tests and generates html coverage file
  6. $ make cover
  7. # Format all go files
  8. $ make fmt
  9. # GolangCI-Lint
  10. $ make lint
  11. # Run tests
  12. $make test

License

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