-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathmyapp_test.go
64 lines (59 loc) · 1.47 KB
/
myapp_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
package main
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/k8s-community/myapp/version"
"github.com/takama/router"
)
func TestHealthHandler(t *testing.T) {
r := router.New()
r.GET("/healthz", healthz)
req, err := http.NewRequest("GET", "/healthz", nil)
if err != nil {
t.Error(err)
}
trw := httptest.NewRecorder()
r.ServeHTTP(trw, req)
if trw.Body.String() != "Ok" {
t.Error("Expected", "Ok", "got", trw.Body.String())
}
if trw.Code != 200 {
t.Error("Expected status:", 200, "got", trw.Body.String())
}
}
func TestInfoHandler(t *testing.T) {
r := router.New()
r.GET("/info", info)
req, err := http.NewRequest("GET", "/info", nil)
if err != nil {
t.Error(err)
}
trw := httptest.NewRecorder()
r.ServeHTTP(trw, req)
inf := "{\n \"commit\": \"" + version.COMMIT + "\",\n \"repo\": \"" + version.REPO +
"\",\n \"version\": \"" + version.RELEASE + "\"\n}"
if trw.Body.String() != inf {
t.Error("Expected", inf, "got", trw.Body.String())
}
if trw.Code != 200 {
t.Error("Expected status:", 200, "got", trw.Body.String())
}
}
func TestRootHandler(t *testing.T) {
r := router.New()
r.GET("/", root)
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Error(err)
}
trw := httptest.NewRecorder()
r.ServeHTTP(trw, req)
inf := "myapp v" + version.RELEASE
if trw.Body.String() != inf {
t.Error("Expected", inf, "got", trw.Body.String())
}
if trw.Code != 200 {
t.Error("Expected status:", 200, "got", trw.Body.String())
}
}