server_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package http_test
  2. import (
  3. "bufio"
  4. "net/http"
  5. "strings"
  6. "testing"
  7. testdispatcher "github.com/v2ray/v2ray-core/app/dispatcher/testing"
  8. v2nettesting "github.com/v2ray/v2ray-core/common/net/testing"
  9. . "github.com/v2ray/v2ray-core/proxy/http"
  10. "github.com/v2ray/v2ray-core/testing/assert"
  11. )
  12. func TestHopByHopHeadersStrip(t *testing.T) {
  13. assert := assert.On(t)
  14. rawRequest := `GET /pkg/net/http/ HTTP/1.1
  15. Host: golang.org
  16. Connection: keep-alive,Foo, Bar
  17. Foo: foo
  18. Bar: bar
  19. Proxy-Connection: keep-alive
  20. Proxy-Authenticate: abc
  21. 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
  22. Accept-Encoding: gzip
  23. Accept-Charset: ISO-8859-1,UTF-8;q=0.7,*;q=0.7
  24. Cache-Control: no-cache
  25. Accept-Language: de,en;q=0.7,en-us;q=0.3
  26. `
  27. b := bufio.NewReader(strings.NewReader(rawRequest))
  28. req, err := http.ReadRequest(b)
  29. assert.Error(err).IsNil()
  30. assert.String(req.Header.Get("Foo")).Equals("foo")
  31. assert.String(req.Header.Get("Bar")).Equals("bar")
  32. assert.String(req.Header.Get("Connection")).Equals("keep-alive,Foo, Bar")
  33. assert.String(req.Header.Get("Proxy-Connection")).Equals("keep-alive")
  34. assert.String(req.Header.Get("Proxy-Authenticate")).Equals("abc")
  35. StripHopByHopHeaders(req)
  36. assert.String(req.Header.Get("Connection")).Equals("close")
  37. assert.String(req.Header.Get("Foo")).Equals("")
  38. assert.String(req.Header.Get("Bar")).Equals("")
  39. assert.String(req.Header.Get("Proxy-Connection")).Equals("")
  40. assert.String(req.Header.Get("Proxy-Authenticate")).Equals("")
  41. }
  42. func TestNormalGetRequest(t *testing.T) {
  43. assert := assert.On(t)
  44. testPacketDispatcher := testdispatcher.NewTestPacketDispatcher(nil)
  45. httpProxy := NewHttpProxyServer(&Config{}, testPacketDispatcher)
  46. defer httpProxy.Close()
  47. port := v2nettesting.PickPort()
  48. err := httpProxy.Listen(port)
  49. assert.Error(err).IsNil()
  50. assert.Port(port).Equals(httpProxy.Port())
  51. httpClient := &http.Client{}
  52. resp, err := httpClient.Get("http://127.0.0.1:" + port.String() + "/")
  53. assert.Error(err).IsNil()
  54. assert.Int(resp.StatusCode).Equals(400)
  55. }