Files
EdgeNode/internal/waf/checkpoints/request_json_arg_test.go

101 lines
2.6 KiB
Go
Raw Permalink Normal View History

2020-10-08 15:06:42 +08:00
package checkpoints
import (
"bytes"
2022-08-04 11:34:06 +08:00
"io"
2020-10-08 15:06:42 +08:00
"net/http"
"testing"
2024-07-27 15:42:50 +08:00
"github.com/TeaOSLab/EdgeNode/internal/waf/requests"
2020-10-08 15:06:42 +08:00
)
func TestRequestJSONArgCheckpoint_RequestValue_Map(t *testing.T) {
rawReq, err := http.NewRequest(http.MethodPost, "http://teaos.cn", bytes.NewBuffer([]byte(`
{
"name": "lu",
"age": 20,
"books": [ "PHP", "Golang", "Python" ]
}
`)))
if err != nil {
t.Fatal(err)
}
2021-07-18 15:51:49 +08:00
req := requests.NewTestRequest(rawReq)
2020-10-08 15:06:42 +08:00
//req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
checkpoint := new(RequestJSONArgCheckpoint)
t.Log(checkpoint.RequestValue(req, "name", nil, 1))
t.Log(checkpoint.RequestValue(req, "age", nil, 1))
t.Log(checkpoint.RequestValue(req, "Hello", nil, 1))
t.Log(checkpoint.RequestValue(req, "", nil, 1))
t.Log(checkpoint.RequestValue(req, "books", nil, 1))
t.Log(checkpoint.RequestValue(req, "books.1", nil, 1))
2020-10-08 15:06:42 +08:00
2022-08-04 11:34:06 +08:00
body, err := io.ReadAll(req.WAFRaw().Body)
2020-10-08 15:06:42 +08:00
if err != nil {
t.Fatal(err)
}
t.Log(string(body))
}
func TestRequestJSONArgCheckpoint_RequestValue_Array(t *testing.T) {
rawReq, err := http.NewRequest(http.MethodPost, "http://teaos.cn", bytes.NewBuffer([]byte(`
[{
"name": "lu",
"age": 20,
"books": [ "PHP", "Golang", "Python" ]
}]
`)))
if err != nil {
t.Fatal(err)
}
2021-07-18 15:51:49 +08:00
req := requests.NewTestRequest(rawReq)
2020-10-08 15:06:42 +08:00
//req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
checkpoint := new(RequestJSONArgCheckpoint)
t.Log(checkpoint.RequestValue(req, "0.name", nil, 1))
t.Log(checkpoint.RequestValue(req, "0.age", nil, 1))
t.Log(checkpoint.RequestValue(req, "0.Hello", nil, 1))
t.Log(checkpoint.RequestValue(req, "", nil, 1))
t.Log(checkpoint.RequestValue(req, "0.books", nil, 1))
t.Log(checkpoint.RequestValue(req, "0.books.1", nil, 1))
2020-10-08 15:06:42 +08:00
2022-08-04 11:34:06 +08:00
body, err := io.ReadAll(req.WAFRaw().Body)
2020-10-08 15:06:42 +08:00
if err != nil {
t.Fatal(err)
}
t.Log(string(body))
}
func TestRequestJSONArgCheckpoint_RequestValue_Error(t *testing.T) {
rawReq, err := http.NewRequest(http.MethodPost, "http://teaos.cn", bytes.NewBuffer([]byte(`
[{
"name": "lu",
"age": 20,
"books": [ "PHP", "Golang", "Python" ]
}]
`)))
if err != nil {
t.Fatal(err)
}
2021-07-18 15:51:49 +08:00
req := requests.NewTestRequest(rawReq)
2020-10-08 15:06:42 +08:00
//req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
checkpoint := new(RequestJSONArgCheckpoint)
t.Log(checkpoint.RequestValue(req, "0.name", nil, 1))
t.Log(checkpoint.RequestValue(req, "0.age", nil, 1))
t.Log(checkpoint.RequestValue(req, "0.Hello", nil, 1))
t.Log(checkpoint.RequestValue(req, "", nil, 1))
t.Log(checkpoint.RequestValue(req, "0.books", nil, 1))
t.Log(checkpoint.RequestValue(req, "0.books.1", nil, 1))
2020-10-08 15:06:42 +08:00
2022-08-04 11:34:06 +08:00
body, err := io.ReadAll(req.WAFRaw().Body)
2020-10-08 15:06:42 +08:00
if err != nil {
t.Fatal(err)
}
t.Log(string(body))
}