-
Notifications
You must be signed in to change notification settings - Fork 161
Expand file tree
/
Copy pathclean_response_middleware_test.go
More file actions
836 lines (774 loc) · 22.3 KB
/
Copy pathclean_response_middleware_test.go
File metadata and controls
836 lines (774 loc) · 22.3 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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
/*
Copyright 2022 The Numaproj Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package routes
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
func TestCleanResponseMiddleware(t *testing.T) {
// Set Gin to test mode
gin.SetMode(gin.TestMode)
tests := []struct {
name string
handlerFunc gin.HandlerFunc
expectedBody string
expectedStatus int
}{
{
name: "removes managedFields from simple JSON response",
handlerFunc: func(c *gin.Context) {
response := map[string]interface{}{
"name": "test-pipeline",
"managedFields": "should be removed",
"namespace": "default",
}
c.JSON(http.StatusOK, response)
},
expectedBody: `{"name":"test-pipeline","namespace":"default"}`,
expectedStatus: http.StatusOK,
},
{
name: "removes managedFields from nested JSON response",
handlerFunc: func(c *gin.Context) {
response := map[string]interface{}{
"kind": "Pipeline",
"metadata": map[string]interface{}{
"name": "my-pipeline",
"managedFields": "should be removed",
"namespace": "default",
},
"spec": map[string]interface{}{
"vertices": []interface{}{
map[string]interface{}{
"name": "input",
"managedFields": "should be removed",
},
},
},
}
c.JSON(http.StatusOK, response)
},
expectedBody: `{"kind":"Pipeline","metadata":{"name":"my-pipeline","namespace":"default"},"spec":{"vertices":[{"name":"input"}]}}`,
expectedStatus: http.StatusOK,
},
{
name: "handles response without managedFields",
handlerFunc: func(c *gin.Context) {
response := map[string]interface{}{
"name": "test-pipeline",
"namespace": "default",
"status": "running",
}
c.JSON(http.StatusOK, response)
},
expectedBody: `{"name":"test-pipeline","namespace":"default","status":"running"}`,
expectedStatus: http.StatusOK,
},
{
name: "handles empty JSON response",
handlerFunc: func(c *gin.Context) {
c.JSON(http.StatusOK, map[string]interface{}{})
},
expectedBody: `{}`,
expectedStatus: http.StatusOK,
},
{
name: "handles JSON array response (leaves unchanged - middleware only processes objects)",
handlerFunc: func(c *gin.Context) {
response := []interface{}{
map[string]interface{}{
"name": "pipeline1",
"managedFields": "remove",
},
map[string]interface{}{
"name": "pipeline2",
"managedFields": "remove",
},
}
c.JSON(http.StatusOK, response)
},
// Note: The middleware only processes JSON objects (maps), not arrays at root level
// So the array response is returned as-is without cleaning
expectedBody: `[{"managedFields":"remove","name":"pipeline1"},{"managedFields":"remove","name":"pipeline2"}]`,
expectedStatus: http.StatusOK,
},
{
name: "handles non-JSON response (plain text)",
handlerFunc: func(c *gin.Context) {
c.String(http.StatusOK, "plain text response")
},
expectedBody: `plain text response`,
expectedStatus: http.StatusOK,
},
{
name: "skips processing for text/html content type",
handlerFunc: func(c *gin.Context) {
c.Header("Content-Type", "text/html")
c.String(http.StatusOK, "<html><body>HTML content</body></html>")
},
expectedBody: `<html><body>HTML content</body></html>`,
expectedStatus: http.StatusOK,
},
{
name: "skips processing for text/xml content type",
handlerFunc: func(c *gin.Context) {
c.Header("Content-Type", "text/xml")
c.String(http.StatusOK, "<xml><data>test</data></xml>")
},
expectedBody: `<xml><data>test</data></xml>`,
expectedStatus: http.StatusOK,
},
{
name: "processes application/json content type",
handlerFunc: func(c *gin.Context) {
c.Header("Content-Type", "application/json")
response := map[string]interface{}{
"name": "test",
"managedFields": "should be removed",
}
c.JSON(http.StatusOK, response)
},
expectedBody: `{"name":"test"}`,
expectedStatus: http.StatusOK,
},
{
name: "processes application/json; charset=utf-8 content type",
handlerFunc: func(c *gin.Context) {
response := map[string]interface{}{
"name": "test",
"managedFields": "should be removed",
}
c.JSON(http.StatusOK, response)
},
expectedBody: `{"name":"test"}`,
expectedStatus: http.StatusOK,
},
{
name: "handles error status codes",
handlerFunc: func(c *gin.Context) {
response := map[string]interface{}{
"error": "something went wrong",
"managedFields": "should still be removed",
}
c.JSON(http.StatusInternalServerError, response)
},
expectedBody: `{"error":"something went wrong"}`,
expectedStatus: http.StatusInternalServerError,
},
{
name: "handles 404 status",
handlerFunc: func(c *gin.Context) {
response := map[string]interface{}{
"error": "not found",
"managedFields": "remove",
}
c.JSON(http.StatusNotFound, response)
},
expectedBody: `{"error":"not found"}`,
expectedStatus: http.StatusNotFound,
},
{
name: "handles deeply nested structure with multiple managedFields",
handlerFunc: func(c *gin.Context) {
response := map[string]interface{}{
"apiVersion": "v1",
"items": []interface{}{
map[string]interface{}{
"metadata": map[string]interface{}{
"name": "item1",
"managedFields": "remove1",
"annotations": map[string]interface{}{
"key": "value",
"managedFields": "remove2",
},
},
},
map[string]interface{}{
"metadata": map[string]interface{}{
"name": "item2",
"managedFields": "remove3",
},
},
},
"managedFields": "remove4",
}
c.JSON(http.StatusOK, response)
},
expectedBody: `{"apiVersion":"v1","items":[{"metadata":{"annotations":{"key":"value"},"name":"item1"}},{"metadata":{"name":"item2"}}]}`,
expectedStatus: http.StatusOK,
},
{
name: "preserves other fields and data types",
handlerFunc: func(c *gin.Context) {
response := map[string]interface{}{
"name": "test",
"count": 42,
"active": true,
"value": 3.14,
"nullable": nil,
"managedFields": "remove",
"tags": []string{"tag1", "tag2"},
}
c.JSON(http.StatusOK, response)
},
expectedBody: `{"active":true,"count":42,"name":"test","nullable":null,"tags":["tag1","tag2"],"value":3.14}`,
expectedStatus: http.StatusOK,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create a new gin router with the middleware
router := gin.New()
router.Use(cleanResponseMiddleware())
router.GET("/test", tt.handlerFunc)
// Create a test request
req, err := http.NewRequest(http.MethodGet, "/test", nil)
assert.NoError(t, err)
// Create a response recorder
w := httptest.NewRecorder()
// Serve the request
router.ServeHTTP(w, req)
// Assert status code
assert.Equal(t, tt.expectedStatus, w.Code)
// Assert response body
// For JSON responses, we need to compare the actual JSON structure
// not just the string, as field order might differ
if tt.expectedStatus == http.StatusOK || w.Header().Get("Content-Type") == "application/json; charset=utf-8" {
var expected, actual interface{}
if json.Unmarshal([]byte(tt.expectedBody), &expected) == nil && json.Unmarshal(w.Body.Bytes(), &actual) == nil {
assert.Equal(t, expected, actual)
} else {
// If not JSON, compare as strings
assert.Equal(t, tt.expectedBody, w.Body.String())
}
} else {
assert.Equal(t, tt.expectedBody, w.Body.String())
}
})
}
}
func TestCleanResponseMiddleware_HandlerChain(t *testing.T) {
gin.SetMode(gin.TestMode)
t.Run("middleware allows handler chain to continue", func(t *testing.T) {
router := gin.New()
var middlewareCalled bool
var handlerCalled bool
// Add clean response middleware
router.Use(cleanResponseMiddleware())
// Add another middleware to verify chain continues
router.Use(func(c *gin.Context) {
middlewareCalled = true
c.Next()
})
// Add handler
router.GET("/test", func(c *gin.Context) {
handlerCalled = true
c.JSON(http.StatusOK, map[string]interface{}{
"status": "ok",
"managedFields": "remove",
})
})
req, _ := http.NewRequest(http.MethodGet, "/test", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assert.True(t, middlewareCalled, "middleware should be called")
assert.True(t, handlerCalled, "handler should be called")
assert.Equal(t, http.StatusOK, w.Code)
var response map[string]interface{}
err := json.Unmarshal(w.Body.Bytes(), &response)
assert.NoError(t, err)
assert.Equal(t, "ok", response["status"])
assert.NotContains(t, response, "managedFields")
})
}
func TestCleanResponseMiddleware_CustomResponseWriter(t *testing.T) {
gin.SetMode(gin.TestMode)
t.Run("custom response writer captures response correctly", func(t *testing.T) {
router := gin.New()
router.Use(cleanResponseMiddleware())
router.POST("/test", func(c *gin.Context) {
// Test with multiple writes
response := map[string]interface{}{
"message": "success",
"managedFields": "should be removed",
"data": map[string]interface{}{
"id": 1,
"managedFields": "also removed",
},
}
c.JSON(http.StatusCreated, response)
})
req, _ := http.NewRequest(http.MethodPost, "/test", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusCreated, w.Code)
var response map[string]interface{}
err := json.Unmarshal(w.Body.Bytes(), &response)
assert.NoError(t, err)
assert.Equal(t, "success", response["message"])
assert.NotContains(t, response, "managedFields")
data, ok := response["data"].(map[string]interface{})
assert.True(t, ok)
assert.Equal(t, float64(1), data["id"])
assert.NotContains(t, data, "managedFields")
})
}
func TestCustomResponseWriter_Write(t *testing.T) {
t.Run("custom response writer Write method", func(t *testing.T) {
gin.SetMode(gin.TestMode)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
customWriter := &customResponseWriter{
ResponseWriter: c.Writer,
body: &bytes.Buffer{},
}
data := []byte(`{"test":"data"}`)
n, err := customWriter.Write(data)
assert.NoError(t, err)
assert.Equal(t, len(data), n)
assert.Equal(t, data, customWriter.body.Bytes())
})
t.Run("custom response writer WriteString method", func(t *testing.T) {
gin.SetMode(gin.TestMode)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
customWriter := &customResponseWriter{
ResponseWriter: c.Writer,
body: &bytes.Buffer{},
}
testString := "test string"
n, err := customWriter.WriteString(testString)
assert.NoError(t, err)
assert.Equal(t, len(testString), n)
assert.Equal(t, testString, customWriter.body.String())
})
}
func TestCleanResponseMiddleware_SkipsPodLogsStreaming(t *testing.T) {
gin.SetMode(gin.TestMode)
t.Run("pod logs route bypasses response buffering", func(t *testing.T) {
router := gin.New()
router.Use(cleanResponseMiddleware())
w := httptest.NewRecorder()
var bytesReceivedDuringHandler int
router.GET("/api/v1/namespaces/:namespace/pods/:pod/logs", func(c *gin.Context) {
_, _ = c.Writer.WriteString("log line 1\n")
c.Writer.Flush()
bytesReceivedDuringHandler = w.Body.Len()
_, _ = c.Writer.WriteString("log line 2\n")
})
req, _ := http.NewRequest(http.MethodGet, "/api/v1/namespaces/ns/pods/my-pod/logs?container=numa&follow=true", nil)
router.ServeHTTP(w, req)
assert.Greater(t, bytesReceivedDuringHandler, 0, "logs should reach the client while the handler is still streaming")
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "log line 1\nlog line 2\n", w.Body.String())
})
}
func TestCleanResponseMiddleware_EdgeCases(t *testing.T) {
gin.SetMode(gin.TestMode)
t.Run("handles empty response body", func(t *testing.T) {
router := gin.New()
router.Use(cleanResponseMiddleware())
router.GET("/test", func(c *gin.Context) {
c.Status(http.StatusNoContent)
})
req, _ := http.NewRequest(http.MethodGet, "/test", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusNoContent, w.Code)
assert.Empty(t, w.Body.String())
})
t.Run("handles malformed JSON gracefully", func(t *testing.T) {
router := gin.New()
router.Use(cleanResponseMiddleware())
router.GET("/test", func(c *gin.Context) {
// Write malformed JSON directly
c.Writer.WriteHeader(http.StatusOK)
_, _ = c.Writer.Write([]byte(`{invalid json`))
})
req, _ := http.NewRequest(http.MethodGet, "/test", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
// Should return the original malformed JSON as-is
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, `{invalid json`, w.Body.String())
})
t.Run("handles large nested structures", func(t *testing.T) {
router := gin.New()
router.Use(cleanResponseMiddleware())
router.GET("/test", func(c *gin.Context) {
// Create a large nested structure
items := make([]interface{}, 100)
for i := 0; i < 100; i++ {
items[i] = map[string]interface{}{
"id": i,
"name": "item" + string(rune(i)),
"managedFields": "remove",
"nested": map[string]interface{}{
"managedFields": "remove",
"value": i * 2,
},
}
}
response := map[string]interface{}{
"items": items,
"managedFields": "remove",
}
c.JSON(http.StatusOK, response)
})
req, _ := http.NewRequest(http.MethodGet, "/test", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
var response map[string]interface{}
err := json.Unmarshal(w.Body.Bytes(), &response)
assert.NoError(t, err)
assert.NotContains(t, response, "managedFields")
items, ok := response["items"].([]interface{})
assert.True(t, ok)
assert.Equal(t, 100, len(items))
// Check first and last items
firstItem := items[0].(map[string]interface{})
assert.NotContains(t, firstItem, "managedFields")
assert.Contains(t, firstItem, "nested")
nested := firstItem["nested"].(map[string]interface{})
assert.NotContains(t, nested, "managedFields")
})
}
func TestRemoveFieldsRecursively(t *testing.T) {
tests := []struct {
name string
input interface{}
fields []string
expected interface{}
}{
{
name: "simple map with single field to remove",
input: map[string]interface{}{
"name": "test",
"managedFields": "should be removed",
"value": 42,
},
fields: []string{"managedFields"},
expected: map[string]interface{}{
"name": "test",
"value": 42,
},
},
{
name: "nested map with field to remove",
input: map[string]interface{}{
"name": "test",
"metadata": map[string]interface{}{
"managedFields": "should be removed",
"namespace": "default",
},
"value": 42,
},
fields: []string{"managedFields"},
expected: map[string]interface{}{
"name": "test",
"metadata": map[string]interface{}{
"namespace": "default",
},
"value": 42,
},
},
{
name: "deeply nested map with field to remove at multiple levels",
input: map[string]interface{}{
"name": "test",
"managedFields": "level1",
"nested": map[string]interface{}{
"managedFields": "level2",
"data": "keep this",
"deepNested": map[string]interface{}{
"managedFields": "level3",
"value": 100,
},
},
},
fields: []string{"managedFields"},
expected: map[string]interface{}{
"name": "test",
"nested": map[string]interface{}{
"data": "keep this",
"deepNested": map[string]interface{}{
"value": 100,
},
},
},
},
{
name: "array with maps containing field to remove",
input: []interface{}{
map[string]interface{}{
"name": "item1",
"managedFields": "should be removed",
},
map[string]interface{}{
"name": "item2",
"managedFields": "should be removed",
},
},
fields: []string{"managedFields"},
expected: []interface{}{
map[string]interface{}{
"name": "item1",
},
map[string]interface{}{
"name": "item2",
},
},
},
{
name: "map with array containing nested maps with field to remove",
input: map[string]interface{}{
"name": "test",
"items": []interface{}{
map[string]interface{}{
"name": "item1",
"managedFields": "remove",
},
map[string]interface{}{
"name": "item2",
"managedFields": "remove",
},
},
},
fields: []string{"managedFields"},
expected: map[string]interface{}{
"name": "test",
"items": []interface{}{
map[string]interface{}{
"name": "item1",
},
map[string]interface{}{
"name": "item2",
},
},
},
},
{
name: "multiple fields to remove",
input: map[string]interface{}{
"name": "test",
"managedFields": "remove1",
"annotations": "remove2",
"value": 42,
},
fields: []string{"managedFields", "annotations"},
expected: map[string]interface{}{
"name": "test",
"value": 42,
},
},
{
name: "multiple fields to remove in nested structure",
input: map[string]interface{}{
"name": "test",
"managedFields": "remove1",
"metadata": map[string]interface{}{
"annotations": "remove2",
"managedFields": "remove3",
"namespace": "default",
},
},
fields: []string{"managedFields", "annotations"},
expected: map[string]interface{}{
"name": "test",
"metadata": map[string]interface{}{
"namespace": "default",
},
},
},
{
name: "empty map",
input: map[string]interface{}{},
fields: []string{"managedFields"},
expected: map[string]interface{}{},
},
{
name: "empty array",
input: []interface{}{},
fields: []string{"managedFields"},
expected: []interface{}{},
},
{
name: "map without the field to remove",
input: map[string]interface{}{
"name": "test",
"value": 42,
},
fields: []string{"managedFields"},
expected: map[string]interface{}{
"name": "test",
"value": 42,
},
},
{
name: "complex nested structure with arrays and maps",
input: map[string]interface{}{
"kind": "Pipeline",
"metadata": map[string]interface{}{
"name": "my-pipeline",
"managedFields": "should be removed",
},
"spec": map[string]interface{}{
"vertices": []interface{}{
map[string]interface{}{
"name": "input",
"managedFields": "should be removed",
"source": map[string]interface{}{
"http": map[string]interface{}{},
"managedFields": "should be removed",
},
},
map[string]interface{}{
"name": "output",
"sink": map[string]interface{}{
"log": map[string]interface{}{},
"managedFields": "should be removed",
},
},
},
},
},
fields: []string{"managedFields"},
expected: map[string]interface{}{
"kind": "Pipeline",
"metadata": map[string]interface{}{
"name": "my-pipeline",
},
"spec": map[string]interface{}{
"vertices": []interface{}{
map[string]interface{}{
"name": "input",
"source": map[string]interface{}{
"http": map[string]interface{}{},
},
},
map[string]interface{}{
"name": "output",
"sink": map[string]interface{}{
"log": map[string]interface{}{},
},
},
},
},
},
},
{
name: "primitive types in array should remain unchanged",
input: []interface{}{
"string",
42,
true,
nil,
},
fields: []string{"managedFields"},
expected: []interface{}{
"string",
42,
true,
nil,
},
},
{
name: "primitive value should remain unchanged",
input: "string value",
fields: []string{"managedFields"},
expected: "string value",
},
{
name: "nil value",
input: nil,
fields: []string{"managedFields"},
expected: nil,
},
{
name: "map with mixed primitive and complex values",
input: map[string]interface{}{
"string": "value",
"number": 42,
"boolean": true,
"null": nil,
"managedFields": "remove",
"nested": map[string]interface{}{
"managedFields": "remove",
"data": "keep",
},
},
fields: []string{"managedFields"},
expected: map[string]interface{}{
"string": "value",
"number": 42,
"boolean": true,
"null": nil,
"nested": map[string]interface{}{
"data": "keep",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Make a copy of input to avoid modifying test data
var input interface{}
switch v := tt.input.(type) {
case map[string]interface{}:
input = copyMap(v)
case []interface{}:
input = copySlice(v)
default:
input = tt.input
}
removeFieldsRecursively(input, tt.fields...)
assert.Equal(t, tt.expected, input)
})
}
}
// Helper function to deep copy a map
func copyMap(m map[string]interface{}) map[string]interface{} {
result := make(map[string]interface{})
for k, v := range m {
switch val := v.(type) {
case map[string]interface{}:
result[k] = copyMap(val)
case []interface{}:
result[k] = copySlice(val)
default:
result[k] = val
}
}
return result
}
// Helper function to deep copy a slice
func copySlice(s []interface{}) []interface{} {
result := make([]interface{}, len(s))
for i, v := range s {
switch val := v.(type) {
case map[string]interface{}:
result[i] = copyMap(val)
case []interface{}:
result[i] = copySlice(val)
default:
result[i] = val
}
}
return result
}