httputil

package
v0.0.11 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// PortFlag string for flag port
	PortFlag = "port"
)

Variables

This section is empty.

Functions

func NewHTTPAddressFromContext

func NewHTTPAddressFromContext(c *cli.Context) string

NewHTTPAddressFromContext returns the configured address to listen to from cli flags configuration.

func NewHTTPCliFlags

func NewHTTPCliFlags(defaultPort HTTPPort) []cli.Flag

NewHTTPCliFlags creates new cli flags for HTTP Server.

func ResponseFailure

func ResponseFailure(c *gin.Context, options ...ResponseOption)

ResponseFailure responses the request with 200 status code and a failure message.

func ResponseSuccess

func ResponseSuccess(c *gin.Context, options ...ResponseOption)

ResponseSuccess responses the request with 200 status code and a success message.

func RunHTTPTestCase

func RunHTTPTestCase(t *testing.T, tc HTTPTestCase, handler http.Handler)

RunHTTPTestCase run http request test case.

Types

type AssertFn

type AssertFn func(t *testing.T, resp *httptest.ResponseRecorder)

AssertFn is the function used to asserts the test response.

func AssertCode

func AssertCode(code int) AssertFn

AssertCode asserts that the response matched the expected error code.

type CommonError

type CommonError struct {
	Error string `json:"error"`
}

type HTTPPort

type HTTPPort int

HTTPPort define custom type for port

const (
	Port HTTPPort = 8030
)

type HTTPTestCase

type HTTPTestCase struct {
	Msg      string
	Endpoint string
	Method   string
	Params   map[string]string
	Body     []byte
	Assert   AssertFn
}

HTTPTestCase struct for http test case.

type ResponseOption

type ResponseOption func(h gin.H)

ResponseOption is the additional data to include in response.

func WithData

func WithData(data interface{}) ResponseOption

WithData includes the given data to the response.

func WithError

func WithError(err error) ResponseOption

WithError includes the error as a failure reason. It is usually used along with ResponseFailure. If err is nil, empty reason will be used.

func WithField

func WithField(k string, v interface{}) ResponseOption

WithField includes the given field in the response.

func WithMultipleFields

func WithMultipleFields(extra gin.H) ResponseOption

WithMultipleFields includes the extra data to the response.

func WithReason

func WithReason(reason string) ResponseOption

WithReason includes the given reason to the response. It is intended to use for operation failed response.

Jump to

Keyboard shortcuts

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