github.com/go-kivik/kivik/v4@v4.3.2/x/kivikd/cookies/cookies_test.go (about) 1 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 2 // use this file except in compliance with the License. You may obtain a copy of 3 // the License at 4 // 5 // http://www.apache.org/licenses/LICENSE-2.0 6 // 7 // Unless required by applicable law or agreed to in writing, software 8 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 // License for the specific language governing permissions and limitations under 11 // the License. 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 }