smocker

package
v0.2.14 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2023 License: AGPL-3.0 Imports: 8 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultClient = &Client{HTTPClient: &http.Client{}}

DefaultClient is used if no custom HTTP client is defined

Functions

func AddQueryParameters

func AddQueryParameters(baseURL string, queryParams map[string]string) string

AddQueryParameters adds query parameters to the URL.

func BuildRequestObject

func BuildRequestObject(request Request) (*http.Request, error)

BuildRequestObject creates the HTTP request object.

func MakeRequest

func MakeRequest(req *http.Request) (*http.Response, error)

MakeRequest makes the API call.

Types

type API

type API struct {
	URL string
}

func NewAPI

func NewAPI(url string) *API

func (*API) AddMocks added in v0.2.6

func (a *API) AddMocks(ctx context.Context, mock []*Mock) error

AddMocks Add a mock to the mocks list.

func (*API) Reset

func (a *API) Reset(ctx context.Context) error

Reset Clear the mocks and the history of calls.

type BodyMatcher added in v0.2.0

type BodyMatcher struct {
	BodyString *StringMatcher
	BodyJSON   map[string]StringMatcher
}

func (BodyMatcher) MarshalJSON added in v0.2.0

func (bm BodyMatcher) MarshalJSON() ([]byte, error)

type Client

type Client struct {
	HTTPClient *http.Client
}

Client allows modification of client headers, redirect policy and other settings See https://golang.org/pkg/net/http

func (*Client) API deprecated

func (c *Client) API(request Request) (*Response, error)

Deprecated: API supports old implementation

func (*Client) AddMocks added in v0.2.6

func (c *Client) AddMocks(_ []*Mock) error

func (*Client) MakeRequest

func (c *Client) MakeRequest(req *http.Request) (*http.Response, error)

MakeRequest makes the API call.

func (*Client) Reset

func (c *Client) Reset() error

Reset Clears the mocks and the history of calls.

func (*Client) Send

func (c *Client) Send(request Request) (*Response, error)

Send will build your request, make the request, and build your response.

func (*Client) SendWithContext

func (c *Client) SendWithContext(ctx context.Context, request Request) (*Response, error)

SendWithContext will build your request passing in the provided context, make the request, and build your response.

type Delay added in v0.2.0

type Delay struct {
	Min time.Duration `json:"min,omitempty" yaml:"min,omitempty"`
	Max time.Duration `json:"max,omitempty" yaml:"max,omitempty"`
}

type MapStringSlice added in v0.2.0

type MapStringSlice map[string]StringSlice

type Method

type Method string

Method contains the supported HTTP verbs.

const (
	Get    Method = "GET"
	Post   Method = "POST"
	Put    Method = "PUT"
	Patch  Method = "PATCH"
	Delete Method = "DELETE"
)

Supported HTTP verbs.

type Mock

type Mock struct {
	Request  MockRequest   `json:"request,omitempty" yaml:"request"`
	Response *MockResponse `json:"response,omitempty" yaml:"response,omitempty"`
	Context  *MockContext  `json:"context,omitempty" yaml:"context,omitempty"`
}

func (*Mock) Validate added in v0.2.0

func (bm *Mock) Validate() error

type MockBuilder added in v0.2.6

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

func NewMockBuilder added in v0.2.6

func NewMockBuilder() *MockBuilder

func (*MockBuilder) AddRequestBodyJSON added in v0.2.6

func (mb *MockBuilder) AddRequestBodyJSON(key string, body StringMatcher) *MockBuilder

func (*MockBuilder) AddRequestHeader added in v0.2.6

func (mb *MockBuilder) AddRequestHeader(name string, value StringMatcher) *MockBuilder

func (*MockBuilder) AddRequestQueryParam added in v0.2.6

func (mb *MockBuilder) AddRequestQueryParam(name string, value StringMatcher) *MockBuilder

func (*MockBuilder) AddResponseHeader added in v0.2.6

func (mb *MockBuilder) AddResponseHeader(name string, value string) *MockBuilder

func (*MockBuilder) Mock added in v0.2.6

func (mb *MockBuilder) Mock() *Mock

func (*MockBuilder) SetContextTimes added in v0.2.6

func (mb *MockBuilder) SetContextTimes(times uint) *MockBuilder

func (*MockBuilder) SetRequestBodyString added in v0.2.6

func (mb *MockBuilder) SetRequestBodyString(body StringMatcher) *MockBuilder

func (*MockBuilder) SetRequestHeaders added in v0.2.6

func (mb *MockBuilder) SetRequestHeaders(headers MultiMapMatcher) *MockBuilder

func (*MockBuilder) SetRequestMethod added in v0.2.6

func (mb *MockBuilder) SetRequestMethod(method StringMatcher) *MockBuilder

func (*MockBuilder) SetRequestPath added in v0.2.6

func (mb *MockBuilder) SetRequestPath(path StringMatcher) *MockBuilder

