Home / Function/ Test_CustomSuccessAndFailureHandlers() — fiber Function Reference

Test_CustomSuccessAndFailureHandlers() — fiber Function Reference

Architecture documentation for the Test_CustomSuccessAndFailureHandlers() function in keyauth_test.go from the fiber codebase.

Entity Profile

Dependency Diagram

graph TD
  9279279a_4ade_6e00_3acd_54de9c5ec0a8["Test_CustomSuccessAndFailureHandlers()"]
  71f55784_a001_0646_0ce7_7ad97067c49c["keyauth_test.go"]
  9279279a_4ade_6e00_3acd_54de9c5ec0a8 -->|defined in| 71f55784_a001_0646_0ce7_7ad97067c49c
  style 9279279a_4ade_6e00_3acd_54de9c5ec0a8 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

middleware/keyauth/keyauth_test.go lines 389–440

func Test_CustomSuccessAndFailureHandlers(t *testing.T) {
	app := fiber.New()

	app.Use(New(Config{
		SuccessHandler: func(c fiber.Ctx) error {
			return c.Status(fiber.StatusOK).SendString("API key is valid and request was handled by custom success handler")
		},
		ErrorHandler: func(c fiber.Ctx, _ error) error {
			return c.Status(fiber.StatusUnauthorized).SendString("API key is invalid and request was handled by custom error handler")
		},
		Validator: func(_ fiber.Ctx, key string) (bool, error) {
			if key == CorrectKey {
				return true, nil
			}
			return false, ErrMissingOrMalformedAPIKey
		},
	}))

	// Define a test handler that should not be called
	app.Get("/", func(_ fiber.Ctx) error {
		t.Error("Test handler should not be called")
		return nil
	})

	// Create a request without an API key and send it to the app
	res, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", http.NoBody))
	require.NoError(t, err)

	// Read the response body into a string
	body, err := io.ReadAll(res.Body)
	require.NoError(t, err)

	// Check that the response has the expected status code and body
	require.Equal(t, http.StatusUnauthorized, res.StatusCode)
	require.Equal(t, "API key is invalid and request was handled by custom error handler", string(body))

	// Create a request with a valid API key in the Authorization header
	req := httptest.NewRequest(fiber.MethodGet, "/", http.NoBody)
	req.Header.Add("Authorization", "Bearer "+CorrectKey)

	// Send the request to the app
	res, err = app.Test(req)
	require.NoError(t, err)

	// Read the response body into a string
	body, err = io.ReadAll(res.Body)
	require.NoError(t, err)

	// Check that the response has the expected status code and body
	require.Equal(t, http.StatusOK, res.StatusCode)
	require.Equal(t, "API key is valid and request was handled by custom success handler", string(body))
}

Domain

Subdomains

Frequently Asked Questions

What does Test_CustomSuccessAndFailureHandlers() do?
Test_CustomSuccessAndFailureHandlers() is a function in the fiber codebase, defined in middleware/keyauth/keyauth_test.go.
Where is Test_CustomSuccessAndFailureHandlers() defined?
Test_CustomSuccessAndFailureHandlers() is defined in middleware/keyauth/keyauth_test.go at line 389.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free