Merge pull request #208 from ericchiang/parse_int
db: strconv.ParseInt specify base 10
This commit is contained in:
commit
2d611ad508
2 changed files with 32 additions and 1 deletions
|
@ -52,7 +52,7 @@ func parseToken(token string) (int64, []byte, error) {
|
||||||
if len(parts) != 2 {
|
if len(parts) != 2 {
|
||||||
return -1, nil, refresh.ErrorInvalidToken
|
return -1, nil, refresh.ErrorInvalidToken
|
||||||
}
|
}
|
||||||
id, err := strconv.ParseInt(parts[0], 0, 64)
|
id, err := strconv.ParseInt(parts[0], 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return -1, nil, refresh.ErrorInvalidToken
|
return -1, nil, refresh.ErrorInvalidToken
|
||||||
}
|
}
|
||||||
|
|
31
db/refresh_test.go
Normal file
31
db/refresh_test.go
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
package db
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestBuildAndParseToken(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
id int64
|
||||||
|
payload []byte
|
||||||
|
}{
|
||||||
|
{11111, []byte("may the force be with you")},
|
||||||
|
{123213, []byte("If we can hit that bullseye the rest of the dominoes will fall like a house of cards, checkmate!")},
|
||||||
|
{1, []byte{0xd3, 0x22, 0xa8, 0x44, 0x34, 0x94, 0xd8}},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, tt := range tests {
|
||||||
|
id, payload, err := parseToken(buildToken(tt.id, tt.payload))
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("case %d: failed to parse token: %v", i, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if tt.id != id {
|
||||||
|
t.Errorf("case %d: want id=%d, got id=%d", i, tt.id, id)
|
||||||
|
}
|
||||||
|
if bytes.Compare(tt.payload, payload) != 0 {
|
||||||
|
t.Errorf("case %d: want payload=%x, got payload=%x", i, tt.payload, payload)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in a new issue