github.com/gofiber/fiber/v2@v2.47.0/middleware/basicauth/basicauth_test.go (about) 1 package basicauth 2 3 import ( 4 "encoding/base64" 5 "fmt" 6 "io" 7 "net/http/httptest" 8 "testing" 9 10 "github.com/gofiber/fiber/v2" 11 "github.com/gofiber/fiber/v2/utils" 12 13 "github.com/valyala/fasthttp" 14 ) 15 16 // go test -run Test_BasicAuth_Next 17 func Test_BasicAuth_Next(t *testing.T) { 18 t.Parallel() 19 app := fiber.New() 20 app.Use(New(Config{ 21 Next: func(_ *fiber.Ctx) bool { 22 return true 23 }, 24 })) 25 26 resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil)) 27 utils.AssertEqual(t, nil, err) 28 utils.AssertEqual(t, fiber.StatusNotFound, resp.StatusCode) 29 } 30 31 func Test_Middleware_BasicAuth(t *testing.T) { 32 t.Parallel() 33 app := fiber.New() 34 35 app.Use(New(Config{ 36 Users: map[string]string{ 37 "john": "doe", 38 "admin": "123456", 39 }, 40 })) 41 42 //nolint:forcetypeassert,errcheck // TODO: Do not force-type assert 43 app.Get("/testauth", func(c *fiber.Ctx) error { 44 username := c.Locals("username").(string) 45 password := c.Locals("password").(string) 46 47 return c.SendString(username + password) 48 }) 49 50 tests := []struct { 51 url string 52 statusCode int 53 username string 54 password string 55 }{ 56 { 57 url: "/testauth", 58 statusCode: 200, 59 username: "john", 60 password: "doe", 61 }, 62 { 63 url: "/testauth", 64 statusCode: 200, 65 username: "admin", 66 password: "123456", 67 }, 68 { 69 url: "/testauth", 70 statusCode: 401, 71 username: "ee", 72 password: "123456", 73 }, 74 } 75 76 for _, tt := range tests { 77 // Base64 encode credentials for http auth header 78 creds := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", tt.username, tt.password))) 79 80 req := httptest.NewRequest(fiber.MethodGet, "/testauth", nil) 81 req.Header.Add("Authorization", "Basic "+creds) 82 resp, err := app.Test(req) 83 utils.AssertEqual(t, nil, err) 84 85 body, err := io.ReadAll(resp.Body) 86 87 utils.AssertEqual(t, nil, err) 88 utils.AssertEqual(t, tt.statusCode, resp.StatusCode) 89 90 if tt.statusCode == 200 { 91 utils.AssertEqual(t, fmt.Sprintf("%s%s", tt.username, tt.password), string(body)) 92 } 93 } 94 } 95 96 // go test -v -run=^$ -bench=Benchmark_Middleware_BasicAuth -benchmem -count=4 97 func Benchmark_Middleware_BasicAuth(b *testing.B) { 98 app := fiber.New() 99 100 app.Use(New(Config{ 101 Users: map[string]string{ 102 "john": "doe", 103 }, 104 })) 105 app.Get("/", func(c *fiber.Ctx) error { 106 return c.SendStatus(fiber.StatusTeapot) 107 }) 108 109 h := app.Handler() 110 111 fctx := &fasthttp.RequestCtx{} 112 fctx.Request.Header.SetMethod(fiber.MethodGet) 113 fctx.Request.SetRequestURI("/") 114 fctx.Request.Header.Set(fiber.HeaderAuthorization, "basic am9objpkb2U=") // john:doe 115 116 b.ReportAllocs() 117 b.ResetTimer() 118 119 for n := 0; n < b.N; n++ { 120 h(fctx) 121 } 122 123 utils.AssertEqual(b, fiber.StatusTeapot, fctx.Response.Header.StatusCode()) 124 } 125 126 // go test -v -run=^$ -bench=Benchmark_Middleware_BasicAuth -benchmem -count=4 127 func Benchmark_Middleware_BasicAuth_Upper(b *testing.B) { 128 app := fiber.New() 129 130 app.Use(New(Config{ 131 Users: map[string]string{ 132 "john": "doe", 133 }, 134 })) 135 app.Get("/", func(c *fiber.Ctx) error { 136 return c.SendStatus(fiber.StatusTeapot) 137 }) 138 139 h := app.Handler() 140 141 fctx := &fasthttp.RequestCtx{} 142 fctx.Request.Header.SetMethod(fiber.MethodGet) 143 fctx.Request.SetRequestURI("/") 144 fctx.Request.Header.Set(fiber.HeaderAuthorization, "Basic am9objpkb2U=") // john:doe 145 146 b.ReportAllocs() 147 b.ResetTimer() 148 149 for n := 0; n < b.N; n++ { 150 h(fctx) 151 } 152 153 utils.AssertEqual(b, fiber.StatusTeapot, fctx.Response.Header.StatusCode()) 154 }