github.com/TeaOSLab/EdgeNode@v1.3.8/internal/waf/checkpoints/request_json_arg_test.go (about)

     1  package checkpoints
     2  
     3  import (
     4  	"bytes"
     5  	"github.com/TeaOSLab/EdgeNode/internal/waf/requests"
     6  	"io"
     7  	"net/http"
     8  	"testing"
     9  )
    10  
    11  func TestRequestJSONArgCheckpoint_RequestValue_Map(t *testing.T) {
    12  	rawReq, err := http.NewRequest(http.MethodPost, "http://teaos.cn", bytes.NewBuffer([]byte(`
    13  {
    14  	"name": "lu",
    15  	"age": 20,
    16  	"books": [ "PHP", "Golang", "Python" ]
    17  }
    18  `)))
    19  	if err != nil {
    20  		t.Fatal(err)
    21  	}
    22  
    23  	req := requests.NewTestRequest(rawReq)
    24  	//req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
    25  
    26  	checkpoint := new(RequestJSONArgCheckpoint)
    27  	t.Log(checkpoint.RequestValue(req, "name", nil, 1))
    28  	t.Log(checkpoint.RequestValue(req, "age", nil, 1))
    29  	t.Log(checkpoint.RequestValue(req, "Hello", nil, 1))
    30  	t.Log(checkpoint.RequestValue(req, "", nil, 1))
    31  	t.Log(checkpoint.RequestValue(req, "books", nil, 1))
    32  	t.Log(checkpoint.RequestValue(req, "books.1", nil, 1))
    33  
    34  	body, err := io.ReadAll(req.WAFRaw().Body)
    35  	if err != nil {
    36  		t.Fatal(err)
    37  	}
    38  	t.Log(string(body))
    39  }
    40  
    41  func TestRequestJSONArgCheckpoint_RequestValue_Array(t *testing.T) {
    42  	rawReq, err := http.NewRequest(http.MethodPost, "http://teaos.cn", bytes.NewBuffer([]byte(`
    43  [{
    44  	"name": "lu",
    45  	"age": 20,
    46  	"books": [ "PHP", "Golang", "Python" ]
    47  }]
    48  `)))
    49  	if err != nil {
    50  		t.Fatal(err)
    51  	}
    52  
    53  	req := requests.NewTestRequest(rawReq)
    54  	//req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
    55  
    56  	checkpoint := new(RequestJSONArgCheckpoint)
    57  	t.Log(checkpoint.RequestValue(req, "0.name", nil, 1))
    58  	t.Log(checkpoint.RequestValue(req, "0.age", nil, 1))
    59  	t.Log(checkpoint.RequestValue(req, "0.Hello", nil, 1))
    60  	t.Log(checkpoint.RequestValue(req, "", nil, 1))
    61  	t.Log(checkpoint.RequestValue(req, "0.books", nil, 1))
    62  	t.Log(checkpoint.RequestValue(req, "0.books.1", nil, 1))
    63  
    64  	body, err := io.ReadAll(req.WAFRaw().Body)
    65  	if err != nil {
    66  		t.Fatal(err)
    67  	}
    68  	t.Log(string(body))
    69  }
    70  
    71  func TestRequestJSONArgCheckpoint_RequestValue_Error(t *testing.T) {
    72  	rawReq, err := http.NewRequest(http.MethodPost, "http://teaos.cn", bytes.NewBuffer([]byte(`
    73  [{
    74  	"name": "lu",
    75  	"age": 20,
    76  	"books": [ "PHP", "Golang", "Python" ]
    77  }]
    78  `)))
    79  	if err != nil {
    80  		t.Fatal(err)
    81  	}
    82  
    83  	req := requests.NewTestRequest(rawReq)
    84  	//req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
    85  
    86  	checkpoint := new(RequestJSONArgCheckpoint)
    87  	t.Log(checkpoint.RequestValue(req, "0.name", nil, 1))
    88  	t.Log(checkpoint.RequestValue(req, "0.age", nil, 1))
    89  	t.Log(checkpoint.RequestValue(req, "0.Hello", nil, 1))
    90  	t.Log(checkpoint.RequestValue(req, "", nil, 1))
    91  	t.Log(checkpoint.RequestValue(req, "0.books", nil, 1))
    92  	t.Log(checkpoint.RequestValue(req, "0.books.1", nil, 1))
    93  
    94  	body, err := io.ReadAll(req.WAFRaw().Body)
    95  	if err != nil {
    96  		t.Fatal(err)
    97  	}
    98  	t.Log(string(body))
    99  }