sniff_test.go 3.0 KB

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