authhandler_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. flusher, ok := w.(http.Flusher)
  40. assert.True(t, ok)
  41. flusher.Flush()
  42. }))
  43. resp := httptest.NewRecorder()
  44. handler.ServeHTTP(resp, req)
  45. assert.Equal(t, http.StatusOK, resp.Code)
  46. assert.Equal(t, "test", resp.Header().Get("X-Test"))
  47. assert.Equal(t, "content", resp.Body.String())
  48. }
  49. func TestAuthHandlerWithPrevSecret(t *testing.T) {
  50. const (
  51. key = "14F17379-EB8F-411B-8F12-6929002DCA76"
  52. prevKey = "B63F477D-BBA3-4E52-96D3-C0034C27694A"
  53. )
  54. req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
  55. token, err := buildToken(key, map[string]interface{}{
  56. "key": "value",
  57. }, 3600)
  58. assert.Nil(t, err)
  59. req.Header.Set("Authorization", "Bearer "+token)
  60. handler := Authorize(key, WithPrevSecret(prevKey))(
  61. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  62. w.Header().Set("X-Test", "test")
  63. _, err := w.Write([]byte("content"))
  64. assert.Nil(t, err)
  65. }))
  66. resp := httptest.NewRecorder()
  67. handler.ServeHTTP(resp, req)
  68. assert.Equal(t, http.StatusOK, resp.Code)
  69. assert.Equal(t, "test", resp.Header().Get("X-Test"))
  70. assert.Equal(t, "content", resp.Body.String())
  71. }
  72. func TestAuthHandler_NilError(t *testing.T) {
  73. req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
  74. resp := httptest.NewRecorder()
  75. assert.NotPanics(t, func() {
  76. unauthorized(resp, req, nil, nil)
  77. })
  78. }
  79. func buildToken(secretKey string, payloads map[string]interface{}, seconds int64) (string, error) {
  80. now := time.Now().Unix()
  81. claims := make(jwt.MapClaims)
  82. claims["exp"] = now + seconds
  83. claims["iat"] = now
  84. for k, v := range payloads {
  85. claims[k] = v
  86. }
  87. token := jwt.New(jwt.SigningMethodHS256)
  88. token.Claims = claims
  89. return token.SignedString([]byte(secretKey))
  90. }