test

package
v1.8.0 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2024 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertPanic added in v1.5.0

func AssertPanic(t Testing, cb func())

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

func AssertResponseBody added in v1.8.0

func AssertResponseBody(t *testing.T, r io.Reader, body interface{})

AssertResponseBody asserts that the response has the expected body

func AssertResponseContainsMetrics added in v1.8.0

func AssertResponseContainsMetrics(t *testing.T, r *http.Response, metrics []string)

AssertResponseContainsMetrics asserts that the body of the response contains the metric values.

func AssertResponseHeaders added in v1.8.0

func AssertResponseHeaders(t *testing.T, r *http.Response, h map[string]string)

AssertResponseHeaders asserts that the response includes the expected headers

func AssertResponseMultiHeaders added in v1.8.0

func AssertResponseMultiHeaders(t *testing.T, r *http.Response, h map[string][]string)

AssertResponseMultiHeaders asserts that the response includes the expected headers, including repeated headers such as Set-Cookie.

func AssertResponseStatusCode added in v1.8.0

func AssertResponseStatusCode(t *testing.T, r *http.Response, code int)

AssertResponseStatusCode asserts that the response has the expected status code

func DefaultConfig added in v1.5.0

func DefaultConfig(cfgs ...func(*server.Config)) server.Config

DefaultConfig returns a default server configuration used for testing

func SplitAtLastSpace added in v1.8.0

func SplitAtLastSpace(s string) (string, string)

SplitAtLastSpace splits a string at the last space found.

Types

type ClientEvent

type ClientEvent struct {
	Event string
	Data  interface{}
}

ClientEvent represents a RES-client event sent to the client

func (*ClientEvent) AssertData

func (ev *ClientEvent) AssertData(t *testing.T, data interface{}) *ClientEvent

AssertData asserts that the event has the expected data

func (*ClientEvent) AssertEventName

func (ev *ClientEvent) AssertEventName(t *testing.T, event string) *ClientEvent

AssertEventName asserts that the event has the expected event name

func (*ClientEvent) Equals

func (ev *ClientEvent) Equals(t *testing.T, event string, data interface{}) *ClientEvent

Equals asserts that the event has the expected event name and payload

func (*ClientEvent) IsData added in v1.7.0

func (ev *ClientEvent) IsData(data interface{}) bool

IsData checks if the data matches, and returns true if it does, otherwise false.

type ClientRequest

type ClientRequest struct {
	Method string
	Params interface{}
	// contains filtered or unexported fields
}

ClientRequest represents a RES-client request

func (*ClientRequest) GetResponse

func (cr *ClientRequest) GetResponse(t *testing.T) *ClientResponse

GetResponse awaits for a response and returns it. Fails if a response hasn't arrived within 1 second.

type ClientResponse

type ClientResponse struct {
	Result interface{}
	Error  *reserr.Error
}

ClientResponse represents a response to a RES-client request

func (*ClientResponse) AssertError

func (cr *ClientResponse) AssertError(t *testing.T, err *reserr.Error) *ClientResponse

AssertError asserts that the response has the expected error

func (*ClientResponse) AssertErrorCode

func (cr *ClientResponse) AssertErrorCode(t *testing.T, code string) *ClientResponse

AssertErrorCode asserts that the response has the expected error code

func (*ClientResponse) AssertIsError added in v1.2.2

func (cr *ClientResponse) AssertIsError(t *testing.T) *ClientResponse

AssertIsError asserts that the response is an error

func (*ClientResponse) AssertResult

func (cr *ClientResponse) AssertResult(t *testing.T, result interface{}) *ClientResponse

AssertResult asserts that the response has the expected result

type Conn

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

Conn represents a client websocket connection

func NewConn

func NewConn(s *Session, d *websocket.Dialer, ws *websocket.Conn, evs chan *ClientEvent) *Conn

NewConn creates a new Conn instance

func (*Conn) AssertClosed added in v1.1.0

