Home / Function/ Test_AuthSchemeBasic() — fiber Function Reference

Test_AuthSchemeBasic() — fiber Function Reference

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

Entity Profile

Dependency Diagram

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

Relationship Graph

Source Code

middleware/keyauth/keyauth_test.go lines 590–635

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

	app.Use(New(Config{
		Extractor: extractors.FromAuthHeader("Basic"),
		Validator: func(_ fiber.Ctx, key string) (bool, error) {
			if key == CorrectKey {
				return true, nil
			}
			return false, ErrMissingOrMalformedAPIKey
		},
	}))

	// Define a test handler
	app.Get("/", func(c fiber.Ctx) error {
		return c.SendString("API key is valid")
	})

	// Create a request without an API key and  Send the request 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, ErrMissingOrMalformedAPIKey.Error(), string(body))

	// Create a request with a valid API key in the "Authorization" header using the "Basic" scheme
	req := httptest.NewRequest(fiber.MethodGet, "/", http.NoBody)
	req.Header.Add("Authorization", "Basic "+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", string(body))
}

Domain

Subdomains

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free