29 lines
764 B
Go
29 lines
764 B
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"modelRT/constants"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestSetTokenMiddlewarePropagatesClientToken(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
router := gin.New()
|
|
router.Use(SetTokenMiddleware("test-token"))
|
|
router.GET("/test", func(c *gin.Context) {
|
|
assert.Equal(t, "test-token", c.GetString(constants.ClientTokenContextName))
|
|
assert.Equal(t, "test-token", c.Request.Context().Value(constants.CtxKeyClientToken))
|
|
c.Status(http.StatusNoContent)
|
|
})
|
|
|
|
request := httptest.NewRequest(http.MethodGet, "/test", nil)
|
|
response := httptest.NewRecorder()
|
|
router.ServeHTTP(response, request)
|
|
assert.Equal(t, http.StatusNoContent, response.Code)
|
|
}
|