headers_test.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package http_test
  2. import (
  3. "bufio"
  4. "net/http"
  5. "strings"
  6. "testing"
  7. "v2ray.com/core/common/net"
  8. . "v2ray.com/core/common/protocol/http"
  9. . "v2ray.com/ext/assert"
  10. )
  11. func TestParseXForwardedFor(t *testing.T) {
  12. assert := With(t)
  13. header := http.Header{}
  14. header.Add("X-Forwarded-For", "129.78.138.66, 129.78.64.103")
  15. addrs := ParseXForwardedFor(header)
  16. assert(len(addrs), Equals, 2)
  17. assert(addrs[0].String(), Equals, "129.78.138.66")
  18. assert(addrs[1].String(), Equals, "129.78.64.103")
  19. }
  20. func TestHopByHopHeadersRemoving(t *testing.T) {
  21. assert := With(t)
  22. rawRequest := `GET /pkg/net/http/ HTTP/1.1
  23. Host: golang.org
  24. Connection: keep-alive,Foo, Bar
  25. Foo: foo
  26. Bar: bar
  27. Proxy-Connection: keep-alive
  28. Proxy-Authenticate: abc
  29. Accept-Encoding: gzip
  30. Accept-Charset: ISO-8859-1,UTF-8;q=0.7,*;q=0.7
  31. Cache-Control: no-cache
  32. Accept-Language: de,en;q=0.7,en-us;q=0.3
  33. `
  34. b := bufio.NewReader(strings.NewReader(rawRequest))
  35. req, err := http.ReadRequest(b)
  36. assert(err, IsNil)
  37. assert(req.Header.Get("Foo"), Equals, "foo")
  38. assert(req.Header.Get("Bar"), Equals, "bar")
  39. assert(req.Header.Get("Connection"), Equals, "keep-alive,Foo, Bar")
  40. assert(req.Header.Get("Proxy-Connection"), Equals, "keep-alive")
  41. assert(req.Header.Get("Proxy-Authenticate"), Equals, "abc")
  42. RemoveHopByHopHeaders(req.Header)
  43. assert(req.Header.Get("Connection"), IsEmpty)
  44. assert(req.Header.Get("Foo"), IsEmpty)
  45. assert(req.Header.Get("Bar"), IsEmpty)
  46. assert(req.Header.Get("Proxy-Connection"), IsEmpty)
  47. assert(req.Header.Get("Proxy-Authenticate"), IsEmpty)
  48. }
  49. func TestParseHost(t *testing.T) {
  50. testCases := []struct {
  51. RawHost string
  52. DefaultPort net.Port
  53. Destination net.Destination
  54. Error bool
  55. }{
  56. {
  57. RawHost: "v2ray.com:80",
  58. DefaultPort: 443,
  59. Destination: net.TCPDestination(net.DomainAddress("v2ray.com"), 80),
  60. },
  61. {
  62. RawHost: "tls.v2ray.com",
  63. DefaultPort: 443,
  64. Destination: net.TCPDestination(net.DomainAddress("tls.v2ray.com"), 443),
  65. },
  66. {
  67. RawHost: "[2401:1bc0:51f0:ec08::1]:80",
  68. DefaultPort: 443,
  69. Destination: net.TCPDestination(net.ParseAddress("[2401:1bc0:51f0:ec08::1]"), 80),
  70. },
  71. }
  72. for _, testCase := range testCases {
  73. dest, err := ParseHost(testCase.RawHost, testCase.DefaultPort)
  74. if testCase.Error {
  75. if err == nil {
  76. t.Error("for test case: ", testCase.RawHost, " expected error, but actually nil")
  77. }
  78. } else {
  79. if dest != testCase.Destination {
  80. t.Error("for test case: ", testCase.RawHost, " expected host: ", testCase.Destination.String(), " but got ", dest.String())
  81. }
  82. }
  83. }
  84. }