Some checks are pending
Security Checks / security (push) Waiting to run
统一 frontend、backend、test、test-log 和 out 目录,更新构建、部署及测试入口;纳入配置管理兼容与首次安全引导修复;排除本机配置、密钥和测试产物。
42 lines
1.5 KiB
Go
42 lines
1.5 KiB
Go
package framework
|
|
|
|
import (
|
|
"bytes"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
func TestDecodePluginRequestRejectsInvalidMethodAndOversizedBody(t *testing.T) {
|
|
var dto HookDTO
|
|
methodRequest := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
methodRecorder := httptest.NewRecorder()
|
|
if decodePluginRequest(methodRecorder, methodRequest, &dto) {
|
|
t.Fatal("GET request was accepted")
|
|
}
|
|
if methodRecorder.Code != http.StatusMethodNotAllowed {
|
|
t.Fatalf("status = %d, want %d", methodRecorder.Code, http.StatusMethodNotAllowed)
|
|
}
|
|
|
|
oversizedBody := append([]byte(`{"ServerVersion":"`), bytes.Repeat([]byte{'a'}, int(maxPluginRPCBytes))...)
|
|
oversizedBody = append(oversizedBody, []byte(`"}`)...)
|
|
oversizedRequest := httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(oversizedBody))
|
|
oversizedRecorder := httptest.NewRecorder()
|
|
if decodePluginRequest(oversizedRecorder, oversizedRequest, &dto) {
|
|
t.Fatal("oversized request was accepted")
|
|
}
|
|
if oversizedRecorder.Code != http.StatusRequestEntityTooLarge {
|
|
t.Fatalf("status = %d, want %d", oversizedRecorder.Code, http.StatusRequestEntityTooLarge)
|
|
}
|
|
}
|
|
|
|
func TestWritePluginResponseRejectsOversizedBody(t *testing.T) {
|
|
recorder := httptest.NewRecorder()
|
|
if err := writePluginResponse(recorder, "text/plain", bytes.Repeat([]byte{'a'}, int(maxPluginRPCBytes)+1)); err == nil {
|
|
t.Fatal("oversized response was accepted")
|
|
}
|
|
if recorder.Code != http.StatusRequestEntityTooLarge {
|
|
t.Fatalf("status = %d, want %d", recorder.Code, http.StatusRequestEntityTooLarge)
|
|
}
|
|
}
|