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
- func AssertEqualJSON(t *testing.T, name string, result, expected interface{}, ctx ...interface{}) bool
- func AssertError(t *testing.T, err error, ctx ...interface{})
- func AssertErrorCode(t *testing.T, err error, code string, ctx ...interface{})
- func AssertNil(t *testing.T, v interface{}, ctx ...interface{})
- func AssertNoError(t *testing.T, err error, ctx ...interface{})
- func AssertNotNil(t *testing.T, v interface{}, ctx ...interface{})
- func AssertPanic(t *testing.T, cb func(), ctx ...interface{})
- func AssertPanicNoRecover(t *testing.T, cb func(), ctx ...interface{})
- func AssertResError(t *testing.T, err error, rerr *res.Error, ctx ...interface{})
- func AssertTrue(t *testing.T, expectation string, isTrue bool, ctx ...interface{}) bool
- func ToResultEvents(evs []Event) []store.ResultEvent[any]
- func WithFailSubscription(cfg *SessionConfig)
- func WithGnatsd(cfg *SessionConfig)
- func WithKeepLogger(cfg *SessionConfig)
- func WithReset(resources []string, access []string) func(*SessionConfig)
- func WithTest(name string) func(*SessionConfig)
- func WithoutReset(cfg *SessionConfig)
- type Event
- type MockConn
- func (c *MockConn) Access(rid string, req *Request) *NATSRequest
- func (c *MockConn) AssertNoMsg(delay time.Duration)
- func (c *MockConn) AssertNoSubscription(subj string)
- func (c *MockConn) AssertQueueSubscription(subj, queue string)
- func (c *MockConn) AssertSubscription(subj string)
- func (c *MockConn) Auth(rid string, method string, req *Request) *NATSRequest
- func (c *MockConn) Call(rid string, method string, req *Request) *NATSRequest
- func (c *MockConn) ChanQueueSubscribe(subj, queue string, ch chan *nats.Msg) (*nats.Subscription, error)
- func (c *MockConn) ChanSubscribe(subj string, ch chan *nats.Msg) (*nats.Subscription, error)
- func (c *MockConn) Close()
- func (c *MockConn) FailNextSubscription()
- func (c *MockConn) Get(rid string) *NATSRequest
- func (c *MockConn) GetMsg() *Msg
- func (c *MockConn) GetParallelMsgs(n int) ParallelMsgs
- func (c *MockConn) IsClosed() bool
- func (c *MockConn) Publish(subj string, payload []byte) error
- func (c *MockConn) PublishRequest(subj, reply string, payload []byte) error
- func (c *MockConn) QueryRequest(querySubj string, query string) *NATSRequest
- func (c *MockConn) Request(subj string, payload interface{}) *NATSRequest
- func (c *MockConn) RequestRaw(subj string, data []byte) string
- func (c *MockConn) SendMessage(subj string, reply string, data []byte)
- func (c *MockConn) StopServer()
- type MockConnConfig
- type Msg
- func (m *Msg) AssertAccess(get bool, call string) *Msg
- func (m *Msg) AssertAddEvent(rid string, value interface{}, idx int) *Msg
- func (m *Msg) AssertChangeEvent(rid string, values interface{}) *Msg
- func (m *Msg) AssertCollection(collection interface{}) *Msg
- func (m *Msg) AssertCreateEvent(rid string) *Msg
- func (m *Msg) AssertCustomEvent(rid string, event string, payload interface{}) *Msg
- func (m *Msg) AssertDeleteEvent(rid string) *Msg
- func (m *Msg) AssertError(rerr *res.Error) *Msg
- func (m *Msg) AssertErrorCode(code string) *Msg
- func (m *Msg) AssertEvent(rid string, ev Event) *Msg
- func (m *Msg) AssertEventName(rid string, name string) *Msg
- func (m *Msg) AssertEvents(events ...Event) *Msg
- func (m *Msg) AssertModel(model interface{}) *Msg
- func (m *Msg) AssertNoPath(path string) *Msg
- func (m *Msg) AssertPathPayload(path string, payload interface{}) *Msg
- func (m *Msg) AssertPathType(path string, typ interface{}) *Msg
- func (m *Msg) AssertPayload(payload interface{}) *Msg
- func (m *Msg) AssertQuery(query string) *Msg
- func (m *Msg) AssertQueryEvent(rid string, subject *string) *Msg
- func (m *Msg) AssertRawPayload(payload []byte) *Msg
- func (m *Msg) AssertReaccessEvent(rid string) *Msg
- func (m *Msg) AssertRemoveEvent(rid string, idx int) *Msg
- func (m *Msg) AssertResource(rid string) *Msg
- func (m *Msg) AssertResult(result interface{}) *Msg
- func (m *Msg) AssertSubject(subject string) *Msg
- func (m *Msg) AssertSystemReset(resources []string, access []string) *Msg
- func (m *Msg) AssertTokenEvent(cid string, token interface{}) *Msg
- func (m *Msg) AssertTokenEventWithID(cid string, tid string, token interface{}) *Msg
- func (m *Msg) Equals(subject string, payload interface{}) *Msg
- func (m *Msg) HasPath(path string) (interface{}, bool)
- func (m *Msg) PathPayload(path string) interface{}
- func (m *Msg) Payload() interface{}
- type NATSRequest
- type ParallelMsgs
- type Request
- type Session
- type SessionConfig
Constants ¶
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 ¶
AssertError expects that err is not nil, otherwise logs an error with t.Fatalf
func AssertErrorCode ¶
AssertErrorCode expects that err is of type *res.Error with given code.
func AssertNoError ¶
AssertNoError expects that err is nil, otherwise logs an error with t.Fatalf
func AssertNotNil ¶
AssertNotNil expects that a value is non-nil, otherwise it logs an error with t.Fatalf.
func AssertPanic ¶
AssertPanic expects the callback function to panic, otherwise logs an error with t.Errorf
func AssertPanicNoRecover ¶
AssertPanicNoRecover expects the callback function to panic, otherwise logs an error with t.Errorf. Does not recover from the panic
func AssertResError ¶
AssertResError expects that err is of type *res.Error and matches rerr.
func AssertTrue ¶
AssertTrue expects that a condition is true.
func ToResultEvents ¶
func ToResultEvents(evs []Event) []store.ResultEvent[any]
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 ¶
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 ¶
AssertNoMsg asserts that there are no more messages to get.
func (*MockConn) AssertNoSubscription ¶
AssertNoSubscription asserts that there is no subscription for the given subject.
func (*MockConn) AssertQueueSubscription ¶
AssertQueueSubscription asserts that the given subjects is subscribed with the channel, on a specific queue group.
func (*MockConn) AssertSubscription ¶
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 ¶
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 ¶
ChanSubscribe subscribes to messages matching the subject pattern.
Same as ChanQueueSubscribe with an empty ("") queue name.
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 ¶
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) PublishRequest ¶
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 ¶
RequestRaw mocks a raw byte request from NATS and returns the reply inbox used.
func (*MockConn) SendMessage ¶
SendMessage sends a raw message from nats to the the subscribing client.
type MockConnConfig ¶
MockConnConfig holds MockConn configuration.
type Msg ¶
Msg represent a message sent to NATS.
func (*Msg) AssertAccess ¶
AssertAccess asserts that the access response matches the get and call values.
Only valid for access requests.
func (*Msg) AssertAddEvent ¶
AssertAddEvent asserts that the message is an add event for the given resource ID, matching value and idx.
func (*Msg) AssertChangeEvent ¶
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 ¶
AssertCollection asserts that a the result is a collection response.
Only valid for get requests and query requests.
func (*Msg) AssertCreateEvent ¶
AssertCreateEvent asserts that the message is a create event for the given resource ID, with no payload.
func (*Msg) AssertCustomEvent ¶
AssertCustomEvent asserts that the message is a custom event for the given resource ID, with matching payload.
func (*Msg) AssertDeleteEvent ¶
AssertDeleteEvent asserts that the message is a delete event for the given resource ID, with no payload.
func (*Msg) AssertError ¶
AssertError asserts that the response has the expected error.
func (*Msg) AssertErrorCode ¶
AssertErrorCode asserts that the response has the expected error code.
func (*Msg) AssertEvent ¶
AssertEvent asserts that the message is a matching event for the given resource ID.
func (*Msg) AssertEventName ¶
AssertEventName asserts that the message is an event matching the name for the given resource ID.
func (*Msg) AssertEvents ¶
AssertEvents asserts that a the result is an events response.
Only valid for query requests.
func (*Msg) AssertModel ¶
AssertModel asserts that a the result is a model response.
Only valid for get requests and query requests.
func (*Msg) AssertNoPath ¶
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 ¶
AssertPathPayload asserts that a the message payload at a given dot-separated path in a nested object has the expected payload.
func (*Msg) AssertPathType ¶
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 ¶
AssertPayload asserts that the message has the expected payload.
func (*Msg) AssertQuery ¶
AssertQuery asserts that the result query matches query.
Only valid for get requests.
func (*Msg) AssertQueryEvent ¶
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 ¶
AssertRawPayload asserts that the message has the expected payload bytes.
func (*Msg) AssertReaccessEvent ¶
AssertReaccessEvent asserts that the message is a reaccess event for the given resource ID, with no payload.
func (*Msg) AssertRemoveEvent ¶
AssertRemoveEvent asserts that the message is a remove event for the given resource ID, matching idx.
func (*Msg) AssertResource ¶
AssertResource asserts that the response is a resource response matching rid.
Only valid for call and auth requests.
func (*Msg) AssertResult ¶
AssertResult asserts that the response has the expected result.
Only valid for call and auth requests.
func (*Msg) AssertSubject ¶
AssertSubject asserts that the message has the expected subject.
func (*Msg) AssertSystemReset ¶
AssertSystemReset asserts that the message is a system reset event, matching the resources and access.
func (*Msg) AssertTokenEvent ¶
AssertTokenEvent asserts that the message is a connection token event for the given connection ID, cid, with matching token.
func (*Msg) AssertTokenEventWithID ¶
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) HasPath ¶
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 ¶
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.
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 ¶
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 ¶
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
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.