1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-26 03:57:42 -05:00

add test case for http proxy

This commit is contained in:
v2ray 2016-02-20 23:27:06 +01:00
parent ca42fa1fd5
commit b60f491cec
2 changed files with 33 additions and 5 deletions

View File

@ -165,7 +165,8 @@ func (this *HttpProxyServer) transport(input io.Reader, output io.Writer, ray ra
}() }()
} }
func stripHopByHopHeaders(request *http.Request) { // @VisibleForTesting
func StripHopByHopHeaders(request *http.Request) {
// Strip hop-by-hop header basaed on RFC: // Strip hop-by-hop header basaed on RFC:
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.5.1 // http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.5.1
// https://www.mnot.net/blog/2011/07/11/what_proxies_must_do // https://www.mnot.net/blog/2011/07/11/what_proxies_must_do
@ -213,7 +214,7 @@ func (this *HttpProxyServer) handlePlainHTTP(request *http.Request, dest v2net.D
} }
request.Host = request.URL.Host request.Host = request.URL.Host
stripHopByHopHeaders(request) StripHopByHopHeaders(request)
requestBuffer := alloc.NewBuffer().Clear() // Don't release this buffer as it is passed into a Packet. requestBuffer := alloc.NewBuffer().Clear() // Don't release this buffer as it is passed into a Packet.
request.Write(requestBuffer) request.Write(requestBuffer)

View File

@ -1,14 +1,22 @@
package http package http_test
import ( import (
"bufio" "bufio"
"github.com/v2ray/v2ray-core/testing/assert"
"net/http" "net/http"
"strings" "strings"
"testing" "testing"
testdispatcher "github.com/v2ray/v2ray-core/app/dispatcher/testing"
v2nettesting "github.com/v2ray/v2ray-core/common/net/testing"
netassert "github.com/v2ray/v2ray-core/common/net/testing/assert"
. "github.com/v2ray/v2ray-core/proxy/http"
v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert"
) )
func TestHopByHopHeadersStrip(t *testing.T) { func TestHopByHopHeadersStrip(t *testing.T) {
v2testing.Current(t)
rawRequest := `GET /pkg/net/http/ HTTP/1.1 rawRequest := `GET /pkg/net/http/ HTTP/1.1
Host: golang.org Host: golang.org
Connection: keep-alive,Foo, Bar Connection: keep-alive,Foo, Bar
@ -32,10 +40,29 @@ Accept-Language: de,en;q=0.7,en-us;q=0.3
assert.StringLiteral(req.Header.Get("Proxy-Connection")).Equals("keep-alive") assert.StringLiteral(req.Header.Get("Proxy-Connection")).Equals("keep-alive")
assert.StringLiteral(req.Header.Get("Proxy-Authenticate")).Equals("abc") assert.StringLiteral(req.Header.Get("Proxy-Authenticate")).Equals("abc")
stripHopByHopHeaders(req) StripHopByHopHeaders(req)
assert.StringLiteral(req.Header.Get("Connection")).Equals("close") assert.StringLiteral(req.Header.Get("Connection")).Equals("close")
assert.StringLiteral(req.Header.Get("Foo")).Equals("") assert.StringLiteral(req.Header.Get("Foo")).Equals("")
assert.StringLiteral(req.Header.Get("Bar")).Equals("") assert.StringLiteral(req.Header.Get("Bar")).Equals("")
assert.StringLiteral(req.Header.Get("Proxy-Connection")).Equals("") assert.StringLiteral(req.Header.Get("Proxy-Connection")).Equals("")
assert.StringLiteral(req.Header.Get("Proxy-Authenticate")).Equals("") assert.StringLiteral(req.Header.Get("Proxy-Authenticate")).Equals("")
} }
func TestNormalGetRequest(t *testing.T) {
v2testing.Current(t)
testPacketDispatcher := testdispatcher.NewTestPacketDispatcher(nil)
httpProxy := NewHttpProxyServer(&Config{}, testPacketDispatcher)
defer httpProxy.Close()
port := v2nettesting.PickPort()
err := httpProxy.Listen(port)
assert.Error(err).IsNil()
netassert.Port(port).Equals(httpProxy.Port())
httpClient := &http.Client{}
resp, err := httpClient.Get("http://127.0.0.1:" + port.String() + "/")
assert.Error(err).IsNil()
assert.Int(resp.StatusCode).Equals(400)
}