github.com/TeaOSLab/EdgeNode@v1.3.8/internal/waf/checkpoints/request_upload_test.go (about) 1 package checkpoints 2 3 import ( 4 "bytes" 5 "github.com/TeaOSLab/EdgeNode/internal/waf/requests" 6 "io" 7 "mime/multipart" 8 "net/http" 9 "testing" 10 ) 11 12 func TestRequestUploadCheckpoint_RequestValue(t *testing.T) { 13 body := bytes.NewBuffer([]byte{}) 14 15 writer := multipart.NewWriter(body) 16 17 { 18 part, err := writer.CreateFormField("name") 19 if err == nil { 20 _, err := part.Write([]byte("lu")) 21 if err != nil { 22 t.Fatal(err) 23 } 24 } 25 } 26 27 { 28 part, err := writer.CreateFormField("age") 29 if err == nil { 30 _, err = part.Write([]byte("20")) 31 if err != nil { 32 t.Fatal(err) 33 } 34 } 35 } 36 37 { 38 part, err := writer.CreateFormFile("myFile", "hello.txt") 39 if err == nil { 40 _, err = part.Write([]byte("Hello, World!")) 41 if err != nil { 42 t.Fatal(err) 43 } 44 } 45 } 46 47 { 48 part, err := writer.CreateFormFile("myFile2", "hello.PHP") 49 if err == nil { 50 _, err = part.Write([]byte("Hello, World, PHP!")) 51 if err != nil { 52 t.Fatal(err) 53 } 54 } 55 } 56 57 { 58 part, err := writer.CreateFormFile("myFile3", "hello.asp") 59 if err == nil { 60 _, err = part.Write([]byte("Hello, World, ASP Pages!")) 61 if err != nil { 62 t.Fatal(err) 63 } 64 } 65 } 66 67 { 68 part, err := writer.CreateFormFile("myFile4", "hello.asp") 69 if err == nil { 70 _, err = part.Write([]byte("Hello, World, ASP Pages!")) 71 if err != nil { 72 t.Fatal(err) 73 } 74 } 75 } 76 77 err := writer.Close() 78 if err != nil { 79 t.Fatal(err) 80 } 81 82 rawReq, err := http.NewRequest(http.MethodPost, "http://teaos.cn/", body) 83 if err != nil { 84 t.Fatal() 85 } 86 87 req := requests.NewTestRequest(rawReq) 88 req.WAFRaw().Header.Add("Content-Type", writer.FormDataContentType()) 89 90 checkpoint := new(RequestUploadCheckpoint) 91 t.Log(checkpoint.RequestValue(req, "field", nil, 1)) 92 t.Log(checkpoint.RequestValue(req, "minSize", nil, 1)) 93 t.Log(checkpoint.RequestValue(req, "maxSize", nil, 1)) 94 t.Log(checkpoint.RequestValue(req, "name", nil, 1)) 95 t.Log(checkpoint.RequestValue(req, "ext", nil, 1)) 96 97 data, err := io.ReadAll(req.WAFRaw().Body) 98 if err != nil { 99 t.Fatal(err) 100 } 101 t.Log(string(data)) 102 }