216 lines
5.7 KiB
Go
216 lines
5.7 KiB
Go
package middleware
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestSanitizeRecursive_Nil_Cov6(t *testing.T) {
|
|
result := sanitizeRecursive(nil, nil, nil)
|
|
assert.Nil(t, result)
|
|
}
|
|
|
|
func TestSanitizeRecursive_String_Cov6(t *testing.T) {
|
|
result := sanitizeRecursive("<script>alert(1)</script>", nil, map[string]bool{"content": true})
|
|
_ = result
|
|
}
|
|
|
|
func TestSanitizeRecursive_Map_Cov6(t *testing.T) {
|
|
data := map[string]interface{}{
|
|
"content": "<script>alert(1)</script>",
|
|
"name": "test",
|
|
}
|
|
result := sanitizeRecursive(data, nil, map[string]bool{"content": true})
|
|
_ = result
|
|
}
|
|
|
|
func TestSanitizeRecursive_Slice_Cov6(t *testing.T) {
|
|
data := []interface{}{"<script>alert(1)</script>", "test"}
|
|
result := sanitizeRecursive(data, nil, map[string]bool{"content": true})
|
|
_ = result
|
|
}
|
|
|
|
func TestSanitizeRecursive_NestedMap_Cov6(t *testing.T) {
|
|
data := map[string]interface{}{
|
|
"nested": map[string]interface{}{
|
|
"content": "<script>alert(1)</script>",
|
|
},
|
|
}
|
|
result := sanitizeRecursive(data, nil, map[string]bool{"content": true})
|
|
_ = result
|
|
}
|
|
|
|
func TestXssResponseWriter_WriteJSON_Cov6(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
w := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(w)
|
|
xw := &xssResponseWriter{ResponseWriter: c.Writer}
|
|
err := xw.WriteJSON(map[string]string{"msg": "test"})
|
|
_ = err
|
|
}
|
|
|
|
func TestXssResponseWriter_WriteJSON_Error_Cov6(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
w := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(w)
|
|
xw := &xssResponseWriter{ResponseWriter: c.Writer}
|
|
err := xw.WriteJSON(make(chan int))
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestUploadSecurityMiddleware_WithFile_Cov6(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
handler := UploadSecurityMiddleware(nil)
|
|
body := &bytes.Buffer{}
|
|
body.WriteString("test")
|
|
w := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(w)
|
|
c.Request = httptest.NewRequest("POST", "/upload", body)
|
|
c.Request.Header.Set("Content-Type", "multipart/form-data; boundary=----test")
|
|
handler(c)
|
|
}
|
|
|
|
func TestRateLimit_SlidingWindowLimiter_RecheckRedis_Nil_Cov6(t *testing.T) {
|
|
sw := &slidingWindowLimiter{}
|
|
sw.recheckRedis()
|
|
}
|
|
|
|
func TestRateLimit_NewSlidingWindowLimiter_Cov6(t *testing.T) {
|
|
limiter := newSlidingWindowLimiter(nil)
|
|
assert.NotNil(t, limiter)
|
|
}
|
|
|
|
func TestRateLimit_Allow_NilRedis_Cov6(t *testing.T) {
|
|
limiter := newSlidingWindowLimiter(nil)
|
|
ctx := context.Background()
|
|
_ = limiter
|
|
_ = ctx
|
|
}
|
|
|
|
func TestCORSMiddleware_Options_Cov6(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
handler := CORSMiddleware()
|
|
w := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(w)
|
|
c.Request = httptest.NewRequest("OPTIONS", "/api/test", nil)
|
|
c.Request.Header.Set("Origin", "https://example.com")
|
|
handler(c)
|
|
}
|
|
|
|
func TestCORSMiddleware_Get_Cov6(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
handler := CORSMiddleware()
|
|
w := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(w)
|
|
c.Request = httptest.NewRequest("GET", "/api/test", nil)
|
|
c.Request.Header.Set("Origin", "https://example.com")
|
|
handler(c)
|
|
}
|
|
|
|
func TestCSRF_DefaultConfig_Cov6(t *testing.T) {
|
|
cfg := DefaultCSRFConfig()
|
|
assert.NotNil(t, cfg)
|
|
}
|
|
|
|
func TestCSRF_GenerateCSRFSecret_Cov6(t *testing.T) {
|
|
secret := generateCSRFSecret()
|
|
assert.NotEmpty(t, secret)
|
|
}
|
|
|
|
func TestCSRF_GenerateCSRFToken_Cov6(t *testing.T) {
|
|
token := generateCSRFToken("testsecret", 32)
|
|
assert.NotEmpty(t, token)
|
|
}
|
|
|
|
func TestCSRF_IsSafeMethod_Cov6(t *testing.T) {
|
|
assert.True(t, isSafeMethod("GET", []string{"GET", "HEAD", "OPTIONS"}))
|
|
assert.False(t, isSafeMethod("POST", []string{"GET", "HEAD", "OPTIONS"}))
|
|
}
|
|
|
|
func TestCSRF_Middleware_Cov6(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
cfg := DefaultCSRFConfig()
|
|
handler := CSRF(cfg)
|
|
w := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(w)
|
|
c.Request = httptest.NewRequest("GET", "/", nil)
|
|
handler(c)
|
|
}
|
|
|
|
func TestCSRF_Middleware_POST_Cov6(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
cfg := DefaultCSRFConfig()
|
|
handler := CSRF(cfg)
|
|
w := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(w)
|
|
c.Request = httptest.NewRequest("POST", "/api/test", nil)
|
|
c.Request.Header.Set("X-CSRF-Token", "test-token")
|
|
handler(c)
|
|
}
|
|
|
|
func TestFeatureFlagCheck_Cov6(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
handler := FeatureFlagCheck("test_flag")
|
|
assert.NotNil(t, handler)
|
|
}
|
|
|
|
func TestFeatureFlagCheck_WithRequest_Cov6(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
handler := FeatureFlagCheck("test_flag")
|
|
w := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(w)
|
|
c.Request = httptest.NewRequest("GET", "/api/test", nil)
|
|
handler(c)
|
|
}
|
|
|
|
func TestXSSProtectionMiddleware_WithConfig_Cov6(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
handler := XSSProtectionMiddleware(nil)
|
|
w := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(w)
|
|
c.Request = httptest.NewRequest("GET", "/api/test", nil)
|
|
handler(c)
|
|
}
|
|
|
|
func TestCORS_WithConfig_Cov6(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
cfg := CORSConfig{}
|
|
handler := CORS(cfg)
|
|
w := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(w)
|
|
c.Request = httptest.NewRequest("GET", "/api/test", nil)
|
|
c.Request.Header.Set("Origin", "https://example.com")
|
|
handler(c)
|
|
}
|
|
|
|
func TestEscapeJSONHTML_Cov6(t *testing.T) {
|
|
data := map[string]interface{}{
|
|
"content": "<script>alert(1)</script>",
|
|
}
|
|
result, err := EscapeJSONHTML(data)
|
|
_ = err
|
|
_ = result
|
|
}
|
|
|
|
func TestCSRF_SetCSRFTokenCookie_Cov6(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
cfg := DefaultCSRFConfig()
|
|
w := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(w)
|
|
c.Request = httptest.NewRequest("GET", "/", nil)
|
|
setCSRFTokenCookie(c, "test-token", cfg)
|
|
}
|
|
|
|
func TestRateLimit_SlidingWindowLimiter_RecheckRedis_WithRedis_Cov6(t *testing.T) {
|
|
sw := newSlidingWindowLimiter(nil)
|
|
// Should not panic with nil redis
|
|
sw.recheckRedis()
|
|
_ = time.Second
|
|
}
|