token_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package token
  2. import (
  3. "net/url"
  4. "testing"
  5. "time"
  6. )
  7. const secret = "i1ydX9RtHyuJTrw7frcu"
  8. func TestJwtSign(t *testing.T) {
  9. tokenString, err := New(secret).JwtSign(123456789, "xinliangnote", 24*time.Hour)
  10. if err != nil {
  11. t.Error("sign error", err)
  12. return
  13. }
  14. t.Log(tokenString)
  15. }
  16. func TestJwtParse(t *testing.T) {
  17. tokenString := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjEyMzQ1Njc4OSwidXNlcm5hbWUiOiJ4aW5saWFuZyIsImV4cCI6MTYwOTQ2NzcwNCwiaWF0IjoxNjA5MzgxMzA0LCJpc3MiOiJnby1naW4tYXBpIiwibmJmIjoxNjA5MzgxMzA0fQ.hccv8F713WpKcwiSldBrFLZz_2SZzOTPedPi-8ps7M4"
  18. user, err := New(secret).JwtParse(tokenString)
  19. if err != nil {
  20. t.Error("parse error", err)
  21. return
  22. }
  23. t.Log(user)
  24. }
  25. func TestUrlSign(t *testing.T) {
  26. urlPath := "/echo"
  27. method := "post"
  28. params := url.Values{}
  29. params.Add("a", "a1")
  30. params.Add("d", "d1")
  31. params.Add("c", "c1")
  32. tokenString, err := New(secret).UrlSign(urlPath, method, params)
  33. if err != nil {
  34. t.Error("sign error", err)
  35. return
  36. }
  37. t.Log(tokenString)
  38. }
  39. func BenchmarkJwtSignAndParse(b *testing.B) {
  40. b.ResetTimer()
  41. token := New(secret)
  42. for i := 0; i < b.N; i++ {
  43. tokenString, _ := token.JwtSign(123456789, "xinliangnote", 24*time.Hour)
  44. token.JwtParse(tokenString)
  45. }
  46. }