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

     1  package checkpoints
     2  
     3  import (
     4  	"bytes"
     5  	"github.com/TeaOSLab/EdgeNode/internal/waf/requests"
     6  	"github.com/iwind/TeaGo/types"
     7  	"io"
     8  	"net/http"
     9  	"runtime"
    10  	"strings"
    11  	"testing"
    12  )
    13  
    14  func TestRequestAllCheckpoint_RequestValue(t *testing.T) {
    15  	req, err := http.NewRequest(http.MethodPost, "http://teaos.cn/hello/world", bytes.NewBuffer([]byte("123456")))
    16  	if err != nil {
    17  		t.Fatal(err)
    18  	}
    19  
    20  	checkpoint := new(RequestAllCheckpoint)
    21  	v, _, sysErr, userErr := checkpoint.RequestValue(requests.NewTestRequest(req), "", nil, 1)
    22  	if sysErr != nil {
    23  		t.Fatal(sysErr)
    24  	}
    25  	if userErr != nil {
    26  		t.Fatal(userErr)
    27  	}
    28  	if v != nil {
    29  		vv, ok := v.([][]byte)
    30  		if ok {
    31  			for _, v2 := range vv {
    32  				t.Log(string(v2), ":", v2)
    33  			}
    34  		}
    35  	}
    36  
    37  	body, err := io.ReadAll(req.Body)
    38  	if err != nil {
    39  		t.Fatal(err)
    40  	}
    41  	t.Log(string(body))
    42  }
    43  
    44  func TestRequestAllCheckpoint_RequestValue_Max(t *testing.T) {
    45  	req, err := http.NewRequest(http.MethodPost, "http://teaos.cn", bytes.NewBuffer([]byte(strings.Repeat("123456", 10240000))))
    46  	if err != nil {
    47  		t.Fatal(err)
    48  	}
    49  
    50  	checkpoint := new(RequestBodyCheckpoint)
    51  	value, _, err, _ := checkpoint.RequestValue(requests.NewTestRequest(req), "", nil, 1)
    52  	if err != nil {
    53  		t.Fatal(err)
    54  	}
    55  	t.Log("value bytes:", len(types.String(value)))
    56  
    57  	body, err := io.ReadAll(req.Body)
    58  	if err != nil {
    59  		t.Fatal(err)
    60  	}
    61  	t.Log("raw bytes:", len(body))
    62  }
    63  
    64  func BenchmarkRequestAllCheckpoint_RequestValue(b *testing.B) {
    65  	runtime.GOMAXPROCS(1)
    66  
    67  	req, err := http.NewRequest(http.MethodPost, "http://teaos.cn/hello/world", bytes.NewBuffer(bytes.Repeat([]byte("HELLO"), 1024)))
    68  	if err != nil {
    69  		b.Fatal(err)
    70  	}
    71  
    72  	checkpoint := new(RequestAllCheckpoint)
    73  	for i := 0; i < b.N; i++ {
    74  		_, _, _, _ = checkpoint.RequestValue(requests.NewTestRequest(req), "", nil, 1)
    75  	}
    76  }