1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
package jwt
import (
"testing"
)
func TestSplitToken(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input string
expected []string
isValid bool
}{
{
name: "valid token with three parts",
input: "header.claims.signature",
expected: []string{"header", "claims", "signature"},
isValid: true,
},
{
name: "invalid token with two parts only",
input: "header.claims",
expected: nil,
isValid: false,
},
{
name: "invalid token with one part only",
input: "header",
expected: nil,
isValid: false,
},
{
name: "invalid token with extra delimiter",
input: "header.claims.signature.extra",
expected: nil,
isValid: false,
},
{
name: "invalid empty token",
input: "",
expected: nil,
isValid: false,
},
{
name: "valid token with empty parts",
input: "..signature",
expected: []string{"", "", "signature"},
isValid: true,
},
{
// We are just splitting the token into parts, so we don't care about the actual values.
// It is up to the caller to validate the parts.
name: "valid token with all parts empty",
input: "..",
expected: []string{"", "", ""},
isValid: true,
},
{
name: "invalid token with just delimiters and extra part",
input: "...",
expected: nil,
isValid: false,
},
{
name: "invalid token with many delimiters",
input: "header.claims.signature..................",
expected: nil,
isValid: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
parts, ok := splitToken(tt.input)
if ok != tt.isValid {
t.Errorf("expected %t, got %t", tt.isValid, ok)
}
if ok {
for i, part := range tt.expected {
if parts[i] != part {
t.Errorf("expected %s, got %s", part, parts[i])
}
}
}
})
}
}
|