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

101 lines
2.2 KiB
Go
Raw Normal View History

2016-11-04 16:59:19 -04:00
package http_test
import (
2017-01-13 07:47:44 -05:00
"context"
2016-11-04 16:59:19 -04:00
"testing"
2016-12-16 10:52:11 -05:00
"time"
2016-12-09 05:35:27 -05:00
"v2ray.com/core/common/buf"
"v2ray.com/core/common/net"
2016-12-06 05:03:42 -05:00
"v2ray.com/core/common/serial"
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()
b := buf.NewLocal(256)
b.AppendSupplier(serial.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)
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
assert(cache.Len(), Equals, 8)
2016-12-16 10:52:11 -05:00
_, err = cache.Write([]byte{'e', 'f', 'g'})
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
2016-11-04 16:59:19 -04:00
reader := &HeaderReader{}
buffer, err := reader.Read(cache)
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
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
})
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
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)
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
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
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-01-13 07:47:44 -05:00
auth, err := NewHttpAuthenticator(context.Background(), new(Config))
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
2016-12-16 10:52:11 -05:00
listener, err := net.Listen("tcp", "127.0.0.1:0")
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
2016-12-16 10:52:11 -05:00
go func() {
conn, err := listener.Accept()
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
2016-12-16 10:52:11 -05:00
authConn := auth.Server(conn)
b := make([]byte, 256)
for {
n, err := authConn.Read(b)
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
2016-12-16 10:52:11 -05:00
_, err = authConn.Write(b[:n])
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
2016-12-16 10:52:11 -05:00
}
}()
conn, err := net.DialTCP("tcp", nil, listener.Addr().(*net.TCPAddr))
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
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:])
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
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
}