web

package
v0.0.0-...-079c800 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2023 License: MIT Imports: 25 Imported by: 0

Documentation

Index

Constants

View Source
const AWSAccessKey = "AWS_ACCESS_KEY_ID"

AWSAccessKey constant

View Source
const AWSSecretKey = "AWS_SECRET_ACCESS_KEY"

AWSSecretKey constant

View Source
const AWSSecurityToken = "AWS_SECURITY_TOKEN"

AWSSecurityToken constant

View Source
const AWSSessionToken = "AWS_SESSION_TOKEN"

AWSSessionToken constant

Variables

View Source
var IgnoredRequestHeaders = map[string]struct{}{
	types.AuthorizationHeader: {},
	types.ContentLengthHeader: {},
	"User-Agent":              {},
	"Accept":                  {},
	"Accept-Language":         {},
	"Accept-Encoding":         {},
	"Connection":              {},
	"Proxy-Connection":        {},
	"Origin":                  {},
	"Referer":                 {},
	"Host":                    {},
	"X-Amzn-Trace-Id":         {},
	"X-Requested-With":        {},
	"X-Amz-Requestsupertrace": {},
	"X-Amz-Security-Token":    {},
	"Sec-Fetch-Dest":          {},
	"Sec-Fetch-Mode":          {},
	"Sec-Fetch-Site":          {},
	"Sec-GPC":                 {},
}

IgnoredRequestHeaders headers

Functions

func BuildMockScenarioKeyData

func BuildMockScenarioKeyData(req *http.Request) (keyData *types.APIKeyData, err error)

BuildMockScenarioKeyData creates api-scenario key from HTTP request

func GetHeaderParamOrEnvValue

func GetHeaderParamOrEnvValue(params map[string]string, names ...string) string

GetHeaderParamOrEnvValue searches key in map or env variables

func NewStubContext

func NewStubContext(req *http.Request) *stubContext

NewStubContext - creates stubbed server

Types

type APIContext

type APIContext interface {
	// Path Request path
	Path() string

	// Request returns `*http.Request`.
	Request() *http.Request

	// Response returns `*Response`.
	Response() *echo.Response

	// Param returns path parameter by name.
	Param(name string) string

	// QueryParams returns the query parameters as `url.Values`.
	QueryParams() url.Values

	// QueryParam returns the query param for the provided name.
	QueryParam(name string) string

	// FormParams returns the form parameters as `url.Values`.
	FormParams() (url.Values, error)

	// FormValue returns the form field value for the provided name.
	FormValue(name string) string

	// Cookie returns the named cookie provided in the request.
	Cookie(name string) (*http.Cookie, error)

	// SetCookie adds a `Set-Cookie` header in HTTP response.
	SetCookie(cookie *http.Cookie)

	// Get retrieves data from the context.
	Get(key string) any

	// Set saves data in the context.
	Set(key string, val any)

	// Render renders a template with data and sends a text/html response with status
	// code. Renderer must be registered using `Echo.Renderer`.
	Render(code int, name string, data any) error

	// String sends a string response with status code.
	String(code int, s string) error

	// JSON sends a JSON response with status code.
	JSON(code int, i any) error

	// MultipartForm returns the multipart form.
	MultipartForm() (*multipart.Form, error)

	// Redirect redirects the request to a provided URL with status code.
	Redirect(code int, url string) error

	// NoContent sends a response with nobody and a status code.
	NoContent(code int) error

	// Blob sends a blob response with status code and content type.
	Blob(code int, contentType string, b []byte) error

	// Stream sends a streaming response with status code and content type.
	Stream(code int, contentType string, r io.Reader) error
	// Attachment sends a response as attachment, prompting client to save the
	// file.
	Attachment(file string, name string) error
}

APIContext interface

func NewDefaultAPIContext

func NewDefaultAPIContext(
	ctx context.Context,
	request *http.Request,
	response *http.Response,
	eResponse *echo.Response,
	params map[string]string) APIContext

NewDefaultAPIContext constructor

type AWSSigner

type AWSSigner interface {
	AWSSign(req *http.Request, credentials *credentials.Credentials) (bool, string, error)
}

AWSSigner is an interface to make testing http.Client calls easier

func NewAWSSigner

func NewAWSSigner(config *types.Configuration) AWSSigner

NewAWSSigner constructor

type Adapter

