utils

package
v0.0.0-...-2d572df Latest Latest
Warning

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

Go to latest
Published: May 28, 2021 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func JSONBody

func JSONBody(v interface{}) (*bytes.Reader, error)

JSONBody returns the serialized JSON of the given value as a *bytes.Reader Useful for populating HTTP request bodies

func ParseJSONBody

func ParseJSONBody(body io.ReadCloser, target interface{}) error

ParseJSONBody parse the given body into target using a JSON Decoder

func RequireEnv

func RequireEnv(envVars ...string) (map[string]string, error)

RequireEnv takes a variable number of environment variables and ensures they are all set

func TestDB

func TestDB(t *testing.T) (*hnysqlx.DB, func() error)

TestDB returns a test db connection

func TestRedis

func TestRedis(t *testing.T) (*redis.Client, func() error)

TestRedis returns a test redis connection

Types

type Clock

type Clock interface {
	Now() time.Time
}

The Clock interface implements Now(), useful in order to inject specific times during testing

type HTTPClient

type HTTPClient interface {
	Do(*http.Request) (*http.Response, error)
}

HTTPClient describes the interface for an HTTP client

type IDGenerator

type IDGenerator interface {
	Generate() string
}

IDGenerator interface is used to implement uuid generators

type Random

type Random interface {
	String(int) (string, error)
	Bytes(int) ([]byte, error)
}

Random describes an interface to get random strings and bytes

type RealClock

type RealClock struct{}

RealClock Implements Clock by delegating to time.Now()

func (*RealClock) Now

func (clock *RealClock) Now() time.Time

Now returns the current time

type SecureRandom

type SecureRandom struct{}

SecureRandom implements Random using the crypto/rand package

func (*SecureRandom) Bytes

func (s *SecureRandom) Bytes(size int) ([]byte, error)

Bytes returns a slice of random bytes of given size

func (*SecureRandom) String

func (s *SecureRandom) String(size int) (string, error)

String returns a Base-64 encoded string of random bytes of given size

type TestClock

type TestClock struct {
	Time time.Time
}

TestClock implements Clock with a constant value

func (*TestClock) Now

func (clock *TestClock) Now() time.Time

Now returns the Time parameter of TestClock

type TestHTTPClient

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

TestHTTPClient struct represents a mocked HTTPClient

func NewTestHTTPClient

func NewTestHTTPClient() *TestHTTPClient

NewTestHTTPClient creates a new TestHTTPClient struct

func (*TestHTTPClient) Do

func (c *TestHTTPClient) Do(r *http.Request) (*http.Response, error)

Do satisfies the HTTPClient interface, allowing the TestHTTPClient to be used in place of real HTTPClients in tests.

func (*TestHTTPClient) Mock

func (c *TestHTTPClient) Mock(method, url string, handler requestHandler)

Mock sets up a new mocked handler for the given method and URL

type TestIDGenerator

type TestIDGenerator struct {
	ID string
}

TestIDGenerator implements IDGenerator with a constant value

func (*TestIDGenerator) Generate

func (generator *TestIDGenerator) Generate() string

Generate returns the ID parameter of TestIDGenerator

type TestRandom

type TestRandom struct {
	Value []byte
}

TestRandom implements the Random implementation by returning a known value.

func (*TestRandom) Bytes

func (s *TestRandom) Bytes(size int) ([]byte, error)

Bytes returns the configured Bytes

func (*TestRandom) String

func (s *TestRandom) String(size int) (string, error)

String returns the configured Bytes as a String

type UUIDGenerator

type UUIDGenerator struct{}

UUIDGenerator implements IdGenerator via google/uuid generation

func (*UUIDGenerator) Generate

func (generator *UUIDGenerator) Generate() string

Generate returns a random UUID string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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