1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-29 02:35:23 +00:00
v2fly/testing/scenarios/http_test.go

77 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"
2017-01-13 23:27:45 +00:00
"v2ray.com/core/common/net"
2016-08-20 18:55:45 +00:00
"v2ray.com/core/testing/assert"
v2http "v2ray.com/core/testing/servers/http"
2016-01-24 20:48:38 +00:00
)
func TestHttpProxy(t *testing.T) {
2016-05-24 19:55:46 +00:00
assert := assert.On(t)
2016-01-24 20:48:38 +00:00
httpServer := &v2http.Server{
2017-01-13 23:27:45 +00:00
Port: net.Port(50042),
2016-01-24 20:48:38 +00:00
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()
2016-05-24 19:55:46 +00:00
assert.String(string(content)).Equals("Home")
2016-01-24 20:48:38 +00:00
CloseAllServers()
}
2016-06-10 21:01:17 +00:00
func TestBlockHTTP(t *testing.T) {
assert := assert.On(t)
httpServer := &v2http.Server{
2017-01-13 23:27:45 +00:00
Port: net.Port(50042),
2016-06-10 21:01:17 +00:00
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:50049/")
assert.Error(err).IsNil()
assert.Int(resp.StatusCode).Equals(403)
CloseAllServers()
}