type Adapter interface {
	// Invoke http method
	Invoke(request *http.Request) (response *http.Response, err error)
}

Adapter defines methods to delegate HTTP APIs

type AuthAdapter

type AuthAdapter interface {
	HandleAuth(req *http.Request) (bool, string, error)
}

AuthAdapter is an interface to make testing http.Client calls easier

func NewAuthAdapter

func NewAuthAdapter(config *types.Configuration) AuthAdapter

NewAuthAdapter constructor

type DefaultAPIContext

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

DefaultAPIContext struct for defining APIContext using http request/response

func (*DefaultAPIContext) Attachment

func (d *DefaultAPIContext) Attachment(_ string, _ string) error

Attachment sends a response as attachment, prompting client to save the file.

func (*DefaultAPIContext) Blob

func (d *DefaultAPIContext) Blob(code int, contentType string, b []byte) error

Blob sends a blob response with status code and content type.

func (*DefaultAPIContext) Cookie

func (d *DefaultAPIContext) Cookie(name string) (*http.Cookie, error)

Cookie returns the named cookie provided in the request.

func (*DefaultAPIContext) FormParams

func (d *DefaultAPIContext) FormParams() (url.Values, error)

FormParams returns the form parameters as `url.Values`.

func (*DefaultAPIContext) FormValue

func (d *DefaultAPIContext) FormValue(name string) string

FormValue returns the form field value for the provided name.

func (*DefaultAPIContext) Get

func (d *DefaultAPIContext) Get(key string) any

Get retrieves data from the context.

func (*DefaultAPIContext) Host

func (d *DefaultAPIContext) Host() string

Host Request host

func (*DefaultAPIContext) JSON

func (d *DefaultAPIContext) JSON(code int, i any) error

JSON sends a JSON response with status code.

func (*DefaultAPIContext) MultipartForm

func (d *DefaultAPIContext) MultipartForm() (*multipart.Form, error)

MultipartForm returns the multipart form.

func (*DefaultAPIContext) NoContent

func (d *DefaultAPIContext) NoContent(code int) error

NoContent sends a response with nobody and a status code.

func (*DefaultAPIContext) Param

func (d *DefaultAPIContext) Param(name string) string

Param returns path parameter by name.

func (*DefaultAPIContext) Path

func (d *DefaultAPIContext) Path() string

Path Request path

func (*DefaultAPIContext) QueryParam

func (d *DefaultAPIContext) QueryParam(name string) string

QueryParam returns the query param for the provided name.

func (*DefaultAPIContext) QueryParams

func (d *DefaultAPIContext) QueryParams() url.Values

QueryParams returns the query parameters as `url.Values`.

func (*DefaultAPIContext) Redirect

func (d *DefaultAPIContext) Redirect(code int, url string) error

Redirect redirects the request to a provided URL with status code.

func (*DefaultAPIContext) Render

func (d *DefaultAPIContext) Render(_ int, _ string, _ any) error

Render renders a template with data and sends a text/html response with status code. Renderer must be registered using `Echo.Renderer`.

func (*DefaultAPIContext) Request

func (d *DefaultAPIContext) Request() *http.Request

Request returns `*http.Request`.

func (*DefaultAPIContext) Response

func (d *DefaultAPIContext) Response() *echo.Response

Response returns `*Response`.

func (*DefaultAPIContext) Set

func (d *DefaultAPIContext) Set(key string, val any)

Set saves data in the context.

func (*DefaultAPIContext) SetCookie

func (d *DefaultAPIContext) SetCookie(cookie *http.Cookie)

SetCookie adds a `Set-Cookie` header in HTTP response.

func (*DefaultAPIContext) Stream

func (d *DefaultAPIContext) Stream(code int, contentType string, r io.Reader) (err error)

Stream sends a streaming response with status code and content type.

func (*DefaultAPIContext) String

func (d *DefaultAPIContext) String(code int, s string) error

String sends a string response with status code.

type DefaultHTTPClient

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

DefaultHTTPClient implements HTTPClient

func NewHTTPClient

func NewHTTPClient(config *types.Configuration, authAdapter AuthAdapter) *DefaultHTTPClient

NewHTTPClient creates structure for HTTPClient

func (*DefaultHTTPClient) Handle

