1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-29 18:45:23 +00:00
v2fly/transport/internet/headers/http/http_test.go

125 lines
2.6 KiB
Go
Raw Normal View History

2016-11-04 20:59:19 +00:00
package http_test
import (
2017-01-13 12:47:44 +00:00
"context"
2016-11-04 20:59:19 +00:00
"testing"
2016-12-16 15:52:11 +00:00
"time"
2016-12-09 10:35:27 +00:00
"v2ray.com/core/common/buf"
"v2ray.com/core/common/net"
2016-12-06 10:03:42 +00:00
"v2ray.com/core/common/serial"
2016-12-08 15:27:41 +00:00
. "v2ray.com/core/transport/internet/headers/http"
2017-10-26 19:44:22 +00:00
. "v2ray.com/ext/assert"
2016-11-04 20:59:19 +00:00
)
func TestReaderWriter(t *testing.T) {
2017-10-24 14:15:35 +00:00
assert := With(t)
2016-11-04 20:59:19 +00:00
2016-12-09 11:08:25 +00:00
cache := buf.New()
2018-03-11 22:29:17 +00:00
b := buf.NewSize(256)
2016-12-09 11:08:25 +00:00
b.AppendSupplier(serial.WriteString("abcd" + ENDING))
2016-12-06 10:03:42 +00:00
writer := NewHeaderWriter(b)
2016-12-16 15:52:11 +00:00
err := writer.Write(cache)
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
2018-04-02 18:00:50 +00:00
assert(cache.Len(), Equals, int32(8))
2016-12-16 15:52:11 +00:00
_, err = cache.Write([]byte{'e', 'f', 'g'})
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
2016-11-04 20:59:19 +00:00
reader := &HeaderReader{}
buffer, err := reader.Read(cache)
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
assert(buffer.Bytes(), Equals, []byte{'e', 'f', 'g'})
2016-11-04 20:59:19 +00:00
}
2016-11-07 09:47:30 +00:00
func TestRequestHeader(t *testing.T) {
2017-10-24 14:15:35 +00:00
assert := With(t)
2016-11-07 09:47:30 +00:00
2017-01-13 12:47:44 +00:00
auth, err := NewHttpAuthenticator(context.Background(), &Config{
2016-11-07 09:47:30 +00:00
Request: &RequestConfig{
Uri: []string{"/"},
Header: []*Header{
{
Name: "Test",
Value: []string{"Value"},
},
},
},
2017-01-13 12:47:44 +00:00
})
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
2016-11-07 09:47:30 +00:00
2016-12-09 11:08:25 +00:00
cache := buf.New()
2017-01-13 12:47:44 +00:00
err = auth.GetClientWriter().Write(cache)
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
2016-11-07 09:47:30 +00:00
2017-10-24 14:15:35 +00:00
assert(cache.String(), Equals, "GET / HTTP/1.1\r\nTest: Value\r\n\r\n")
2016-11-07 09:47:30 +00:00
}
2016-12-16 15:52:11 +00:00
func TestConnection(t *testing.T) {
2017-10-24 14:15:35 +00:00
assert := With(t)
2016-12-16 15:52:11 +00:00
2017-12-03 22:41:01 +00: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",
},
},
})
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
2016-12-16 15:52:11 +00:00
listener, err := net.Listen("tcp", "127.0.0.1:0")
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
2016-12-16 15:52:11 +00:00
go func() {
conn, err := listener.Accept()
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
2016-12-16 15:52:11 +00:00
authConn := auth.Server(conn)
b := make([]byte, 256)
for {
n, err := authConn.Read(b)
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
2016-12-16 15:52:11 +00:00
_, err = authConn.Write(b[:n])
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
2016-12-16 15:52:11 +00:00
}
}()
conn, err := net.DialTCP("tcp", nil, listener.Addr().(*net.TCPAddr))
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
2016-12-16 15:52:11 +00: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 14:15:35 +00:00
assert(err, IsNil)
2016-12-16 15:52:11 +00:00
totalBytes += n
if totalBytes >= len(expectedResponse) || time.Now().After(deadline) {
break
}
}
2017-10-24 14:15:35 +00:00
assert(string(actualResponse[:totalBytes]), Equals, expectedResponse)
2016-12-16 15:52:11 +00:00
}