From de8d3fa3513f80dca4f6f9da5723342a8b6d0320 Mon Sep 17 00:00:00 2001 From: James Telfer <792299+jamestelfer@users.noreply.github.com> Date: Thu, 25 Apr 2024 11:47:32 +0000 Subject: [PATCH] test: add check of error handler --- internal/jwt/jwt_test.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/internal/jwt/jwt_test.go b/internal/jwt/jwt_test.go index 98303da..a5468af 100644 --- a/internal/jwt/jwt_test.go +++ b/internal/jwt/jwt_test.go @@ -35,6 +35,7 @@ func TestMiddleware(t *testing.T) { customClaims BuildkiteClaims wantStatusCode int wantBodyText string + options []jwtmiddleware.Option }{ { name: "has subject", @@ -91,6 +92,18 @@ func TestMiddleware(t *testing.T) { wantStatusCode: http.StatusUnauthorized, wantBodyText: "JWT is invalid", }, + { + name: "error handler", + claims: valid(jwt.Claims{ + Audience: []string{"audience"}, + Subject: "subject", + Issuer: "issuer", + }), + customClaims: custom("that dog ain't gonna hunt", "test-pipeline"), + wantStatusCode: http.StatusUnauthorized, + wantBodyText: "JWT is invalid", + options: []jwtmiddleware.Option{jwtmiddleware.WithErrorHandler(LogErrorHandler())}, + }, } jwk := generateJWK(t) @@ -122,7 +135,15 @@ func TestMiddleware(t *testing.T) { responseRecorder := httptest.NewRecorder() - mw, err := Middleware(cfg, jwtmiddleware.WithErrorHandler(errorHandler(t))) + options := []jwtmiddleware.Option{ + jwtmiddleware.WithErrorHandler(errorHandler(t)), + } + + if len(test.options) > 0 { + options = append(options, test.options...) + } + + mw, err := Middleware(cfg, options...) require.NoError(t, err) handler := mw(successHandler)