Repository navigation
Expand file tree
/
Copy pathrequest_test.go
More file actions
217 lines (190 loc) · 5.6 KB
/
Copy pathrequest_test.go
File metadata and controls
217 lines (190 loc) · 5.6 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package main
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
func newTestOpt(addr, expectContent string) Opt {
opt := Opt{
Hostname: addr,
URI: "/",
Method: "GET",
Port: 80,
Expect: "HTTP/1.1 200",
bufferSize: 1024,
}
if expectContent != "" {
opt.expectByte = []byte(expectContent)
}
return opt
}
type runRequestOptFunc func(*Opt)
func runRequestTest(t *testing.T, handler http.HandlerFunc, modifyOpt runRequestOptFunc, wantCode int, wantSubstring string) {
t.Helper()
server := httptest.NewServer(handler)
defer server.Close()
opt := newTestOpt(server.Listener.Addr().String(), "")
opt.Consecutive = 1
opt.Interim = 10 * time.Millisecond
if modifyOpt != nil {
modifyOpt(&opt)
}
client := server.Client()
msg, code := opt.runRequest(context.Background(), client)
if code != wantCode {
t.Fatalf("runRequest() code = %d, want %d; msg = %s", code, wantCode, msg)
}
if !strings.Contains(msg, wantSubstring) {
t.Fatalf("runRequest() msg = %q, want substring %q", msg, wantSubstring)
}
}
func TestRunRequestSuccess(t *testing.T) {
runRequestTest(t,
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprint(w, "ok")
}),
nil,
OK,
"HTTP OK:",
)
}
func TestRunRequestStatusNotMatched(t *testing.T) {
runRequestTest(t,
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
_, _ = fmt.Fprint(w, "error")
}),
nil,
CRITICAL,
"Invalid HTTP response received",
)
}
func TestRunRequestContentNotMatched(t *testing.T) {
runRequestTest(t,
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprint(w, "unexpected content")
}),
func(opt *Opt) {
opt.expectByte = []byte("expected body")
},
CRITICAL,
"Not matched",
)
}
func TestRunRequestConsecutiveSuccess(t *testing.T) {
callCount := 0
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
callCount++
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprint(w, "ok")
}))
defer server.Close()
opt := newTestOpt(server.Listener.Addr().String(), "")
opt.Consecutive = 3
opt.Interim = 10 * time.Millisecond
client := server.Client()
msg, code := opt.runRequest(context.Background(), client)
if code != OK {
t.Fatalf("runRequest() code = %d, want %d; msg = %s", code, OK, msg)
}
if callCount != 3 {
t.Fatalf("server called %d times, want 3", callCount)
}
if !strings.HasPrefix(msg, "HTTP OK:") {
t.Fatalf("runRequest() msg = %q, want HTTP OK prefix", msg)
}
}
func TestRequestClientTimeout(t *testing.T) {
block := make(chan struct{})
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
<-block
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
defer close(block)
opt := newTestOpt(server.Listener.Addr().String(), "")
client := server.Client()
client.Timeout = 50 * time.Millisecond
_, errReq := opt.Request(context.Background(), client)
if errReq == nil {
t.Fatal("Request() error = nil, want non-nil")
}
if errReq.Code() != CRITICAL {
t.Fatalf("Request() code = %d, want %d", errReq.Code(), CRITICAL)
}
if !strings.Contains(errReq.Error(), "context deadline exceeded") {
t.Fatalf("Request() error = %q, want context deadline exceeded", errReq.Error())
}
}
func TestRunWaitForEventuallySucceeds(t *testing.T) {
callCount := 0
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
callCount++
if callCount < 3 {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprint(w, "ok")
}))
defer server.Close()
opt := newTestOpt(server.Listener.Addr().String(), "")
opt.Consecutive = 1
opt.WaitFor = true
opt.WaitForInterval = 10 * time.Millisecond
opt.Interim = 10 * time.Millisecond
client := server.Client()
msg, code := opt.runWaitFor(context.Background(), client)
if code != OK {
t.Fatalf("runWaitFor() code = %d, want %d; msg = %s", code, OK, msg)
}
if callCount < 3 {
t.Fatalf("server called %d times, want at least 3", callCount)
}
if !strings.HasPrefix(msg, "HTTP OK:") {
t.Fatalf("runWaitFor() msg = %q, want HTTP OK prefix", msg)
}
}
func TestRunWaitForContextTimeout(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}))
defer server.Close()
opt := newTestOpt(server.Listener.Addr().String(), "")
opt.Consecutive = 1
opt.WaitFor = true
opt.WaitForInterval = 10 * time.Millisecond
opt.Interim = 10 * time.Millisecond
client := server.Client()
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()
msg, code := opt.runWaitFor(ctx, client)
if code != UNKNOWN {
t.Fatalf("runWaitFor() code = %d, want %d; msg = %s", code, UNKNOWN, msg)
}
if msg != "Give up waiting for success" {
t.Fatalf("runWaitFor() msg = %q, want %q", msg, "Give up waiting for success")
}
}
func TestRequestWithContentMatch(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprint(w, "hello world")
}))
defer server.Close()
opt := newTestOpt(server.Listener.Addr().String(), "world")
client := server.Client()
msg, errReq := opt.Request(context.Background(), client)
if errReq != nil {
t.Fatalf("Request() error = %v", errReq)
}
if !strings.Contains(msg, "Response body matched") {
t.Fatalf("Request() msg = %q, want Response body matched", msg)
}
}