mocker

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateRoutes

func GenerateRoutes(path string, r *gin.Engine) error

GenerateRoutes generates the routes and applies them to the r gin.Engine.

Types

type BodyValidator added in v1.1.2

type BodyValidator struct {
	Contains string `yaml:"contains"`
}

BodyValidator holds the values to validate an incoming request's body.

func (BodyValidator) Validate added in v1.1.2

func (bv BodyValidator) Validate(body []byte) error

Validate will validate that the incoming body matches the expectations of the body validator.

type DeleteEndpoint

type DeleteEndpoint struct {
	EndpointMethod `yaml:",inline"`
}

DeleteEndpoint implements the EndpointGenerator interface.

func (DeleteEndpoint) Generate

func (e DeleteEndpoint) Generate(path string, r *gin.Engine)

Generate generates the endpoint

type Dump added in v1.1.2

type Dump []string

Dump is a type alias

func (Dump) Contains added in v1.1.2

func (d Dump) Contains(s string) bool

Contains will return whether or not a specific string is present in the slice or not.

func (Dump) Handle added in v1.1.2

func (d Dump) Handle(r *http.Request, body []byte)

Handle will handle request dumping if any.

type Endpoint

type Endpoint struct {
	Path string `yaml:"path"`

	Get     *GetEndpoint     `yaml:"get"`
	Post    *PostEndpoint    `yaml:"post"`
	Put     *PutEndpoint     `yaml:"put"`
	Patch   *PatchEndpoint   `yaml:"patch"`
	Delete  *DeleteEndpoint  `yaml:"delete"`
	Head    *HeadEndpoint    `yaml:"head"`
	Options *OptionsEndpoint `yaml:"options"`

	All []EndpointGenerator `yaml:"-"`
}

Endpoint represents a single endpoint.

func (*Endpoint) Compute

func (e *Endpoint) Compute()

Compute checks every possible endpoint to generate a slice of EndpointGenerator.

type EndpointGenerator

type EndpointGenerator interface {
	Generate(string, *gin.Engine)
}

EndpointGenerator is a simple interface.

type EndpointMethod added in v1.1.2

type EndpointMethod struct {
	Echo      bool             `yaml:"echo"`
	Dump      Dump             `yaml:"dump"`
	Validate  RequestValidator `yaml:"validate"`
	Responses Responses        `yaml:"responses"`
}

EndpointMethod represents a single method associated to a parent endpoint.

func (EndpointMethod) Info added in v1.1.2

func (e EndpointMethod) Info(last bool) string

Info returns the string representing the information.

func (EndpointMethod) ToHandler added in v1.1.2

func (e EndpointMethod) ToHandler() func(c *gin.Context)

ToHandler generates a handler to apply on the router.

type GetEndpoint

type GetEndpoint struct {
	EndpointMethod `yaml:",inline"`
}

GetEndpoint implements the EndpointGenerator interface.

func (GetEndpoint) Generate

func (e GetEndpoint) Generate(path string, r *gin.Engine)

Generate generates the endpoint

type HeadEndpoint

type HeadEndpoint struct {
	EndpointMethod `yaml:",inline"`
}

HeadEndpoint implements the EndpointGenerator interface.

func (HeadEndpoint) Generate

func (e HeadEndpoint) Generate(path string, r *gin.Engine)

Generate generates the endpoint

type HeadersValidator added in v1.1.2

type HeadersValidator struct {
	Present []string          `yaml:"present"`
	Absent  []string          `yaml:"absent"`
	Match   map[string]string `yaml:"match"`
}

HeadersValidator holds the values to validate the headers of an incoming request.

func (HeadersValidator) Validate added in v1.1.2

func (hv HeadersValidator) Validate(r *http.Request) []error

Validate will validate that the incoming HTTP request validates the various rules.

type MockConf added in v1.1.2

type MockConf struct {
	Endpoints []Endpoint   `yaml:"endpoints"`
	NoRoute   *NoRouteConf `yaml:"noroute"`
}

MockConf contains all the endpoints and is used for parsing.

type NoRouteConf added in v1.1.2

type NoRouteConf struct {
	Dump Dump    `yaml:"dump"`
	Echo bool    `yaml:"echo"`
	Code *int    `yaml:"code"`
	Body *string `yaml:"body"`
}

NoRouteConf defines the behavior when no route is found in the mock. Defaults to a standard 404 response.

func (NoRouteConf) Handle added in v1.1.2

func (nc NoRouteConf) Handle(r *gin.Engine)

Handle will add a special route to customize the 404 behavior.

type OptionsEndpoint

type OptionsEndpoint struct {
	EndpointMethod `yaml:",inline"`
}

OptionsEndpoint implements the EndpointGenerator interface.

func (OptionsEndpoint) Generate

func (e OptionsEndpoint) Generate(path string, r *gin.Engine)

Generate generates the endpoint.

type PatchEndpoint

type PatchEndpoint struct {
	EndpointMethod `yaml:",inline"`
}

PatchEndpoint implements the EndpointGenerator interface.

func (PatchEndpoint) Generate

func (e PatchEndpoint) Generate(path string, r *gin.Engine)

Generate generates the endpoint

type PostEndpoint

type PostEndpoint struct {
	EndpointMethod `yaml:",inline"`
}

PostEndpoint implements the EndpointGenerator interface.

func (PostEndpoint) Generate

func (e PostEndpoint) Generate(path string, r *gin.Engine)

Generate generates the endpoint

type PutEndpoint

type PutEndpoint struct {
	EndpointMethod `yaml:",inline"`
}

PutEndpoint implements the EndpointGenerator interface.

func (PutEndpoint) Generate

func (e PutEndpoint) Generate(path string, r *gin.Engine)

Generate generates the endpoint

type RequestValidator added in v1.1.2

type RequestValidator struct {
	Headers HeadersValidator `yaml:"headers"`
	Body    BodyValidator    `yaml:"body"`
}

RequestValidator wraps a header validator and a body validator.

func (RequestValidator) Handle added in v1.1.2

func (rv RequestValidator) Handle(r *http.Request, body []byte) []error

Handle will handle the validation of an incoming request, given the request and the body in byte form.

type Response

type Response struct {
	Name    string            `yaml:"name"`
	Code    int               `yaml:"code"`
	Echo    bool              `yaml:"echo"`
	Headers map[string]string `yaml:"headers"`
	Preset  string            `yaml:"preset"`
	Body    string            `yaml:"body"`
}

Response represents the response structure for a single endpoint method.

func (Response) Info added in v1.1.0

func (r Response) Info(prefix string, last bool) string

Info returns a string to print out the information of a response.

type Responses added in v1.1.2

type Responses []Response

Responses represents multiple responses.

func (Responses) Pick added in v1.1.2

func (rr Responses) Pick(platy string) Response

Pick will pick the appropriate response.

Jump to

Keyboard shortcuts

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