testjuice

package
v1.34.0 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Description: This package contains helper functions to be used in tests.

Index

Constants

View Source
const (
	MongoIDKey  string = "_id"
	RequestMsg  string = "| Request 🌎  |"
	ResponseMsg string = "| Response 📮 |"
)

Variables

This section is empty.

Functions

func AssertResponse

func AssertResponse(expectedStatusCode int, resp *http.Response, responseBody []byte) error

AssertResponse is a function that checks if the status code of an HTTP response matches the expected status code.

It takes an expected status code, an HTTP response, and a response body as parameters.

If the status code of the response does not match the expected status code, it returns an error with a message indicating the expected and actual status codes and the response body.

If the status code of the response matches the expected status code, it returns nil.

func AssertStatuses added in v1.17.10

func AssertStatuses(acceptedStatuses []int, resp *http.Response, responseBody []byte) error

AssertStatuses is a function that checks if the status code of an HTTP response matches any of the accepted status codes.

It takes a slice of accepted status codes, an HTTP response, and a response body as parameters.

If the status code of the response matches any of the accepted status codes, it returns nil.

If the status code of the response does not match any of the accepted status codes, it returns an error with a message indicating the accepted status codes and the actual status code and response body.

func CheckErrorAndFail

func CheckErrorAndFail(t *testing.T, err error)

CheckErrorAndFail is a function that checks if an error is not nil and fails the test if it is.

It takes a testing.T and an error as parameters.

If the error is not nil, it calls t.Fatalf with the error message, causing the test to fail.

This function is intended to be used with a test level (global) error field.

func CheckErrorAndFailWithMessage

func CheckErrorAndFailWithMessage(t *testing.T, err error, debugMessage string)

CheckErrorAndFail is a function that checks if an error is not nil and fails the test if it is.

It takes a testing.T and an error as parameters.

If the error is not nil, it calls t.Fatalf with the error message, causing the test to fail.

This function is intended to be used with a test level (global) error field.

func LogTest added in v1.33.0

func LogTest(t *testing.T, responseBody []byte, method, URL, testMessage, testCaseName, collectionName, testDescription string) error

LogTest logs the request/response body in a formatted JSON.

It constructs a detailed log message containing information about the HTTP method, URL, and a formatted version of the response body. Returns an error if JSON formatting fails.

func NewMockPubSubPayload

func NewMockPubSubPayload(message *pubsub.Message, subscriptionMockValue string) (*bytes.Buffer, error)

EncodePubSubMessage encodes a Google Pub/Sub message to a bytes.Buffer to use it as a request body in tests.

func RetrieveObjectIDFromResponse added in v1.31.0

func RetrieveObjectIDFromResponse(t *testing.T, responseBody []byte, idField string, appendTo *[]primitive.ObjectID, bypass bool) error

RetrieveObjectIDFromResponse extracts primitive.ObjectIDs from a response body and append them to a slice for later use.

func RetrieveStringFromResponse added in v1.31.0

func RetrieveStringFromResponse(t *testing.T, responseBody []byte, stringField string, appendTo *[]string, bypass bool) error

RetrieveStringFromResponse extracts string values from a response body and appends them to a slice for later use.

func WaitForServer added in v1.32.0

func WaitForServer(url string, timeout time.Duration) error

WaitForServer waits for a server to be ready by trying to connect to the URL.

It will retry at intervals until the timeout expires or a response is received.

Types

type PubSubMessage

type PubSubMessage struct {
	Data        []byte            `json:"data,omitempty"`
	Attributes  map[string]string `json:"attributes,omitempty"`
	MessageID   string            `json:"messageId,omitempty"`
	PublishTime time.Time         `json:"publishTime,omitempty"`
}

PubSubMessage is the model for a Google Pub/Sub message.

type PubSubPayload

type PubSubPayload struct {
	Message      PubSubMessage `json:"message,omitempty"`
	Subscription string        `json:"subscription,omitempty"`
}

PubSubPayload is the model for a Google Pub/Sub payload.

Jump to

Keyboard shortcuts

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