-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
66 lines (50 loc) · 1.25 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"bufio"
"fmt"
"net"
"testing"
)
func TestServer(t *testing.T) {
go main()
conn, err := net.Dial("tcp", "localhost:8080")
if err != nil {
t.Errorf("Couldn't launch server: %s", err)
return
}
//Test that server responds to a Get request
request := "GET / HTTP/1.1\r\n\r\n"
fmt.Fprintf(conn, request)
response := bufio.NewReader(conn)
statusLine, err := response.ReadString('\n')
if err != nil {
t.Errorf("Couldn't read statusLine: %s", err)
return
}
expectedStatusLine := "HTTP/1.1 200 OK\r\n"
if statusLine != expectedStatusLine {
t.Errorf("We expected: %s but we got: %s", expectedStatusLine, statusLine)
return
}
currentLine, err := response.ReadString('\n')
if err != nil {
t.Errorf("Couldn't read currentLine: %s", err)
return
}
expectedBlankLine := "\r\n"
if currentLine != expectedBlankLine {
t.Errorf("We expected %s but we got: %s", expectedBlankLine, currentLine )
return
}
firstBodyLine, err := response.ReadString('\n')
if err != nil {
t.Errorf("Couldn't read firstBodyLine %s", err)
return
}
expectedFirstBodyLine := "Hi there\r\n"
if firstBodyLine != expectedFirstBodyLine {
t.Errorf("We expected %s but we got: %s", expectedFirstBodyLine, firstBodyLine )
return
}
return
}