-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
65 lines (50 loc) · 1.45 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
package main
import (
"net/http"
"net/http/httptest"
"os"
"testing"
)
func executeRequest(req *http.Request, s *Server) *httptest.ResponseRecorder {
rr := httptest.NewRecorder()
s.Router.ServeHTTP(rr, req)
return rr
}
func TestNewServer(t *testing.T) {
s := NewServer()
req, _ := http.NewRequest("GET", "/", nil)
response := executeRequest(req, s)
if http.StatusNotFound != response.Code {
t.Errorf("Expected response code %d. Got %d\n", http.StatusNotFound, response.Code)
}
s.MountHandlers()
req, _ = http.NewRequest("GET", "/", nil)
response = executeRequest(req, s)
if http.StatusOK != response.Code {
t.Errorf("Expected response code %d. Got %d\n", http.StatusOK, response.Code)
}
}
func TestMountHandlers(t *testing.T) {
s := NewServer()
s.MountHandlers()
req, _ := http.NewRequest("GET", "/", nil)
response := executeRequest(req, s)
headers := response.Header()
if amount := len(headers); amount != 1 {
t.Errorf("Expected 1 header. Got %d\n", amount)
}
if headers["Content-Type"][0] != "text/html" {
t.Errorf("Expected text/html as Content-Type. Got %s\n", headers["Content-Type"][0])
}
}
func TestPort(t *testing.T) {
const defaultPort string = "8080"
if Port() != defaultPort {
t.Errorf("Expected port of value %s. Got %s\n", defaultPort, Port())
}
const definedPort string = "9000"
os.Setenv("PORT", definedPort)
if Port() != definedPort {
t.Errorf("Expected port of value %s. Got %s\n", definedPort, Port())
}
}