restest

package
v0.4.7 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2023 License: MIT Imports: 18 Imported by: 1

README

Resgate logo

Testing for Go RES Service
Synchronize Your Clients

License Reference


Package restest provides utilities for testing res services.

Basic usage

func TestService(t *testing.T) {
    // Create service to test
    s := res.NewService("foo")
    s.Handle("bar.$id",
        res.Access(res.AccessGranted),
        res.GetModel(func(r res.ModelRequest) {
            r.Model(struct {
                Message string `json:"msg"`
            }{r.PathParam("id")})
        }),
    )

    // Create test session
    c := restest.NewSession(t, s)
    defer c.Close()

    // Test sending get request and validate response
    c.Get("foo.bar.42").
        Response().
        AssertModel(map[string]string{"msg": "42"})
}

Documentation

Overview

Package restest provides utilities for testing res services:

Usage

func TestService(t *testing.T) {
	// Create service to test
	s := res.NewService("foo")
	s.Handle("bar.$id",
		res.Access(res.AccessGranted),
		res.GetModel(func(r res.ModelRequest) {
			r.Model(struct {
				Message string `json:"msg"`
			}{r.PathParam("id")})
		}),
	)

	// Create test session
	c := restest.NewSession(t, s)
	defer c.Close()

	// Test sending get request and validate response
	c.Get("foo.bar.42").
		Response().
		AssertModel(map[string]string{"msg": "42"})
}

Index

Constants

View Source
const DefaultTimeoutDuration = 1 * time.Second

DefaultTimeoutDuration is the duration the session awaits any message before timing out.

Variables

This section is empty.

Functions

func AssertEqualJSON

func AssertEqualJSON(t *testing.T, name string, result, expected interface{}, ctx ...interface{}) bool

AssertEqualJSON expects that a and b json marshals into equal values, and returns true if they do, otherwise logs a fatal error and returns false.

func AssertError

func AssertError(t *testing.T, err error, ctx ...interface{})

AssertError expects that err is not nil, otherwise logs an error with t.Fatalf

func AssertErrorCode

func AssertErrorCode(t *testing.T, err error, code string, ctx ...interface{})

AssertErrorCode expects that err is of type *res.Error with given code.

func AssertNil

func AssertNil(t *testing.T, v interface{}, ctx ...interface{})

AssertNil expects that a value is nil, otherwise it logs an error with t.Fatalf.

func AssertNoError

func AssertNoError(t *testing.T, err error, ctx ...interface{})

AssertNoError expects that err is nil, otherwise logs an error with t.Fatalf

func AssertNotNil

func AssertNotNil(t *testing.T, v interface{}, ctx ...interface{})

AssertNotNil expects that a value is non-nil, otherwise it logs an error with t.Fatalf.

func AssertPanic

func AssertPanic(t *testing.T, cb func(), ctx ...interface{})

AssertPanic expects the callback function to panic, otherwise logs an error with t.Errorf

func AssertPanicNoRecover

func AssertPanicNoRecover(t *testing.T, cb func(), ctx ...interface{})

AssertPanicNoRecover expects the callback function to panic, otherwise logs an error with t.Errorf. Does not recover from the panic

func AssertResError

func AssertResError(t *testing.T, err error, rerr *res.Error, ctx ...interface{})

AssertResError expects that err is of type *res.Error and matches rerr.

func AssertTrue

func AssertTrue(t *testing.T, expectation string, isTrue bool, ctx ...interface{}) bool

AssertTrue expects that a condition is true.

func ToResultEvents

func ToResultEvents(evs []Event) []store.ResultEvent

ToResultEvents creates a slice of store result events from a slice of events.

func WithFailSubscription

func WithFailSubscription(cfg *SessionConfig)

WithFailSubscription sets FailSubscription to make first subscription to fail.

func WithGnatsd

func WithGnatsd(cfg *SessionConfig)

WithGnatsd sets the UseGnatsd option to use a real NATS instance.

This option should be set if the test involves query events.

func WithKeepLogger

func WithKeepLogger(cfg *SessionConfig)

WithKeepLogger sets the KeepLogger option, to prevent Session to override the service logger with its own MemLogger.

func WithReset

func WithReset(resources []string, access []string) func(*SessionConfig)

WithReset sets the ValidateReset option to validate that the system.reset includes the specific access and resources strings.

func WithTest

func WithTest(name string) func(*SessionConfig)

WithTest sets the TestName option.

The test name will be outputted when logging test errors.

func WithoutReset

func WithoutReset(cfg *SessionConfig)

WithoutReset sets the NoReset option to not expect an initial system.reset event on server start.

Types

type Event

