/
endpoint_test.go
83 lines (72 loc) · 2.44 KB
/
endpoint_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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestHealthCheckHandler(t *testing.T) {
req, err := http.NewRequest("GET", "/health", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(healthHandler)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: (%v : %v)", status, http.StatusOK)
} else {
fmt.Printf("handler returned correct status code: (%v : %v)\n", status, http.StatusOK)
}
expected := "chrisgr: health ok"
if rr.Body.String() != expected {
t.Errorf("handler returned unexpected body: (%v, %v)", rr.Body.String(), expected)
} else {
fmt.Printf("handler returned expected body: (%v, %v)\n", rr.Body.String(), expected)
}
}
func TestEndpointsCheckHandler(t *testing.T) {
endpoint_list := []string{"/about", "/", "/linux", "/apple", "/projects"}
for _, endpoint := range endpoint_list {
fmt.Println("endpoint: ", endpoint)
req, err := http.NewRequest("GET", endpoint, nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(templatePageHandler)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: (%v : %v)", status, http.StatusOK)
} else {
fmt.Printf("handler returned correct status code: (%v : %v)\n", status, http.StatusOK)
}
expected := "linux"
if !strings.Contains(rr.Body.String(), expected) {
t.Errorf("handler did not find expected string body: (expected: %v, endpoint: %v)", expected, endpoint)
} else {
fmt.Printf("handler contained expected string: (expected: %v, endpoint: %v)\n", expected, endpoint)
}
}
}
func TestTimeHandler(t *testing.T) {
req, err := http.NewRequest("GET", "/time", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(timeHandler)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: ( %v : %v )", status, http.StatusOK)
} else {
fmt.Printf("handler returned correct status code: ( %v : %v )\n", status, http.StatusOK)
}
expected := "time:"
if !strings.Contains(rr.Body.String(), expected) {
t.Errorf("handler returned unexpected body: ( %v, %v )", rr.Body.String(), expected)
} else {
fmt.Printf("handler contained expected string: ( %v, %v )\n", rr.Body.String(), expected)
}
}