Skip to content

Commit

Permalink
added test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
himanshu-allen committed Oct 17, 2024
1 parent 1ecdd29 commit 773a57e
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions pkg/config/jwtmiddleware/jwt_middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package jwtmiddleware

import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -332,3 +333,79 @@ func TestJWTMiddleware_Handler(t *testing.T) {
})
}
}

func TestFromFirst(t *testing.T) {
// Mock TokenExtractor that returns a token or error based on input.
mockExtractor := func(token string, err error) TokenExtractor {
return func(r *http.Request) (string, error) {
return token, err
}
}

tests := []struct {
name string
extractors []TokenExtractor
request *http.Request
wantToken string
wantErr bool
}{
{
name: "First extractor returns valid token",
extractors: []TokenExtractor{
mockExtractor("token1", nil),
mockExtractor("token2", nil),
},
request: httptest.NewRequest("GET", "/", nil),
wantToken: "token1",
wantErr: false,
},
{
name: "First extractor returns error, second returns valid token",
extractors: []TokenExtractor{
mockExtractor("", errors.New("error")),
mockExtractor("token2", nil),
},
request: httptest.NewRequest("GET", "/", nil),
wantToken: "",
wantErr: true,
},
{
name: "All extractors return empty token",
extractors: []TokenExtractor{
mockExtractor("", nil),
mockExtractor("", nil),
},
request: httptest.NewRequest("GET", "/", nil),
wantToken: "",
wantErr: false,
},
{
name: "First extractor returns error, second returns empty token",
extractors: []TokenExtractor{
mockExtractor("", errors.New("error")),
mockExtractor("", nil),
},
request: httptest.NewRequest("GET", "/", nil),
wantToken: "",
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Call FromFirst with the mock extractors
token, err := FromFirst(tt.extractors...)(tt.request)

// Check if error matches the expectation
if (err != nil) != tt.wantErr {
t.Errorf("FromFirst() error = %v, wantErr %v", err, tt.wantErr)
return
}

// Check if token matches the expectation
if token != tt.wantToken {
t.Errorf("FromFirst() token = %v, want %v", token, tt.wantToken)
}
})
}
}

0 comments on commit 773a57e

Please sign in to comment.