tests

package
v0.18.7 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ApiScenario

type ApiScenario struct {
	// Name is the test name.
	Name string

	// Method is the HTTP method of the test request to use.
	Method string

	// URL is the url/path of the endpoint you want to test.
	URL string

	// Body specifies the body to send with the request.
	//
	// For example:
	//
	//	strings.NewReader(`{"title":"abc"}`)
	Body io.Reader

	// Headers specifies the headers to send with the request (e.g. "Authorization": "abc")
	Headers map[string]string

	// Delay adds a delay before checking the expectations usually
	// to ensure that all fired non-awaited go routines have finished
	Delay time.Duration

	// Timeout specifies how long to wait before cancelling the request context.
	//
	// A zero or negative value means that there will be no timeout.
	Timeout time.Duration

	// ExpectedStatus specifies the expected response HTTP status code.
	ExpectedStatus int

	// List of keywords that MUST exist in the response body.
	//
	// Either ExpectedContent or NotExpectedContent must be set if the response body is non-empty.
	// Leave both fields empty if you want to ensure that the response didn't have any body (e.g. 204).
	ExpectedContent []string

	// List of keywords that MUST NOT exist in the response body.
	//
	// Either ExpectedContent or NotExpectedContent must be set if the response body is non-empty.
	// Leave both fields empty if you want to ensure that the response didn't have any body (e.g. 204).
	NotExpectedContent []string

	// List of hook events to check whether they were fired or not.
	//
	// You can use the wildcard "*" event key if you want to ensure
	// that no other hook events except those listed have been fired.
	//
	// For example:
	//
	//	map[string]int{ "*": 0 } // no hook events were fired
	//	map[string]int{ "*": 0, "EventA": 2 } // no hook events, except EventA were fired
	//	map[string]int{ "EventA": 2, "EventB": 0 } // ensures that EventA was fired exactly 2 times and EventB exactly 0 times.
	ExpectedEvents map[string]int

	TestAppFactory func(t testing.TB) *pbtests.TestApp
	BeforeTestFunc func(t testing.TB, app *pbtests.TestApp, e *core.ServeEvent)
	AfterTestFunc  func(t testing.TB, app *pbtests.TestApp, res *http.Response)
}

ApiScenario defines a single api request test case/scenario.

func (*ApiScenario) Benchmark

func (scenario *ApiScenario) Benchmark(b *testing.B)

Benchmark benchmarks the test scenario.

Example:

func BenchmarkListExample(b *testing.B) {
    scenario := tests.ApiScenario{
        Name:           "list example collection",
        Method:         http.MethodGet,
        URL:            "/api/collections/example/records",
        ExpectedStatus: 200,
        ExpectedContent: []string{
            `"totalItems":3`,
            `"id":"0yxhwia2amd8gec"`,
            `"id":"achvryl401bhse3"`,
            `"id":"llvuca81nly1qls"`,
        },
        ExpectedEvents: map[string]int{
            "OnRecordsListRequest": 1,
            "OnRecordEnrich":       3,
        },
    }

    scenario.Benchmark(b)
}

func (*ApiScenario) Test

func (scenario *ApiScenario) Test(t *testing.T)

Test executes the test scenario.

Example:

func TestListExample(t *testing.T) {
    scenario := tests.ApiScenario{
        Name:           "list example collection",
        Method:         http.MethodGet,
        URL:            "/api/collections/example/records",
        ExpectedStatus: 200,
        ExpectedContent: []string{
            `"totalItems":3`,
            `"id":"0yxhwia2amd8gec"`,
            `"id":"achvryl401bhse3"`,
            `"id":"llvuca81nly1qls"`,
        },
        ExpectedEvents: map[string]int{
            "OnRecordsListRequest": 1,
            "OnRecordEnrich":       3,
        },
    }

    scenario.Test(t)
}

Jump to

Keyboard shortcuts

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