func (c *Conn) AssertClosed(t *testing.T)

AssertClosed asserts that the connection is closed

func (*Conn) AssertNoEvent

func (c *Conn) AssertNoEvent(t *testing.T, rid string)

AssertNoEvent assert that no events are queued

func (*Conn) AssertNoNATSRequest

func (c *Conn) AssertNoNATSRequest(t *testing.T, rid string)

AssertNoNATSRequest assert that no request are queued on NATS

func (*Conn) Disconnect

func (c *Conn) Disconnect()

Disconnect closes the connection to the gateway

func (*Conn) Error added in v1.2.1

func (c *Conn) Error() error

func (*Conn) GetEvent

func (c *Conn) GetEvent(t *testing.T) *ClientEvent

GetEvent gets a pending event that is sent to the client. If no event is received within a set amount of time, it will log it as a fatal error.

func (*Conn) GetParallelEvents

func (c *Conn) GetParallelEvents(t *testing.T, n int) ParallelEvents

GetParallelEvents gets n number of events where the order is uncertain.

func (*Conn) PanicOnError added in v1.2.1

func (c *Conn) PanicOnError()

PanicOnError panics if the connection has encountered an error.

func (*Conn) Request

func (c *Conn) Request(method string, params interface{}) *ClientRequest

Request sends a properly formatted request to the gateway using the method and parameters provided.

type CountLogger added in v1.4.0

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

CountLogger writes log messages to os.Stderr

func NewCountLogger added in v1.4.0

func NewCountLogger(debug bool, trace bool) *CountLogger

NewCountLogger returns a new logger that writes to a bytes buffer

func (*CountLogger) AssertErrorsLogged added in v1.4.0

func (l *CountLogger) AssertErrorsLogged(t *testing.T, count int)

AssertErrorsLogged asserts that some error has been logged and clears the error count

func (*CountLogger) AssertNoErrorsLogged added in v1.4.0

func (l *CountLogger) AssertNoErrorsLogged(t *testing.T)

AssertNoErrorsLogged asserts that no error has been logged

func (*CountLogger) Debug added in v1.4.0

func (l *CountLogger) Debug(s string)

Debug writes a debug entry

func (*CountLogger) Error added in v1.4.0

func (l *CountLogger) Error(s string)

Error writes an error entry

func (*CountLogger) IsDebug added in v1.4.0

func (l *CountLogger) IsDebug() bool

IsDebug returns true if debug logging is active

func (*CountLogger) IsTrace added in v1.4.0

func (l *CountLogger) IsTrace() bool

IsTrace returns true if trace logging is active

func (*CountLogger) Log added in v1.4.0

func (l *CountLogger) Log(s string)

Log writes a log entry

func (*CountLogger) String added in v1.4.0

func (l *CountLogger) String() string

String returns the log

func (*CountLogger) Trace added in v1.4.0

func (l *CountLogger) Trace(s string)

Trace writes a trace entry

type HTTPRequest

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

HTTPRequest represents a HTTP requests made to the gateway

func (*HTTPRequest) GetResponse

func (hr *HTTPRequest) GetResponse(t *testing.T) *HTTPResponse

GetResponse awaits for a response and returns it. Fails if a response hasn't arrived within 1 second.

type HTTPResponse

type HTTPResponse struct {
	*httptest.ResponseRecorder
}

HTTPResponse represents a response received from a HTTP request made to the gateway

func (*HTTPResponse) AssertBody

func (hr *HTTPResponse) AssertBody(t *testing.T, body interface{}) *HTTPResponse

AssertBody asserts that the response has the expected body

func (*HTTPResponse) AssertError

func (hr *HTTPResponse) AssertError(t *testing.T, err *reserr.Error) *HTTPResponse

AssertError asserts that the response does not have status 200, and has the expected error

func (*HTTPResponse) AssertErrorCode

func (hr *HTTPResponse) AssertErrorCode(t *testing.T, code string) *HTTPResponse

