sniff_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package http_test
  2. import (
  3. "testing"
  4. . "github.com/xtls/xray-core/common/protocol/http"
  5. )
  6. func TestHTTPHeaders(t *testing.T) {
  7. cases := []struct {
  8. input string
  9. domain string
  10. err bool
  11. }{
  12. {
  13. input: `GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
  14. Host: net.tutsplus.com
  15. User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)
  16. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  17. Accept-Language: en-us,en;q=0.5
  18. Accept-Encoding: gzip,deflate
  19. Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
  20. Keep-Alive: 300
  21. Connection: keep-alive
  22. Cookie: PHPSESSID=r2t5uvjq435r4q7ib3vtdjq120
  23. Pragma: no-cache
  24. Cache-Control: no-cache`,
  25. domain: "net.tutsplus.com",
  26. },
  27. {
  28. input: `POST /foo.php HTTP/1.1
  29. Host: localhost
  30. User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)
  31. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  32. Accept-Language: en-us,en;q=0.5
  33. Accept-Encoding: gzip,deflate
  34. Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
  35. Keep-Alive: 300
  36. Connection: keep-alive
  37. Referer: http://localhost/test.php
  38. Content-Type: application/x-www-form-urlencoded
  39. Content-Length: 43
  40. first_name=John&last_name=Doe&action=Submit`,
  41. domain: "localhost",
  42. },
  43. {
  44. input: `X /foo.php HTTP/1.1
  45. Host: localhost
  46. User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)
  47. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  48. Accept-Language: en-us,en;q=0.5
  49. Accept-Encoding: gzip,deflate
  50. Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
  51. Keep-Alive: 300
  52. Connection: keep-alive
  53. Referer: http://localhost/test.php
  54. Content-Type: application/x-www-form-urlencoded
  55. Content-Length: 43
  56. first_name=John&last_name=Doe&action=Submit`,
  57. domain: "",
  58. err: true,
  59. },
  60. {
  61. input: `GET /foo.php HTTP/1.1
  62. User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)
  63. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  64. Accept-Language: en-us,en;q=0.5
  65. Accept-Encoding: gzip,deflate
  66. Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
  67. Keep-Alive: 300
  68. Connection: keep-alive
  69. Referer: http://localhost/test.php
  70. Content-Type: application/x-www-form-urlencoded
  71. Content-Length: 43
  72. Host: localhost
  73. first_name=John&last_name=Doe&action=Submit`,
  74. domain: "",
  75. err: true,
  76. },
  77. {
  78. input: `GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1`,
  79. domain: "",
  80. err: true,
  81. },
  82. }
  83. for _, test := range cases {
  84. header, err := SniffHTTP([]byte(test.input))
  85. if test.err {
  86. if err == nil {
  87. t.Errorf("Expect error but nil, in test: %v", test)
  88. }
  89. } else {
  90. if err != nil {
  91. t.Errorf("Expect no error but actually %s in test %v", err.Error(), test)
  92. }
  93. if header.Domain() != test.domain {
  94. t.Error("expected domain ", test.domain, " but got ", header.Domain())
  95. }
  96. }
  97. }
  98. }