1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-30 05:56:54 -05:00

test case for http post and connect

This commit is contained in:
Darien Raymond 2017-11-17 16:53:00 +01:00
parent afcad409e2
commit b859bdf684
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169

View File

@ -1,11 +1,17 @@
package scenarios
import (
"bytes"
"crypto/rand"
"io"
"io/ioutil"
"net/http"
"net/url"
"testing"
"v2ray.com/core/common"
"v2ray.com/core/common/buf"
"v2ray.com/core"
"v2ray.com/core/app/proxyman"
"v2ray.com/core/common/net"
@ -13,6 +19,7 @@ import (
"v2ray.com/core/proxy/freedom"
v2http "v2ray.com/core/proxy/http"
v2httptest "v2ray.com/core/testing/servers/http"
"v2ray.com/core/testing/servers/tcp"
. "v2ray.com/ext/assert"
)
@ -73,6 +80,141 @@ func TestHttpConformance(t *testing.T) {
CloseAllServers(servers)
}
func TestHttpConnectMethod(t *testing.T) {
assert := With(t)
tcpServer := tcp.Server{
MsgProcessor: xor,
}
dest, err := tcpServer.Start()
assert(err, IsNil)
defer tcpServer.Close()
serverPort := pickPort()
serverConfig := &core.Config{
Inbound: []*proxyman.InboundHandlerConfig{
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(serverPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&v2http.ServerConfig{}),
},
},
Outbound: []*proxyman.OutboundHandlerConfig{
{
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
},
},
}
servers, err := InitializeServerConfigs(serverConfig)
assert(err, IsNil)
{
transport := &http.Transport{
Proxy: func(req *http.Request) (*url.URL, error) {
return url.Parse("http://127.0.0.1:" + serverPort.String())
},
}
client := &http.Client{
Transport: transport,
}
payload := make([]byte, 1024*64)
common.Must2(rand.Read(payload))
req, err := http.NewRequest("Connect", "http://"+dest.NetAddr()+"/", bytes.NewReader(payload))
common.Must(err)
resp, err := client.Do(req)
assert(err, IsNil)
assert(resp.StatusCode, Equals, 200)
content := make([]byte, len(payload))
common.Must2(io.ReadFull(resp.Body, content))
assert(err, IsNil)
assert(content, Equals, xor(payload))
}
CloseAllServers(servers)
}
func TestHttpPost(t *testing.T) {
assert := With(t)
httpServerPort := pickPort()
httpServer := &v2httptest.Server{
Port: httpServerPort,
PathHandler: map[string]http.HandlerFunc{
"/testpost": func(w http.ResponseWriter, r *http.Request) {
payload, err := buf.ReadAllToBytes(r.Body)
r.Body.Close()
if err != nil {
w.WriteHeader(500)
w.Write([]byte("Unable to read all payload"))
return
}
payload = xor(payload)
w.Write(payload)
},
},
}
_, err := httpServer.Start()
assert(err, IsNil)
defer httpServer.Close()
serverPort := pickPort()
serverConfig := &core.Config{
Inbound: []*proxyman.InboundHandlerConfig{
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(serverPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&v2http.ServerConfig{}),
},
},
Outbound: []*proxyman.OutboundHandlerConfig{
{
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
},
},
}
servers, err := InitializeServerConfigs(serverConfig)
assert(err, IsNil)
{
transport := &http.Transport{
Proxy: func(req *http.Request) (*url.URL, error) {
return url.Parse("http://127.0.0.1:" + serverPort.String())
},
}
client := &http.Client{
Transport: transport,
}
payload := make([]byte, 1024*64)
common.Must2(rand.Read(payload))
resp, err := client.Post("http://127.0.0.1:"+httpServerPort.String()+"/testpost", "application/x-www-form-urlencoded", bytes.NewReader(payload))
assert(err, IsNil)
assert(resp.StatusCode, Equals, 200)
content, err := ioutil.ReadAll(resp.Body)
assert(err, IsNil)
assert(content, Equals, xor(payload))
}
CloseAllServers(servers)
}
func setProxyBasicAuth(req *http.Request, user, pass string) {
req.SetBasicAuth(user, pass)
req.Header.Set("Proxy-Authorization", req.Header.Get("Authorization"))