isugata

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2023 License: MIT Imports: 11 Imported by: 0

README

isugata

CI Pipeline Go Reference license

HTTP response validator for ISUCON benchmarker with Functional Option Pattern

Usage

An executable example is in example/main.go.

  • Status Code
err := isugata.Validate(res,
  isugata.WithStatusCode(http.StatusOK),
)
  • Content Type
err := isugata.Validate(res,
  isugata.WithContentType("application/json"),
)
  • JSON Body Validation
type user struct {
 ID   int    `json:"id"`
 Name string `json:"name"`
}

err := isugata.Validate(res,
  isugata.WithJSONValidation[user](
    isugata.JSONEquals[user](
      user{
        ID:   1,
        Name: "test",
      },
    ),
  ),
)
  • JSON Array Body Validation
type user struct {
 ID   int    `json:"id"`
 Name string `json:"name"`
}

err := isugata.Validate(res,
  isugata.WithJSONArrayValidation[user](
    isugata.JSONArrayLengthEquals[user](2),
    isugata.JSONArrayValidateOrder[user, int](
      func(u user) int { return u.ID },
      isugata.Asc,
    ),
    isugata.JSONArrayValidateEach[user](
      func(body user) error {
        if body.Name != fmt.Sprintf("test%d", body.ID) {
          return fmt.Errorf("expected: test%d, actual: %s", body.ID, body.Name)
        }
        return nil
      },
    ),
  ),
)

Documentation

Overview

Package isugata is HTTP response validator for ISUCON benchmarker

Index

Constants

View Source
const (
	// Asc is ascending order
	Asc order = "asc"

	// Desc is descending order
	Desc order = "desc"
)
View Source
const (
	// CorrectionLevelL is correction level L (approx 7%)
	CorrectionLevelL correctionLevel = "L"

	// CorrectionLevelM is correction level M (approx 15%)
	CorrectionLevelM correctionLevel = "M"

	// CorrectionLevelQ is correction level Q (approx 25%)
	CorrectionLevelQ correctionLevel = "Q"

	// CorrectionLevelH is correction level H (approx 30%)
	CorrectionLevelH correctionLevel = "H"
)

Variables

View Source
var (
	// ErrUndecodableBody is returned when the body cannot be decoded
	ErrUndecodableBody = errors.New("undecodable body")

	// ErrInvalidStatusCode is returned when the status code is invalid
	ErrInvalidStatusCode = errors.New("invalid status code")

	// ErrInvalidContentType is returned when the Content-Type header is invalid
	ErrInvalidContentType = errors.New("invalid Content-Type header")

	// ErrInvalidBody is returned when the body doesn't match the expected
	ErrInvalidBody = errors.New("invalid body")
)

Functions

func Validate

func Validate(res *http.Response, opts ...ValidateOpt) error

Validate validates http.Response with opts

validate http.Response in order of opts, and return the first error

Types

type JSONArrayValidateOpt

type JSONArrayValidateOpt[V any] func(body []V) error

JSONArrayValidateOpt is function type to validate JSON array body

func JSONArrayLengthEquals

func JSONArrayLengthEquals[V any](length int) JSONArrayValidateOpt[V]

JSONArrayLengthEquals validates if JSON array body length equals to length

func JSONArrayLengthRange

func JSONArrayLengthRange[V any](min, max int) JSONArrayValidateOpt[V]

JSONArrayLengthRange validates if JSON array body length is in range [min, max]

func JSONArrayValidateEach

func JSONArrayValidateEach[V any](opts ...JSONValidateOpt[V]) JSONArrayValidateOpt[V]

JSONArrayValidateEach validates each element of JSON array body with opts

func JSONArrayValidateOrder

func JSONArrayValidateOrder[V any, I constraints.Ordered](idxFunc func(v V) I, ord order) JSONArrayValidateOpt[V]

JSONArrayValidateOrder validates if JSON array body is ordered by idxFunc in ord

type JSONValidateOpt

type JSONValidateOpt[V any] func(body V) error

JSONValidateOpt is function type to validate JSON body

func JSONArrayFieldValidate

func JSONArrayFieldValidate[V, F any](name string, opts ...JSONArrayValidateOpt[F]) JSONValidateOpt[V]

JSONArrayFieldValidate validates JSON array filed with opts

func JSONEquals

func JSONEquals[V comparable](v V) JSONValidateOpt[V]

JSONEquals validates if JSON body deeply equals to v

func JSONFieldValidate

func JSONFieldValidate[V, F any](name string, opts ...JSONValidateOpt[F]) JSONValidateOpt[V]

JSONFieldValidate validates JSON filed with opts

type ValidateOpt

type ValidateOpt func(*http.Response) error

ValidateOpt is function type to validate http.Response

func WithContentType

func WithContentType(contentType string) ValidateOpt

WithContentType validates if the Content-Type header starts with contentType

func WithJSONArrayValidation

func WithJSONArrayValidation[V any](opts ...JSONArrayValidateOpt[V]) ValidateOpt

WithJSONArrayValidation decodes JSON array body and validates it with opts

func WithJSONValidation

func WithJSONValidation[V any](opts ...JSONValidateOpt[V]) ValidateOpt

WithJSONValidation decodes JSON body and validates it with opts

func WithQRCodeEqual

func WithQRCodeEqual(size int, corrLevel correctionLevel, content string, decryptFunc func(string) (string, error)) ValidateOpt

WithQRCodeEqual validates if the QR code equals to the expected

func WithStatusCode

func WithStatusCode(code int) ValidateOpt

WithStatusCode validates if the status code equals to code

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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