-
Notifications
You must be signed in to change notification settings - Fork 91
/
handlers_test.go
151 lines (133 loc) · 3.39 KB
/
handlers_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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/julienschmidt/httprouter"
"github.com/sirupsen/logrus"
"github.com/techjacker/diffence"
)
type testDiffGetter struct {
fixt string
}
func (t testDiffGetter) Get(_ string) (*http.Response, error) {
return &http.Response{
Body: ioutil.NopCloser(getFixture(t.fixt)),
}, nil
}
type testLogger struct {
log *logrus.Logger
}
func (l testLogger) Log(v ...interface{}) {
return
}
func TestGithubHandler(t *testing.T) {
const (
testPath = "/github"
)
type args struct {
githubPayloadPath string
rulesPath string
diffPath string
}
tests := []struct {
name string
args args
wantStatusCode int
wantResBody string
}{
{
name: "Incorrect JSON payload returns 4xx status code",
args: args{
githubPayloadPath: "test/fixtures/nonsense.json",
rulesPath: gitrobRules,
diffPath: "",
},
wantStatusCode: http.StatusBadRequest,
wantResBody: msgBadRequest,
},
{
name: "No offenses in diff",
args: args{
githubPayloadPath: "test/fixtures/github_event_push.json",
rulesPath: gitrobRules,
diffPath: "test/fixtures/no_offenses.diff",
},
wantStatusCode: http.StatusOK,
wantResBody: msgNoViolationsFound,
},
{
name: "1 offense in diff",
args: args{
githubPayloadPath: "test/fixtures/github_event_push.json",
rulesPath: gitrobRules,
diffPath: "test/fixtures/offenses_x1.diff",
},
wantStatusCode: http.StatusOK,
wantResBody: msgViolationFound,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
router := httprouter.New()
router.Handler("POST", testPath, GithubHandler(
diffence.DiffChecker{Rules: getTestRules(t, tt.args.rulesPath)},
testDiffGetter{tt.args.diffPath},
testLogger{},
))
params := getFixture(tt.args.githubPayloadPath)
r, err := http.NewRequest("POST", testPath, params)
if err != nil {
t.Error(err)
}
w := httptest.NewRecorder()
router.ServeHTTP(w, r)
if w.Code != tt.wantStatusCode {
t.Fatalf("Githubhandler returned unexpected status code: got %v want %v",
w.Code, tt.wantStatusCode)
}
if strings.EqualFold(strings.TrimSpace(w.Body.String()), tt.wantResBody) != true {
t.Fatalf("Githubhandler returned unexpected body: got %v want %v",
w.Body.String(), tt.wantResBody)
}
})
}
}
func TestHealthHandler(t *testing.T) {
const (
testPath = "/healthz"
)
tests := []struct {
name string
wantStatusCode int
wantResBody string
}{
{
name: "Healthcheck handler",
wantStatusCode: http.StatusOK,
wantResBody: msgHealthOk,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
router := httprouter.New()
router.Handler("GET", testPath, http.HandlerFunc(HealthHandler))
r, err := http.NewRequest("GET", testPath, nil)
if err != nil {
t.Error(err)
}
w := httptest.NewRecorder()
router.ServeHTTP(w, r)
if w.Code != tt.wantStatusCode {
t.Fatalf("Healthhandler returned unexpected status code: got %v want %v",
w.Code, tt.wantStatusCode)
}
if strings.EqualFold(strings.TrimSpace(w.Body.String()), tt.wantResBody) != true {
t.Fatalf("Healthhandler returned unexpected body: got %v want %v",
w.Body.String(), tt.wantResBody)
}
})
}
}