-
Notifications
You must be signed in to change notification settings - Fork 3
/
validation_test.go
62 lines (53 loc) · 1.25 KB
/
validation_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
package common_test
import (
"context"
"encoding/json"
"math/rand"
"testing"
"time"
. "github.com/cloudtrust/common-healthcheck"
"github.com/cloudtrust/common-healthcheck/mock"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func TestValidationMW(t *testing.T) {
var mockCtrl = gomock.NewController(t)
defer mockCtrl.Finish()
var mockHealthChecker = mock.NewHealthChecker(mockCtrl)
var (
validValues = map[string]struct{}{
"valid1": struct{}{},
"valid2": struct{}{},
"valid3": struct{}{},
}
m = MakeValidationMiddleware(validValues)(mockHealthChecker)
rep = json.RawMessage(`{"key":"value"}`)
)
var tsts = []struct {
name string
isValid bool
}{
{"valid1", true},
{"valid2", true},
{"valid3", true},
{"invalid1", false},
{"invalid2", false},
{"invalid3", false},
}
for _, tst := range tsts {
if tst.isValid {
mockHealthChecker.EXPECT().HealthCheck(context.Background(), gomock.Any()).Return(rep, nil).Times(1)
}
var report, err = m.HealthCheck(context.Background(), tst.name)
if tst.isValid {
assert.Nil(t, err)
assert.NotNil(t, report)
} else {
assert.IsType(t, &ErrInvalidHCName{}, err)
assert.Nil(t, report)
}
}
}