1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-10-31 00:08:08 -04:00
v2fly/transport/internet/headers/http/http_test.go

101 lines
2.3 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-12-16 10:52:11 -05:00
"net"
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"
2016-12-06 05:03:42 -05:00
"v2ray.com/core/common/serial"
2016-11-04 16:59:19 -04:00
"v2ray.com/core/testing/assert"
2016-12-08 10:27:41 -05:00
. "v2ray.com/core/transport/internet/headers/http"
2016-11-04 16:59:19 -04:00
)
func TestReaderWriter(t *testing.T) {
assert := assert.On(t)
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)
assert.Error(err).IsNil()
assert.Int(cache.Len()).Equals(8)
_, err = cache.Write([]byte{'e', 'f', 'g'})
assert.Error(err).IsNil()
2016-11-04 16:59:19 -04:00
reader := &HeaderReader{}
buffer, err := reader.Read(cache)
assert.Error(err).IsNil()
2016-12-06 05:03:42 -05:00
assert.Bytes(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) {
assert := assert.On(t)
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
})
assert.Error(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)
2016-11-07 04:47:30 -05:00
assert.Error(err).IsNil()
assert.String(cache.String()).Equals("GET / HTTP/1.1\r\nTest: Value\r\n\r\n")
}
2016-12-16 10:52:11 -05:00
func TestConnection(t *testing.T) {
assert := assert.On(t)
2017-01-13 07:47:44 -05:00
auth, err := NewHttpAuthenticator(context.Background(), new(Config))
assert.Error(err).IsNil()
2016-12-16 10:52:11 -05:00
listener, err := net.Listen("tcp", "127.0.0.1:0")
assert.Error(err).IsNil()
go func() {
conn, err := listener.Accept()
assert.Error(err).IsNil()
authConn := auth.Server(conn)
b := make([]byte, 256)
for {
n, err := authConn.Read(b)
assert.Error(err).IsNil()
_, err = authConn.Write(b[:n])
assert.Error(err).IsNil()
}
}()
conn, err := net.DialTCP("tcp", nil, listener.Addr().(*net.TCPAddr))
assert.Error(err).IsNil()
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:])
assert.Error(err).IsNil()
totalBytes += n
if totalBytes >= len(expectedResponse) || time.Now().After(deadline) {
break
}
}
assert.String(string(actualResponse[:totalBytes])).Equals(expectedResponse)
}