-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest_test.go
162 lines (156 loc) · 3.51 KB
/
request_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
152
153
154
155
156
157
158
159
160
161
162
//go:build integration
package healthchecks
import (
"context"
"io"
"net/http"
"strings"
"testing"
)
const (
_uuidInvalid = "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb"
_pingKeyInvalid = "ijklmnop"
_slugInvalid = "bar"
)
func TestRequest(t *testing.T) {
config := configFromEnv()
type args struct {
opts *options
body io.Reader
path []string
}
tests := []struct {
name string
operations []string
serverPathPrefix string
args args
wantErr bool
}{
{
name: "uuid valid",
operations: []string{"", "/start", "/fail"},
serverPathPrefix: "",
args: args{
opts: &options{
RootURL: mustURL(config.URLPrefix),
HTTPClient: http.DefaultClient,
},
body: nil,
path: []string{"/", config.UUID},
},
wantErr: false,
},
{
name: "uuid invalid",
operations: []string{""},
serverPathPrefix: "",
args: args{
opts: &options{
RootURL: mustURL(config.URLPrefix),
HTTPClient: http.DefaultClient,
},
body: nil,
path: []string{"/", _uuidInvalid},
},
wantErr: true,
},
{
name: "ping key valid, slug valid",
operations: []string{"", "/start", "/fail"},
serverPathPrefix: "",
args: args{
opts: &options{
RootURL: mustURL(config.URLPrefix),
HTTPClient: http.DefaultClient,
},
body: nil,
path: []string{"/", config.PingKey, "/", config.Slug},
},
wantErr: false,
},
{
name: "ping key valid, slug invalid",
operations: []string{""},
serverPathPrefix: "",
args: args{
opts: &options{
RootURL: mustURL(config.URLPrefix),
HTTPClient: http.DefaultClient,
},
body: nil,
path: []string{"/", config.PingKey, "/", _slugInvalid},
},
wantErr: true,
},
{
name: "ping key invalid",
operations: []string{""},
serverPathPrefix: "",
args: args{
opts: &options{
RootURL: mustURL(config.URLPrefix),
HTTPClient: http.DefaultClient,
},
body: nil,
path: []string{"/", _pingKeyInvalid, "/", config.Slug},
},
wantErr: true,
},
{
name: "path invalid",
operations: []string{""},
serverPathPrefix: "",
args: args{
opts: &options{
RootURL: mustURL(config.URLPrefix),
HTTPClient: http.DefaultClient,
},
body: nil,
path: []string{"/", "invalid"},
},
wantErr: true,
},
{
name: "operation invalid",
operations: []string{"/bar"},
serverPathPrefix: "",
args: args{
opts: &options{
RootURL: mustURL(config.URLPrefix),
HTTPClient: http.DefaultClient,
},
body: nil,
path: []string{"/", config.UUID},
},
wantErr: true,
},
{
name: "with body",
operations: []string{"/log"},
serverPathPrefix: "",
args: args{
opts: &options{
RootURL: mustURL(config.URLPrefix),
HTTPClient: http.DefaultClient,
},
body: strings.NewReader("Foo Bar"),
path: []string{"/", config.UUID},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
for _, op := range tt.operations {
t.Run(op, func(t *testing.T) {
path := make([]string, len(tt.args.path)+1)
copy(path, tt.args.path)
path[len(path)-1] = op
if err := request(context.Background(), tt.args.opts, tt.args.body, path...); (err != nil) != tt.wantErr {
t.Errorf("request() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
})
}
}