package jwtwrapper import ( "github.com/dgrijalva/jwt-go" "reflect" "testing" "time" ) func Test_JwtGenToken(t *testing.T) { SetSecret("12345678abcdefg") type args struct { id string issuer string subject string seconds time.Duration } tests := []struct { name string args args want string wantErr bool }{ { name:"Test_JwtGenToken", args:args{ id:"1", issuer:"ycl", subject:"ycl", seconds:60*time.Second, }, want:"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1ODY1Njk0NTgsImp0aSI6IjEiLCJpc3MiOiJ5Y2wiLCJzdWIiOiJ5Y2wifQ.Sl4QXE_DrNX8JmF_TSl9atorkLys64Dvss_bdsAlCe8", wantErr:false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := GenToken(tt.args.id, tt.args.issuer, tt.args.subject, tt.args.seconds) if (err != nil) != tt.wantErr { t.Errorf("GenToken() error = %v, wantErr %v", err, tt.wantErr) return } if got != tt.want { t.Errorf("GenToken() got = %v, want %v", got, tt.want) } }) } } func Test_JwtParseToken(t *testing.T) { SetSecret("12345678abcdefg") add := time.Now().Add(60*time.Second).Unix() token, _ := GenToken("1","ycl","ycl",60*time.Second) type args struct { token string } tests := []struct { name string args args want *jwt.StandardClaims wantErr bool }{ { name:"Test_JwtParseToken", args:args{ token:token, }, want:&jwt.StandardClaims{ Audience:"", ExpiresAt:add, Id:"1", IssuedAt:0, Issuer:"ycl", NotBefore:0, Subject:"ycl", }, wantErr:false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := ParseToken(tt.args.token) if (err != nil) != tt.wantErr { t.Errorf("ParseToken() error = %v, wantErr %v", err, tt.wantErr) return } if !reflect.DeepEqual(got, tt.want) { t.Errorf("ParseToken() got = %v, want %v", got, tt.want) } }) } }