Skip to content

Commit

Permalink
add more tests to boost coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Lee <dave@gray101.com>
  • Loading branch information
dave-gray101 committed Jun 10, 2024
1 parent 4e061aa commit 397ff49
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions middleware/keyauth/keyauth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,30 @@ func Test_AuthSources(t *testing.T) {
}
}

func TestPanicOnInvalidConfiguration(t *testing.T) {
require.Panics(t, func() {
authMiddleware := New(Config{
KeyLookup: "invalid",
})
// We shouldn't even make it this far, but these next two lines prevent authMiddleware from being an unused variable.
app := fiber.New()
app.Use(authMiddleware)
})
}

func TestCustomKeyUtilityFunctionErrors(t *testing.T) {
const (
scheme = "Bearer"
)

// Invalid element while parsing
_, err := SingleKeyLookup("invalid", scheme)
require.Error(t, err)

_, err = MultipleKeySourceLookup([]string{"header:key", "invalid"}, scheme)
require.Error(t, err)
}

func TestMultipleKeyLookup(t *testing.T) {
const (
desc = "auth with correct key"
Expand Down Expand Up @@ -178,6 +202,12 @@ func TestMultipleKeyLookup(t *testing.T) {

err = res.Body.Close()
require.NoError(t, err)

// construct a second request without proper key
req, err = http.NewRequestWithContext(context.Background(), fiber.MethodGet, "/foo", nil)
require.NoError(t, err)
_, err = app.Test(req, -1)
require.Error(t, err)
}

func Test_MultipleKeyAuth(t *testing.T) {
Expand Down

0 comments on commit 397ff49

Please sign in to comment.