-
-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathinfo_test.go
59 lines (53 loc) · 1.49 KB
/
info_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
package controller_test
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
ffclient "github.com/thomaspoignant/go-feature-flag"
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/controller"
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/service"
)
func Test_info_Handler(t *testing.T) {
type want struct {
httpCode int
bodyFile string
handlerErr bool
}
tests := []struct {
name string
want want
}{
{
name: "valid info",
want: want{
httpCode: http.StatusOK,
bodyFile: "../testdata/controller/info/valid.json",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// init GO feature flag
goFF, _ := ffclient.New(ffclient.Config{Offline: true})
srv := service.NewMonitoring(goFF)
infoCtrl := controller.NewInfo(srv)
e := echo.New()
rec := httptest.NewRecorder()
req := httptest.NewRequest(echo.GET, "/info", nil)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
c := e.NewContext(req, rec)
res := infoCtrl.Handler(c)
if tt.want.handlerErr {
assert.Error(t, res, "handler should return an error")
return
}
body, err := ioutil.ReadFile(tt.want.bodyFile)
assert.NoError(t, err, "Impossible the expected body file %s", tt.want.bodyFile)
assert.Equal(t, tt.want.httpCode, rec.Code, "Invalid HTTP Code")
assert.JSONEq(t, string(body), rec.Body.String(), "Invalid response body")
})
}
}