AssertErrorCode asserts that the response does not have status 200, and has the expected error code

func (*HTTPResponse) AssertHeaders

func (hr *HTTPResponse) AssertHeaders(t *testing.T, h map[string]string) *HTTPResponse

AssertHeaders asserts that the response includes the expected headers

func (*HTTPResponse) AssertIsError added in v1.2.2

func (hr *HTTPResponse) AssertIsError(t *testing.T) *HTTPResponse

AssertIsError asserts that the response does not have status 200, and that the body has an error code.

func (*HTTPResponse) AssertMissingHeaders added in v1.5.0

func (hr *HTTPResponse) AssertMissingHeaders(t *testing.T, h []string) *HTTPResponse

AssertMissingHeaders asserts that the response does not include the given headers

func (*HTTPResponse) AssertMultiHeaders added in v1.8.0

func (hr *HTTPResponse) AssertMultiHeaders(t *testing.T, h map[string][]string) *HTTPResponse

AssertMultiHeaders asserts that the response includes the expected headers, including repeated headers such as Set-Cookie.

func (*HTTPResponse) AssertStatusCode

func (hr *HTTPResponse) AssertStatusCode(t *testing.T, code int) *HTTPResponse

AssertStatusCode asserts that the response has the expected status code

func (*HTTPResponse) Equals

func (hr *HTTPResponse) Equals(t *testing.T, code int, body interface{}) *HTTPResponse

Equals asserts that the response has the expected status code and body

type LogTesting added in v1.8.0

type LogTesting struct {
	// If true, does not panic when Fatal or Fatalf is called.
	NoPanic bool
	Err     error
}

func (*LogTesting) Defer added in v1.8.0

func (t *LogTesting) Defer()

func (*LogTesting) Error added in v1.8.0

func (t *LogTesting) Error(args ...interface{})

func (*LogTesting) Errorf added in v1.8.0

func (t *LogTesting) Errorf(format string, args ...interface{})

func (*LogTesting) Fatal added in v1.8.0

func (t *LogTesting) Fatal(args ...interface{})

func (*LogTesting) Fatalf added in v1.8.0

func (t *LogTesting) Fatalf(format string, args ...interface{})

type NATSTestClient

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

NATSTestClient holds a client connection to a nats server.

func NewNATSTestClient

func NewNATSTestClient(l logger.Logger) *NATSTestClient

NewNATSTestClient creates a new NATSTestClient instance

func (*NATSTestClient) Close

func (c *NATSTestClient) Close()

Close closes the client connection.

func (*NATSTestClient) ConnEvent

func (c *NATSTestClient) ConnEvent(cid string, event string, payload interface{})

ConnEvent sends a connection event to resgate. The subject will be "conn."+cid+"."+event . It panics if there is no subscription for such event.

func (*NATSTestClient) Connect

func (c *NATSTestClient) Connect() error

Connect establishes a connection to the MQ

func (*NATSTestClient) Debugf

func (c *NATSTestClient) Debugf(format string, v ...interface{})

Debugf writes a formatted debug message

func (*NATSTestClient) Errorf added in v1.4.0

func (c *NATSTestClient) Errorf(format string, v ...interface{})

Errorf writes a formatted error message

func (*NATSTestClient) GetParallelRequests

func (c *NATSTestClient) GetParallelRequests(t Testing, n int) ParallelRequests

GetParallelRequests gets n number of requests where the order is uncertain.

func (*NATSTestClient) GetRequest

func (c *NATSTestClient) GetRequest(t Testing) *Request

GetRequest gets a pending request that is sent to NATS. If no request is received within a set amount of time, it will log it as a fatal error.

func (*NATSTestClient) HasSubscriptions

func (c *NATSTestClient) HasSubscriptions(t Testing, rids ...string)

HasSubscriptions asserts that there is a subscription for the given resource IDs

func (*NATSTestClient) IsClosed

