v2ray-core/testing/scenarios/http_test.go

75 lines
1.6 KiB
Go
Raw Normal View History

2016-01-24 20:48:38 +00:00
package scenarios
import (
"io/ioutil"
"net/http"
"net/url"
"testing"
"v2ray.com/core"
"v2ray.com/core/app/proxyman"
"v2ray.com/core/common/net"
"v2ray.com/core/common/serial"
"v2ray.com/core/proxy/freedom"
v2http "v2ray.com/core/proxy/http"
2017-10-24 14:15:35 +00:00
. "v2ray.com/ext/assert"
v2httptest "v2ray.com/core/testing/servers/http"
2016-01-24 20:48:38 +00:00
)
func TestHttpConformance(t *testing.T) {
2017-10-24 14:15:35 +00:00
assert := With(t)
2016-01-24 20:48:38 +00:00
httpServerPort := pickPort()
httpServer := &v2httptest.Server{
Port: httpServerPort,
2016-01-24 20:48:38 +00:00
PathHandler: make(map[string]http.HandlerFunc),
}
_, err := httpServer.Start()
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
2016-01-24 20:48:38 +00:00
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{}),
},
2016-01-24 20:48:38 +00:00
},
}
2017-05-17 22:39:30 +00:00
servers, err := InitializeServerConfigs(serverConfig)
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
2016-06-10 21:01:17 +00:00
{
transport := &http.Transport{
Proxy: func(req *http.Request) (*url.URL, error) {
return url.Parse("http://127.0.0.1:" + serverPort.String())
},
}
2016-06-10 21:01:17 +00:00
client := &http.Client{
Transport: transport,
}
2016-06-10 21:01:17 +00:00
resp, err := client.Get("http://127.0.0.1:" + httpServerPort.String())
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
assert(resp.StatusCode, Equals, 200)
2016-06-10 21:01:17 +00:00
content, err := ioutil.ReadAll(resp.Body)
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
assert(string(content), Equals, "Home")
2016-06-10 21:01:17 +00:00
}
2017-05-17 22:39:30 +00:00
CloseAllServers(servers)
2016-06-10 21:01:17 +00:00
}