verifier

package
v0.0.0-...-a24f398 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 3, 2020 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package verifier exposes a json-schema verifier for http request and response bodies

Example (Verifier)
verifier, err := verifierFromJSON(sampleConfig)
if err != nil {
	fmt.Println(err.Error())
}

fmt.Println(verifier.Validate([]byte(`{"firstName": "foo", "lastName": "bar", "age": 42}`)))
fmt.Println(verifier.Validate([]byte(`{"firstName": "foo", "lastName": 1, "age": 42}`)))
fmt.Println(verifier.Validate([]byte(`{"firstName": "foo", "age": 42}`)))
fmt.Println(verifier.Validate([]byte(`{"firstName": "foo", "lastName": "bar", "age": -42}`)))
fmt.Println(verifier.Validate([]byte(`{`)))
Output:

<nil>
lastName: Invalid type. Expected: string, given: integer
lastName: lastName is required
age: Must be greater than or equal to 0
unexpected EOF

Index

Examples

Constants

View Source
const MIMEJSON = "application/json"

Variables

View Source
var ErrNoJSONRequest = errors.New("request is not a json message")
View Source
var ErrNoJSONResponse = errors.New("response is not a json message")

Functions

This section is empty.

Types

type RequestVerifier

type RequestVerifier struct {
	// contains filtered or unexported fields
}
Example
verifier, err := RequestVerifierFromJSON(sampleConfig)
if err != nil {
	fmt.Println(err.Error())
}

fmt.Println(verifier.ModifyRequest(newRequest(`{"firstName": "foo", "lastName": "bar", "age": 42}`)))
fmt.Println(verifier.ModifyRequest(newRequest(`{"firstName": "foo", "lastName": 1, "age": 42}`)))
fmt.Println(verifier.ModifyRequest(newRequest(`{"firstName": "foo", "age": 42}`)))
fmt.Println(verifier.ModifyRequest(newRequest(`{"firstName": "foo", "lastName": "bar", "age": -42}`)))
fmt.Println(verifier.ModifyRequest(newRequest("{")))
fmt.Println(verifier.ModifyRequest(&http.Request{Body: ioutil.NopCloser(bytes.NewBufferString("{"))}))
fmt.Println(verifier.ModifyRequest(&http.Request{Header: http.Header{"Content-Type": []string{MIMEJSON}}}))
Output:

<nil>
lastName: Invalid type. Expected: string, given: integer
lastName: lastName is required
age: Must be greater than or equal to 0
unexpected EOF
request is not a json message
request is not a json message

func RequestVerifierFromJSON

func RequestVerifierFromJSON(b []byte) (*RequestVerifier, error)

func (*RequestVerifier) ModifyRequest

func (m *RequestVerifier) ModifyRequest(req *http.Request) error

ModifyRequest verifies the body of the response with the given JSON verifier.

func (*RequestVerifier) Validate

func (v *RequestVerifier) Validate(data []byte) error

type ResponseVerifier

type ResponseVerifier struct {
	// contains filtered or unexported fields
}
Example
verifier, err := ResponseVerifierFromJSON(sampleConfig)
if err != nil {
	fmt.Println(err.Error())
}

fmt.Println(verifier.ModifyResponse(newResponse(`{"firstName": "foo", "lastName": "bar", "age": 42}`)))
fmt.Println(verifier.ModifyResponse(newResponse(`{"firstName": "foo", "lastName": 1, "age": 42}`)))
fmt.Println(verifier.ModifyResponse(newResponse(`{"firstName": "foo", "age": 42}`)))
fmt.Println(verifier.ModifyResponse(newResponse(`{"firstName": "foo", "lastName": "bar", "age": -42}`)))
fmt.Println(verifier.ModifyResponse(newResponse(`{`)))
fmt.Println(verifier.ModifyResponse(&http.Response{Body: ioutil.NopCloser(bytes.NewBufferString("{"))}))
fmt.Println(verifier.ModifyResponse(&http.Response{Header: http.Header{"Content-Type": []string{MIMEJSON}}}))
Output:

<nil>
lastName: Invalid type. Expected: string, given: integer
lastName: lastName is required
age: Must be greater than or equal to 0
unexpected EOF
response is not a json message
response is not a json message

func ResponseVerifierFromJSON

func ResponseVerifierFromJSON(b []byte) (*ResponseVerifier, error)

func (*ResponseVerifier) ModifyResponse

func (m *ResponseVerifier) ModifyResponse(res *http.Response) error

ModifyResponse verifies the body of the response with the given JSON verifier.

func (*ResponseVerifier) Validate

func (v *ResponseVerifier) Validate(data []byte) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL