authhandler_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package handler
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "testing"
  6. "time"
  7. "github.com/dgrijalva/jwt-go"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestAuthHandlerFailed(t *testing.T) {
  11. req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
  12. handler := Authorize("B63F477D-BBA3-4E52-96D3-C0034C27694A", WithUnauthorizedCallback(
  13. func(w http.ResponseWriter, r *http.Request, err error) {
  14. w.Header().Set("X-Test", "test")
  15. w.WriteHeader(http.StatusUnauthorized)
  16. _, err = w.Write([]byte("content"))
  17. assert.Nil(t, err)
  18. }))(
  19. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  20. w.WriteHeader(http.StatusOK)
  21. }))
  22. resp := httptest.NewRecorder()
  23. handler.ServeHTTP(resp, req)
  24. assert.Equal(t, http.StatusUnauthorized, resp.Code)
  25. }
  26. func TestAuthHandler(t *testing.T) {
  27. const key = "B63F477D-BBA3-4E52-96D3-C0034C27694A"
  28. req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
  29. token, err := buildToken(key, map[string]interface{}{
  30. "key": "value",
  31. }, 3600)
  32. assert.Nil(t, err)
  33. req.Header.Set("Authorization", "Bearer "+token)
  34. handler := Authorize(key)(
  35. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  36. w.Header().Set("X-Test", "test")
  37. _, err := w.Write([]byte("content"))
  38. assert.Nil(t, err)
  39. }))
  40. resp := httptest.NewRecorder()
  41. handler.ServeHTTP(resp, req)
  42. assert.Equal(t, http.StatusOK, resp.Code)
  43. assert.Equal(t, "test", resp.Header().Get("X-Test"))
  44. assert.Equal(t, "content", resp.Body.String())
  45. }
  46. func TestAuthHandlerWithPrevSecret(t *testing.T) {
  47. const (
  48. key = "14F17379-EB8F-411B-8F12-6929002DCA76"
  49. prevKey = "B63F477D-BBA3-4E52-96D3-C0034C27694A"
  50. )
  51. req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
  52. token, err := buildToken(key, map[string]interface{}{
  53. "key": "value",
  54. }, 3600)
  55. assert.Nil(t, err)
  56. req.Header.Set("Authorization", "Bearer "+token)
  57. handler := Authorize(key, WithPrevSecret(prevKey))(
  58. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  59. w.Header().Set("X-Test", "test")
  60. _, err := w.Write([]byte("content"))
  61. assert.Nil(t, err)
  62. }))
  63. resp := httptest.NewRecorder()
  64. handler.ServeHTTP(resp, req)
  65. assert.Equal(t, http.StatusOK, resp.Code)
  66. assert.Equal(t, "test", resp.Header().Get("X-Test"))
  67. assert.Equal(t, "content", resp.Body.String())
  68. }
  69. func TestAuthHandler_NilError(t *testing.T) {
  70. req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
  71. resp := httptest.NewRecorder()
  72. assert.NotPanics(t, func() {
  73. unauthorized(resp, req, nil, nil)
  74. })
  75. }
  76. func buildToken(secretKey string, payloads map[string]interface{}, seconds int64) (string, error) {
  77. now := time.Now().Unix()
  78. claims := make(jwt.MapClaims)
  79. claims["exp"] = now + seconds
  80. claims["iat"] = now
  81. for k, v := range payloads {
  82. claims[k] = v
  83. }
  84. token := jwt.New(jwt.SigningMethodHS256)
  85. token.Claims = claims
  86. return token.SignedString([]byte(secretKey))
  87. }