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