Documentation
¶
Overview ¶
Package zentests provides utilities for testing Go Fiber applications.
Basic usage:
func TestEndpoint(t *testing.T) {
zt := zentests.New(t).Use(app)
zt.Get("/users").ExpectStatus(200)
}
Index ¶
- func SetHeader(key, value string) map[string]string
- type Response
- func (r *Response) Accepted() *Response
- func (r *Response) ArrayLength(path string, expected int) *Response
- func (r *Response) BadRequest() *Response
- func (r *Response) Body() []byte
- func (r *Response) BodyMatches(pattern string) *Response
- func (r *Response) BodyString() string
- func (r *Response) Contains(substring string) *Response
- func (r *Response) CookieHasValues(expected map[string]string) *Response
- func (r *Response) Created() *Response
- func (r *Response) Debug() *Response
- func (r *Response) Dump() string
- func (r *Response) Equals(expected string) *Response
- func (r *Response) Forbidden() *Response
- func (r *Response) Has(path string, expected any) *Response
- func (r *Response) HasBool(path string, expected bool) *Response
- func (r *Response) HasContentType(contentType string) *Response
- func (r *Response) HasFloat(path string, expected float64) *Response
- func (r *Response) HasHeader(key, value string) *Response
- func (r *Response) HasInt(path string, expected int) *Response
- func (r *Response) HasKey(path string) *Response
- func (r *Response) HasString(path, expected string) *Response
- func (r *Response) HeaderContains(key, substring string) *Response
- func (r *Response) HeaderHasValues(key string, values []string) *Response
- func (r *Response) HeaderNotPresent(key string) *Response
- func (r *Response) HeaderPresent(key string) *Response
- func (r *Response) IsCSS() *Response
- func (r *Response) IsEmpty() *Response
- func (r *Response) IsGIF() *Response
- func (r *Response) IsHTML() *Response
- func (r *Response) IsImage() *Response
- func (r *Response) IsJPEG() *Response
- func (r *Response) IsJS() *Response
- func (r *Response) IsJSON() *Response
- func (r *Response) IsNotNull(path string) *Response
- func (r *Response) IsNull(path string) *Response
- func (r *Response) IsPNG() *Response
- func (r *Response) IsPlainText() *Response
- func (r *Response) IsSVG() *Response
- func (r *Response) IsWebP() *Response
- func (r *Response) IsXHR() *Response
- func (r *Response) IsXML() *Response
- func (r *Response) JSON() *Response
- func (r *Response) JSONMatches(expected map[string]any) *Response
- func (r *Response) MatchesRegex(path, pattern string) *Response
- func (r *Response) NoContent() *Response
- func (r *Response) NotContains(substring string) *Response
- func (r *Response) NotFound() *Response
- func (r *Response) OK() *Response
- func (r *Response) ServerError() *Response
- func (r *Response) Status(expected int) *Response
- func (r *Response) Unauthorized() *Response
- func (r *Response) Unprocessable() *Response
- type SuiteContext
- type T
- func (zt *T) Delete(app *fiber.App, path string) *Response
- func (zt *T) DeleteJSON(app *fiber.App, path string, data any) *Response
- func (zt *T) DeleteJSONWithConfig(app *fiber.App, path string, data any, cfg fiber.TestConfig) *Response
- func (zt *T) DeleteWithConfig(app *fiber.App, path string, cfg fiber.TestConfig) *Response
- func (zt *T) Describe(name string, fn func(ctx *SuiteContext))
- func (zt *T) Get(app *fiber.App, path string) *Response
- func (zt *T) GetWithConfig(app *fiber.App, path string, cfg fiber.TestConfig) *Response
- func (zt *T) Patch(app *fiber.App, path string, body []byte) *Response
- func (zt *T) PatchJSON(app *fiber.App, path string, data any) *Response
- func (zt *T) PatchJSONWithConfig(app *fiber.App, path string, data any, cfg fiber.TestConfig) *Response
- func (zt *T) PatchWithConfig(app *fiber.App, path string, body []byte, cfg fiber.TestConfig) *Response
- func (zt *T) Post(app *fiber.App, path string, body []byte) *Response
- func (zt *T) PostForm(app *fiber.App, path string, data map[string]string) *Response
- func (zt *T) PostFormWithConfig(app *fiber.App, path string, data map[string]string, cfg fiber.TestConfig) *Response
- func (zt *T) PostJSON(app *fiber.App, path string, data any) *Response
- func (zt *T) PostJSONWithConfig(app *fiber.App, path string, data any, cfg fiber.TestConfig) *Response
- func (zt *T) PostWithConfig(app *fiber.App, path string, body []byte, cfg fiber.TestConfig) *Response
- func (zt *T) Put(app *fiber.App, path string, body []byte) *Response
- func (zt *T) PutJSON(app *fiber.App, path string, data any) *Response
- func (zt *T) PutJSONWithConfig(app *fiber.App, path string, data any, cfg fiber.TestConfig) *Response
- func (zt *T) PutWithConfig(app *fiber.App, path string, body []byte, cfg fiber.TestConfig) *Response
- func (zt *T) Use(app *fiber.App) *T
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SetHeader ¶
SetHeader creates a header map with a single key-value pair. This is a utility function to help construct header maps for custom headers when using raw body request methods.
Parameters:
- key: The header name
- value: The header value
Returns:
- map[string]string: A map containing the single header
Example:
headers := zentests.SetHeader("Authorization", "Bearer token123")
// Use with a method that accepts headers parameter
Types ¶
type Response ¶
type Response struct {
Header http.Header
StatusCode int
// contains filtered or unexported fields
}
Response wraps an HTTP response with fluent assertion methods. It provides a chainable API for making assertions on HTTP responses including status codes, headers, body content, and JSON data. The response body is lazily loaded when first accessed to avoid unnecessary reads.
Response maintains internal state including the parsed JSON body and tracks whether the body has been read to prevent multiple reads from the response stream.
Fields:
- t: The testing.T instance for making assertions
- StatusCode: The HTTP status code from the response
- Header: The HTTP headers from the response
- resp: The underlying http.Response
- body: The cached response body bytes (lazy loaded)
- bodyRead: Flag indicating if body has been read
- parsedJSON: The parsed JSON body as a map (cached after first parse)
func (*Response) Accepted ¶
Accepted asserts that the HTTP status code is 202 (Accepted). Convenience method equivalent to Status(202). Indicates the request has been accepted for processing but not completed.
Returns:
- *Response: The receiver for method chaining
Example:
zt.PostJSON(app, "/jobs", jobData).Accepted()
func (*Response) ArrayLength ¶
ArrayLength asserts that the JSON key is an array with the expected length.
Parameters:
- path: The JSON key path using dot notation
- expected: The expected array length
Returns:
- *Response: The receiver for method chaining
Example:
zt.Get(app, "/api/users").ArrayLength("data.users", 10)
func (*Response) BadRequest ¶
BadRequest asserts that the HTTP status code is 400 (Bad Request). Convenience method equivalent to Status(400). Indicates the server cannot process the request due to client error.
Returns:
- *Response: The receiver for method chaining
Example:
zt.PostJSON(app, "/users", invalidData).BadRequest()
func (*Response) Body ¶
Body returns the response body as bytes. The body is lazily loaded on first call and cached for subsequent accesses. This prevents multiple reads from the response stream which would return empty.
Returns:
- []byte: The response body as bytes
Example:
body := resp.Body() fmt.Println(string(body))
func (*Response) BodyMatches ¶
BodyMatches asserts that the response body matches the regex pattern. Useful for validating dynamic content or complex text patterns.
Parameters:
- pattern: The regex pattern to match against the body
Returns:
- *Response: The receiver for method chaining
Example:
zt.Get(app, "/users/1").BodyMatches(`User ID: \d+`)
func (*Response) BodyString ¶
BodyString returns the response body as a string. Convenience method that converts Body() bytes to string.
Returns:
- string: The response body as a string
Example:
bodyStr := resp.BodyString() assert.Contains(t, bodyStr, "success")
func (*Response) Contains ¶
Contains asserts that the response body contains the specified substring. Case-sensitive substring search in the response body.
Parameters:
- substring: The substring to search for in the body
Returns:
- *Response: The receiver for method chaining
Example:
zt.Get(app, "/page").Contains("Welcome")
func (*Response) CookieHasValues ¶
CookieHasValues asserts that response cookies match the expected values. Parses all Set-Cookie headers and verifies each cookie name-value pair.
Parameters:
- expected: Map of cookie names to expected values
Returns:
- *Response: The receiver for method chaining
Example:
zt.Get(app, "/settings").CookieHasValues(map[string]string{
"lang": "en",
"theme": "dark",
})
func (*Response) Created ¶
Created asserts that the HTTP status code is 201 (Created). Convenience method equivalent to Status(201). Typically used after POST requests that create new resources.
Returns:
- *Response: The receiver for method chaining
Example:
zt.PostJSON(app, "/users", data).Created()
func (*Response) Debug ¶
Debug prints detailed response information to stdout for troubleshooting. Outputs status code, headers, body, and parsed JSON (if available). This is useful during test development to inspect response details. Returns the receiver for method chaining.
Returns:
- *Response: The receiver for method chaining
Example:
zt.Get(app, "/users").Debug().OK()
func (*Response) Dump ¶
Dump returns raw response details as a formatted string. Useful for custom logging or including response details in test failure messages. Unlike Debug(), this returns a string instead of printing to stdout.
Returns:
- string: Formatted string with status, headers, and body
Example:
details := resp.Dump()
t.Logf("Response details: %s", details)
func (*Response) Equals ¶
Equals asserts that the response body equals the exact expected string. Performs an exact string comparison of the entire body.
Parameters:
- expected: The expected body content
Returns:
- *Response: The receiver for method chaining
Example:
zt.Get(app, "/ping").Equals("pong")
func (*Response) Forbidden ¶
Forbidden asserts that the HTTP status code is 403 (Forbidden). Convenience method equivalent to Status(403). Indicates the server understood the request but refuses to authorize it.
Returns:
- *Response: The receiver for method chaining
Example:
zt.Get(app, "/admin").Forbidden()
func (*Response) Has ¶
Has asserts that the JSON key equals the expected value with strict type checking. Supports dot notation for nested keys. Fails if the key doesn't exist or if the type doesn't match exactly.
Parameters:
- path: The JSON key path using dot notation
- expected: The expected value (type must match exactly)
Returns:
- *Response: The receiver for method chaining
Example:
zt.Get(app, "/api/users/1").Has("data.user.name", "John")
zt.Get(app, "/api/count").Has("data.count", float64(42)) // JSON numbers are float64
func (*Response) HasBool ¶
HasBool asserts that the JSON key equals the expected boolean value.
Parameters:
- path: The JSON key path using dot notation
- expected: The expected boolean value
Returns:
- *Response: The receiver for method chaining
Example:
zt.Get(app, "/api/status").HasBool("data.active", true)
func (*Response) HasContentType ¶
HasContentType asserts that the Content-Type header equals the expected value. Performs an exact match on the Content-Type header.
Parameters:
- contentType: The expected Content-Type value (e.g., "application/json")
Returns:
- *Response: The receiver for method chaining
Example:
zt.Get(app, "/page").HasContentType("text/html; charset=utf-8")
func (*Response) HasFloat ¶
HasFloat asserts that the JSON key equals the expected float64 value. Uses InDelta for comparison to handle floating point precision issues.
Parameters:
- path: The JSON key path using dot notation
- expected: The expected float64 value
Returns:
- *Response: The receiver for method chaining
Example:
zt.Get(app, "/api/score").HasFloat("data.score", 95.5)
func (*Response) HasHeader ¶
HasHeader asserts that a header exists and has the expected value. Performs an exact match comparison. Header names are case-insensitive.
Parameters:
- key: The header name (e.g., "Content-Type", "X-Custom-Header")
- value: The expected header value
Returns:
- *Response: The receiver for method chaining
Example:
zt.Get(app, "/api/users").HasHeader("Content-Type", "application/json")
zt.Get(app, "/api/users").HasHeader("Content-Type", "application/css") // raises an error
func (*Response) HasInt ¶
HasInt asserts that the JSON key equals the expected integer value. Handles JSON number conversion (JSON numbers are parsed as float64).
Parameters:
- path: The JSON key path using dot notation
- expected: The expected integer value
Returns:
- *Response: The receiver for method chaining
Example:
zt.Get(app, "/api/count").HasInt("data.total", 42)
func (*Response) HasKey ¶
HasKey asserts that the JSON response contains the specified key path. Supports dot notation for nested keys and array indices (e.g., "users.0.name").
Parameters:
- path: The JSON key path using dot notation (e.g., "user.email", "items.0.id")
Returns:
- *Response: The receiver for method chaining
Example:
zt.Get(app, "/api/users/1").HasKey("data.user.name")
func (*Response) HasString ¶
HasString asserts that the JSON key equals the expected string value.
Parameters:
- path: The JSON key path using dot notation
- expected: The expected string value
Returns:
- *Response: The receiver for method chaining
Example:
zt.Get(app, "/api/users/1").HasString("data.user.email", "john@example.com")
func (*Response) HeaderContains ¶
HeaderContains asserts that a header value contains the expected substring. Useful for flexible checks like verifying "application/json" is in Content-Type without matching the entire header value including charset.
Parameters:
- key: The header name
- substring: The substring that must be present in the header value
Returns:
- *Response: The receiver for method chaining
Example:
zt.Get(app, "/api/users").HeaderContains("Content-Type", "json")
zt.Get(app, "/api/users").HeaderContains("Content-Type", "image") // raises an error
func (*Response) HeaderHasValues ¶
HeaderHasValues asserts that a header has exactly the expected values. For headers that can have multiple values (e.g., Set-Cookie, X-Features). Order of values does not matter.
Parameters:
- key: The header name
- values: Slice of expected header values
Returns:
- *Response: The receiver for method chaining
Example:
zt.Get(app, "/features").HeaderHasValues("X-Features", []string{"audio", "video"})
func (*Response) HeaderNotPresent ¶
HeaderNotPresent asserts that a header is not present or is empty. Useful for security checks to ensure sensitive headers are not exposed.
Parameters:
- key: The header name
Returns:
- *Response: The receiver for method chaining
Example:
zt.Get(app, "/public").HeaderNotPresent("Authorization")
func (*Response) HeaderPresent ¶
HeaderPresent asserts that a header is present and non-empty. Checks for the existence of a header without validating its value.
Parameters:
- key: The header name
Returns:
- *Response: The receiver for method chaining
Example:
zt.Get(app, "/secure").HeaderPresent("X-Auth-Token")
func (*Response) IsCSS ¶
IsCSS asserts that the Content-Type header contains "text/css". Quick check for CSS stylesheet responses.
Returns:
- *Response: The receiver for method chaining
Example:
zt.Get(app, "/style.css").OK().IsCSS()
func (*Response) IsEmpty ¶
IsEmpty asserts that the response body is empty. Checks that the body has zero length.
Returns:
- *Response: The receiver for method chaining
Example:
zt.Delete(app, "/users/1").NoContent().IsEmpty()
func (*Response) IsGIF ¶
IsGIF asserts that the Content-Type header contains "image/gif". Quick check for GIF image responses.
Returns:
- *Response: The receiver for method chaining
Example:
zt.Get(app, "/animation.gif").OK().IsGIF()
func (*Response) IsHTML ¶
IsHTML asserts that the Content-Type header contains "text/html". Quick check for HTML page responses. Case-sensitive substring match.
Returns:
- *Response: The receiver for method chaining
Example:
zt.Get(app, "/").OK().IsHTML()
func (*Response) IsImage ¶
IsImage asserts that the Content-Type header contains "image/". General check for any image type responses.
Returns:
- *Response: The receiver for method chaining
Example:
zt.Get(app, "/photo.jpg").OK().IsImage()
func (*Response) IsJPEG ¶
IsJPEG asserts that the Content-Type header contains "image/jpeg". Quick check for JPEG image responses.
Returns:
- *Response: The receiver for method chaining
Example:
zt.Get(app, "/photo.jpg").OK().IsJPEG()
func (*Response) IsJS ¶
IsJS asserts that the Content-Type header contains "javascript". Matches both "application/javascript" and "text/javascript". Quick check for JavaScript responses.
Returns:
- *Response: The receiver for method chaining
Example:
zt.Get(app, "/app.js").OK().IsJS()
func (*Response) IsJSON ¶
IsJSON asserts that the Content-Type header contains "application/json". Quick check for JSON API responses. Case-sensitive substring match.
Returns:
- *Response: The receiver for method chaining
Example:
zt.Get(app, "/api/users").OK().IsJSON()
func (*Response) IsNotNull ¶
IsNotNull asserts that the JSON key has a non-null value.
Parameters:
- path: The JSON key path using dot notation
Returns:
- *Response: The receiver for method chaining
Example:
zt.Get(app, "/api/users/1").IsNotNull("data.user")
func (*Response) IsNull ¶
IsNull asserts that the JSON key has a null value.
Parameters:
- path: The JSON key path using dot notation
Returns:
- *Response: The receiver for method chaining
Example:
zt.Get(app, "/api/users/999").IsNull("data.user")
func (*Response) IsPNG ¶
IsPNG asserts that the Content-Type header contains "image/png". Quick check for PNG image responses.
Returns:
- *Response: The receiver for method chaining
Example:
zt.Get(app, "/logo.png").OK().IsPNG()
func (*Response) IsPlainText ¶
IsPlainText asserts that the Content-Type header contains "text/plain". Quick check for plain text responses. Case-sensitive substring match.
Returns:
- *Response: The receiver for method chaining
Example:
zt.Get(app, "/health").OK().IsPlainText()
func (*Response) IsSVG ¶
IsSVG asserts that the Content-Type header contains "image/svg+xml". Quick check for SVG image responses.
Returns:
- *Response: The receiver for method chaining
Example:
zt.Get(app, "/icon.svg").OK().IsSVG()
func (*Response) IsWebP ¶
IsWebP asserts that the Content-Type header contains "image/webp". Quick check for WebP image responses.
Returns:
- *Response: The receiver for method chaining
Example:
zt.Get(app, "/photo.webp").OK().IsWebP()
func (*Response) IsXHR ¶
IsXHR asserts that the X-Requested-With header contains "XMLHttpRequest". Useful for detecting AJAX requests in responses.
Returns:
- *Response: The receiver for method chaining
Example:
zt.Get(app, "/api/users").OK().IsXHR()
func (*Response) IsXML ¶
IsXML asserts that the Content-Type header contains "xml". Matches both "application/xml" and "text/xml". Quick check for XML responses.
Returns:
- *Response: The receiver for method chaining
Example:
zt.Get(app, "/api/data.xml").OK().IsXML()
func (*Response) JSON ¶
JSON parses and caches the response body as JSON. Enables JSON-specific assertions on the response. The parsed JSON is cached to avoid re-parsing on subsequent JSON assertions. Fails the test if JSON parsing fails.
Returns:
- *Response: The receiver for method chaining
Example:
zt.Get(app, "/api/users").JSON().Has("data.0.name", "John")
func (*Response) JSONMatches ¶
JSONMatches asserts that the entire JSON structure matches the expected map. Performs Has() assertions for each key-value pair in the expected map.
Parameters:
- expected: Map of key paths to expected values
Returns:
- *Response: The receiver for method chaining
Example:
zt.Get(app, "/api/users/1").JSONMatches(map[string]interface{}{
"data.user.name": "John",
"data.user.active": true,
})
func (*Response) MatchesRegex ¶
MatchesRegex asserts that the JSON key value matches the regex pattern. The value at the specified path must be a string.
Parameters:
- path: The JSON key path using dot notation
- pattern: The regex pattern to match
Returns:
- *Response: The receiver for method chaining
Example:
zt.Get(app, "/api/users/1").MatchesRegex("data.user.email", `^[\w.-]+@[\w.-]+\.\w+$`)
func (*Response) NoContent ¶
NoContent asserts that the HTTP status code is 204 (No Content). Convenience method equivalent to Status(204). Typically used for DELETE operations or successful operations with no response body.
Returns:
- *Response: The receiver for method chaining
Example:
zt.Delete(app, "/users/1").NoContent()
func (*Response) NotContains ¶
NotContains asserts that the response body does not contain the specified substring. Useful for ensuring sensitive data is not present in responses.
Parameters:
- substring: The substring that should not be in the body
Returns:
- *Response: The receiver for method chaining
Example:
zt.Get(app, "/public").NotContains("password")
func (*Response) NotFound ¶
NotFound asserts that the HTTP status code is 404 (Not Found). Convenience method equivalent to Status(404). Indicates the requested resource was not found.
Returns:
- *Response: The receiver for method chaining
Example:
zt.Get(app, "/users/999").NotFound()
func (*Response) OK ¶
OK asserts that the HTTP status code is 200 (OK). Convenience method equivalent to Status(200).
Returns:
- *Response: The receiver for method chaining
Example:
zt.Get(app, "/users").OK()
func (*Response) ServerError ¶
ServerError asserts that the HTTP status code is 500 (Internal Server Error). Convenience method equivalent to Status(500). Indicates an unexpected condition prevented the server from fulfilling the request.
Returns:
- *Response: The receiver for method chaining
Example:
zt.Get(app, "/broken").ServerError()
func (*Response) Status ¶
Status asserts that the HTTP status code equals the expected value. This is the base status assertion method used by all other status shortcuts.
Parameters:
- expected: The expected HTTP status code (e.g., 200, 404, 500)
Returns:
- *Response: The receiver for method chaining
Example:
zt.Get(app, "/users").Status(200) zt.Get(app, "/notfound").Status(404)
func (*Response) Unauthorized ¶
Unauthorized asserts that the HTTP status code is 401 (Unauthorized). Convenience method equivalent to Status(401). Indicates authentication is required or has failed.
Returns:
- *Response: The receiver for method chaining
Example:
zt.Get(app, "/protected").Unauthorized()
func (*Response) Unprocessable ¶
Unprocessable asserts that the HTTP status code is 422 (Unprocessable Entity). Convenience method equivalent to Status(422). Indicates the request was well-formed but contains semantic errors.
Returns:
- *Response: The receiver for method chaining
Example:
zt.PostJSON(app, "/users", invalidUserData).Unprocessable()
type SuiteContext ¶
type SuiteContext struct {
// contains filtered or unexported fields
}
SuiteContext holds hooks and state for a BDD-style describe block. Provides lifecycle hooks (BeforeEach, AfterEach) for test setup and teardown. Each describe block gets its own SuiteContext to manage test isolation.
Fields:
- t: The testing.T instance for running subtests
- beforeEach: Function to run before each test case in the suite
- afterEach: Function to run after each test case in the suite
func (*SuiteContext) AfterEach ¶
func (ctx *SuiteContext) AfterEach(fn func(*T)) *SuiteContext
AfterEach sets the teardown hook function to run after each test case. Use this for cleanup like closing database connections or shutting down apps. Returns the SuiteContext for method chaining.
Parameters:
- fn: Function to execute after each test case, receives *T
Returns:
- *SuiteContext: The receiver for method chaining
Example:
ctx.AfterEach(func(zt *zentests.T) {
app.Shutdown()
db.Cleanup()
})
func (*SuiteContext) BeforeEach ¶
func (ctx *SuiteContext) BeforeEach(fn func(*T)) *SuiteContext
BeforeEach sets the setup hook function to run before each test case. Use this for common setup like creating fresh app instances or test data. Returns the SuiteContext for method chaining with AfterEach.
Parameters:
- fn: Function to execute before each test case, receives *T
Returns:
- *SuiteContext: The receiver for method chaining
Example:
ctx.BeforeEach(func(zt *zentests.T) {
app = fiber.New()
SetupRoutes(app)
}).AfterEach(func(zt *zentests.T) {
app.Shutdown()
})
func (*SuiteContext) It ¶
func (ctx *SuiteContext) It(name string, fn func(*T))
It runs a test case within a describe block. Individual test cases that execute with BeforeEach/AfterEach hooks. Each It() call creates a sub-test that runs independently.
Parameters:
- name: The descriptive name for the test case (e.g., "returns user list")
- fn: The test function receiving a *T for making requests
Example:
ctx.It("returns user list", func(zt *zentests.T) {
zt.Get(app, "/users").OK().IsJSON()
})
type T ¶
T wraps testing.T with additional context for testing Fiber applications. It provides a fluent API for making HTTP requests against a Fiber app in tests. T maintains a reference to the Fiber app and testing context to enable chainable assertions.
Basic usage:
func TestEndpoint(t *testing.T) {
zt := zentests.New(t).Use(app)
zt.Get("/users").ExpectStatus(200)
}
Fields:
- T: The embedded testing.T instance for standard Go test functionality
- app: The Fiber application instance for making HTTP requests
func New ¶
New creates a new zentests context wrapping the provided testing.T. This is the main entry point for using zentests. The returned *T has no app set initially - call Use() to set the Fiber application for method chaining, or pass the app directly to request methods like Get(app, "/").
Parameters:
- t: The testing.T instance from your test function
Returns:
- *T: A new zentests context with no Fiber app configured
Example:
func TestExample(t *testing.T) {
zt := zentests.New(t)
app := fiber.New()
// declare app routes
zt.Use(app)
}
func TestExample(t *testing.T) {
zt := zentests.New(t)
app := fiber.New()
// declare app routes
zt.Get(app, "/").OK()
}
func (*T) Delete ¶
Delete performs a DELETE request. Returns a *Response for chaining assertions. Use this for testing DELETE endpoints.
Parameters:
- app: The Fiber application to test against
- path: The request path (e.g., "/api/users/1")
Returns:
- *Response: A Response wrapper for making assertions
Example:
zt.Delete(app, "/users/1").NoContent()
func (*T) DeleteJSON ¶
DeleteJSON performs a DELETE request with a JSON body. Some APIs require a request body for DELETE operations (e.g., bulk delete with IDs). Automatically marshals the data to JSON and sets Content-Type to application/json.
Parameters:
- app: The Fiber application to test against
- path: The request path (e.g., "/api/users/bulk")
- data: Any data structure to be JSON-encoded
Returns:
- *Response: A Response wrapper for making assertions
Example:
data := map[string][]int{"ids": {1, 2, 3}}
zt.DeleteJSON(app, "/users/bulk", data).OK()
func (*T) DeleteJSONWithConfig ¶ added in v0.2.0
func (zt *T) DeleteJSONWithConfig(app *fiber.App, path string, data any, cfg fiber.TestConfig) *Response
DeleteJSONWithConfig performs a DELETE request with JSON body and custom TestConfig.
Parameters:
- app: The Fiber application to test against
- path: The request path (e.g., "/api/users/bulk")
- data: Any data structure to be JSON-encoded
- cfg: TestConfig for custom timeout/behavior settings
Returns:
- *Response: A Response wrapper for making assertions
func (*T) DeleteWithConfig ¶ added in v0.2.0
DeleteWithConfig performs a DELETE request with custom TestConfig.
Parameters:
- app: The Fiber application to test against
- path: The request path (e.g., "/api/users/1")
- cfg: TestConfig for custom timeout/behavior settings
Returns:
- *Response: A Response wrapper for making assertions
func (*T) Describe ¶
func (zt *T) Describe(name string, fn func(ctx *SuiteContext))
Describe creates a BDD-style test group with lifecycle hooks. Organizes related tests under a descriptive name with optional setup/teardown. Similar to describe/it blocks in testing frameworks like RSpec or Mocha.
Parameters:
- name: The descriptive name for the test group (e.g., "User API", "Auth Service")
- fn: Function that receives the SuiteContext and defines test cases using It()
Example:
zt.Describe("User API", func(ctx *zentests.SuiteContext) {
ctx.BeforeEach(func(zt *zentests.T) {
// Setup fresh app before each test
app = setupTestApp()
})
ctx.It("creates a user", func(zt *zentests.T) {
zt.PostJSON(app, "/users", data).Created()
})
})
func (*T) Get ¶
Get performs a GET request against the Fiber app. Returns a *Response for chaining assertions. Use this for testing GET endpoints.
Parameters:
- app: The Fiber application to test against
- path: The request path (e.g., "/api/users")
Returns:
- *Response: A Response wrapper for making assertions
Example:
zt := zentests.New(t)
resp := zt.Get(app, "/users")
resp.OK().IsJSON().Has("data.users.0.name", "John")
func (*T) GetWithConfig ¶ added in v0.2.0
GetWithConfig performs a GET request with custom TestConfig for timeout/behavior control. Use this when you need to customize test behavior beyond defaults.
Parameters:
- app: The Fiber application to test against
- path: The request path (e.g., "/api/users")
- cfg: TestConfig for custom timeout/behavior settings
Returns:
- *Response: A Response wrapper for making assertions
Example:
zt.GetWithConfig(app, "/slow-endpoint", fiber.TestConfig{
Timeout: 5 * time.Second,
}).OK()
func (*T) Patch ¶
Patch performs a PATCH request with a raw body. Use PatchJSON for JSON payloads. PATCH is typically used for partial resource updates.
Parameters:
- app: The Fiber application to test against
- path: The request path (e.g., "/api/users/1")
- body: The raw request body as bytes
Returns:
- *Response: A Response wrapper for making assertions
Example:
body := []byte(`{"status": "active"}`)
zt.Patch(app, "/users/1", body).OK()
func (*T) PatchJSON ¶
PatchJSON performs a PATCH request with JSON-encoded data. Automatically marshals the data to JSON and sets Content-Type to application/json. Fails the test if JSON marshaling fails. PATCH is typically used for partial updates.
Parameters:
- app: The Fiber application to test against
- path: The request path (e.g., "/api/users/1")
- data: Any data structure to be JSON-encoded
Returns:
- *Response: A Response wrapper for making assertions
Example:
data := map[string]string{"status": "active"}
zt.PatchJSON(app, "/users/1", data).OK()
func (*T) PatchJSONWithConfig ¶ added in v0.2.0
func (zt *T) PatchJSONWithConfig(app *fiber.App, path string, data any, cfg fiber.TestConfig) *Response
PatchJSONWithConfig performs a PATCH request with JSON data and custom TestConfig.
Parameters:
- app: The Fiber application to test against
- path: The request path (e.g., "/api/users/1")
- data: Any data structure to be JSON-encoded
- cfg: TestConfig for custom timeout/behavior settings
Returns:
- *Response: A Response wrapper for making assertions
func (*T) PatchWithConfig ¶ added in v0.2.0
func (zt *T) PatchWithConfig(app *fiber.App, path string, body []byte, cfg fiber.TestConfig) *Response
PatchWithConfig performs a PATCH request with raw body and custom TestConfig.
Parameters:
- app: The Fiber application to test against
- path: The request path (e.g., "/api/users/1")
- body: The raw request body as bytes
- cfg: TestConfig for custom timeout/behavior settings
Returns:
- *Response: A Response wrapper for making assertions
func (*T) Post ¶
Post performs a POST request with a raw body. Use PostJSON for JSON payloads or PostForm for form data. This method is suitable for sending raw bytes when you need full control over the request body.
Parameters:
- app: The Fiber application to test against
- path: The request path (e.g., "/api/users")
- body: The raw request body as bytes
Returns:
- *Response: A Response wrapper for making assertions
Example:
body := []byte("raw data")
zt.Post(app, "/upload", body).OK()
func (*T) PostForm ¶
PostForm performs a POST request with form data. Automatically encodes the data as application/x-www-form-urlencoded. Useful for testing HTML form submissions or traditional form endpoints.
Parameters:
- app: The Fiber application to test against
- path: The request path (e.g., "/login")
- data: Map of form field names to values
Returns:
- *Response: A Response wrapper for making assertions
Example:
data := map[string]string{"username": "john", "password": "secret"}
zt.PostForm(app, "/login", data).OK()
func (*T) PostFormWithConfig ¶ added in v0.2.0
func (zt *T) PostFormWithConfig(app *fiber.App, path string, data map[string]string, cfg fiber.TestConfig) *Response
PostFormWithConfig performs a POST request with form data and custom TestConfig.
Parameters:
- app: The Fiber application to test against
- path: The request path (e.g., "/login")
- data: Map of form field names to values
- cfg: TestConfig for custom timeout/behavior settings
Returns:
- *Response: A Response wrapper for making assertions
Example:
zt.PostFormWithConfig(app, "/login", data, fiber.TestConfig{
Timeout: 5 * time.Second,
}).OK()
func (*T) PostJSON ¶
PostJSON performs a POST request with JSON-encoded data. Automatically marshals the data to JSON and sets Content-Type to application/json. Fails the test if JSON marshaling fails.
Parameters:
- app: The Fiber application to test against
- path: The request path (e.g., "/api/users")
- data: Any data structure to be JSON-encoded (maps, structs, etc.)
Returns:
- *Response: A Response wrapper for making assertions
Example:
data := map[string]string{"name": "John", "email": "john@example.com"}
zt.PostJSON(app, "/users", data).Created().Has("id", float64(1))
func (*T) PostJSONWithConfig ¶ added in v0.2.0
func (zt *T) PostJSONWithConfig(app *fiber.App, path string, data any, cfg fiber.TestConfig) *Response
PostJSONWithConfig performs a POST request with JSON data and custom TestConfig.
Parameters:
- app: The Fiber application to test against
- path: The request path (e.g., "/api/users")
- data: Any data structure to be JSON-encoded
- cfg: TestConfig for custom timeout/behavior settings
Returns:
- *Response: A Response wrapper for making assertions
Example:
zt.PostJSONWithConfig(app, "/users", data, fiber.TestConfig{
Timeout: 5 * time.Second,
FailOnTimeout: true,
}).Created()
func (*T) PostWithConfig ¶ added in v0.2.0
func (zt *T) PostWithConfig(app *fiber.App, path string, body []byte, cfg fiber.TestConfig) *Response
PostWithConfig performs a POST request with raw body and custom TestConfig.
Parameters:
- app: The Fiber application to test against
- path: The request path (e.g., "/api/users")
- body: The raw request body as bytes
- cfg: TestConfig for custom timeout/behavior settings
Returns:
- *Response: A Response wrapper for making assertions
Example:
zt.PostWithConfig(app, "/upload", body, fiber.TestConfig{
Timeout: 10 * time.Second,
}).OK()
func (*T) Put ¶
Put performs a PUT request with a raw body. Use PutJSON for JSON payloads. PUT is typically used for full resource updates.
Parameters:
- app: The Fiber application to test against
- path: The request path (e.g., "/api/users/1")
- body: The raw request body as bytes
Returns:
- *Response: A Response wrapper for making assertions
Example:
body := []byte(`{"name": "Updated Name"}`)
zt.Put(app, "/users/1", body).OK()
func (*T) PutJSON ¶
PutJSON performs a PUT request with JSON-encoded data. Automatically marshals the data to JSON and sets Content-Type to application/json. Fails the test if JSON marshaling fails. PUT is typically used for full resource updates.
Parameters:
- app: The Fiber application to test against
- path: The request path (e.g., "/api/users/1")
- data: Any data structure to be JSON-encoded
Returns:
- *Response: A Response wrapper for making assertions
Example:
data := map[string]string{"name": "Updated Name"}
zt.PutJSON(app, "/users/1", data).OK()
func (*T) PutJSONWithConfig ¶ added in v0.2.0
func (zt *T) PutJSONWithConfig(app *fiber.App, path string, data any, cfg fiber.TestConfig) *Response
PutJSONWithConfig performs a PUT request with JSON data and custom TestConfig.
Parameters:
- app: The Fiber application to test against
- path: The request path (e.g., "/api/users/1")
- data: Any data structure to be JSON-encoded
- cfg: TestConfig for custom timeout/behavior settings
Returns:
- *Response: A Response wrapper for making assertions
func (*T) PutWithConfig ¶ added in v0.2.0
func (zt *T) PutWithConfig(app *fiber.App, path string, body []byte, cfg fiber.TestConfig) *Response
PutWithConfig performs a PUT request with raw body and custom TestConfig.
Parameters:
- app: The Fiber application to test against
- path: The request path (e.g., "/api/users/1")
- body: The raw request body as bytes
- cfg: TestConfig for custom timeout/behavior settings
Returns:
- *Response: A Response wrapper for making assertions
func (*T) Use ¶
Use sets the Fiber app for making test requests and enables fluent method chaining. After calling Use(), subsequent request methods can be called without passing the app parameter.
Parameters:
- app: The Fiber application instance to test against
Returns:
- *T: The receiver for method chaining
TODO: Add nil check for app parameter to prevent panics
Example:
zt := zentests.New(t).Use(app)
zt.Get("/users").OK()