代码拉取完成,页面将自动刷新
/**
* @Author: aesoper
* @Description:
* @File: csrf_test
* @Version: 1.0.0
* @Date: 2020/5/19 21:06
*/
package gin_middleware
import (
"encoding/base64"
"fmt"
"gitee.com/aesoper/utils"
"gitee.com/gin-ecosystem/gin-middleware/consts"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestNewCSRF(t *testing.T) {
e := gin.New()
e.GET("/", NewCSRF(CSRFConfig{
Skipper: DefaultSkipper,
QueryAllowHosts: nil,
QueryAllowPattern: nil,
}))
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.Header.Set(consts.HeaderReferer, "http://my11sql.com")
resp := httptest.NewRecorder()
e.ServeHTTP(resp, req)
assert.Equal(t, resp.Result().StatusCode, http.StatusForbidden)
e.GET("/referer", NewCSRF(CSRFConfig{
Skipper: DefaultSkipper,
QueryAllowHosts: func() []string {
return []string{"my11sql.com"}
},
QueryAllowPattern: nil,
}))
req = httptest.NewRequest(http.MethodGet, "/referer", nil)
req.Header.Set(consts.HeaderReferer, "http://my11sql.com")
resp = httptest.NewRecorder()
e.ServeHTTP(resp, req)
assert.Equal(t, resp.Result().StatusCode, http.StatusOK)
e.GET("/token", NewCSRF(CSRFConfig{
Skipper: DefaultSkipper,
QueryAllowHosts: func() []string {
return []string{"my11sql.com"}
},
QueryAllowPattern: nil,
Validator: DefaultTokenValidator,
}))
req = httptest.NewRequest(http.MethodGet, "/token", nil)
req.Header.Set(consts.HeaderReferer, "http://my11sql.com")
input := fmt.Sprintf("%s-%d-%s", defaultCsrfSecret, time.Now().Unix(), utils.GenerateRandomStrings(8))
encodeString := base64.StdEncoding.EncodeToString([]byte(input))
req.Header.Set(consts.HeaderXCSRFToken, encodeString)
resp = httptest.NewRecorder()
e.ServeHTTP(resp, req)
assert.Equal(t, resp.Result().StatusCode, http.StatusOK)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。