(t *testing.T)
| 30 | } |
| 31 | |
| 32 | func TestIssue518(t *testing.T) { |
| 33 | server := &impl{} |
| 34 | |
| 35 | assert.NotPanics(t, func() { |
| 36 | r := fiber.New() |
| 37 | RegisterHandlers(r, server) |
| 38 | }) |
| 39 | |
| 40 | assert.NotPanics(t, func() { |
| 41 | r := fiber.New() |
| 42 | RegisterHandlersWithOptions(r, server, FiberServerOptions{ |
| 43 | Middlewares: []MiddlewareFunc{ |
| 44 | func(c *fiber.Ctx) error { |
| 45 | return nil |
| 46 | }, |
| 47 | }, |
| 48 | HandlerMiddlewares: []HandlerMiddlewareFunc{ |
| 49 | func(c *fiber.Ctx, next fiber.Handler) error { |
| 50 | if hasSecurityScopes(c) && c.Get(fiber.HeaderAuthorization) == "" { |
| 51 | return c.SendStatus(fiber.StatusUnauthorized) |
| 52 | } |
| 53 | return next(c) |
| 54 | }, |
| 55 | }, |
| 56 | }) |
| 57 | }) |
| 58 | |
| 59 | t.Run("secured endpoint requires auth when scopes are present", func(t *testing.T) { |
| 60 | r := fiber.New() |
| 61 | RegisterHandlersWithOptions(r, server, FiberServerOptions{ |
| 62 | HandlerMiddlewares: []HandlerMiddlewareFunc{ |
| 63 | func(c *fiber.Ctx, next fiber.Handler) error { |
| 64 | if hasSecurityScopes(c) && c.Get(fiber.HeaderAuthorization) == "" { |
| 65 | return c.SendStatus(fiber.StatusUnauthorized) |
| 66 | } |
| 67 | return next(c) |
| 68 | }, |
| 69 | }, |
| 70 | }) |
| 71 | |
| 72 | req := httptest.NewRequest(http.MethodGet, "/auth-check", nil) |
| 73 | resp, err := r.Test(req) |
| 74 | assert.NoError(t, err) |
| 75 | assert.Equal(t, fiber.StatusUnauthorized, resp.StatusCode) |
| 76 | |
| 77 | req = httptest.NewRequest(http.MethodGet, "/auth-check", nil) |
| 78 | req.Header.Set(fiber.HeaderAuthorization, "Bearer token") |
| 79 | resp, err = r.Test(req) |
| 80 | assert.NoError(t, err) |
| 81 | assert.Equal(t, fiber.StatusOK, resp.StatusCode) |
| 82 | |
| 83 | req = httptest.NewRequest(http.MethodGet, "/test", nil) |
| 84 | resp, err = r.Test(req) |
| 85 | assert.NoError(t, err) |
| 86 | assert.Equal(t, fiber.StatusOK, resp.StatusCode) |
| 87 | }) |
| 88 | } |
nothing calls this directly
no test coverage detected