mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-21 01:27:03 -05:00
test case for http proxy
This commit is contained in:
parent
3f5bda898c
commit
96f582da98
24
testing/scenarios/data/test_5_client.json
Normal file
24
testing/scenarios/data/test_5_client.json
Normal file
@ -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
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
19
testing/scenarios/data/test_5_server.json
Normal file
19
testing/scenarios/data/test_5_server.json
Normal file
@ -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": {}
|
||||
}
|
||||
}
|
47
testing/scenarios/http_test.go
Normal file
47
testing/scenarios/http_test.go
Normal file
@ -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()
|
||||
}
|
@ -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"
|
||||
|
36
testing/servers/http/http.go
Normal file
36
testing/servers/http/http.go
Normal file
@ -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
|
||||
}
|
Loading…
Reference in New Issue
Block a user