-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
336 lines (307 loc) · 9.6 KB
/
example_test.go
File metadata and controls
336 lines (307 loc) · 9.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
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
// Package example_test demonstrates how to use Trenchcoat as a mock
// HTTP server within Go test suites. Each test function shows a different
// feature of Trenchcoat's matching engine.
//
// To run these tests:
//
// go test -v ./examples/go-tests/
package example_test
import (
"encoding/json"
"io"
"net/http"
"strings"
"testing"
"github.com/yesdevnull/trenchcoat"
)
// TestBasicMock demonstrates a simple coat serving a JSON response.
// The test starts a Trenchcoat server with a single coat, makes an
// HTTP request, and asserts the response body and status code.
func TestBasicMock(t *testing.T) {
srv := trenchcoat.NewServer(
trenchcoat.WithCoat(trenchcoat.Coat{
Name: "get-users",
Request: trenchcoat.Request{
Method: "GET",
URI: "/api/v1/users",
},
Response: &trenchcoat.Response{
Code: 200,
Headers: map[string]string{
"Content-Type": "application/json",
},
Body: `{"users": [{"id": 1, "name": "Alice"}]}`,
},
}),
)
srv.Start(t)
defer srv.Stop()
resp, err := http.Get(srv.URL + "/api/v1/users")
if err != nil {
t.Fatalf("request failed: %v", err)
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != 200 {
t.Fatalf("expected status 200, got %d", resp.StatusCode)
}
if resp.Header.Get("Content-Type") != "application/json" {
t.Fatalf("expected Content-Type application/json, got %s", resp.Header.Get("Content-Type"))
}
body := readBody(t, resp)
var result map[string]interface{}
if err := json.Unmarshal([]byte(body), &result); err != nil {
t.Fatalf("failed to parse JSON response: %v", err)
}
users := result["users"].([]interface{})
if len(users) != 1 {
t.Fatalf("expected 1 user, got %d", len(users))
}
}
// TestMultipleCoats demonstrates loading several coats together.
// Each test function hits a different endpoint and verifies it returns
// the correct mock response.
func TestMultipleCoats(t *testing.T) {
srv := trenchcoat.NewServer(
trenchcoat.WithCoats(
trenchcoat.Coat{
Name: "users-list",
Request: trenchcoat.Request{Method: "GET", URI: "/api/users"},
Response: &trenchcoat.Response{Code: 200, Body: `{"users": []}`},
},
trenchcoat.Coat{
Name: "health-check",
Request: trenchcoat.Request{Method: "GET", URI: "/health"},
Response: &trenchcoat.Response{Code: 200, Body: `{"status": "ok"}`},
},
trenchcoat.Coat{
Name: "not-found",
Request: trenchcoat.Request{Method: "GET", URI: "/api/missing"},
Response: &trenchcoat.Response{Code: 404, Body: `{"error": "not found"}`},
},
),
)
srv.Start(t)
defer srv.Stop()
t.Run("users endpoint", func(t *testing.T) {
resp, err := http.Get(srv.URL + "/api/users")
if err != nil {
t.Fatalf("request failed: %v", err)
}
defer func() { _ = resp.Body.Close() }()
assertEqual(t, "status", 200, resp.StatusCode)
assertBodyContains(t, resp, "users")
})
t.Run("health endpoint", func(t *testing.T) {
resp, err := http.Get(srv.URL + "/health")
if err != nil {
t.Fatalf("request failed: %v", err)
}
defer func() { _ = resp.Body.Close() }()
assertEqual(t, "status", 200, resp.StatusCode)
assertBodyContains(t, resp, "ok")
})
t.Run("missing endpoint", func(t *testing.T) {
resp, err := http.Get(srv.URL + "/api/missing")
if err != nil {
t.Fatalf("request failed: %v", err)
}
defer func() { _ = resp.Body.Close() }()
assertEqual(t, "status", 404, resp.StatusCode)
})
}
// TestMethodDifferentiation demonstrates GET and POST to the same URI
// returning different responses.
func TestMethodDifferentiation(t *testing.T) {
srv := trenchcoat.NewServer(
trenchcoat.WithCoats(
trenchcoat.Coat{
Name: "list-users",
Request: trenchcoat.Request{Method: "GET", URI: "/api/users"},
Response: &trenchcoat.Response{
Code: 200,
Headers: map[string]string{"Content-Type": "application/json"},
Body: `{"users": [{"id": 1, "name": "Alice"}]}`,
},
},
trenchcoat.Coat{
Name: "create-user",
Request: trenchcoat.Request{Method: "POST", URI: "/api/users"},
Response: &trenchcoat.Response{
Code: 201,
Headers: map[string]string{
"Content-Type": "application/json",
"Location": "/api/users/2",
},
Body: `{"id": 2, "name": "Bob"}`,
},
},
),
)
srv.Start(t)
defer srv.Stop()
// GET returns the list of users.
resp, err := http.Get(srv.URL + "/api/users")
if err != nil {
t.Fatalf("GET failed: %v", err)
}
defer func() { _ = resp.Body.Close() }()
assertEqual(t, "GET status", 200, resp.StatusCode)
assertBodyContains(t, resp, "Alice")
// POST creates a new user.
resp2, err := http.Post(srv.URL+"/api/users", "application/json", nil)
if err != nil {
t.Fatalf("POST failed: %v", err)
}
defer func() { _ = resp2.Body.Close() }()
assertEqual(t, "POST status", 201, resp2.StatusCode)
assertEqual(t, "Location header", "/api/users/2", resp2.Header.Get("Location"))
}
// TestHeaderMatching demonstrates a coat that only matches when a
// specific header is present.
func TestHeaderMatching(t *testing.T) {
srv := trenchcoat.NewServer(
trenchcoat.WithCoat(trenchcoat.Coat{
Name: "authenticated-endpoint",
Request: trenchcoat.Request{
Method: "GET",
URI: "/api/protected",
// Only matches requests with an Authorization header matching "Bearer *".
Headers: map[string]string{
"Authorization": "Bearer *",
},
},
Response: &trenchcoat.Response{
Code: 200,
Body: `{"message": "welcome, authorised user"}`,
},
}),
)
srv.Start(t)
defer srv.Stop()
// Request WITH the required header — should match and return 200.
t.Run("with auth header", func(t *testing.T) {
req, _ := http.NewRequest("GET", srv.URL+"/api/protected", nil)
req.Header.Set("Authorization", "Bearer my-secret-token")
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatalf("request failed: %v", err)
}
defer func() { _ = resp.Body.Close() }()
assertEqual(t, "status", 200, resp.StatusCode)
assertBodyContains(t, resp, "welcome")
})
// Request WITHOUT the header — no match, returns 404.
t.Run("without auth header", func(t *testing.T) {
resp, err := http.Get(srv.URL + "/api/protected")
if err != nil {
t.Fatalf("request failed: %v", err)
}
defer func() { _ = resp.Body.Close() }()
assertEqual(t, "status", 404, resp.StatusCode)
})
}
// TestResponseSequences demonstrates a coat using responses (plural)
// with cycle mode. Multiple requests return rotating responses.
func TestResponseSequences(t *testing.T) {
srv := trenchcoat.NewServer(
trenchcoat.WithCoat(trenchcoat.Coat{
Name: "health-sequence",
Request: trenchcoat.Request{Method: "GET", URI: "/health"},
Responses: []trenchcoat.Response{
{Code: 503, Body: "Service Unavailable"},
{Code: 503, Body: "Service Unavailable"},
{Code: 200, Body: `{"status": "ok"}`},
},
Sequence: "cycle",
}),
)
srv.Start(t)
defer srv.Stop()
// First two requests return 503 (simulating a service warming up).
for i := 0; i < 2; i++ {
resp, err := http.Get(srv.URL + "/health")
if err != nil {
t.Fatalf("request %d failed: %v", i+1, err)
}
defer func() { _ = resp.Body.Close() }()
assertEqual(t, "status", 503, resp.StatusCode)
}
// Third request returns 200 (service is ready).
resp, err := http.Get(srv.URL + "/health")
if err != nil {
t.Fatalf("request failed: %v", err)
}
defer func() { _ = resp.Body.Close() }()
assertEqual(t, "status", 200, resp.StatusCode)
// Fourth request cycles back to first response (503).
resp2, err := http.Get(srv.URL + "/health")
if err != nil {
t.Fatalf("request failed: %v", err)
}
defer func() { _ = resp2.Body.Close() }()
assertEqual(t, "cycle status", 503, resp2.StatusCode)
}
// TestGlobURIMatching demonstrates a coat with a wildcard URI pattern.
// It matches any path under /api/v1/users/.
func TestGlobURIMatching(t *testing.T) {
srv := trenchcoat.NewServer(
trenchcoat.WithCoat(trenchcoat.Coat{
Name: "user-by-id",
Request: trenchcoat.Request{
Method: "GET",
// Glob pattern: matches any single path segment after /api/v1/users/.
URI: "/api/v1/users/*",
},
Response: &trenchcoat.Response{
Code: 200,
Headers: map[string]string{"Content-Type": "application/json"},
Body: `{"id": "matched", "name": "Wildcard User"}`,
},
}),
)
srv.Start(t)
defer srv.Stop()
// Various user IDs should all match the glob pattern.
for _, id := range []string{"1", "42", "alice", "user-123"} {
t.Run("user/"+id, func(t *testing.T) {
resp, err := http.Get(srv.URL + "/api/v1/users/" + id)
if err != nil {
t.Fatalf("request failed: %v", err)
}
defer func() { _ = resp.Body.Close() }()
assertEqual(t, "status", 200, resp.StatusCode)
assertBodyContains(t, resp, "Wildcard User")
})
}
// Path with additional segments should NOT match (glob * doesn't cross /);
t.Run("nested path does not match", func(t *testing.T) {
resp, err := http.Get(srv.URL + "/api/v1/users/42/posts")
if err != nil {
t.Fatalf("request failed: %v", err)
}
defer func() { _ = resp.Body.Close() }()
assertEqual(t, "status", 404, resp.StatusCode)
})
}
// --- Helpers ---
func readBody(t *testing.T, resp *http.Response) string {
t.Helper()
b, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("failed to read body: %v", err)
}
return string(b)
}
func assertEqual[T comparable](t *testing.T, field string, expected, actual T) {
t.Helper()
if expected != actual {
t.Errorf("%s: expected %v, got %v", field, expected, actual)
}
}
func assertBodyContains(t *testing.T, resp *http.Response, substring string) {
t.Helper()
body := readBody(t, resp)
if !strings.Contains(body, substring) {
t.Errorf("expected body to contain %q, got: %s", substring, body)
}
}