type Event struct {
	// Name of the event.
	Name string

	// Index position where the resource is added or removed from the query
	// result.
	//
	// Only valid for "add" and "remove" events.
	Idx int

	// ID of resource being added or removed from the query result.
	//
	// Only valid for "add" events.
	Value interface{}

	// Changed property values for the model emitting the event.
	//
	// Only valid for "change" events, and should marshal into a json object
	// with changed key/value properties.
	Changed interface{}

	// Payload of a custom event.
	Payload interface{}
}

Event represents an event.

func (Event) MarshalJSON

func (ev Event) MarshalJSON() ([]byte, error)

MarshalJSON marshals the event into json.

type MockConn

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

MockConn mocks a client connection to a NATS server.

func NewMockConn

func NewMockConn(t *testing.T, cfg *MockConnConfig) *MockConn

NewMockConn creates a new MockConn.

func (*MockConn) Access

func (c *MockConn) Access(rid string, req *Request) *NATSRequest

Access sends an access request to the service.

A nil req value sends a DefaultAccessRequest.

The resource ID, rid, may contain a query part:

test.model?q=foo

func (*MockConn) AssertNoMsg added in v0.4.6

func (c *MockConn) AssertNoMsg(delay time.Duration)

AssertNoMsg asserts that there are no more messages to get.

func (*MockConn) AssertNoSubscription

func (c *MockConn) AssertNoSubscription(subj string)

AssertNoSubscription asserts that there is no subscription for the given subject.

func (*MockConn) AssertQueueSubscription added in v0.4.0

func (c *MockConn) AssertQueueSubscription(subj, queue string)

AssertQueueSubscription asserts that the given subjects is subscribed with the channel, on a specific queue group.

func (*MockConn) AssertSubscription

func (c *MockConn) AssertSubscription(subj string)

AssertSubscription asserts that the given subjects is subscribed to with the channel.

func (*MockConn) Auth

func (c *MockConn) Auth(rid string, method string, req *Request) *NATSRequest

Auth sends an auth request to the service.

A nil req value sends a DefaultAuthRequest.

The resource ID, rid, may contain a query part:

test.model?q=foo

func (*MockConn) Call

func (c *MockConn) Call(rid string, method string, req *Request) *NATSRequest

Call sends a call request to the service.

A nil req value sends a DefaultCallRequest.

The resource ID, rid, may contain a query part:

test.model?q=foo

func (*MockConn) ChanQueueSubscribe added in v0.4.0

func (c *MockConn) ChanQueueSubscribe(subj, queue string, ch chan *nats.Msg) (*nats.Subscription, error)

ChanQueueSubscribe subscribes to messages matching the subject pattern.

func (*MockConn) ChanSubscribe

func (c *MockConn) ChanSubscribe(subj string, ch chan *nats.Msg) (*nats.Subscription, error)

ChanSubscribe subscribes to messages matching the subject pattern.

Same as ChanQueueSubscribe with an empty ("") queue name.

func (*MockConn) Close

func (c *MockConn) Close()

Close will close the connection to the server.

func (*MockConn) FailNextSubscription

func (c *MockConn) FailNextSubscription()

FailNextSubscription flags that the next subscription attempt should fail.

func (*MockConn) Get

func (c *MockConn) Get(rid string) *NATSRequest

Get sends a get request to the service.

The resource ID, rid, may contain a query part:

test.model?q=foo

func (*MockConn) GetMsg

func (c *MockConn) GetMsg() *Msg

GetMsg gets a pending message that is published to NATS.

If no message is received within a set amount of time, it will log it as a fatal error.

func (*MockConn) GetParallelMsgs

func (c *MockConn) GetParallelMsgs(n int) ParallelMsgs

GetParallelMsgs gets n number of published messages where the order is uncertain.

func (*MockConn) IsClosed

func (c *MockConn) IsClosed() bool

IsClosed tests if the client connection has been closed.

func (*MockConn) Publish

func (c *MockConn) Publish(subj string, payload []byte) error

Publish publishes the data argument to the given subject.

func (*MockConn) PublishRequest added in v0.4.0

func (c *MockConn) PublishRequest(subj, reply string, payload []byte) error

PublishRequest publishes a request expecting a response on the reply subject.

func (*MockConn) QueryRequest

func (c *MockConn) QueryRequest(querySubj string, query string) *NATSRequest

QueryRequest mocks a query request from NATS and returns a NATSRequest.

func (*MockConn) Request

func (c *MockConn) Request(subj string, payload interface{}) *NATSRequest

Request mocks a request from NATS and returns a NATSRequest.

func (*MockConn) RequestRaw

func (c *MockConn) RequestRaw(subj string, data []byte) string

