-
Notifications
You must be signed in to change notification settings - Fork 2
/
ngroklistener_test.go
126 lines (105 loc) · 2.76 KB
/
ngroklistener_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
package ngroklistener
import (
"context"
"reflect"
"testing"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.ngrok.com/ngrok/config"
)
type TestConfig interface {
Provision(caddy.Context) error
UnmarshalCaddyfile(*caddyfile.Dispenser) error
NgrokTunnel() config.Tunnel
}
type genericTest[C TestConfig] struct {
name string
caddyInput string
expectUnmarshalErr bool
expectProvisionErr bool
expectConfig func(t *testing.T, actual C)
expectedOpts config.Tunnel
}
type genericTestCases[C TestConfig] []genericTest[C]
func (gt genericTest[C]) run(t *testing.T) {
t.Run(gt.name, func(t *testing.T) {
d := caddyfile.NewTestDispenser(gt.caddyInput)
var dummy C
ct := reflect.TypeOf(dummy)
tun := reflect.New(ct.Elem()).Interface().(C)
err := tun.UnmarshalCaddyfile(d)
if gt.expectUnmarshalErr {
require.NotNil(t, err)
return
}
require.Nil(t, err)
if assert.NotNil(t, gt.expectConfig) {
gt.expectConfig(t, tun)
}
ctx, cancel := caddy.NewContext(caddy.Context{Context: context.Background()})
defer cancel()
err = tun.Provision(ctx)
if gt.expectProvisionErr {
require.NotNil(t, err)
return
}
require.Nil(t, err)
ngrokTun := tun.NgrokTunnel()
require.Equal(t, gt.expectedOpts, ngrokTun)
})
}
func (gtcs genericTestCases[C]) runAll(t *testing.T) {
for _, tc := range gtcs {
tc.run(t)
}
}
type NgrokTestConfig interface {
Provision(caddy.Context) error
UnmarshalCaddyfile(*caddyfile.Dispenser) error
}
type genericNgrokTest[C NgrokTestConfig] struct {
name string
caddyInput string
expectUnmarshalErr bool
expectProvisionErr bool
expectConfig func(t *testing.T, actual C)
expectedOptsFunc func(t *testing.T, actual C)
}
type genericNgrokTestCases[C NgrokTestConfig] []genericNgrokTest[C]
func (gt genericNgrokTest[C]) run(t *testing.T) {
t.Run(gt.name, func(t *testing.T) {
d := caddyfile.NewTestDispenser(gt.caddyInput)
var dummy C
ct := reflect.TypeOf(dummy)
ngrok := reflect.New(ct.Elem()).Interface().(C)
err := ngrok.UnmarshalCaddyfile(d)
if gt.expectUnmarshalErr {
require.NotNil(t, err)
return
} else {
require.Nil(t, err)
if assert.NotNil(t, gt.expectConfig) {
gt.expectConfig(t, ngrok)
}
}
ctx, cancel := caddy.NewContext(caddy.Context{Context: context.Background()})
defer cancel()
err = ngrok.Provision(ctx)
if gt.expectProvisionErr {
require.NotNil(t, err)
return
} else {
require.Nil(t, err)
if gt.expectedOptsFunc != nil {
gt.expectedOptsFunc(t, ngrok)
}
}
})
}
func (gtcs genericNgrokTestCases[C]) runAll(t *testing.T) {
for _, tc := range gtcs {
tc.run(t)
}
}