func (c *NATSTestClient) IsClosed() bool

IsClosed tests if the client connection has been closed.

func (*NATSTestClient) Logf

func (c *NATSTestClient) Logf(format string, v ...interface{})

Logf writes a formatted log message

func (*NATSTestClient) NoSubscriptions added in v1.6.0

func (c *NATSTestClient) NoSubscriptions(t Testing, rids ...string)

NoSubscriptions asserts that there isn't any subscription for the given resource IDs.

func (*NATSTestClient) ResourceEvent

func (c *NATSTestClient) ResourceEvent(rid string, event string, payload interface{})

ResourceEvent sends a resource event to resgate. The subject will be "event."+rid+"."+event . It panics if there is no subscription for such event.

func (*NATSTestClient) SendRequest

func (c *NATSTestClient) SendRequest(subj string, payload []byte, cb mq.Response)

SendRequest sends an asynchronous request on a subject, expecting the Response callback to be called once.

func (*NATSTestClient) SetClosedHandler

func (c *NATSTestClient) SetClosedHandler(_ func(error))

SetClosedHandler sets the handler when the connection is closed

func (*NATSTestClient) Subscribe

func (c *NATSTestClient) Subscribe(namespace string, cb mq.Response) (mq.Unsubscriber, error)

Subscribe to all events on a resource namespace. The namespace has the format "event."+resource

func (*NATSTestClient) SystemEvent

func (c *NATSTestClient) SystemEvent(event string, payload interface{})

SystemEvent sends a system event to resgate. The subject will be "system."+event . It panics if there is no subscription for such event.

func (*NATSTestClient) Tracef

func (c *NATSTestClient) Tracef(format string, v ...interface{})

Tracef writes a formatted trace message

type ParallelEvents

type ParallelEvents []*ClientEvent

ParallelEvents holds multiple events in undetermined order

func (ParallelEvents) GetEvent

func (pr ParallelEvents) GetEvent(t *testing.T, event string) *ClientEvent

GetEvent returns a event based on event name.

type ParallelRequests

type ParallelRequests []*Request

ParallelRequests holds multiple requests in undetermined order

func (ParallelRequests) GetRequest

func (pr ParallelRequests) GetRequest(t Testing, subject string) *Request

GetRequest returns a request based on subject.

type Request

type Request struct {
	Subject    string
	RawPayload []byte
	Payload    interface{}
	// contains filtered or unexported fields
}

Request represent a request to NATS

func (*Request) AssertNotPathPayload added in v1.8.0

func (r *Request) AssertNotPathPayload(t Testing, path string, payload interface{}) *Request

AssertNotPathPayload asserts that the request payload at a given dot-separated path in a nested object does not have the provided payload or is missing.

func (*Request) AssertPathMissing added in v1.8.0

func (r *Request) AssertPathMissing(t Testing, path string) *Request

AssertPathMissing asserts that the request payload at a given dot-separated path in a nested object is missing. It gives a fatal error if any other part of the patch except for the last part of the path is missing.

func (*Request) AssertPathPayload

func (r *Request) AssertPathPayload(t Testing, path string, payload interface{}) *Request

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

func (*Request) AssertPathType

func (r *Request) AssertPathType(t Testing, path string, typ interface{}) *Request

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

func (*Request) AssertPayload

func (r *Request) AssertPayload(t Testing, payload interface{}) *Request

AssertPayload asserts that the request has the expected payload

func (*Request) AssertSubject

func (r *Request) AssertSubject(t Testing, subject string) *Request

AssertSubject asserts that the request has the expected subject

func (*Request) Equals

func (r *Request) Equals(t Testing, subject string, payload interface{}) *Request

Equals asserts that the request has the expected subject and payload

func (*Request) PathPayload

func (r *Request) PathPayload(t Testing, path string) interface{}

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

func (*Request) Respond

func (r *Request) Respond(data interface{})

Respond sends a low level response

func (*Request) RespondError

