2016-01-24 20:48:38 +00:00
|
|
|
package scenarios
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
|
|
|
|
2017-03-31 15:06:16 +00:00
|
|
|
"v2ray.com/core"
|
|
|
|
"v2ray.com/core/app/proxyman"
|
|
|
|
v2net "v2ray.com/core/common/net"
|
|
|
|
"v2ray.com/core/common/serial"
|
|
|
|
"v2ray.com/core/proxy/freedom"
|
|
|
|
v2http "v2ray.com/core/proxy/http"
|
2016-08-20 18:55:45 +00:00
|
|
|
"v2ray.com/core/testing/assert"
|
2017-03-31 15:06:16 +00:00
|
|
|
v2httptest "v2ray.com/core/testing/servers/http"
|
2016-01-24 20:48:38 +00:00
|
|
|
)
|
|
|
|
|
2017-03-31 15:06:16 +00:00
|
|
|
func TestHttpConformance(t *testing.T) {
|
2016-05-24 19:55:46 +00:00
|
|
|
assert := assert.On(t)
|
2016-01-24 20:48:38 +00:00
|
|
|
|
2017-03-31 15:06:16 +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()
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
defer httpServer.Close()
|
|
|
|
|
2017-03-31 15:06:16 +00:00
|
|
|
serverPort := pickPort()
|
|
|
|
serverConfig := &core.Config{
|
|
|
|
Inbound: []*proxyman.InboundHandlerConfig{
|
|
|
|
{
|
|
|
|
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
|
|
|
PortRange: v2net.SinglePortRange(serverPort),
|
|
|
|
Listen: v2net.NewIPOrDomain(v2net.LocalHostIP),
|
|
|
|
}),
|
|
|
|
ProxySettings: serial.ToTypedMessage(&v2http.ServerConfig{}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Outbound: []*proxyman.OutboundHandlerConfig{
|
|
|
|
{
|
|
|
|
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
|
|
|
|
},
|
2016-01-24 20:48:38 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-03-31 15:06:16 +00:00
|
|
|
assert.Error(InitializeServerConfig(serverConfig)).IsNil()
|
2016-06-10 21:01:17 +00:00
|
|
|
|
2017-03-31 15:06:16 +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
|
|
|
|
2017-03-31 15:06:16 +00:00
|
|
|
client := &http.Client{
|
|
|
|
Transport: transport,
|
|
|
|
}
|
2016-06-10 21:01:17 +00:00
|
|
|
|
2017-03-31 15:06:16 +00:00
|
|
|
resp, err := client.Get("http://127.0.0.1:" + httpServerPort.String())
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
assert.Int(resp.StatusCode).Equals(200)
|
2016-06-10 21:01:17 +00:00
|
|
|
|
2017-03-31 15:06:16 +00:00
|
|
|
content, err := ioutil.ReadAll(resp.Body)
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
assert.String(string(content)).Equals("Home")
|
2016-06-10 21:01:17 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseAllServers()
|
|
|
|
}
|