https://github.com/faabiosr/openapi-assert
Asserting data against OpenAPI docs.
https://github.com/faabiosr/openapi-assert
assert golang openapi swagger
Last synced: about 2 months ago
JSON representation
Asserting data against OpenAPI docs.
- Host: GitHub
- URL: https://github.com/faabiosr/openapi-assert
- Owner: faabiosr
- License: mit
- Archived: true
- Created: 2018-10-18T00:58:25.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2023-02-22T19:19:58.000Z (over 2 years ago)
- Last Synced: 2024-10-01T15:10:09.430Z (about 1 year ago)
- Topics: assert, golang, openapi, swagger
- Language: Go
- Size: 129 KB
- Stars: 17
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# OpenAPI - Assert
[](https://github.com/faabiosr/openapi-assert/actions?query=workflow:test)
[](https://codecov.io/gh/faabiosr/openapi-assert)
[](https://pkg.go.dev/github.com/faabiosr/openapi-assert)
[](https://goreportcard.com/report/github.com/faabiosr/openapi-assert)
[](https://github.com/faabiosr/openapi-assert/blob/master/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](https://github.com/Maks3w/SwaggerAssertions). 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.
```sh
$ 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:
```go
package main
import (
"log"
"net/http"
assert "github.com/faabiosr/openapi-assert"
)
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:
```go
package main
import (
"fmt"
"log"
"net/http"
assert "github.com/faabiosr/openapi-assert"
)
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 response object using the swagger schema file:
```go
package main
import (
"log"
"net/http"
assert "github.com/faabiosr/openapi-assert"
)
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
* Simple example with [Echo Framework](https://github.com/faabiosr/openapi-assert/blob/master/_examples/echo/main.go)
## Development
### Requirements
- Install [Go](https://golang.org)
- Install [GolangCI-Lint](https://github.com/golangci/golangci-lint#install) - Linter
### Makefile
```sh
# Clean up
$ make clean
# Download project dependencies
$ make configure
# Run tests and generates html coverage file
$ make cover
# Format all go files
$ make fmt
# GolangCI-Lint
$ make lint
# Run tests
$make test
```
## License
This project is released under the MIT licence. See [LICENSE](https://github.com/faabiosr/openapi-assert/blob/master/LICENSE) for more details.