forked from openfaas/of-watchdog
-
Notifications
You must be signed in to change notification settings - Fork 1
/
requesthandler_test.go
85 lines (69 loc) · 1.9 KB
/
requesthandler_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
84
85
package main
import (
"log"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
)
func TestHealthHandler_StatusOK_LockFilePresent(t *testing.T) {
rr := httptest.NewRecorder()
present := lockFilePresent()
if present {
path := filepath.Join(os.TempDir(), ".lock")
os.Remove(path)
}
if tmpPath, err := createLockFile(); err != nil {
log.Fatalf("Error writing to %s - %s\n", tmpPath, err)
}
req, err := http.NewRequest(http.MethodGet, "/_/health", nil)
if err != nil {
t.Fatal(err)
}
handler := makeHealthHandler()
handler(rr, req)
required := http.StatusOK
if status := rr.Code; status != required {
t.Errorf("handler returned wrong status code - want: %v, got: %v", required, status)
}
}
func TestHealthHandler_StatusInternalServerError_LockFileNotPresent(t *testing.T) {
rr := httptest.NewRecorder()
if lockFilePresent() == true {
if err := removeLockFile(); err != nil {
t.Fatal(err)
}
}
req, err := http.NewRequest(http.MethodGet, "/_/health", nil)
if err != nil {
t.Fatal(err)
}
handler := makeHealthHandler()
handler(rr, req)
required := http.StatusServiceUnavailable
if status := rr.Code; status != required {
t.Errorf("handler returned wrong status code - want: %v, got: %v", required, status)
}
}
func TestHealthHandler_StatusMethodNotAllowed_ForWriteableVerbs(t *testing.T) {
rr := httptest.NewRecorder()
verbs := []string{http.MethodPost, http.MethodPut, http.MethodDelete}
for _, verb := range verbs {
req, err := http.NewRequest(verb, "/_/health", nil)
if err != nil {
t.Fatal(err)
}
handler := makeHealthHandler()
handler(rr, req)
required := http.StatusMethodNotAllowed
if status := rr.Code; status != required {
t.Errorf("handler returned wrong status code - want: %v, got: %v", required, status)
}
}
}
func removeLockFile() error {
path := filepath.Join(os.TempDir(), ".lock")
removeErr := os.Remove(path)
return removeErr
}