Home / Function/ Test_CustomNextFunc() — fiber Function Reference

Test_CustomNextFunc() — fiber Function Reference

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

Entity Profile

Dependency Diagram

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

Relationship Graph

Source Code

middleware/keyauth/keyauth_test.go lines 442–505

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

	app.Use(New(Config{
		Next: func(c fiber.Ctx) bool {
			return c.Path() == "/allowed"
		},
		Validator: func(_ fiber.Ctx, key string) (bool, error) {
			if key == CorrectKey {
				return true, nil
			}
			return false, ErrMissingOrMalformedAPIKey
		},
	}))

	// Define a test handler
	app.Get("/allowed", func(c fiber.Ctx) error {
		return c.SendString("API key is valid and request was allowed by custom filter")
	})
	app.Get("/not-allowed", func(c fiber.Ctx) error {
		return c.SendString("Should be protected")
	})

	// Create a request with the "/allowed" path and send it to the app
	req := httptest.NewRequest(fiber.MethodGet, "/allowed", http.NoBody)
	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 allowed by custom filter", string(body))

	// Create a request with a different path and send it to the app without correct key
	req = httptest.NewRequest(fiber.MethodGet, "/not-allowed", http.NoBody)
	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.StatusUnauthorized, res.StatusCode)
	require.Equal(t, ErrMissingOrMalformedAPIKey.Error(), string(body))

	// Create a request with a different path and send it to the app with correct key
	req = httptest.NewRequest(fiber.MethodGet, "/not-allowed", http.NoBody)
	req.Header.Add("Authorization", "Bearer "+CorrectKey)

	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, "Should be protected", string(body))
}

Domain

Subdomains

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free