Documentation ¶
Overview ¶
Package assert provides methods that allow you to assert raw data using swagger files.
Example Usage ¶
See it in action:
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:
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.Fprintf(w, err) }) log.Fatal( http.ListenAndServer("127.0.0.1:9000", nil), ) }
Asserting http response object using the swagger schema file:
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)) }
Index ¶
- Constants
- type Assertions
- func (a *Assertions) Request(req *http.Request) error
- func (a *Assertions) RequestBody(body io.Reader, path, method string) error
- func (a *Assertions) RequestHeaders(header http.Header, path, method string) error
- func (a *Assertions) RequestMediaType(mediaType, path, method string) error
- func (a *Assertions) RequestQuery(query url.Values, path, method string) error
- func (a *Assertions) Response(res *http.Response) error
- func (a *Assertions) ResponseBody(body io.Reader, path, method string, statusCode int) error
- func (a *Assertions) ResponseHeaders(header http.Header, path, method string, statusCode int) error
- func (a *Assertions) ResponseMediaType(mediaType, path, method string) error
- type Body
- type Document
- type Headers
- type Param
- type Query
- type Required
Constants ¶
const ( // ErrSwaggerLoad returns an error when load swagger document. ErrSwaggerLoad = err("unable to load the document by uri") // ErrBodyNotFound returns an error when body does not exists. ErrBodyNotFound = err("body does not exists") )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Assertions ¶
type Assertions struct {
// contains filtered or unexported fields
}
Assertions packs all assert methods into one structure.
func (*Assertions) Request ¶ added in v0.8.0
func (a *Assertions) Request(req *http.Request) error
Request asserts http request against a schema.
func (*Assertions) RequestBody ¶ added in v0.6.0
func (a *Assertions) RequestBody(body io.Reader, path, method string) error
RequestBody asserts request body against a schema.
func (*Assertions) RequestHeaders ¶
func (a *Assertions) RequestHeaders(header http.Header, path, method string) error
RequestHeaders asserts rquest headers againt a schema header list.
func (*Assertions) RequestMediaType ¶
func (a *Assertions) RequestMediaType(mediaType, path, method string) error
RequestMediaType asserts request media type against a list.
func (*Assertions) RequestQuery ¶
func (a *Assertions) RequestQuery(query url.Values, path, method string) error
RequestQuery asserts request query againt a schema query list.
func (*Assertions) Response ¶ added in v0.9.0
func (a *Assertions) Response(res *http.Response) error
Response asserts http response against a schema.
func (*Assertions) ResponseBody ¶ added in v0.7.0
ResponseBody asserts response body against a schema.
func (*Assertions) ResponseHeaders ¶
ResponseHeaders asserts response headers againt a schema header list.
func (*Assertions) ResponseMediaType ¶
func (a *Assertions) ResponseMediaType(mediaType, path, method string) error
ResponseMediaType asserts response media type against a list.
type Document ¶
type Document interface { // RequestMediaTypes retrives a list of request media types allowed. RequestMediaTypes(path, method string) ([]string, error) // ResponseMediaTypes retrives a list of response media types allowed. ResponseMediaTypes(path, method string) ([]string, error) // RequestHeaders retrieves a list of request headers. RequestHeaders(path, method string) (Headers, error) // ResponseHeaders retrieves a list of response headers. ResponseHeaders(path, method string, statusCode int) (Headers, error) // RequestQuery retrieves a list of request query. RequestQuery(path, method string) (Query, error) // RequestBody retrieves the request body. RequestBody(path, method string) (Body, error) // ResponseBody retrieves the response body. ResponseBody(path, method string, statusCode int) (Body, error) }
Document that defines the contract for reading OpenAPI documents.
func LoadFromReader ¶ added in v0.11.0
LoadFromReader loads and expand swagger document from io.Reader.
func LoadFromURI ¶
LoadFromURI loads and expands swagger document by uri.
type Headers ¶
type Headers map[string]interface{}
Headers is a list of headers in json schema format.