diff --git a/testing/scenarios/data/test_5_client.json b/testing/scenarios/data/test_5_client.json new file mode 100644 index 00000000..7b3615d1 --- /dev/null +++ b/testing/scenarios/data/test_5_client.json @@ -0,0 +1,24 @@ +{ + "port": 50040, + "inbound": { + "protocol": "http", + "settings": {} + }, + "outbound": { + "protocol": "vmess", + "settings": { + "vnext": [ + { + "address": "127.0.0.1", + "port": 50041, + "users": [ + { + "id": "d17a1af7-efa5-42ca-b7e9-6a35282d737f", + "alterId": 10 + } + ] + } + ] + } + } +} diff --git a/testing/scenarios/data/test_5_server.json b/testing/scenarios/data/test_5_server.json new file mode 100644 index 00000000..2908d7a7 --- /dev/null +++ b/testing/scenarios/data/test_5_server.json @@ -0,0 +1,19 @@ +{ + "port": 50041, + "inbound": { + "protocol": "vmess", + "settings": { + "clients": [ + { + "id": "d17a1af7-efa5-42ca-b7e9-6a35282d737f", + "level": 1, + "alterId": 10 + } + ] + } + }, + "outbound": { + "protocol": "freedom", + "settings": {} + } +} diff --git a/testing/scenarios/http_test.go b/testing/scenarios/http_test.go new file mode 100644 index 00000000..5b3b19b2 --- /dev/null +++ b/testing/scenarios/http_test.go @@ -0,0 +1,47 @@ +package scenarios + +import ( + "io/ioutil" + "net/http" + "net/url" + "testing" + + v2net "github.com/v2ray/v2ray-core/common/net" + v2testing "github.com/v2ray/v2ray-core/testing" + "github.com/v2ray/v2ray-core/testing/assert" + v2http "github.com/v2ray/v2ray-core/testing/servers/http" +) + +func TestHttpProxy(t *testing.T) { + v2testing.Current(t) + + httpServer := &v2http.Server{ + Port: v2net.Port(50042), + PathHandler: make(map[string]http.HandlerFunc), + } + _, err := httpServer.Start() + assert.Error(err).IsNil() + defer httpServer.Close() + + assert.Error(InitializeServerSetOnce("test_5")).IsNil() + + transport := &http.Transport{ + Proxy: func(req *http.Request) (*url.URL, error) { + return url.Parse("http://127.0.0.1:50040/") + }, + } + + client := &http.Client{ + Transport: transport, + } + + resp, err := client.Get("http://127.0.0.1:50042/") + assert.Error(err).IsNil() + assert.Int(resp.StatusCode).Equals(200) + + content, err := ioutil.ReadAll(resp.Body) + assert.Error(err).IsNil() + assert.StringLiteral(string(content)).Equals("Home") + + CloseAllServers() +} diff --git a/testing/scenarios/server_env.go b/testing/scenarios/server_env.go index 812f873b..0bc010c1 100644 --- a/testing/scenarios/server_env.go +++ b/testing/scenarios/server_env.go @@ -12,6 +12,7 @@ import ( _ "github.com/v2ray/v2ray-core/proxy/blackhole" _ "github.com/v2ray/v2ray-core/proxy/dokodemo" _ "github.com/v2ray/v2ray-core/proxy/freedom" + _ "github.com/v2ray/v2ray-core/proxy/http" _ "github.com/v2ray/v2ray-core/proxy/socks" _ "github.com/v2ray/v2ray-core/proxy/vmess/inbound" _ "github.com/v2ray/v2ray-core/proxy/vmess/outbound" diff --git a/testing/servers/http/http.go b/testing/servers/http/http.go new file mode 100644 index 00000000..112783bf --- /dev/null +++ b/testing/servers/http/http.go @@ -0,0 +1,36 @@ +package tcp + +import ( + "net/http" + + v2net "github.com/v2ray/v2ray-core/common/net" +) + +type Server struct { + Port v2net.Port + PathHandler map[string]http.HandlerFunc + accepting bool +} + +func (server *Server) ServeHTTP(resp http.ResponseWriter, req *http.Request) { + if req.URL.Path == "/" { + resp.Header().Set("Content-Type", "text/plain; charset=utf-8") + resp.WriteHeader(http.StatusOK) + resp.Write([]byte("Home")) + return + } + + handler, found := server.PathHandler[req.URL.Path] + if found { + handler(resp, req) + } +} + +func (server *Server) Start() (v2net.Destination, error) { + go http.ListenAndServe(":"+server.Port.String(), server) + return v2net.TCPDestination(v2net.IPAddress([]byte{127, 0, 0, 1}), v2net.Port(server.Port)), nil +} + +func (this *Server) Close() { + this.accepting = false +}