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