-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrenchcoat_test.go
More file actions
487 lines (430 loc) · 11.1 KB
/
Copy pathtrenchcoat_test.go
File metadata and controls
487 lines (430 loc) · 11.1 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
package trenchcoat
import (
"encoding/json"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
// httpClient is a shared test client with an explicit timeout to prevent
// tests from hanging indefinitely if the server stalls.
var httpClient = &http.Client{Timeout: 5 * time.Second}
func TestWithCoat(t *testing.T) {
srv := NewServer(
WithCoat(Coat{
Name: "test",
Request: Request{Method: "GET", URI: "/test"},
Response: &Response{Code: 200, Body: "hello"},
}),
)
srv.Start(t)
resp, err := httpClient.Get(srv.URL + "/test")
if err != nil {
t.Fatalf("request failed: %v", err)
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != 200 {
t.Fatalf("expected 200, got %d", resp.StatusCode)
}
}
func TestWithCoats(t *testing.T) {
srv := NewServer(
WithCoats(
Coat{
Name: "a",
Request: Request{Method: "GET", URI: "/a"},
Response: &Response{Code: 200, Body: "a"},
},
Coat{
Name: "b",
Request: Request{Method: "GET", URI: "/b"},
Response: &Response{Code: 201, Body: "b"},
},
),
)
srv.Start(t)
resp, err := httpClient.Get(srv.URL + "/a")
if err != nil {
t.Fatalf("request failed: %v", err)
}
_ = resp.Body.Close()
if resp.StatusCode != 200 {
t.Fatalf("expected 200, got %d", resp.StatusCode)
}
resp2, err := httpClient.Get(srv.URL + "/b")
if err != nil {
t.Fatalf("request failed: %v", err)
}
_ = resp2.Body.Close()
if resp2.StatusCode != 201 {
t.Fatalf("expected 201, got %d", resp2.StatusCode)
}
}
func TestWithCoatFile(t *testing.T) {
dir := t.TempDir()
coatFile := filepath.Join(dir, "test.yaml")
content := `
coats:
- name: "from-file"
request:
method: GET
uri: "/from-file"
response:
code: 200
body: "loaded from file"
`
if err := os.WriteFile(coatFile, []byte(content), 0644); err != nil {
t.Fatal(err)
}
srv := NewServer(WithCoatFile(coatFile))
srv.Start(t)
resp, err := httpClient.Get(srv.URL + "/from-file")
if err != nil {
t.Fatalf("request failed: %v", err)
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != 200 {
t.Fatalf("expected 200, got %d", resp.StatusCode)
}
}
func TestWithVerbose(t *testing.T) {
srv := NewServer(
WithCoat(Coat{
Name: "verbose-test",
Request: Request{Method: "GET", URI: "/verbose"},
Response: &Response{Code: 200, Body: "ok"},
}),
WithVerbose(),
)
if !srv.verbose {
t.Fatal("expected verbose to be true")
}
srv.Start(t)
resp, err := httpClient.Get(srv.URL + "/verbose")
if err != nil {
t.Fatalf("request failed: %v", err)
}
_ = resp.Body.Close()
if resp.StatusCode != 200 {
t.Fatalf("expected 200, got %d", resp.StatusCode)
}
}
func TestStop_BeforeStart(t *testing.T) {
srv := NewServer()
// Should not panic.
srv.Stop()
}
func TestNewServer_NoOptions(t *testing.T) {
srv := NewServer()
if srv == nil {
t.Fatal("expected non-nil server")
}
if len(srv.coats) != 0 {
t.Fatalf("expected 0 coats, got %d", len(srv.coats))
}
}
func TestWithCoat_BodyMatching(t *testing.T) {
srv := NewServer(
WithCoats(
Coat{
Name: "create-alice",
Request: Request{Method: "POST", URI: "/api/v1/users", Body: StringPtr(`{"name": "alice"}`)},
Response: &Response{
Code: 201,
Body: "alice created",
},
},
Coat{
Name: "create-bob",
Request: Request{Method: "POST", URI: "/api/v1/users", Body: StringPtr(`{"name": "bob"}`)},
Response: &Response{
Code: 201,
Body: "bob created",
},
},
),
)
srv.Start(t)
// POST with alice body.
resp, err := httpClient.Post(srv.URL+"/api/v1/users", "application/json", strings.NewReader(`{"name": "alice"}`))
if err != nil {
t.Fatalf("request failed: %v", err)
}
body, err := io.ReadAll(resp.Body)
_ = resp.Body.Close()
if err != nil {
t.Fatalf("failed to read response body: %v", err)
}
if resp.StatusCode != 201 {
t.Fatalf("expected 201, got %d", resp.StatusCode)
}
if string(body) != "alice created" {
t.Fatalf("expected 'alice created', got %q", body)
}
// POST with bob body.
resp2, err := httpClient.Post(srv.URL+"/api/v1/users", "application/json", strings.NewReader(`{"name": "bob"}`))
if err != nil {
t.Fatalf("request failed: %v", err)
}
body2, err := io.ReadAll(resp2.Body)
_ = resp2.Body.Close()
if err != nil {
t.Fatalf("failed to read response body: %v", err)
}
if resp2.StatusCode != 201 {
t.Fatalf("expected 201, got %d", resp2.StatusCode)
}
if string(body2) != "bob created" {
t.Fatalf("expected 'bob created', got %q", body2)
}
}
func TestNewServer_NoCoats_Returns404(t *testing.T) {
srv := NewServer()
srv.Start(t)
resp, err := httpClient.Get(srv.URL + "/anything")
if err != nil {
t.Fatalf("request failed: %v", err)
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != 404 {
t.Fatalf("expected 404, got %d", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("failed to read body: %v", err)
}
var errResp map[string]string
if err := json.Unmarshal(body, &errResp); err != nil {
t.Fatalf("expected JSON error body, got: %s", body)
}
if _, ok := errResp["error"]; !ok {
t.Fatalf("expected 'error' key in JSON response, got: %s", body)
}
}
func TestWithCoatFile_NonExistent(t *testing.T) {
missingCoat := filepath.Join(t.TempDir(), "nonexistent-coat.yaml")
srv := NewServer(WithCoatFile(missingCoat))
if len(srv.loadErrs) == 0 {
t.Fatal("expected load errors for non-existent coat file")
}
}
// --- Request Assertions / Call Counting ---
func TestAssertCalled(t *testing.T) {
srv := NewServer(
WithCoat(Coat{
Name: "get-test",
Request: Request{Method: "GET", URI: "/test"},
Response: &Response{Code: 200, Body: "ok"},
}),
)
srv.Start(t)
resp, err := httpClient.Get(srv.URL + "/test")
if err != nil {
t.Fatalf("request failed: %v", err)
}
_ = resp.Body.Close()
srv.AssertCalled(t, "get-test")
}
func TestAssertCalledN(t *testing.T) {
srv := NewServer(
WithCoat(Coat{
Name: "counted",
Request: Request{Method: "GET", URI: "/counted"},
Response: &Response{Code: 200, Body: "ok"},
}),
)
srv.Start(t)
for range 3 {
resp, err := httpClient.Get(srv.URL + "/counted")
if err != nil {
t.Fatalf("request failed: %v", err)
}
_ = resp.Body.Close()
}
srv.AssertCalledN(t, "counted", 3)
}
func TestAssertNotCalled(t *testing.T) {
srv := NewServer(
WithCoats(
Coat{
Name: "used",
Request: Request{Method: "GET", URI: "/used"},
Response: &Response{Code: 200, Body: "ok"},
},
Coat{
Name: "unused",
Request: Request{Method: "GET", URI: "/unused"},
Response: &Response{Code: 200, Body: "ok"},
},
),
)
srv.Start(t)
resp, err := httpClient.Get(srv.URL + "/used")
if err != nil {
t.Fatalf("request failed: %v", err)
}
_ = resp.Body.Close()
srv.AssertNotCalled(t, "unused")
}
func TestRequests_CapturesDetails(t *testing.T) {
srv := NewServer(
WithCoat(Coat{
Name: "capture-test",
Request: Request{Method: "POST", URI: "/capture"},
Response: &Response{Code: 201, Body: "created"},
}),
)
srv.Start(t)
req, _ := http.NewRequest("POST", srv.URL+"/capture", strings.NewReader(`{"name":"alice"}`))
req.Header.Set("X-Custom", "test-value")
resp, err := httpClient.Do(req)
if err != nil {
t.Fatalf("request failed: %v", err)
}
_ = resp.Body.Close()
reqs := srv.Requests("capture-test")
if len(reqs) != 1 {
t.Fatalf("expected 1 captured request, got %d", len(reqs))
}
cr := reqs[0]
if cr.Method != "POST" {
t.Errorf("expected method POST, got %q", cr.Method)
}
if cr.URI != "/capture" {
t.Errorf("expected URI /capture, got %q", cr.URI)
}
if cr.Header.Get("X-Custom") != "test-value" {
t.Errorf("expected X-Custom header 'test-value', got %q", cr.Header.Get("X-Custom"))
}
if cr.Body != `{"name":"alice"}` {
t.Errorf("expected body %q, got %q", `{"name":"alice"}`, cr.Body)
}
}
func TestCapturedRequestQuerySeparation(t *testing.T) {
srv := NewServer(
WithCoat(Coat{
Name: "query-test",
Request: Request{Method: "GET", URI: "/search"},
Response: &Response{Code: 200, Body: "ok"},
}),
)
srv.Start(t)
resp, err := httpClient.Get(srv.URL + "/search?q=hello&page=2")
if err != nil {
t.Fatalf("request failed: %v", err)
}
_ = resp.Body.Close()
reqs := srv.Requests("query-test")
if len(reqs) != 1 {
t.Fatalf("expected 1 captured request, got %d", len(reqs))
}
cr := reqs[0]
if cr.URI != "/search" {
t.Errorf("expected URI /search (path only), got %q", cr.URI)
}
if cr.RawQuery != "q=hello&page=2" {
t.Errorf("expected RawQuery 'q=hello&page=2', got %q", cr.RawQuery)
}
}
func TestResetCalls(t *testing.T) {
srv := NewServer(
WithCoat(Coat{
Name: "resettable",
Request: Request{Method: "GET", URI: "/reset"},
Response: &Response{Code: 200, Body: "ok"},
}),
)
srv.Start(t)
resp, err := httpClient.Get(srv.URL + "/reset")
if err != nil {
t.Fatalf("request failed: %v", err)
}
_ = resp.Body.Close()
srv.AssertCalledN(t, "resettable", 1)
srv.ResetCalls()
srv.AssertNotCalled(t, "resettable")
}
// --- Public API TLS Support ---
func TestWithSelfSignedTLS(t *testing.T) {
srv := NewServer(
WithCoat(Coat{
Name: "tls-test",
Request: Request{Method: "GET", URI: "/secure"},
Response: &Response{Code: 200, Body: "secure-ok"},
}),
WithSelfSignedTLS(),
)
srv.Start(t)
if !strings.HasPrefix(srv.URL, "https://") {
t.Fatalf("expected https:// URL, got %q", srv.URL)
}
if srv.TLSClient == nil {
t.Fatal("expected TLSClient to be set")
}
resp, err := srv.TLSClient.Get(srv.URL + "/secure")
if err != nil {
t.Fatalf("TLS request failed: %v", err)
}
defer func() { _ = resp.Body.Close() }()
body, _ := io.ReadAll(resp.Body)
if resp.StatusCode != 200 {
t.Fatalf("expected 200, got %d", resp.StatusCode)
}
if string(body) != "secure-ok" {
t.Fatalf("expected 'secure-ok', got %q", body)
}
}
func TestWithSelfSignedTLS_AssertionsWork(t *testing.T) {
srv := NewServer(
WithCoat(Coat{
Name: "tls-assert",
Request: Request{Method: "GET", URI: "/check"},
Response: &Response{Code: 200, Body: "ok"},
}),
WithSelfSignedTLS(),
)
srv.Start(t)
resp, err := srv.TLSClient.Get(srv.URL + "/check")
if err != nil {
t.Fatalf("request failed: %v", err)
}
_ = resp.Body.Close()
srv.AssertCalled(t, "tls-assert")
srv.AssertCalledN(t, "tls-assert", 1)
}
func TestRequests_EmptyForUnknownCoat(t *testing.T) {
srv := NewServer(
WithCoat(Coat{
Name: "known",
Request: Request{Method: "GET", URI: "/known"},
Response: &Response{Code: 200, Body: "ok"},
}),
)
srv.Start(t)
reqs := srv.Requests("nonexistent")
if len(reqs) != 0 {
t.Fatalf("expected 0 captured requests for unknown coat, got %d", len(reqs))
}
}
func TestWithCoatFile_InvalidCoat(t *testing.T) {
dir := t.TempDir()
coatFile := filepath.Join(dir, "bad.yaml")
// Coat without a URI — should fail validation.
content := `
coats:
- name: "missing-uri"
response:
code: 200
body: "oops"
`
if err := os.WriteFile(coatFile, []byte(content), 0644); err != nil {
t.Fatal(err)
}
srv := NewServer(WithCoatFile(coatFile))
if len(srv.loadErrs) == 0 {
t.Fatal("expected validation errors for coat without URI")
}
}