...
1
2
3
4
5
6
7
8
9
10
11
12
13 package cookies
14
15 import "testing"
16
17 func TestDecodeCookie(t *testing.T) {
18 type cookieTest struct {
19 TestName string
20 Input string
21 Name string
22 Created int64
23 Err string
24 }
25 tests := []cookieTest{
26 {TestName: "Valid", Input: "YWRtaW46NThDNTQzN0Y697rnaWCa_rarAm25wbOg3Gm3mqc", Name: "admin", Created: 1489322879},
27 {TestName: "InvalidBasae64", Input: "bogus base64", Err: "illegal base64 data at input byte 5"},
28 {TestName: "Extra colons", Input: "Zm9vOjEyMzQ1OmJhcjpiYXoK", Name: "foo", Created: 74565},
29 {TestName: "InvalidTimestamp", Input: "Zm9vOmJhcjpiYXoK", Err: `invalid timestamp: strconv.ParseInt: parsing "bar": invalid syntax`},
30 }
31 for _, test := range tests {
32 func(test cookieTest) {
33 t.Run(test.TestName, func(t *testing.T) {
34 name, created, err := DecodeCookie(test.Input)
35 var errMsg string
36 if err != nil {
37 errMsg = err.Error()
38 }
39 if name != test.Name {
40 t.Errorf("Name: Expected '%s', got '%s'", test.Name, name)
41 }
42 if created != test.Created {
43 t.Errorf("Created: Expected %d, got %d", test.Created, created)
44 }
45 if errMsg != test.Err {
46 t.Errorf("Error: Expected '%s', got '%s'", test.Err, errMsg)
47 }
48 })
49 }(test)
50 }
51 }
52
View as plain text