func (r *Request) RespondError(err *reserr.Error)

RespondError sends an error response

func (*Request) RespondRaw

func (r *Request) RespondRaw(out []byte)

RespondRaw sends a raw byte response

func (*Request) RespondResource added in v1.4.0

func (r *Request) RespondResource(rid string)

RespondResource sends a resource response

func (*Request) RespondSuccess

func (r *Request) RespondSuccess(result interface{})

RespondSuccess sends a success response

func (*Request) SendError

func (r *Request) SendError(err error)

SendError sends an error response

func (*Request) Timeout

func (r *Request) Timeout()

Timeout lets the request timeout

func (*Request) TryPathPayload added in v1.8.0

func (r *Request) TryPathPayload(t Testing, path string) (interface{}, bool)

TryPathPayload returns the request payload at a given dot-separated path in a nested object, and true, if it exist. Otherwise it returns nil and false. It gives a fatal error if any other part of the patch except for the last part of the path is missing. It gives a fatal error if the path doesn't exist.

type Session

type Session struct {
	*NATSTestClient

	*CountLogger
	// contains filtered or unexported fields
}

Session represents a test session with a resgate server

func (*Session) AssertUnsubscribe added in v1.8.0

func (s *Session) AssertUnsubscribe(rids ...string) *Session

AssertUnsubscribe awaits for one or more resources to be unsubscribed by the cache, and asserts that they match the provided resource IDs.

func (*Session) AwaitUnsubscribe added in v1.8.0

func (s *Session) AwaitUnsubscribe() string

AwaitUnsubscribe awaits for a resource to be unsubscribed by the cache, returning the resource ID.

func (*Session) Connect

func (s *Session) Connect() *Conn

Connect makes a new mock client websocket connection that handshakes with version v1.999.999.

func (*Session) ConnectWithChannel added in v1.1.0

func (s *Session) ConnectWithChannel(evs chan *ClientEvent) *Conn

ConnectWithChannel makes a new mock client websocket connection with a ClientEvent channel.

func (*Session) ConnectWithHeader added in v1.5.0

func (s *Session) ConnectWithHeader(h http.Header) *Conn

ConnectWithHeader makes a new mock client websocket connection using provided headers. It does not send a version handshake.

func (*Session) ConnectWithResponse added in v1.8.0

func (s *Session) ConnectWithResponse() (*Conn, *http.Response, error)

ConnectWithResponse makes a new mock client websocket connection that handshakes with version v1.999.999, if a connection is established. If an error occurs, it returns the error without handshake.

func (*Session) ConnectWithVersion added in v1.6.0

func (s *Session) ConnectWithVersion(version string) *Conn

ConnectWithVersion makes a new mock client websocket connection that handshakes with the version string provided.

func (*Session) ConnectWithoutVersion added in v1.4.0

func (s *Session) ConnectWithoutVersion() *Conn

ConnectWithoutVersion makes a new mock client websocket connection without any version handshake.

func (*Session) HTTPRequest

func (s *Session) HTTPRequest(method, url string, body []byte, opts ...func(r *http.Request)) *HTTPRequest

HTTPRequest sends a request over HTTP

func (*Session) IsBadHandshake added in v1.8.0

func (s *Session) IsBadHandshake(err error) bool

func (*Session) MetricsHTTPRequest added in v1.8.0

func (s *Session) MetricsHTTPRequest(opts ...func(r *http.Request)) *http.Response

MetricsHTTPRequest sends a request over HTTP to the metrics handler.

type Subscription

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

Subscription implements the mq.Unsubscriber interface.

func (*Subscription) Unsubscribe

func (s *Subscription) Unsubscribe() error

Unsubscribe removes the subscription.

type Testing added in v1.8.0

type Testing interface {
	Error(args ...interface{})
	Errorf(format string, args ...interface{})
	Fatal(args ...interface{})
	Fatalf(format string, args ...interface{})
}

Jump to

Keyboard shortcuts

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