server_test.go 2.1 KB

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