Skip to content

Commit

Permalink
token/jwt: add claims tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arekkas authored and arekkas committed Jul 9, 2017
1 parent d67d52d commit c55d679
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions token/jwt/claims.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ func ToTime(i interface{}) time.Time {
return time.Unix(t, 0)
} else if t, ok := i.(float64); ok {
return time.Unix(int64(t), 0)
} else if t, ok := i.(time.Time); ok {
return t
}

return time.Time{}
Expand Down
24 changes: 24 additions & 0 deletions token/jwt/claims_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package jwt

import (
"testing"
"github.com/stretchr/testify/assert"
"time"
)

func TestToString(t *testing.T) {
assert.Equal(t, "foo", ToString("foo"))
assert.Equal(t, "foo", ToString([]string{"foo"}))
assert.Empty(t, ToString(1234))
assert.Empty(t, ToString(nil))
}

func TestToTime(t *testing.T) {
assert.Equal(t, time.Time{}, ToTime(nil))
assert.Equal(t, time.Time{}, ToTime("1234"))

now := time.Now().Round(time.Second)
assert.Equal(t, now, ToTime(now))
assert.Equal(t, now, ToTime(now.Unix()))
assert.Equal(t, now, ToTime(float64(now.Unix())))
}

0 comments on commit c55d679

Please sign in to comment.