1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-27 22:36:12 -04:00
v2fly/transport/internet/headers/http/http_test.go

141 lines
3.0 KiB
Go
Raw Normal View History

2016-11-04 16:59:19 -04:00
package http_test
import (
2018-11-07 11:16:57 -05:00
"bytes"
2017-01-13 07:47:44 -05:00
"context"
2018-11-07 11:16:57 -05:00
"crypto/rand"
2016-11-04 16:59:19 -04:00
"testing"
2016-12-16 10:52:11 -05:00
"time"
2018-11-02 16:34:04 -04:00
"v2ray.com/core/common"
2016-12-09 05:35:27 -05:00
"v2ray.com/core/common/buf"
"v2ray.com/core/common/net"
2016-12-08 10:27:41 -05:00
. "v2ray.com/core/transport/internet/headers/http"
2017-10-26 15:44:22 -04:00
. "v2ray.com/ext/assert"
2016-11-04 16:59:19 -04:00
)
func TestReaderWriter(t *testing.T) {
2017-10-24 10:15:35 -04:00
assert := With(t)
2016-11-04 16:59:19 -04:00
2016-12-09 06:08:25 -05:00
cache := buf.New()
2018-08-16 06:05:33 -04:00
b := buf.New()
2018-11-02 16:34:04 -04:00
common.Must2(b.WriteString("abcd" + ENDING))
2016-12-06 05:03:42 -05:00
writer := NewHeaderWriter(b)
2016-12-16 10:52:11 -05:00
err := writer.Write(cache)
2019-02-02 16:19:30 -05:00
common.Must(err)
2018-04-02 14:00:50 -04:00
assert(cache.Len(), Equals, int32(8))
2016-12-16 10:52:11 -05:00
_, err = cache.Write([]byte{'e', 'f', 'g'})
2019-02-02 16:19:30 -05:00
common.Must(err)
2016-11-04 16:59:19 -04:00
reader := &HeaderReader{}
buffer, err := reader.Read(cache)
2019-02-02 16:19:30 -05:00
common.Must(err)
2017-10-24 10:15:35 -04:00
assert(buffer.Bytes(), Equals, []byte{'e', 'f', 'g'})
2016-11-04 16:59:19 -04:00
}
2016-11-07 04:47:30 -05:00
func TestRequestHeader(t *testing.T) {
2017-10-24 10:15:35 -04:00
assert := With(t)
2016-11-07 04:47:30 -05:00
2017-01-13 07:47:44 -05:00
auth, err := NewHttpAuthenticator(context.Background(), &Config{
2016-11-07 04:47:30 -05:00
Request: &RequestConfig{
Uri: []string{"/"},
Header: []*Header{
{
Name: "Test",
Value: []string{"Value"},
},
},
},
2017-01-13 07:47:44 -05:00
})
2019-02-02 16:19:30 -05:00
common.Must(err)
2016-11-07 04:47:30 -05:00
2016-12-09 06:08:25 -05:00
cache := buf.New()
2017-01-13 07:47:44 -05:00
err = auth.GetClientWriter().Write(cache)
2019-02-02 16:19:30 -05:00
common.Must(err)
2016-11-07 04:47:30 -05:00
2017-10-24 10:15:35 -04:00
assert(cache.String(), Equals, "GET / HTTP/1.1\r\nTest: Value\r\n\r\n")
2016-11-07 04:47:30 -05:00
}
2016-12-16 10:52:11 -05:00
2018-11-07 11:16:57 -05:00
func TestLongRequestHeader(t *testing.T) {
payload := make([]byte, buf.Size+2)
common.Must2(rand.Read(payload[:buf.Size-2]))
copy(payload[buf.Size-2:], []byte(ENDING))
payload = append(payload, []byte("abcd")...)
reader := HeaderReader{}
b, err := reader.Read(bytes.NewReader(payload))
common.Must(err)
if b.String() != "abcd" {
t.Error("expect content abcd, but actually ", b.String())
}
}
2016-12-16 10:52:11 -05:00
func TestConnection(t *testing.T) {
2017-10-24 10:15:35 -04:00
assert := With(t)
2016-12-16 10:52:11 -05:00
2017-12-03 17:41:01 -05:00
auth, err := NewHttpAuthenticator(context.Background(), &Config{
Request: &RequestConfig{
Method: &Method{Value: "Post"},
Uri: []string{"/testpath"},
Header: []*Header{
{
Name: "Host",
Value: []string{"www.v2ray.com", "www.google.com"},
},
{
Name: "User-Agent",
Value: []string{"Test-Agent"},
},
},
},
Response: &ResponseConfig{
Version: &Version{
Value: "1.1",
},
Status: &Status{
Code: "404",
Reason: "Not Found",
},
},
})
2019-02-02 16:19:30 -05:00
common.Must(err)
2016-12-16 10:52:11 -05:00
listener, err := net.Listen("tcp", "127.0.0.1:0")
2019-02-02 16:19:30 -05:00
common.Must(err)
2016-12-16 10:52:11 -05:00
go func() {
conn, err := listener.Accept()
2019-02-02 16:19:30 -05:00
common.Must(err)
2016-12-16 10:52:11 -05:00
authConn := auth.Server(conn)
b := make([]byte, 256)
for {
n, err := authConn.Read(b)
2019-02-02 16:19:30 -05:00
common.Must(err)
2016-12-16 10:52:11 -05:00
_, err = authConn.Write(b[:n])
2019-02-02 16:19:30 -05:00
common.Must(err)
2016-12-16 10:52:11 -05:00
}
}()
conn, err := net.DialTCP("tcp", nil, listener.Addr().(*net.TCPAddr))
2019-02-02 16:19:30 -05:00
common.Must(err)
2016-12-16 10:52:11 -05:00
authConn := auth.Client(conn)
authConn.Write([]byte("Test payload"))
authConn.Write([]byte("Test payload 2"))
expectedResponse := "Test payloadTest payload 2"
actualResponse := make([]byte, 256)
deadline := time.Now().Add(time.Second * 5)
totalBytes := 0
for {
n, err := authConn.Read(actualResponse[totalBytes:])
2019-02-02 16:19:30 -05:00
common.Must(err)
2016-12-16 10:52:11 -05:00
totalBytes += n
if totalBytes >= len(expectedResponse) || time.Now().After(deadline) {
break
}
}
2017-10-24 10:15:35 -04:00
assert(string(actualResponse[:totalBytes]), Equals, expectedResponse)
2016-12-16 10:52:11 -05:00
}