mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2026-04-22 23:27:15 +08:00
chore: add mode restrictions test back
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
package xhttp
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
"github.com/metacubex/http"
|
||||
"github.com/metacubex/http/httptest"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestServerHandlerModeRestrictions(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
mode string
|
||||
method string
|
||||
target string
|
||||
wantStatus int
|
||||
}{
|
||||
{
|
||||
name: "StreamOneAcceptsStreamOne",
|
||||
mode: "stream-one",
|
||||
method: http.MethodPost,
|
||||
target: "https://example.com/xhttp/",
|
||||
wantStatus: http.StatusOK,
|
||||
},
|
||||
{
|
||||
name: "StreamOneRejectsSessionDownload",
|
||||
mode: "stream-one",
|
||||
method: http.MethodGet,
|
||||
target: "https://example.com/xhttp/session",
|
||||
wantStatus: http.StatusNotFound,
|
||||
},
|
||||
{
|
||||
name: "StreamUpAcceptsStreamOne",
|
||||
mode: "stream-up",
|
||||
method: http.MethodPost,
|
||||
target: "https://example.com/xhttp/",
|
||||
wantStatus: http.StatusOK,
|
||||
},
|
||||
{
|
||||
name: "StreamUpAllowsDownloadEndpoint",
|
||||
mode: "stream-up",
|
||||
method: http.MethodGet,
|
||||
target: "https://example.com/xhttp/session",
|
||||
wantStatus: http.StatusOK,
|
||||
},
|
||||
{
|
||||
name: "StreamUpRejectsPacketUpload",
|
||||
mode: "stream-up",
|
||||
method: http.MethodPost,
|
||||
target: "https://example.com/xhttp/session/0",
|
||||
wantStatus: http.StatusNotFound,
|
||||
},
|
||||
{
|
||||
name: "PacketUpAllowsDownloadEndpoint",
|
||||
mode: "packet-up",
|
||||
method: http.MethodGet,
|
||||
target: "https://example.com/xhttp/session",
|
||||
wantStatus: http.StatusOK,
|
||||
},
|
||||
{
|
||||
name: "PacketUpRejectsStreamOne",
|
||||
mode: "packet-up",
|
||||
method: http.MethodPost,
|
||||
target: "https://example.com/xhttp/",
|
||||
wantStatus: http.StatusNotFound,
|
||||
},
|
||||
{
|
||||
name: "PacketUpRejectsStreamUpUpload",
|
||||
mode: "packet-up",
|
||||
method: http.MethodPost,
|
||||
target: "https://example.com/xhttp/session",
|
||||
wantStatus: http.StatusNotFound,
|
||||
},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
config := Config{
|
||||
Path: "/xhttp",
|
||||
Mode: testCase.mode,
|
||||
}
|
||||
handler, err := NewServerHandler(ServerOption{
|
||||
Config: config,
|
||||
ConnHandler: func(conn net.Conn) {
|
||||
_ = conn.Close()
|
||||
},
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
|
||||
req := httptest.NewRequest(testCase.method, testCase.target, io.NopCloser(http.NoBody))
|
||||
recorder := httptest.NewRecorder()
|
||||
|
||||
err = config.FillStreamRequest(req, "")
|
||||
assert.NoError(t, err)
|
||||
|
||||
handler.ServeHTTP(recorder, req)
|
||||
|
||||
assert.Equal(t, testCase.wantStatus, recorder.Result().StatusCode)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user