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"
|
2017-08-29 06:56:57 -04:00
|
|
|
"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()
|
2018-08-16 06:05:33 -04:00
|
|
|
b := buf.New()
|
2016-12-09 06:08:25 -05:00
|
|
|
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)
|
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'})
|
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-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",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
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
|
|
|
}
|