1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-10-30 15:57:35 -04:00
v2fly/proxy/http/server_test.go

80 lines
2.3 KiB
Go
Raw Normal View History

2016-02-20 17:27:06 -05:00
package http_test
import (
"bufio"
"net/http"
"strings"
"testing"
2016-02-20 17:27:06 -05:00
2016-08-20 14:55:45 -04:00
testdispatcher "v2ray.com/core/app/dispatcher/testing"
"v2ray.com/core/common/dice"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/proxy"
. "v2ray.com/core/proxy/http"
"v2ray.com/core/testing/assert"
"v2ray.com/core/transport/internet"
2016-06-14 16:54:08 -04:00
2016-08-20 14:55:45 -04:00
_ "v2ray.com/core/transport/internet/tcp"
)
func TestHopByHopHeadersStrip(t *testing.T) {
2016-05-24 15:55:46 -04:00
assert := assert.On(t)
2016-02-20 17:27:06 -05:00
2016-02-03 15:36:52 -05:00
rawRequest := `GET /pkg/net/http/ HTTP/1.1
Host: golang.org
Connection: keep-alive,Foo, Bar
Foo: foo
Bar: bar
Proxy-Connection: keep-alive
Proxy-Authenticate: abc
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X; de-de) AppleWebKit/523.10.3 (KHTML, like Gecko) Version/3.0.4 Safari/523.10
Accept-Encoding: gzip
Accept-Charset: ISO-8859-1,UTF-8;q=0.7,*;q=0.7
Cache-Control: no-cache
Accept-Language: de,en;q=0.7,en-us;q=0.3
`
b := bufio.NewReader(strings.NewReader(rawRequest))
req, err := http.ReadRequest(b)
assert.Error(err).IsNil()
2016-05-24 15:55:46 -04:00
assert.String(req.Header.Get("Foo")).Equals("foo")
assert.String(req.Header.Get("Bar")).Equals("bar")
assert.String(req.Header.Get("Connection")).Equals("keep-alive,Foo, Bar")
assert.String(req.Header.Get("Proxy-Connection")).Equals("keep-alive")
assert.String(req.Header.Get("Proxy-Authenticate")).Equals("abc")
2016-02-20 17:27:06 -05:00
StripHopByHopHeaders(req)
2016-05-24 15:55:46 -04:00
assert.String(req.Header.Get("Connection")).Equals("close")
assert.String(req.Header.Get("Foo")).Equals("")
assert.String(req.Header.Get("Bar")).Equals("")
assert.String(req.Header.Get("Proxy-Connection")).Equals("")
assert.String(req.Header.Get("Proxy-Authenticate")).Equals("")
}
2016-02-20 17:27:06 -05:00
func TestNormalGetRequest(t *testing.T) {
2016-05-24 15:55:46 -04:00
assert := assert.On(t)
2016-02-20 17:27:06 -05:00
testPacketDispatcher := testdispatcher.NewTestPacketDispatcher(nil)
2016-08-15 16:12:11 -04:00
port := v2net.Port(dice.Roll(20000) + 10000)
2016-06-14 16:54:08 -04:00
httpProxy := NewServer(
2016-08-25 15:55:49 -04:00
&ServerConfig{},
2016-06-14 16:54:08 -04:00
testPacketDispatcher,
&proxy.InboundHandlerMeta{
Address: v2net.LocalHostIP,
Port: port,
2016-10-02 17:43:58 -04:00
StreamSettings: &internet.StreamConfig{
Network: v2net.Network_RawTCP,
2016-06-14 16:54:08 -04:00
}})
2016-02-20 17:27:06 -05:00
defer httpProxy.Close()
2016-06-03 18:38:22 -04:00
err := httpProxy.Start()
2016-02-20 17:27:06 -05:00
assert.Error(err).IsNil()
2016-05-24 09:29:08 -04:00
assert.Port(port).Equals(httpProxy.Port())
2016-02-20 17:27:06 -05:00
httpClient := &http.Client{}
resp, err := httpClient.Get("http://127.0.0.1:" + port.String() + "/")
assert.Error(err).IsNil()
assert.Int(resp.StatusCode).Equals(400)
}