faulty listener test

This commit is contained in:
Sławosz
2013-09-23 19:34:53 +02:00
parent 335c86c6e9
commit 1a35a2c651
+51 -25
View File
@@ -7,7 +7,8 @@ import (
"net/http"
"os"
"testing"
"time"
"time"
"io"
)
func getTCPMessage() (msg *TCPMessage) {
@@ -21,8 +22,6 @@ func mockReplayServer() (listener net.Listener) {
Settings.ReplayAddress = listener.Addr().String()
fmt.Println(listener.Addr().String())
return
}
@@ -49,49 +48,76 @@ func TestSendMessage(t *testing.T) {
func TestSaveMessageToFile(t *testing.T) {
Settings.Verbose = true
Settings.FileToReplyPath = "requests.gor"
Settings.FileToReplyPath = "listener_test.gor"
Settings.Address = "127.0.0.1"
Settings.Port = 50000
received := make(chan int)
receivedChan := make(chan int)
requestBytes := []byte("GET / HTTP/1.1\nHost: localhost:50000\r\n\r\n")
// receivedChan <- 1
// requestBytes := []byte("GET / HTTP/1.1\nHost: localhost:50000\r\n\r\n")
handler := func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "OK", http.StatusNotFound)
received <- 1
fmt.Println("handler called")
// fmt.Fprintf(w, "Hello, aaa")
// this is faulty
io.WriteString(w, "hello, world!\n")
}
go Run()
go func() {
http.ListenAndServe(":50000", http.HandlerFunc(handler))
}()
time.Sleep(time.Millisecond * 100)
time.Sleep(time.Millisecond * 100)
time.Sleep(time.Millisecond * 100)
go Run()
go func() {
conn, _ := net.Dial("tcp", ":50000")
conn.Write(requestBytes)
}()
select {
case <-received:
time.Sleep(time.Millisecond * 100)
case msg, ok := <-receivedChan:
fmt.Println("received something")
case <-time.After(time.Second):
t.Error("Timeout error")
fmt.Println("in timeout section")
// t.Error("Server not started and I dont know what is going on :(")
}
file, err := os.Open("requests.gor")
request := getRequest()
resp, err := http.DefaultClient.Do(request)
if err != nil {
t.Errorf("Problem with default client", err)
}
fmt.Println("RESPONSE", resp)
file, err := os.Open("listener_test.gor")
if err != nil {
t.Errorf("Problem with opening file: ", err)
}
fileBuf := make([]byte, 100)
file.Read(fileBuf)
fileBuf := make([]byte, 1024)
n, err := file.Read(fileBuf)
fileBuf = fileBuf[:n]
if bytes.Compare(fileBuf, requestBytes) != 0 {
t.Errorf("Original and received requests does not match")
}
//requestBuffer := bytes.NewBuffer(fileBuf)
//requestReader := bufio.NewReader(requestBuffer)
//readRequest, _ := http.ReadRequest(requestReader)
fmt.Println("Read file: \n", string(fileBuf))
fmt.Println("Read file: \n", fileBuf)
//if bytes.Compare(fileBuf, make([]byte, 1024)) != 0 {
// t.Errorf("Original and received requests does not match")
//}
// if *request != *readRequest {
// t.Errorf("Original and received requests does not match")
//}
t.Errorf("Original and received requests does not match")
}
func getRequest() (req *http.Request) {
req, _ = http.NewRequest("GET", "http://localhost:50000", nil)
ck := new(http.Cookie)
ck.Name = "test"
ck.Value = "value2"
req.AddCookie(ck)
return
}