http_test.go 3.1 KB

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