http_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package http_test
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto/rand"
  6. "testing"
  7. "time"
  8. "v2ray.com/core/common"
  9. "v2ray.com/core/common/buf"
  10. "v2ray.com/core/common/net"
  11. . "v2ray.com/core/transport/internet/headers/http"
  12. . "v2ray.com/ext/assert"
  13. )
  14. func TestReaderWriter(t *testing.T) {
  15. assert := With(t)
  16. cache := buf.New()
  17. b := buf.New()
  18. common.Must2(b.WriteString("abcd" + ENDING))
  19. writer := NewHeaderWriter(b)
  20. err := writer.Write(cache)
  21. common.Must(err)
  22. assert(cache.Len(), Equals, int32(8))
  23. _, err = cache.Write([]byte{'e', 'f', 'g'})
  24. common.Must(err)
  25. reader := &HeaderReader{}
  26. buffer, err := reader.Read(cache)
  27. common.Must(err)
  28. assert(buffer.Bytes(), Equals, []byte{'e', 'f', 'g'})
  29. }
  30. func TestRequestHeader(t *testing.T) {
  31. assert := With(t)
  32. auth, err := NewHttpAuthenticator(context.Background(), &Config{
  33. Request: &RequestConfig{
  34. Uri: []string{"/"},
  35. Header: []*Header{
  36. {
  37. Name: "Test",
  38. Value: []string{"Value"},
  39. },
  40. },
  41. },
  42. })
  43. common.Must(err)
  44. cache := buf.New()
  45. err = auth.GetClientWriter().Write(cache)
  46. common.Must(err)
  47. assert(cache.String(), Equals, "GET / HTTP/1.1\r\nTest: Value\r\n\r\n")
  48. }
  49. func TestLongRequestHeader(t *testing.T) {
  50. payload := make([]byte, buf.Size+2)
  51. common.Must2(rand.Read(payload[:buf.Size-2]))
  52. copy(payload[buf.Size-2:], []byte(ENDING))
  53. payload = append(payload, []byte("abcd")...)
  54. reader := HeaderReader{}
  55. b, err := reader.Read(bytes.NewReader(payload))
  56. common.Must(err)
  57. if b.String() != "abcd" {
  58. t.Error("expect content abcd, but actually ", b.String())
  59. }
  60. }
  61. func TestConnection(t *testing.T) {
  62. assert := With(t)
  63. auth, err := NewHttpAuthenticator(context.Background(), &Config{
  64. Request: &RequestConfig{
  65. Method: &Method{Value: "Post"},
  66. Uri: []string{"/testpath"},
  67. Header: []*Header{
  68. {
  69. Name: "Host",
  70. Value: []string{"www.v2ray.com", "www.google.com"},
  71. },
  72. {
  73. Name: "User-Agent",
  74. Value: []string{"Test-Agent"},
  75. },
  76. },
  77. },
  78. Response: &ResponseConfig{
  79. Version: &Version{
  80. Value: "1.1",
  81. },
  82. Status: &Status{
  83. Code: "404",
  84. Reason: "Not Found",
  85. },
  86. },
  87. })
  88. common.Must(err)
  89. listener, err := net.Listen("tcp", "127.0.0.1:0")
  90. common.Must(err)
  91. go func() {
  92. conn, err := listener.Accept()
  93. common.Must(err)
  94. authConn := auth.Server(conn)
  95. b := make([]byte, 256)
  96. for {
  97. n, err := authConn.Read(b)
  98. common.Must(err)
  99. _, err = authConn.Write(b[:n])
  100. common.Must(err)
  101. }
  102. }()
  103. conn, err := net.DialTCP("tcp", nil, listener.Addr().(*net.TCPAddr))
  104. common.Must(err)
  105. authConn := auth.Client(conn)
  106. authConn.Write([]byte("Test payload"))
  107. authConn.Write([]byte("Test payload 2"))
  108. expectedResponse := "Test payloadTest payload 2"
  109. actualResponse := make([]byte, 256)
  110. deadline := time.Now().Add(time.Second * 5)
  111. totalBytes := 0
  112. for {
  113. n, err := authConn.Read(actualResponse[totalBytes:])
  114. common.Must(err)
  115. totalBytes += n
  116. if totalBytes >= len(expectedResponse) || time.Now().After(deadline) {
  117. break
  118. }
  119. }
  120. assert(string(actualResponse[:totalBytes]), Equals, expectedResponse)
  121. }