-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
token_test.go
91 lines (79 loc) · 1.61 KB
/
token_test.go
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
90
91
package httpsfv
import (
"strings"
"testing"
)
func TestTokenMarshalSFV(t *testing.T) {
t.Parallel()
data := []struct {
in string
valid bool
}{
{"abc'!#$%*+-.^_|~:/`", true},
{"H3lLo", true},
{"a*foo", true},
{"a!1", true},
{"a#1", true},
{"a$1", true},
{"a%1", true},
{"a&1", true},
{"a'1", true},
{"a*1", true},
{"a+1", true},
{"a-1", true},
{"a.1", true},
{"a^1", true},
{"a_1", true},
{"a`1", true},
{"a|1", true},
{"a~1", true},
{"a:1", true},
{"a/1", true},
{`0foo`, false},
{`!foo`, false},
{"1abc", false},
{"", false},
{"hel\tlo", false},
{"hel\x1flo", false},
{"hel\x7flo", false},
{"Kévin", false},
}
var b strings.Builder
for _, d := range data {
b.Reset()
err := Token(d.in).marshalSFV(&b)
if d.valid && err != nil {
t.Errorf("error not expected for %v, got %v", d.in, err)
} else if !d.valid && err == nil {
t.Errorf("error expected for %v, got %v", d.in, err)
}
if d.valid && b.String() != d.in {
t.Errorf("got %v; want %v", b.String(), d.in)
}
}
}
func TestParseToken(t *testing.T) {
t.Parallel()
data := []struct {
in string
out Token
err bool
}{
{"t", Token("t"), false},
{"tok", Token("tok"), false},
{"*t!o&k", Token("*t!o&k"), false},
{"t=", Token("t"), false},
{"", Token(""), true},
{"é", Token(""), true},
}
for _, d := range data {
s := &scanner{data: d.in}
i, err := parseToken(s)
if d.err && err == nil {
t.Errorf("parseToken(%s): error expected", d.in)
}
if !d.err && d.out != i {
t.Errorf("parseToken(%s) = %v, %v; %v, <nil> expected", d.in, i, err, d.out)
}
}
}