RequestRaw mocks a raw byte request from NATS and returns the reply inbox used.

func (*MockConn) SendMessage added in v0.4.6

func (c *MockConn) SendMessage(subj string, reply string, data []byte)

SendMessage sends a raw message from nats to the the subscribing client.

func (*MockConn) StopServer

func (c *MockConn) StopServer()

StopServer stops the gnatsd server.

type MockConnConfig

type MockConnConfig struct {
	UseGnatsd       bool
	TimeoutDuration time.Duration
}

MockConnConfig holds MockConn configuration.

type Msg

type Msg struct {
	*nats.Msg
	// contains filtered or unexported fields
}

Msg represent a message sent to NATS.

func (*Msg) AssertAccess

func (m *Msg) AssertAccess(get bool, call string) *Msg

AssertAccess asserts that the access response matches the get and call values.

Only valid for access requests.

func (*Msg) AssertAddEvent

func (m *Msg) AssertAddEvent(rid string, value interface{}, idx int) *Msg

AssertAddEvent asserts that the message is an add event for the given resource ID, matching value and idx.

func (*Msg) AssertChangeEvent

func (m *Msg) AssertChangeEvent(rid string, values interface{}) *Msg

AssertChangeEvent asserts that the message is a change event for the given resource ID, matching the change values.

The values parameter should marshal into a JSON object.

func (*Msg) AssertCollection

func (m *Msg) AssertCollection(collection interface{}) *Msg

AssertCollection asserts that a the result is a collection response.

Only valid for get requests and query requests.

func (*Msg) AssertCreateEvent

func (m *Msg) AssertCreateEvent(rid string) *Msg

AssertCreateEvent asserts that the message is a create event for the given resource ID, with no payload.

func (*Msg) AssertCustomEvent

func (m *Msg) AssertCustomEvent(rid string, event string, payload interface{}) *Msg

AssertCustomEvent asserts that the message is a custom event for the given resource ID, with matching payload.

func (*Msg) AssertDeleteEvent

func (m *Msg) AssertDeleteEvent(rid string) *Msg

AssertDeleteEvent asserts that the message is a delete event for the given resource ID, with no payload.

func (*Msg) AssertError

func (m *Msg) AssertError(rerr *res.Error) *Msg

AssertError asserts that the response has the expected error.

func (*Msg) AssertErrorCode

func (m *Msg) AssertErrorCode(code string) *Msg

AssertErrorCode asserts that the response has the expected error code.

func (*Msg) AssertEvent

func (m *Msg) AssertEvent(rid string, ev Event) *Msg

AssertEvent asserts that the message is a matching event for the given resource ID.

func (*Msg) AssertEventName

func (m *Msg) AssertEventName(rid string, name string) *Msg

AssertEventName asserts that the message is an event matching the name for the given resource ID.

func (*Msg) AssertEvents

func (m *Msg) AssertEvents(events ...Event) *Msg

AssertEvents asserts that a the result is an events response.

Only valid for query requests.

func (*Msg) AssertModel

func (m *Msg) AssertModel(model interface{}) *Msg

AssertModel asserts that a the result is a model response.

Only valid for get requests and query requests.

func (*Msg) AssertNoPath

func (m *Msg) AssertNoPath(path string) *Msg

AssertNoPath asserts that a the message payload doesn't have a value at a given dot-separated path in a nested object.

func (*Msg) AssertPathPayload

func (m *Msg) AssertPathPayload(path string, payload interface{}) *Msg

AssertPathPayload asserts that a the message payload at a given dot-separated path in a nested object has the expected payload.

func (*Msg) AssertPathType

func (m *Msg) AssertPathType(path string, typ interface{}) *Msg

AssertPathType asserts that a the message payload at a given dot-separated path in a nested object has the same type as typ.

func (*Msg) AssertPayload

func (m *Msg) AssertPayload(payload interface{}) *Msg

AssertPayload asserts that the message has the expected payload.

func (*Msg) AssertQuery

func (m *Msg) AssertQuery(query string) *Msg

AssertQuery asserts that the result query matches query.

Only valid for get requests.

func (*Msg) AssertQueryEvent

func (m *Msg) AssertQueryEvent(rid string, subject *string) *Msg

AssertQueryEvent asserts that the message is a query event for the given resource ID, setting the event subject.

The subject may be nil, and will then not be set.

func (*Msg) AssertRawPayload

func (m *Msg) AssertRawPayload(payload []byte) *Msg

AssertRawPayload asserts that the message has the expected payload bytes.

func (*Msg) AssertReaccessEvent

func (m *Msg) AssertReaccessEvent(rid string) *Msg

