Home / Function/ Test_Middleware_BasicAuth() — fiber Function Reference

Test_Middleware_BasicAuth() — fiber Function Reference

Architecture documentation for the Test_Middleware_BasicAuth() function in basicauth_test.go from the fiber codebase.

Entity Profile

Dependency Diagram

graph TD
  8739d780_1d60_0839_f2a6_30b7ff18da2f["Test_Middleware_BasicAuth()"]
  c1fa52c0_acd4_56de_8c46_542417f3c9b8["basicauth_test.go"]
  8739d780_1d60_0839_f2a6_30b7ff18da2f -->|defined in| c1fa52c0_acd4_56de_8c46_542417f3c9b8
  bfd8a9b6_0e17_60de_7e62_37b7a2f1750e["sha256Hash()"]
  8739d780_1d60_0839_f2a6_30b7ff18da2f -->|calls| bfd8a9b6_0e17_60de_7e62_37b7a2f1750e
  style 8739d780_1d60_0839_f2a6_30b7ff18da2f fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

middleware/basicauth/basicauth_test.go lines 46–110

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

	hashedJohn := sha256Hash("doe")
	hashedAdmin, err := bcrypt.GenerateFromPassword([]byte("123456"), bcrypt.MinCost)
	require.NoError(t, err)

	app.Use(New(Config{
		Users: map[string]string{
			"john":  hashedJohn,
			"admin": string(hashedAdmin),
		},
	}))

	app.Get("/testauth", func(c fiber.Ctx) error {
		username := UsernameFromContext(c)
		return c.SendString(username)
	})

	tests := []struct {
		url        string
		username   string
		password   string
		statusCode int
	}{
		{
			url:        "/testauth",
			statusCode: 200,
			username:   "john",
			password:   "doe",
		},
		{
			url:        "/testauth",
			statusCode: 200,
			username:   "admin",
			password:   "123456",
		},
		{
			url:        "/testauth",
			statusCode: 401,
			username:   "ee",
			password:   "123456",
		},
	}

	for _, tt := range tests {
		// Base64 encode credentials for http auth header
		creds := base64.StdEncoding.EncodeToString(fmt.Appendf(nil, "%s:%s", tt.username, tt.password))

		req := httptest.NewRequest(fiber.MethodGet, "/testauth", http.NoBody)
		req.Header.Add("Authorization", "Basic "+creds)
		resp, err := app.Test(req)
		require.NoError(t, err)

		body, err := io.ReadAll(resp.Body)

		require.NoError(t, err)
		require.Equal(t, tt.statusCode, resp.StatusCode)

		if tt.statusCode == 200 {
			require.Equal(t, tt.username, string(body))
		}
	}
}

Domain

Subdomains

Calls

Frequently Asked Questions

What does Test_Middleware_BasicAuth() do?
Test_Middleware_BasicAuth() is a function in the fiber codebase, defined in middleware/basicauth/basicauth_test.go.
Where is Test_Middleware_BasicAuth() defined?
Test_Middleware_BasicAuth() is defined in middleware/basicauth/basicauth_test.go at line 46.
What does Test_Middleware_BasicAuth() call?
Test_Middleware_BasicAuth() calls 1 function(s): sha256Hash.

Analyze Your Own Codebase

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

Try Supermodel Free