func (w *DefaultHTTPClient) Handle(
	ctx context.Context,
	url string,
	method string,
	headers map[string][]string,
	params map[string]string,
	body io.ReadCloser,
) (statusCode int, httpVersion string, respBody io.ReadCloser, respHeader map[string][]string, err error)

Handle makes HTTP request

type DefaultWebServer

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

DefaultWebServer defines default web server

func (*DefaultWebServer) AddMiddleware

func (w *DefaultWebServer) AddMiddleware(m echo.MiddlewareFunc)

AddMiddleware adds middleware

func (*DefaultWebServer) CONNECT

func (w *DefaultWebServer) CONNECT(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route

CONNECT calls HTTP CONNECT method

func (*DefaultWebServer) DELETE

func (w *DefaultWebServer) DELETE(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route

DELETE calls HTTP DELETE method

func (*DefaultWebServer) Embed

func (w *DefaultWebServer) Embed(content embed.FS, path string, dir string)

Embed - serve assets

func (*DefaultWebServer) GET

func (w *DefaultWebServer) GET(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route

GET calls HTTP GET method

func (*DefaultWebServer) HEAD

func (w *DefaultWebServer) HEAD(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route

HEAD calls HTTP HEAD method

func (*DefaultWebServer) OPTIONS

func (w *DefaultWebServer) OPTIONS(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route

OPTIONS calls HTTP OPTIONS method

func (*DefaultWebServer) PATCH

func (w *DefaultWebServer) PATCH(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route

PATCH calls HTTP PATCH method

func (*DefaultWebServer) POST

func (w *DefaultWebServer) POST(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route

POST calls HTTP POST method

func (*DefaultWebServer) PUT

func (w *DefaultWebServer) PUT(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route

PUT calls HTTP PUT method

func (*DefaultWebServer) Start

func (w *DefaultWebServer) Start(address string)

Start - starts web server

func (*DefaultWebServer) Static

func (w *DefaultWebServer) Static(path string, dir string)

Static - serve assets

func (*DefaultWebServer) Stop

func (w *DefaultWebServer) Stop()

Stop - stops web server

func (*DefaultWebServer) TRACE

func (w *DefaultWebServer) TRACE(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route

TRACE calls HTTP TRACE method

type HTTPClient

type HTTPClient interface {
	Handle(
		ctx context.Context,
		url string,
		method string,
		headers map[string][]string,
		params map[string]string,
		body io.ReadCloser,
	) (int, string, io.ReadCloser, map[string][]string, error)
}

HTTPClient defines methods for http get and post methods

type HandlerFunc

type HandlerFunc func(APIContext) error

HandlerFunc defines a function to serve HTTP requests.

func WrapHandler

func WrapHandler(h http.Handler) HandlerFunc

WrapHandler wraps `http.Handler` into `echo.HandlerFunc`.

type MethodPathHandler

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

MethodPathHandler mapping

type Server

type Server interface {
	GET(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	POST(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	PUT(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	DELETE(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	CONNECT(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	HEAD(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	OPTIONS(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	PATCH(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	TRACE(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	AddMiddleware(m echo.MiddlewareFunc)
	Start(address string)
	Static(path string, dir string)
	Embed(content embed.FS, path string, dir string)
	Stop()
}

Server defines methods for binding http methods

func NewDefaultWebServer

func NewDefaultWebServer(config *types.Configuration) Server

NewDefaultWebServer creates new instance of web server

func NewStubWebServer

func NewStubWebServer() Server

NewStubWebServer creates stubbed web server

type ServerAdapter

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

ServerAdapter struct

func NewWebServerAdapter

func NewWebServerAdapter() *ServerAdapter

NewWebServerAdapter constructor

func (*ServerAdapter) AddMiddleware

func (a *ServerAdapter) AddMiddleware(_ echo.MiddlewareFunc)

AddMiddleware middleware

func (*ServerAdapter) CONNECT

func (a *ServerAdapter) CONNECT(path string, handler HandlerFunc, _ ...echo.MiddlewareFunc) *echo.Route

CONNECT calls HTTP CONNECT method

func (*ServerAdapter) DELETE

func (a *ServerAdapter) DELETE(path string, handler HandlerFunc, _ ...echo.MiddlewareFunc) *echo.Route

DELETE handler calls HTTP DELETE method

func (*ServerAdapter) Embed

func (a *ServerAdapter) Embed(embed.FS, string, string)

Embed skeleton

func (*ServerAdapter) GET

func (a *ServerAdapter) GET(path string, handler HandlerFunc, _ ...echo.MiddlewareFunc) *echo.Route

GET handler calls HTTP GET method

func (*ServerAdapter) HEAD

func (a *ServerAdapter) HEAD(path string, handler HandlerFunc, _ ...echo.MiddlewareFunc) *echo.Route

HEAD calls HTTP HEAD method

func (*ServerAdapter) Invoke

func (a *ServerAdapter) Invoke(request *http.Request) (response *http.Response, err error)

Invoke finds handler in controllers and invokes it if matched

func (*ServerAdapter) OPTIONS

func (a *ServerAdapter) OPTIONS(path string, handler HandlerFunc, _ ...echo.MiddlewareFunc) *echo.Route

OPTIONS calls HTTP OPTIONS method

func (*ServerAdapter) PATCH

func (a *ServerAdapter) PATCH(path string, handler HandlerFunc, _ ...echo.MiddlewareFunc) *echo.Route

PATCH calls HTTP PATCH method

func (*ServerAdapter) POST

func (a *ServerAdapter) POST(path string, handler HandlerFunc, _ ...echo.MiddlewareFunc) *echo.Route

POST handler calls HTTP POST method

func (*ServerAdapter) PUT

func (a *ServerAdapter) PUT(path string, handler HandlerFunc, _ ...echo.MiddlewareFunc) *echo.Route

PUT handler calls HTTP PUT method

func (*ServerAdapter) Start

func (a *ServerAdapter) Start(string)

Start server

func (*ServerAdapter) Static

func (a *ServerAdapter) Static(string, string)

Static serve static assets

func (*ServerAdapter) Stop

func (a *ServerAdapter) Stop()

Stop server

func (*ServerAdapter) TRACE

func (a *ServerAdapter) TRACE(path string, handler HandlerFunc, _ ...echo.MiddlewareFunc) *echo.Route

TRACE calls HTTP TRACE method

type StubHTTPClient

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

StubHTTPClient implements HTTPClient for stubbed response

func NewStubHTTPClient

func NewStubHTTPClient() *StubHTTPClient

NewStubHTTPClient - creates structure for HTTPClient

func (*StubHTTPClient) AddMapping

func (w *StubHTTPClient) AddMapping(method string, url string, resp *StubHTTPResponse)

AddMapping adds mapping for stub response

func (*StubHTTPClient) Handle

func (w *StubHTTPClient) Handle(
	_ context.Context,
	url string,
	method string,
	_ map[string][]string,
	_ map[string]string,
	_ io.ReadCloser) (int, string, io.ReadCloser, map[string][]string, error)

Handle makes HTTP request

type StubHTTPResponse

type StubHTTPResponse struct {
	Filename string
	Bytes    []byte
	Status   int
	Headers  map[string][]string
	Error    error
	// contains filtered or unexported fields
}

StubHTTPResponse defines stub response

func NewStubHTTPResponse

func NewStubHTTPResponse(status int, unk any) *StubHTTPResponse

NewStubHTTPResponse creates stubbed response

func NewStubHTTPResponseError

func NewStubHTTPResponseError(status int, sleep time.Duration, err error) *StubHTTPResponse

NewStubHTTPResponseError creates stubbed response with error

func (*StubHTTPResponse) WithHeader

func (r *StubHTTPResponse) WithHeader(name string, val string) *StubHTTPResponse

type StubResponseWriter

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

StubResponseWriter wraps the standard http.ResponseWriter allowing for more verbose logging

func (*StubResponseWriter) Header

func (w *StubResponseWriter) Header() http.Header

Header returns & satisfies the http.ResponseWriter interface

func (*StubResponseWriter) Size

func (w *StubResponseWriter) Size() int

Size provides an easy way to retrieve the response size in bytes

func (*StubResponseWriter) Status

func (w *StubResponseWriter) Status() int

Status provides an easy way to retrieve the status code

func (*StubResponseWriter) Write

func (w *StubResponseWriter) Write(_ []byte) (int, error)

Write satisfies the http.ResponseWriter interface and captures data written, in bytes

func (*StubResponseWriter) WriteHeader

func (w *StubResponseWriter) WriteHeader(statusCode int)

WriteHeader satisfies the http.ResponseWriter interface and allows us to catch the status code

Jump to

Keyboard shortcuts

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