AssertReaccessEvent asserts that the message is a reaccess event for the given resource ID, with no payload.

func (*Msg) AssertRemoveEvent

func (m *Msg) AssertRemoveEvent(rid string, idx int) *Msg

AssertRemoveEvent asserts that the message is a remove event for the given resource ID, matching idx.

func (*Msg) AssertResource

func (m *Msg) AssertResource(rid string) *Msg

AssertResource asserts that the response is a resource response matching rid.

Only valid for call and auth requests.

func (*Msg) AssertResult

func (m *Msg) AssertResult(result interface{}) *Msg

AssertResult asserts that the response has the expected result.

Only valid for call and auth requests.

func (*Msg) AssertSubject

func (m *Msg) AssertSubject(subject string) *Msg

AssertSubject asserts that the message has the expected subject.

func (*Msg) AssertSystemReset

func (m *Msg) AssertSystemReset(resources []string, access []string) *Msg

AssertSystemReset asserts that the message is a system reset event, matching the resources and access.

func (*Msg) AssertTokenEvent

func (m *Msg) AssertTokenEvent(cid string, token interface{}) *Msg

AssertTokenEvent asserts that the message is a connection token event for the given connection ID, cid, with matching token.

func (*Msg) AssertTokenEventWithID added in v0.4.5

func (m *Msg) AssertTokenEventWithID(cid string, tid string, token interface{}) *Msg

AssertTokenEventWithID asserts that the message is a connection token event for the given connection ID (cid) and token ID (tid), with matching token.

func (*Msg) Equals

func (m *Msg) Equals(subject string, payload interface{}) *Msg

Equals asserts that the message has the expected subject and payload.

func (*Msg) HasPath

func (m *Msg) HasPath(path string) (interface{}, bool)

HasPath checks if a a given dot-separated path in a nested object exists. If it does, it returns the path value and true, otherwise nil and false.

func (*Msg) PathPayload

func (m *Msg) PathPayload(path string) interface{}

PathPayload returns the message payload at a given dot-separated path in a nested object. It gives a fatal error if the path doesn't exist.

func (*Msg) Payload

func (m *Msg) Payload() interface{}

Payload unmarshals the message data into an empty interface. Panics if the data is not valid json.

type NATSRequest

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

NATSRequest represents a requests sent over NATS to the service.

func (*NATSRequest) Response

func (nr *NATSRequest) Response() *Msg

Response gets the next pending message that is published to NATS by the service.

If no message is received within a set amount of time, or if the message is not a response to the request, it will log it as a fatal error.

type ParallelMsgs

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

ParallelMsgs holds multiple requests in undetermined order.

func (ParallelMsgs) GetMsg

func (pm ParallelMsgs) GetMsg(subject string) *Msg

GetMsg returns a published message based on subject.

type Request

type Request struct {
	CID        string              `json:"cid,omitempty"`
	Params     json.RawMessage     `json:"params,omitempty"`
	Token      json.RawMessage     `json:"token,omitempty"`
	Header     map[string][]string `json:"header,omitempty"`
	Host       string              `json:"host,omitempty"`
	RemoteAddr string              `json:"remoteAddr,omitempty"`
	URI        string              `json:"uri,omitempty"`
	Query      string              `json:"query,omitempty"`
}

Request represents a request payload.

func DefaultAccessRequest added in v0.4.0

func DefaultAccessRequest() *Request

DefaultAccessRequest returns a default access request without token.

func DefaultAuthRequest

func DefaultAuthRequest() *Request

DefaultAuthRequest returns a default auth request.

func DefaultCallRequest

func DefaultCallRequest() *Request

DefaultCallRequest returns a default call request.

type Session

type Session struct {
	*MockConn
	// contains filtered or unexported fields
}

Session represents a test session with a res server

func NewSession

func NewSession(t *testing.T, service *res.Service, opts ...func(*SessionConfig)) *Session

NewSession creates a new Session and connects the service to a mock NATS connection.

A service logger will by default be set to a new MemLogger. To set any other logger, add the option:

WithLogger(logger)

If the tests sends any query event, a real NATS instance is required, which is slower than using the default mock connection. To use a real NATS instance, add the option:

WithGnatsd

func (*Session) Close

func (s *Session) Close() error

Close closes the session.

func (*Session) Service

func (s *Session) Service() *res.Service

Service returns the associated res.Service.

type SessionConfig

type SessionConfig struct {
	TestName         string
	KeepLogger       bool
	NoReset          bool
	ValidateReset    bool
	ResetResources   []string
	ResetAccess      []string
	FailSubscription bool
	MockConnConfig
}

SessionConfig represents the configuration for a session.

Jump to

Keyboard shortcuts

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