func (*MockBuilder) SetRequestQueryParams added in v0.2.6

func (mb *MockBuilder) SetRequestQueryParams(queryParams MultiMapMatcher) *MockBuilder

func (*MockBuilder) SetResponseBody added in v0.2.6

func (mb *MockBuilder) SetResponseBody(body string) *MockBuilder

func (*MockBuilder) SetResponseDelay added in v0.2.6

func (mb *MockBuilder) SetResponseDelay(min, max time.Duration) *MockBuilder

func (*MockBuilder) SetResponseHeaders added in v0.2.6

func (mb *MockBuilder) SetResponseHeaders(headers MapStringSlice) *MockBuilder

func (*MockBuilder) SetResponseStatus added in v0.2.6

func (mb *MockBuilder) SetResponseStatus(status int) *MockBuilder

type MockContext added in v0.2.6

type MockContext struct {
	Times uint `json:"times" yaml:"times"`
}

type MockRequest added in v0.2.0

type MockRequest struct {
	Path        StringMatcher   `json:"path" yaml:"path"`
	Method      StringMatcher   `json:"method" yaml:"method"`
	Body        *BodyMatcher    `json:"body,omitempty" yaml:"body,omitempty"`
	QueryParams MultiMapMatcher `json:"query_params,omitempty" yaml:"query_params,omitempty"`
	Headers     MultiMapMatcher `json:"headers,omitempty" yaml:"headers,omitempty"`
}

type MockResponse added in v0.2.0

type MockResponse struct {
	Body    string         `json:"body,omitempty" yaml:"body,omitempty"`
	Status  int            `json:"status" yaml:"status"`
	Delay   Delay          `json:"delay,omitempty" yaml:"delay,omitempty"`
	Headers MapStringSlice `json:"headers,omitempty" yaml:"headers,omitempty"`
}

type MultiMapMatcher added in v0.2.0

type MultiMapMatcher map[string]StringMatcherSlice

type Request

type Request struct {
	Method      Method
	BaseURL     string // e.g. https://api.sendgrid.com
	Headers     map[string]string
	QueryParams map[string]string
	Body        []byte
}

Request holds the request to an API Call.

type Response

type Response struct {
	StatusCode int                 // e.g. 200
	Body       string              // e.g. {"result: success"}
	Headers    map[string][]string // e.g. map[X-Ratelimit-Limit:[600]]
}

Response holds the response from an API call.

func BuildResponse

func BuildResponse(res *http.Response) (*Response, error)

BuildResponse builds the response struct.

func Send

func Send(request Request) (*Response, error)

Send uses the DefaultClient to send your request

func SendWithContext

func SendWithContext(ctx context.Context, request Request) (*Response, error)

SendWithContext uses the DefaultClient to send your request with the provided context.

type RestError

type RestError struct {
	Response *Response
}

RestError is a struct for an error handling.

func (*RestError) Error

func (e *RestError) Error() string

Error is the implementation of the error interface.

type StringMatcher added in v0.2.0

type StringMatcher struct {
	Matcher string `json:"matcher" yaml:"matcher,flow"`
	Value   string `json:"value" yaml:"value,flow"`
}

func ShouldBeEmpty added in v0.2.6

func ShouldBeEmpty(value string) StringMatcher

func ShouldContainSubstring added in v0.2.6

func ShouldContainSubstring(value string) StringMatcher

func ShouldEndWith added in v0.2.6

func ShouldEndWith(value string) StringMatcher

func ShouldEqual added in v0.2.6

func ShouldEqual(value string) StringMatcher

func ShouldEqualJSON added in v0.2.6

func ShouldEqualJSON(value string) StringMatcher

func ShouldMatch added in v0.2.6

func ShouldMatch(value string) StringMatcher

func ShouldNotBeEmpty added in v0.2.6

func ShouldNotBeEmpty(value string) StringMatcher

func ShouldNotContainSubstring added in v0.2.6

func ShouldNotContainSubstring(value string) StringMatcher

func ShouldNotEndWith added in v0.2.6

func ShouldNotEndWith(value string) StringMatcher

func ShouldNotEqual added in v0.2.6

func ShouldNotEqual(value string) StringMatcher

func ShouldNotMatch added in v0.2.6

func ShouldNotMatch(value string) StringMatcher

func ShouldNotResemble added in v0.2.6

func ShouldNotResemble(value string) StringMatcher

func ShouldNotStartWith added in v0.2.6

func ShouldNotStartWith(value string) StringMatcher

func ShouldResemble added in v0.2.6

func ShouldResemble(value string) StringMatcher

func ShouldStartWith added in v0.2.6

func ShouldStartWith(value string) StringMatcher

type StringMatcherSlice added in v0.2.0

type StringMatcherSlice []StringMatcher

type StringSlice added in v0.2.0

type StringSlice []string

Jump to

Keyboard shortcuts

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