-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrender_test.go
More file actions
603 lines (469 loc) · 12.6 KB
/
render_test.go
File metadata and controls
603 lines (469 loc) · 12.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
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
package fox
import (
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"testing"
"github.com/fox-gonic/fox/httperrors"
"github.com/fox-gonic/fox/render"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Test StatusCoder interface
type testStatusCoder struct {
code int
}
func (t *testStatusCoder) StatusCode() int {
return t.code
}
func (t *testStatusCoder) Error() string {
return "test error with status code"
}
// Test render.Render interface
type testRender struct {
data string
}
func (t *testRender) Render(w http.ResponseWriter) error {
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(t.data))
return err
}
func (t *testRender) WriteContentType(w http.ResponseWriter) {
w.Header().Set("Content-Type", "text/plain")
}
// Test json.Marshaler interface
type testJSONMarshaler struct {
Message string
Code int
}
func (t *testJSONMarshaler) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]any{
"message": t.Message,
"code": t.Code,
})
}
func (t *testJSONMarshaler) Error() string {
return t.Message
}
// Test renderError function
func TestRenderError_Nil(t *testing.T) {
engine := New()
w := httptest.NewRecorder()
ginCtx, _ := gin.CreateTestContext(w)
ctx := &Context{
Context: ginCtx,
engine: engine,
}
ctx.renderError(nil)
// Should do nothing for nil error
// Note: Gin sets 200 by default, but no body written
assert.Empty(t, w.Body.String())
}
func TestRenderError_CustomRenderErrorFunc(t *testing.T) {
engine := New()
engine.RenderErrorFunc = func(c *Context, err error) {
c.JSON(http.StatusTeapot, gin.H{"custom": err.Error()})
}
w := httptest.NewRecorder()
ginCtx, _ := gin.CreateTestContext(w)
ctx := &Context{
Context: ginCtx,
engine: engine,
}
ctx.renderError(errors.New("test error"))
assert.Equal(t, http.StatusTeapot, w.Code)
assert.Contains(t, w.Body.String(), "custom")
assert.Contains(t, w.Body.String(), "test error")
}
func TestRenderError_StatusCoder(t *testing.T) {
engine := New()
w := httptest.NewRecorder()
ginCtx, _ := gin.CreateTestContext(w)
ctx := &Context{
Context: ginCtx,
engine: engine,
}
err := &testStatusCoder{code: http.StatusNotFound}
ctx.renderError(err)
assert.Equal(t, http.StatusNotFound, w.Code)
assert.Contains(t, w.Body.String(), "test error with status code")
}
func TestRenderError_DefaultStatusCode(t *testing.T) {
engine := New()
engine.DefaultRenderErrorStatusCode = http.StatusBadRequest
w := httptest.NewRecorder()
ginCtx, _ := gin.CreateTestContext(w)
ctx := &Context{
Context: ginCtx,
engine: engine,
}
ctx.renderError(errors.New("test error"))
assert.Equal(t, http.StatusBadRequest, w.Code)
assert.Contains(t, w.Body.String(), "test error")
}
func TestRenderError_JSONMarshaler(t *testing.T) {
engine := New()
engine.DefaultRenderErrorStatusCode = http.StatusInternalServerError
w := httptest.NewRecorder()
ginCtx, _ := gin.CreateTestContext(w)
ctx := &Context{
Context: ginCtx,
engine: engine,
}
err := &testJSONMarshaler{
Message: "test json error",
Code: 1001,
}
ctx.renderError(err)
assert.Equal(t, http.StatusInternalServerError, w.Code)
assert.Contains(t, w.Body.String(), "test json error")
assert.Contains(t, w.Body.String(), "1001")
}
func TestRenderError_PlainError(t *testing.T) {
engine := New()
engine.DefaultRenderErrorStatusCode = http.StatusInternalServerError
w := httptest.NewRecorder()
ginCtx, _ := gin.CreateTestContext(w)
ctx := &Context{
Context: ginCtx,
engine: engine,
}
ctx.renderError(errors.New("plain error"))
assert.Equal(t, http.StatusInternalServerError, w.Code)
assert.Equal(t, "plain error", w.Body.String())
}
// testRenderError implements both error and render.Render interfaces
type testRenderError struct {
message string
code int
}
func (e *testRenderError) Error() string {
return e.message
}
func (e *testRenderError) StatusCode() int {
return e.code
}
func (e *testRenderError) Render(w http.ResponseWriter) error {
_, err := w.Write([]byte("rendered: " + e.message))
return err
}
func (e *testRenderError) WriteContentType(w http.ResponseWriter) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
}
func TestRenderError_RenderInterface(t *testing.T) {
engine := New()
w := httptest.NewRecorder()
ginCtx, _ := gin.CreateTestContext(w)
ctx := &Context{
Context: ginCtx,
engine: engine,
}
err := &testRenderError{
message: "test render error",
code: http.StatusBadRequest,
}
ctx.renderError(err)
assert.Equal(t, http.StatusBadRequest, w.Code)
assert.Equal(t, "rendered: test render error", w.Body.String())
}
func TestRenderError_HTTPError(t *testing.T) {
engine := New()
w := httptest.NewRecorder()
ginCtx, _ := gin.CreateTestContext(w)
ctx := &Context{
Context: ginCtx,
engine: engine,
}
httpErr := httperrors.New(http.StatusBadRequest, "validation failed").
SetCode("VALIDATION_ERROR").
AddField("field", "email")
ctx.renderError(httpErr)
assert.Equal(t, http.StatusBadRequest, w.Code)
var result map[string]any
err := json.Unmarshal(w.Body.Bytes(), &result)
require.NoError(t, err)
assert.Equal(t, "VALIDATION_ERROR", result["code"])
assert.Equal(t, "email", result["field"])
}
// Test render function
func TestRender_Nil(t *testing.T) {
engine := New()
w := httptest.NewRecorder()
ginCtx, _ := gin.CreateTestContext(w)
ctx := &Context{
Context: ginCtx,
engine: engine,
}
ctx.render(nil)
// Should do nothing for nil result
// Note: Gin sets 200 by default, but no body written
assert.Empty(t, w.Body.String())
assert.False(t, ctx.IsAborted())
}
func TestRender_Error(t *testing.T) {
engine := New()
engine.DefaultRenderErrorStatusCode = http.StatusInternalServerError
w := httptest.NewRecorder()
ginCtx, _ := gin.CreateTestContext(w)
ctx := &Context{
Context: ginCtx,
engine: engine,
}
ctx.render(errors.New("test error"))
assert.Equal(t, http.StatusInternalServerError, w.Code)
assert.Contains(t, w.Body.String(), "test error")
assert.True(t, ctx.IsAborted())
}
func TestRender_String(t *testing.T) {
engine := New()
w := httptest.NewRecorder()
ginCtx, _ := gin.CreateTestContext(w)
ctx := &Context{
Context: ginCtx,
engine: engine,
}
ctx.render("hello world")
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "hello world", w.Body.String())
assert.True(t, ctx.IsAborted())
}
func TestRender_Redirect(t *testing.T) {
engine := New()
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/", nil)
ginCtx, _ := gin.CreateTestContext(w)
ginCtx.Request = req
ctx := &Context{
Context: ginCtx,
engine: engine,
}
redirect := render.Redirect{
Code: http.StatusFound,
Location: "https://example.com",
}
ctx.render(redirect)
assert.Equal(t, http.StatusFound, w.Code)
assert.Equal(t, "https://example.com", w.Header().Get("Location"))
assert.True(t, ctx.IsAborted())
}
func TestRender_RenderInterface(t *testing.T) {
engine := New()
w := httptest.NewRecorder()
ginCtx, _ := gin.CreateTestContext(w)
ctx := &Context{
Context: ginCtx,
engine: engine,
}
customRender := &testRender{data: "custom data"}
ctx.render(customRender)
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "custom data", w.Body.String())
assert.True(t, ctx.IsAborted())
}
func TestRender_JSON(t *testing.T) {
engine := New()
w := httptest.NewRecorder()
ginCtx, _ := gin.CreateTestContext(w)
ctx := &Context{
Context: ginCtx,
engine: engine,
}
data := map[string]any{
"name": "John",
"age": 30,
}
ctx.render(data)
assert.Equal(t, http.StatusOK, w.Code)
assert.Contains(t, w.Header().Get("Content-Type"), "application/json")
var result map[string]any
err := json.Unmarshal(w.Body.Bytes(), &result)
require.NoError(t, err)
assert.Equal(t, "John", result["name"])
assert.InDelta(t, 30, result["age"], 0.001) // JSON numbers are float64
assert.True(t, ctx.IsAborted())
}
func TestRender_Struct(t *testing.T) {
engine := New()
w := httptest.NewRecorder()
ginCtx, _ := gin.CreateTestContext(w)
ctx := &Context{
Context: ginCtx,
engine: engine,
}
type User struct {
Name string `json:"name"`
Email string `json:"email"`
}
user := User{
Name: "Jane",
Email: "jane@example.com",
}
ctx.render(user)
assert.Equal(t, http.StatusOK, w.Code)
assert.Contains(t, w.Header().Get("Content-Type"), "application/json")
var result User
err := json.Unmarshal(w.Body.Bytes(), &result)
require.NoError(t, err)
assert.Equal(t, "Jane", result.Name)
assert.Equal(t, "jane@example.com", result.Email)
assert.True(t, ctx.IsAborted())
}
func TestRender_Array(t *testing.T) {
engine := New()
w := httptest.NewRecorder()
ginCtx, _ := gin.CreateTestContext(w)
ctx := &Context{
Context: ginCtx,
engine: engine,
}
data := []string{"a", "b", "c"}
ctx.render(data)
assert.Equal(t, http.StatusOK, w.Code)
assert.Contains(t, w.Header().Get("Content-Type"), "application/json")
var result []string
err := json.Unmarshal(w.Body.Bytes(), &result)
require.NoError(t, err)
assert.Equal(t, []string{"a", "b", "c"}, result)
assert.True(t, ctx.IsAborted())
}
func TestRender_Number(t *testing.T) {
engine := New()
w := httptest.NewRecorder()
ginCtx, _ := gin.CreateTestContext(w)
ctx := &Context{
Context: ginCtx,
engine: engine,
}
ctx.render(42)
assert.Equal(t, http.StatusOK, w.Code)
assert.Contains(t, w.Header().Get("Content-Type"), "application/json")
assert.Equal(t, "42", w.Body.String())
assert.True(t, ctx.IsAborted())
}
func TestRender_Boolean(t *testing.T) {
engine := New()
w := httptest.NewRecorder()
ginCtx, _ := gin.CreateTestContext(w)
ctx := &Context{
Context: ginCtx,
engine: engine,
}
ctx.render(true)
assert.Equal(t, http.StatusOK, w.Code)
assert.Contains(t, w.Header().Get("Content-Type"), "application/json")
assert.Equal(t, "true", w.Body.String())
assert.True(t, ctx.IsAborted())
}
// Test edge cases
func TestRender_EmptyString(t *testing.T) {
engine := New()
w := httptest.NewRecorder()
ginCtx, _ := gin.CreateTestContext(w)
ctx := &Context{
Context: ginCtx,
engine: engine,
}
ctx.render("")
assert.Equal(t, http.StatusOK, w.Code)
assert.Empty(t, w.Body.String())
assert.True(t, ctx.IsAborted())
}
func TestRender_EmptyStruct(t *testing.T) {
engine := New()
w := httptest.NewRecorder()
ginCtx, _ := gin.CreateTestContext(w)
ctx := &Context{
Context: ginCtx,
engine: engine,
}
type Empty struct{}
ctx.render(Empty{})
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "{}", w.Body.String())
assert.True(t, ctx.IsAborted())
}
func TestRender_Pointer(t *testing.T) {
engine := New()
w := httptest.NewRecorder()
ginCtx, _ := gin.CreateTestContext(w)
ctx := &Context{
Context: ginCtx,
engine: engine,
}
type User struct {
Name string `json:"name"`
}
user := &User{Name: "Bob"}
ctx.render(user)
assert.Equal(t, http.StatusOK, w.Code)
assert.Contains(t, w.Body.String(), "Bob")
assert.True(t, ctx.IsAborted())
}
// Benchmark tests
func BenchmarkRenderError_Plain(b *testing.B) {
engine := New()
engine.DefaultRenderErrorStatusCode = http.StatusInternalServerError
err := errors.New("test error")
b.ResetTimer()
for i := 0; i < b.N; i++ {
w := httptest.NewRecorder()
ginCtx, _ := gin.CreateTestContext(w)
ctx := &Context{Context: ginCtx, engine: engine}
ctx.renderError(err)
}
}
func BenchmarkRenderError_HTTPError(b *testing.B) {
engine := New()
httpErr := httperrors.New(http.StatusBadRequest, "validation failed")
b.ResetTimer()
for i := 0; i < b.N; i++ {
w := httptest.NewRecorder()
ginCtx, _ := gin.CreateTestContext(w)
ctx := &Context{Context: ginCtx, engine: engine}
ctx.renderError(httpErr)
}
}
func BenchmarkRender_String(b *testing.B) {
engine := New()
data := "hello world"
b.ResetTimer()
for i := 0; i < b.N; i++ {
w := httptest.NewRecorder()
ginCtx, _ := gin.CreateTestContext(w)
ctx := &Context{Context: ginCtx, engine: engine}
ctx.render(data)
}
}
func BenchmarkRender_JSON(b *testing.B) {
engine := New()
data := map[string]any{
"name": "John",
"age": 30,
"tags": []string{"go", "testing"},
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
w := httptest.NewRecorder()
ginCtx, _ := gin.CreateTestContext(w)
ctx := &Context{Context: ginCtx, engine: engine}
ctx.render(data)
}
}
func BenchmarkRender_Struct(b *testing.B) {
engine := New()
type User struct {
Name string `json:"name"`
Email string `json:"email"`
Age int `json:"age"`
}
user := User{Name: "Jane", Email: "jane@example.com", Age: 25}
b.ResetTimer()
for i := 0; i < b.N; i++ {
w := httptest.NewRecorder()
ginCtx, _ := gin.CreateTestContext(w)
ctx := &Context{Context: ginCtx, engine: engine}
ctx.render(user)
}
}