openapi-assert: github.com/faabiosr/openapi-assert Index | Files

package assert

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

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

Example Usage

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.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 (
    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))
}

Index

Package Files

assert.go assertions.go doc.go document.go swagger.go

Variables

var (
    // FailMessage define the base error message.
    FailMessage = "failed asserting that %s"

    // ErrMediaType shows the media type error format.
    ErrMediaType = `'%s' is an allowed media type (%s)`

    // ErrRequestHeaders shows the request headers error format.
    ErrRequestHeaders = `'%s' is a valid request header (%s)`

    // ErrRequestQuery shows the request query error format.
    ErrRequestQuery = `'%s' is a valid request query (%s)`

    // ErrResponseHeaders shows the response headers error format.
    ErrResponseHeaders = `'%s' is a valid response header (%s)`

    // ErrValidation wrap the json schema validation errors
    ErrValidation = "unable to load the validation"

    // ErrJson wrap the json marshall errors
    ErrJson = "unable to marshal"

    // ErrRequestBody shows the request body error format.
    ErrRequestBody = `'%s' is a valid request body (%s)`

    // ErrResponseBody shows the response body error format.
    ErrResponseBody = `'%s' is a valid response body (%s)`
)
var (
    // ErrResourceURI returns an error when uri does not match.
    ErrResourceURI = "resource uri does not match"

    // ErrSwaggerLoad returns an error when load swagger document.
    ErrSwaggerLoad = "unable to load the document by uri"

    // ErrSwaggerExpand returns an error of expanding schema.
    ErrSwaggerExpand = "unable to expand the document"

    // ErrNodeNotFound returns an error when node does not exists.
    ErrNodeNotFound = "node does not exists"

    // ErrBodyNotFound returns an error when body does not exists.
    ErrBodyNotFound = "body does not exists"
)

func Request Uses

func Request(req *http.Request, doc Document) error

Requery asserts http request against a schema.

func RequestBody Uses

func RequestBody(body io.Reader, doc Document, path, method string) error

RequestBody asserts request body against a schema.

func RequestHeaders Uses

func RequestHeaders(header http.Header, doc Document, path, method string) error

RequestHeaders asserts rquest headers againt a schema header list.

func RequestMediaType Uses

func RequestMediaType(mediaType string, doc Document, path, method string) error

RequestMediaType asserts request media type against a list.

func RequestQuery Uses

func RequestQuery(query url.Values, doc Document, path, method string) error

RequestQuery asserts request query againt a schema.

func Response Uses

func Response(res *http.Response, doc Document) error

Response asserts http response against a schema.

func ResponseBody Uses

func ResponseBody(body io.Reader, doc Document, path, method string, statusCode int) error

ResponseBody asserts response body against a schema.

func ResponseHeaders Uses

func ResponseHeaders(header http.Header, doc Document, path, method string, statusCode int) error

ResponseHeaders asserts response headers againt a schema header list.

func ResponseMediaType Uses

func ResponseMediaType(mediaType string, doc Document, path, method string) error

ResponseMediaType asserts response media type against a list.

type Assertions Uses

type Assertions struct {
    // contains filtered or unexported fields
}

Assertions packs all assert methods into one structure

func New Uses

func New(doc Document) *Assertions

New returns the Assertions instance

func (*Assertions) Request Uses

func (a *Assertions) Request(req *http.Request) error

Requery asserts http request against a schema.

func (*Assertions) RequestBody Uses

func (a *Assertions) RequestBody(body io.Reader, path, method string) error

RequestBody asserts request body against a schema.

func (*Assertions) RequestHeaders Uses

func (a *Assertions) RequestHeaders(header http.Header, path, method string) error

RequestHeaders asserts rquest headers againt a schema header list.

func (*Assertions) RequestMediaType Uses

func (a *Assertions) RequestMediaType(mediaType, path, method string) error

RequestMediaType asserts request media type against a list.

func (*Assertions) RequestQuery Uses

func (a *Assertions) RequestQuery(query url.Values, path, method string) error

RequestQuery asserts request query againt a schema query list.

func (*Assertions) Response Uses

func (a *Assertions) Response(res *http.Response) error

Response asserts http response against a schema.

func (*Assertions) ResponseBody Uses

func (a *Assertions) ResponseBody(body io.Reader, path, method string, statusCode int) error

ResponseBody asserts response body against a schema.

func (*Assertions) ResponseHeaders Uses

func (a *Assertions) ResponseHeaders(header http.Header, path, method string, statusCode int) error

ResponseHeaders asserts response headers againt a schema header list.

func (*Assertions) ResponseMediaType Uses

func (a *Assertions) ResponseMediaType(mediaType, path, method string) error

ResponseMediaType asserts response media type against a list.

type Body Uses

type Body interface{}

Body is a document schema in json format.

type Document Uses

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.

type Headers Uses

type Headers map[string]interface{}

Headers is a list of headers in json schema format.

type Param Uses

type Param struct {
    Type        string
    Description string
    In          string
}

Param is a document parameter in json schema format.

type Query Uses

type Query map[string]interface{}

Query is a list of query parameters in json schema format.

type Required Uses

type Required []string

Required is a list of required parameters.

type Swagger Uses

type Swagger struct {
    // contains filtered or unexported fields
}

Swagger stores the loaded swagger spec.

func LoadFromURI Uses

func LoadFromURI(uri string) (*Swagger, error)

LoadFromURI loads and expands swagger document by uri.

func (*Swagger) RequestBody Uses

func (s *Swagger) RequestBody(path, method string) (Body, error)

RequestBody retrieves the request body.

func (*Swagger) RequestHeaders Uses

func (s *Swagger) RequestHeaders(path, method string) (Headers, error)

RequestHeaders retrieves a list of request headers.

func (*Swagger) RequestMediaTypes Uses

func (s *Swagger) RequestMediaTypes(path, method string) ([]string, error)

RequestMediaTypes retrives a list of request media types allowed.

func (*Swagger) RequestQuery Uses

func (s *Swagger) RequestQuery(path, method string) (Query, error)

RequestQuery retrieves a list of request query.

func (*Swagger) ResponseBody Uses

func (s *Swagger) ResponseBody(path, method string, statusCode int) (Body, error)

ResponseBody retrieves the response body.

func (*Swagger) ResponseHeaders Uses

func (s *Swagger) ResponseHeaders(path, method string, statusCode int) (Headers, error)

ResponseHeaders retrieves a list of response headers.

func (*Swagger) ResponseMediaTypes Uses

func (s *Swagger) ResponseMediaTypes(path, method string) ([]string, error)

ResponseMediaTypes retrives a list of response media types allowed.

Package assert imports 14 packages (graph). Updated 2019-05-18. Refresh now. Tools for package owners.