-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarshalers_test.go
More file actions
488 lines (405 loc) · 10.4 KB
/
Copy pathmarshalers_test.go
File metadata and controls
488 lines (405 loc) · 10.4 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
package grpckit
import (
"bytes"
"strings"
"testing"
)
func TestFormMarshaler_ContentType(t *testing.T) {
m := &FormMarshaler{}
ct := m.ContentType(nil)
if ct != "application/x-www-form-urlencoded" {
t.Errorf("expected application/x-www-form-urlencoded, got %s", ct)
}
}
func TestInferType(t *testing.T) {
tests := []struct {
input string
expected any
}{
{"true", true},
{"false", false},
{"123", int64(123)},
{"-456", int64(-456)},
{"3.14", float64(3.14)},
{"hello", "hello"},
{"", ""},
{"0", int64(0)},
{"1", int64(1)},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
result := inferType(tt.input)
if result != tt.expected {
t.Errorf("inferType(%q) = %v (%T), want %v (%T)", tt.input, result, result, tt.expected, tt.expected)
}
})
}
}
func TestValuesToJSON(t *testing.T) {
tests := []struct {
name string
values map[string][]string
contains []string
}{
{
name: "simple values",
values: map[string][]string{
"name": {"John"},
"age": {"30"},
},
contains: []string{`"name":"John"`, `"age":30`},
},
{
name: "boolean value",
values: map[string][]string{
"active": {"true"},
},
contains: []string{`"active":true`},
},
{
name: "array values",
values: map[string][]string{
"tags": {"a", "b", "c"},
},
contains: []string{`"tags":["a","b","c"]`},
},
{
name: "nested values",
values: map[string][]string{
"address.street": {"123 Main St"},
},
contains: []string{`"address":{`, `"street":"123 Main St"`},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := valuesToJSON(tt.values)
if err != nil {
t.Fatalf("valuesToJSON failed: %v", err)
}
resultStr := string(result)
for _, expected := range tt.contains {
if !strings.Contains(resultStr, expected) {
t.Errorf("expected result to contain %q, got %s", expected, resultStr)
}
}
})
}
}
func TestMarshalJSON(t *testing.T) {
tests := []struct {
name string
input any
expected string
}{
{"null", nil, "null"},
{"true", true, "true"},
{"false", false, "false"},
{"int", int64(42), "42"},
{"float", float64(3.14), "3.14"},
{"string", "hello", `"hello"`},
{"array", []any{"a", "b"}, `["a","b"]`},
{"object", map[string]any{"key": "value"}, `{"key":"value"}`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := marshalJSON(tt.input)
if err != nil {
t.Fatalf("marshalJSON failed: %v", err)
}
if string(result) != tt.expected {
t.Errorf("expected %s, got %s", tt.expected, string(result))
}
})
}
}
func TestWriteJSON(t *testing.T) {
var buf bytes.Buffer
// Test nested structure
input := map[string]any{
"name": "test",
"count": int64(5),
"items": []any{"a", "b"},
}
err := writeJSON(&buf, input)
if err != nil {
t.Fatalf("writeJSON failed: %v", err)
}
result := buf.String()
if !strings.Contains(result, `"name":"test"`) {
t.Error("expected name field in output")
}
}
func TestWriteJSON_UnsupportedType(t *testing.T) {
var buf bytes.Buffer
// Unsupported type should error
err := writeJSON(&buf, struct{ Name string }{Name: "test"})
if err == nil {
t.Error("expected error for unsupported type")
}
}
func TestXMLMarshaler_ContentType(t *testing.T) {
m := &XMLMarshaler{}
ct := m.ContentType(nil)
if ct != "application/xml" {
t.Errorf("expected application/xml, got %s", ct)
}
}
func TestXMLMarshaler_Marshal(t *testing.T) {
m := &XMLMarshaler{}
type TestStruct struct {
Name string `xml:"name"`
Age int `xml:"age"`
}
input := TestStruct{Name: "John", Age: 30}
result, err := m.Marshal(input)
if err != nil {
t.Fatalf("Marshal failed: %v", err)
}
if !strings.Contains(string(result), "<name>John</name>") {
t.Error("expected name element in XML output")
}
}
func TestXMLMarshaler_MarshalIndent(t *testing.T) {
m := &XMLMarshaler{Indent: " "}
type TestStruct struct {
Name string `xml:"name"`
}
input := TestStruct{Name: "Test"}
result, err := m.Marshal(input)
if err != nil {
t.Fatalf("Marshal failed: %v", err)
}
if !strings.Contains(string(result), "\n") {
t.Error("expected indented XML output")
}
}
func TestXMLMarshaler_Unmarshal(t *testing.T) {
m := &XMLMarshaler{}
type TestStruct struct {
Name string `xml:"name"`
}
input := []byte(`<TestStruct><name>John</name></TestStruct>`)
var output TestStruct
err := m.Unmarshal(input, &output)
if err != nil {
t.Fatalf("Unmarshal failed: %v", err)
}
if output.Name != "John" {
t.Errorf("expected name John, got %s", output.Name)
}
}
func TestBinaryMarshaler_ContentType(t *testing.T) {
m := &BinaryMarshaler{}
ct := m.ContentType(nil)
if ct != "application/octet-stream" {
t.Errorf("expected application/octet-stream, got %s", ct)
}
}
func TestBinaryMarshaler_MarshalBytes(t *testing.T) {
m := &BinaryMarshaler{}
input := []byte("raw binary data")
result, err := m.Marshal(input)
if err != nil {
t.Fatalf("Marshal failed: %v", err)
}
if string(result) != string(input) {
t.Errorf("expected %s, got %s", string(input), string(result))
}
}
func TestBinaryMarshaler_UnsupportedType(t *testing.T) {
m := &BinaryMarshaler{}
_, err := m.Marshal("not bytes or proto")
if err == nil {
t.Error("expected error for unsupported type")
}
}
func TestMultipartMarshaler_ContentType(t *testing.T) {
m := &MultipartMarshaler{}
ct := m.ContentType(nil)
if ct != "multipart/form-data" {
t.Errorf("expected multipart/form-data, got %s", ct)
}
}
func TestMultipartMarshaler_Unmarshal_Error(t *testing.T) {
m := &MultipartMarshaler{}
// Direct Unmarshal should error
err := m.Unmarshal([]byte("test"), nil)
if err == nil {
t.Error("expected error for direct Unmarshal")
}
}
func TestDetectBoundary(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "CRLF line ending",
input: "--boundary123\r\nContent-Type: text/plain\r\n",
expected: "boundary123",
},
{
name: "LF line ending",
input: "--boundary456\nContent-Type: text/plain\n",
expected: "boundary456",
},
{
name: "no boundary",
input: "just some data",
expected: "",
},
{
name: "empty",
input: "",
expected: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := detectBoundary([]byte(tt.input))
if result != tt.expected {
t.Errorf("detectBoundary() = %q, want %q", result, tt.expected)
}
})
}
}
func TestTextMarshaler_ContentType(t *testing.T) {
m := &TextMarshaler{}
ct := m.ContentType(nil)
if ct != "text/plain" {
t.Errorf("expected text/plain, got %s", ct)
}
}
func TestTextMarshaler_MarshalString(t *testing.T) {
m := &TextMarshaler{}
result, err := m.Marshal("hello world")
if err != nil {
t.Fatalf("Marshal failed: %v", err)
}
if string(result) != "hello world" {
t.Errorf("expected 'hello world', got %s", string(result))
}
}
func TestTextMarshaler_UnsupportedType(t *testing.T) {
m := &TextMarshaler{}
_, err := m.Marshal(123)
if err == nil {
t.Error("expected error for non-string, non-proto type")
}
}
func TestWithFormURLEncodedSupport(t *testing.T) {
cfg := newServerConfig()
opt := WithFormURLEncodedSupport()
opt(cfg)
if _, ok := cfg.marshalers["application/x-www-form-urlencoded"]; !ok {
t.Error("expected form marshaler to be registered")
}
}
func TestWithXMLSupport(t *testing.T) {
cfg := newServerConfig()
opt := WithXMLSupport()
opt(cfg)
if _, ok := cfg.marshalers["application/xml"]; !ok {
t.Error("expected XML marshaler to be registered")
}
}
func TestWithXMLSupportIndented(t *testing.T) {
cfg := newServerConfig()
opt := WithXMLSupportIndented(" ")
opt(cfg)
marshaler, ok := cfg.marshalers["application/xml"]
if !ok {
t.Fatal("expected XML marshaler to be registered")
}
xmlMarshaler, ok := marshaler.(*XMLMarshaler)
if !ok {
t.Fatal("expected XMLMarshaler type")
}
if xmlMarshaler.Indent != " " {
t.Errorf("expected indent ' ', got %q", xmlMarshaler.Indent)
}
}
func TestWithBinarySupport(t *testing.T) {
cfg := newServerConfig()
opt := WithBinarySupport()
opt(cfg)
if _, ok := cfg.marshalers["application/octet-stream"]; !ok {
t.Error("expected binary marshaler to be registered")
}
}
func TestWithMultipartSupport(t *testing.T) {
cfg := newServerConfig()
opt := WithMultipartSupport()
opt(cfg)
if _, ok := cfg.marshalers["multipart/form-data"]; !ok {
t.Error("expected multipart marshaler to be registered")
}
}
func TestWithMultipartSupportWithMaxMemory(t *testing.T) {
cfg := newServerConfig()
opt := WithMultipartSupportWithMaxMemory(64 << 20) // 64MB
opt(cfg)
marshaler, ok := cfg.marshalers["multipart/form-data"]
if !ok {
t.Fatal("expected multipart marshaler to be registered")
}
multipartMarshaler, ok := marshaler.(*MultipartMarshaler)
if !ok {
t.Fatal("expected MultipartMarshaler type")
}
if multipartMarshaler.MaxMemory != 64<<20 {
t.Errorf("expected MaxMemory 64MB, got %d", multipartMarshaler.MaxMemory)
}
}
func TestWithTextSupport(t *testing.T) {
cfg := newServerConfig()
opt := WithTextSupport()
opt(cfg)
if _, ok := cfg.marshalers["text/plain"]; !ok {
t.Error("expected text marshaler to be registered")
}
}
func TestWithTextSupportFields(t *testing.T) {
cfg := newServerConfig()
opt := WithTextSupportFields("content", "response")
opt(cfg)
marshaler, ok := cfg.marshalers["text/plain"]
if !ok {
t.Fatal("expected text marshaler to be registered")
}
textMarshaler, ok := marshaler.(*TextMarshaler)
if !ok {
t.Fatal("expected TextMarshaler type")
}
if textMarshaler.InputField != "content" {
t.Errorf("expected InputField 'content', got %q", textMarshaler.InputField)
}
if textMarshaler.OutputField != "response" {
t.Errorf("expected OutputField 'response', got %q", textMarshaler.OutputField)
}
}
func TestBuildMarshalerOptions(t *testing.T) {
cfg := newServerConfig()
// Add JSON options
cfg.jsonOptions = &JSONOptions{
UseProtoNames: true,
EmitUnpopulated: true,
}
// Add custom marshaler
cfg.marshalers["application/xml"] = &XMLMarshaler{}
opts := buildMarshalerOptions(cfg)
// Should have options for JSON and XML
if len(opts) < 2 {
t.Errorf("expected at least 2 options, got %d", len(opts))
}
}
func TestBuildMarshalerOptions_NoOptions(t *testing.T) {
cfg := newServerConfig()
opts := buildMarshalerOptions(cfg)
// With no custom options, should have no options
if len(opts) != 0 {
t.Errorf("expected 0 options for empty config, got %d", len(opts))
}
}