mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-09-18 15:30:28 +00:00
https://github.com/XTLS/Xray-core/pull/6632#issuecomment-5430735467 --------- Co-authored-by: 风扇滑翔翼 <Fangliding.fshxy@outlook.com>
116 lines
2.8 KiB
Go
116 lines
2.8 KiB
Go
package splithttp_test
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/xtls/xray-core/common"
|
|
"github.com/xtls/xray-core/common/buf"
|
|
. "github.com/xtls/xray-core/transport/internet/splithttp"
|
|
)
|
|
|
|
func Test_GetNormalizedPath(t *testing.T) {
|
|
tests := []struct {
|
|
TestName string
|
|
Path string
|
|
SessionIDPlacement string
|
|
SeqPlacement string
|
|
Expected string
|
|
}{
|
|
{
|
|
TestName: "default placement keeps trailing slash",
|
|
Path: "/sh",
|
|
Expected: "/sh/",
|
|
},
|
|
{
|
|
TestName: "query string is stripped",
|
|
Path: "/?world",
|
|
Expected: "/",
|
|
},
|
|
{
|
|
TestName: "both off path drops trailing slash",
|
|
Path: "/stream",
|
|
SessionIDPlacement: "query",
|
|
SeqPlacement: "query",
|
|
Expected: "/stream",
|
|
},
|
|
{
|
|
TestName: "both off path keeps file-like path",
|
|
Path: "/stream/filename.extension",
|
|
SessionIDPlacement: "query",
|
|
SeqPlacement: "header",
|
|
Expected: "/stream/filename.extension",
|
|
},
|
|
{
|
|
TestName: "seq in path keeps trailing slash",
|
|
Path: "/stream",
|
|
SessionIDPlacement: "query",
|
|
Expected: "/stream/",
|
|
},
|
|
{
|
|
TestName: "session in path keeps trailing slash",
|
|
Path: "/stream",
|
|
SeqPlacement: "cookie",
|
|
Expected: "/stream/",
|
|
},
|
|
{
|
|
TestName: "existing trailing slash preserved",
|
|
Path: "/stream/",
|
|
SessionIDPlacement: "query",
|
|
SeqPlacement: "query",
|
|
Expected: "/stream/",
|
|
},
|
|
{
|
|
TestName: "root unchanged",
|
|
Path: "/",
|
|
SessionIDPlacement: "query",
|
|
SeqPlacement: "query",
|
|
Expected: "/",
|
|
},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.TestName, func(t *testing.T) {
|
|
c := Config{
|
|
Path: test.Path,
|
|
SessionIDPlacement: test.SessionIDPlacement,
|
|
SeqPlacement: test.SeqPlacement,
|
|
}
|
|
assert.Equal(t, test.Expected, c.GetNormalizedPath())
|
|
})
|
|
}
|
|
}
|
|
|
|
func Test_FillPacketRequest_GetBody(t *testing.T) {
|
|
data := []byte("hello xray")
|
|
payload := buf.MergeBytes(nil, data)
|
|
|
|
req, err := http.NewRequest("POST", "https://example.com/", nil)
|
|
common.Must(err)
|
|
|
|
config := &Config{}
|
|
config.FillPacketRequest(req, "sess", "0", payload)
|
|
|
|
if req.GetBody == nil {
|
|
t.Fatalf("Expected GetBody to be set")
|
|
}
|
|
|
|
first, err := io.ReadAll(req.Body)
|
|
common.Must(err)
|
|
|
|
if string(data) != string(first) {
|
|
t.Fatalf("Body mismatch. Format %q and %q are not equal", data, first)
|
|
}
|
|
|
|
body2, err := req.GetBody()
|
|
common.Must(err)
|
|
|
|
second, err := io.ReadAll(body2)
|
|
common.Must(err)
|
|
|
|
if string(data) != string(second) {
|
|
t.Fatalf("Replayed body mismatch. Format %q and %q are not equal", data, second)
|
|
}
|
|
}
|