headers_test.go 2.7 KB

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