From b17f06bb4e654457a1fceb64f5dc0a99967d8e87 Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Fri, 16 Dec 2016 16:52:11 +0100 Subject: [PATCH] test case for http header --- transport/internet/headers/http/http_test.go | 55 +++++++++++++++++++- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/transport/internet/headers/http/http_test.go b/transport/internet/headers/http/http_test.go index 8fb5a4e2..167f2db8 100644 --- a/transport/internet/headers/http/http_test.go +++ b/transport/internet/headers/http/http_test.go @@ -1,8 +1,11 @@ package http_test import ( + "net" "testing" + "time" + "v2ray.com/core/common/buf" "v2ray.com/core/common/serial" "v2ray.com/core/testing/assert" @@ -16,8 +19,11 @@ func TestReaderWriter(t *testing.T) { b := buf.NewLocal(256) b.AppendSupplier(serial.WriteString("abcd" + ENDING)) writer := NewHeaderWriter(b) - writer.Write(cache) - cache.Write([]byte{'e', 'f', 'g'}) + 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() reader := &HeaderReader{} buffer, err := reader.Read(cache) @@ -47,3 +53,48 @@ func TestRequestHeader(t *testing.T) { assert.String(cache.String()).Equals("GET / HTTP/1.1\r\nTest: Value\r\n\r\n") } + +func TestConnection(t *testing.T) { + assert := assert.On(t) + + factory := HttpAuthenticatorFactory{} + auth := factory.Create(new(Config)) + + 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) +}