Debian Codegraph @

internal/apiform/form_test.go

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 package apiform
2
3 import (
4 "bytes"
5 "github.com/anthropics/anthropic-sdk-go/packages/param"
6 "io"
7 "mime/multipart"
8 "strings"
9 "testing"
10 "time"
11 )
12
13 func P[T any](v T) *T { return &v }
14
15 type Primitives struct {
16 A bool `form:"a"`
17 B int `form:"b"`
18 C uint `form:"c"`
19 D float64 `form:"d"`
20 E float32 `form:"e"`
21 F []int `form:"f"`
22 }
23
24 // These aliases are necessary to bypass the cache.
25 // This only relevant during testing.
26 type int_ int
27 type PrimitivesBrackets struct {
28 F []int_ `form:"f"`
29 }
30
31 type PrimitivePointers struct {
32 A *bool `form:"a"`
33 B *int `form:"b"`
34 C *uint `form:"c"`
35 D *float64 `form:"d"`
36 E *float32 `form:"e"`
37 F *[]int `form:"f"`
38 }
39
40 type Slices struct {
41 Slice []Primitives `form:"slices"`
42 }
43
44 type DateTime struct {
45 Date time.Time `form:"date" format:"date"`
46 DateTime time.Time `form:"date-time" format:"date-time"`
47 }
48
49 type AdditionalProperties struct {
50 A bool `form:"a"`
51 Extras map[string]any `form:"-" api:"extrafields"`
52 }
53
54 type TypedAdditionalProperties struct {
55 A bool `form:"a"`
56 Extras map[string]int `form:"-" api:"extrafields"`
57 }
58
59 type EmbeddedStructs struct {
60 AdditionalProperties
61 A *int `form:"number2"`
62 Extras map[string]any `form:"-" api:"extrafields"`
63 }
64
65 type Recursive struct {
66 Name string `form:"name"`
67 Child *Recursive `form:"child"`
68 }
69
70 type UnknownStruct struct {
71 Unknown any `form:"unknown"`
72 }
73
74 type UnionStruct struct {
75 Union Union `form:"union" format:"date"`
76 }
77
78 type Union interface {
79 union()
80 }
81
82 type UnionInteger int64
83
84 func (UnionInteger) union() {}
85
86 type UnionStructA struct {
87 Type string `form:"type"`
88 A string `form:"a"`
89 B string `form:"b"`
90 }
91
92 func (UnionStructA) union() {}
93
94 type UnionStructB struct {
95 Type string `form:"type"`
96 A string `form:"a"`
97 }
98
99 func (UnionStructB) union() {}
100
101 type UnionTime time.Time
102
103 func (UnionTime) union() {}
104
105 type ReaderStruct struct {
106 File io.Reader `form:"file"`
107 }
108
109 type NamedEnum string
110
111 const NamedEnumFoo NamedEnum = "foo"
112
113 type StructUnionWrapper struct {
114 Union StructUnion `form:"union"`
115 }
116
117 type StructUnion struct {
118 OfInt param.Opt[int64] `form:",omitzero,inline"`
119 OfString param.Opt[string] `form:",omitzero,inline"`
120 OfEnum param.Opt[NamedEnum] `form:",omitzero,inline"`
121 OfA UnionStructA `form:",omitzero,inline"`
122 OfB UnionStructB `form:",omitzero,inline"`
123 param.APIUnion
124 }
125
126 type MultipartMarshalerParent struct {
127 Middle MultipartMarshalerMiddleNext `form:"middle"`
128 }
129
130 type MultipartMarshalerMiddleNext struct {
131 MiddleNext MultipartMarshalerMiddle `form:"middleNext"`
132 }
133
134 type MultipartMarshalerMiddle struct {
135 Child int `form:"child"`
136 }
137
138 var tests = map[string]struct {
139 buf string
140 val any
141 }{
142 "file": {
143 buf: `--xxx
144 Content-Disposition: form-data; name="file"; filename="anonymous_file"
145 Content-Type: application/octet-stream
146
147 some file contents...
148 --xxx--
149 `,
150 val: ReaderStruct{
151 File: io.Reader(bytes.NewBuffer([]byte("some file contents..."))),
152 },
153 },
154 "map_string": {
155 `--xxx
156 Content-Disposition: form-data; name="foo"
157
158 bar
159 --xxx--
160 `,
161 map[string]string{"foo": "bar"},
162 },
163
164 "map_interface": {
165 `--xxx
166 Content-Disposition: form-data; name="a"
167
168 1
169 --xxx
170 Content-Disposition: form-data; name="b"
171
172 str
173 --xxx
174 Content-Disposition: form-data; name="c"
175
176 false
177 --xxx--
178 `,
179 map[string]any{"a": float64(1), "b": "str", "c": false},
180 },
181
182 "primitive_struct": {
183 `--xxx
184 Content-Disposition: form-data; name="a"
185
186 false
187 --xxx
188 Content-Disposition: form-data; name="b"
189
190 237628372683
191 --xxx
192 Content-Disposition: form-data; name="c"
193
194 654
195 --xxx
196 Content-Disposition: form-data; name="d"
197
198 9999.43
199 --xxx
200 Content-Disposition: form-data; name="e"
201
202 43.76
203 --xxx
204 Content-Disposition: form-data; name="f.0"
205
206 1
207 --xxx
208 Content-Disposition: form-data; name="f.1"
209
210 2
211 --xxx
212 Content-Disposition: form-data; name="f.2"
213
214 3
215 --xxx
216 Content-Disposition: form-data; name="f.3"
217
218 4
219 --xxx--
220 `,
221 Primitives{A: false, B: 237628372683, C: uint(654), D: 9999.43, E: 43.76, F: []int{1, 2, 3, 4}},
222 },
223 "primitive_struct,brackets": {
224 `--xxx
225 Content-Disposition: form-data; name="f[]"
226
227 1
228 --xxx
229 Content-Disposition: form-data; name="f[]"
230
231 2
232 --xxx
233 Content-Disposition: form-data; name="f[]"
234
235 3
236 --xxx
237 Content-Disposition: form-data; name="f[]"
238
239 4
240 --xxx--
241 `,
242 PrimitivesBrackets{F: []int_{1, 2, 3, 4}},
243 },
244
245 "slices": {
246 `--xxx
247 Content-Disposition: form-data; name="slices.0.a"
248
249 false
250 --xxx
251 Content-Disposition: form-data; name="slices.0.b"
252
253 237628372683
254 --xxx
255 Content-Disposition: form-data; name="slices.0.c"
256
257 654
258 --xxx
259 Content-Disposition: form-data; name="slices.0.d"
260
261 9999.43
262 --xxx
263 Content-Disposition: form-data; name="slices.0.e"
264
265 43.76
266 --xxx
267 Content-Disposition: form-data; name="slices.0.f.0"
268
269 1
270 --xxx
271 Content-Disposition: form-data; name="slices.0.f.1"
272
273 2
274 --xxx
275 Content-Disposition: form-data; name="slices.0.f.2"
276
277 3
278 --xxx
279 Content-Disposition: form-data; name="slices.0.f.3"
280
281 4
282 --xxx--
283 `,
284 Slices{
285 Slice: []Primitives{{A: false, B: 237628372683, C: uint(654), D: 9999.43, E: 43.76, F: []int{1, 2, 3, 4}}},
286 },
287 },
288 "primitive_pointer_struct": {
289 `--xxx
290 Content-Disposition: form-data; name="a"
291
292 false
293 --xxx
294 Content-Disposition: form-data; name="b"
295
296 237628372683
297 --xxx
298 Content-Disposition: form-data; name="c"
299
300 654
301 --xxx
302 Content-Disposition: form-data; name="d"
303
304 9999.43
305 --xxx
306 Content-Disposition: form-data; name="e"
307
308 43.76
309 --xxx
310 Content-Disposition: form-data; name="f.0"
311
312 1
313 --xxx
314 Content-Disposition: form-data; name="f.1"
315
316 2
317 --xxx
318 Content-Disposition: form-data; name="f.2"
319
320 3
321 --xxx
322 Content-Disposition: form-data; name="f.3"
323
324 4
325 --xxx
326 Content-Disposition: form-data; name="f.4"
327
328 5
329 --xxx--
330 `,
331 PrimitivePointers{
332 A: P(false),
333 B: P(237628372683),
334 C: P(uint(654)),
335 D: P(9999.43),
336 E: P(float32(43.76)),
337 F: &[]int{1, 2, 3, 4, 5},
338 },
339 },
340
341 "datetime_struct": {
342 `--xxx
343 Content-Disposition: form-data; name="date"
344
345 2006-01-02
346 --xxx
347 Content-Disposition: form-data; name="date-time"
348
349 2006-01-02T15:04:05Z
350 --xxx--
351 `,
352 DateTime{
353 Date: time.Date(2006, time.January, 2, 0, 0, 0, 0, time.UTC),
354 DateTime: time.Date(2006, time.January, 2, 15, 4, 5, 0, time.UTC),
355 },
356 },
357
358 "additional_properties": {
359 `--xxx
360 Content-Disposition: form-data; name="a"
361
362 true
363 --xxx
364 Content-Disposition: form-data; name="bar"
365
366 value
367 --xxx
368 Content-Disposition: form-data; name="foo"
369
370 true
371 --xxx--
372 `,
373 AdditionalProperties{
374 A: true,
375 Extras: map[string]any{
376 "bar": "value",
377 "foo": true,
378 },
379 },
380 },
381 "recursive_struct,brackets": {
382 `--xxx
383 Content-Disposition: form-data; name="child[name]"
384
385 Alex
386 --xxx
387 Content-Disposition: form-data; name="name"
388
389 Robert
390 --xxx--
391 `,
392 Recursive{Name: "Robert", Child: &Recursive{Name: "Alex"}},
393 },
394
395 "recursive_struct": {
396 `--xxx
397 Content-Disposition: form-data; name="child.name"
398
399 Alex
400 --xxx
401 Content-Disposition: form-data; name="name"
402
403 Robert
404 --xxx--
405 `,
406 Recursive{Name: "Robert", Child: &Recursive{Name: "Alex"}},
407 },
408
409 "unknown_struct_number": {
410 `--xxx
411 Content-Disposition: form-data; name="unknown"
412
413 12
414 --xxx--
415 `,
416 UnknownStruct{
417 Unknown: 12.,
418 },
419 },
420
421 "unknown_struct_map": {
422 `--xxx
423 Content-Disposition: form-data; name="unknown.foo"
424
425 bar
426 --xxx--
427 `,
428 UnknownStruct{
429 Unknown: map[string]any{
430 "foo": "bar",
431 },
432 },
433 },
434
435 "struct_union_integer": {
436 `--xxx
437 Content-Disposition: form-data; name="union"
438
439 12
440 --xxx--
441 `,
442 StructUnionWrapper{
443 Union: StructUnion{OfInt: param.NewOpt[int64](12)},
444 },
445 },
446
447 "union_integer": {
448 `--xxx
449 Content-Disposition: form-data; name="union"
450
451 12
452 --xxx--
453 `,
454 UnionStruct{
455 Union: UnionInteger(12),
456 },
457 },
458
459 "struct_union_struct_discriminated_a": {
460 `--xxx
461 Content-Disposition: form-data; name="union.a"
462
463 foo
464 --xxx
465 Content-Disposition: form-data; name="union.b"
466
467 bar
468 --xxx
469 Content-Disposition: form-data; name="union.type"
470
471 typeA
472 --xxx--
473 `,
474 StructUnionWrapper{
475 Union: StructUnion{OfA: UnionStructA{
476 Type: "typeA",
477 A: "foo",
478 B: "bar",
479 }},
480 },
481 },
482
483 "union_struct_discriminated_a": {
484 `--xxx
485 Content-Disposition: form-data; name="union.a"
486
487 foo
488 --xxx
489 Content-Disposition: form-data; name="union.b"
490
491 bar
492 --xxx
493 Content-Disposition: form-data; name="union.type"
494
495 typeA
496 --xxx--
497 `,
498
499 UnionStruct{
500 Union: UnionStructA{
501 Type: "typeA",
502 A: "foo",
503 B: "bar",
504 },
505 },
506 },
507
508 "struct_union_struct_discriminated_b": {
509 `--xxx
510 Content-Disposition: form-data; name="union.a"
511
512 foo
513 --xxx
514 Content-Disposition: form-data; name="union.type"
515
516 typeB
517 --xxx--
518 `,
519 StructUnionWrapper{
520 Union: StructUnion{OfB: UnionStructB{
521 Type: "typeB",
522 A: "foo",
523 }},
524 },
525 },
526
527 "union_struct_discriminated_b": {
528 `--xxx
529 Content-Disposition: form-data; name="union.a"
530
531 foo
532 --xxx
533 Content-Disposition: form-data; name="union.type"
534
535 typeB
536 --xxx--
537 `,
538 UnionStruct{
539 Union: UnionStructB{
540 Type: "typeB",
541 A: "foo",
542 },
543 },
544 },
545
546 "union_struct_time": {
547 `--xxx
548 Content-Disposition: form-data; name="union"
549
550 2010-05-23
551 --xxx--
552 `,
553 UnionStruct{
554 Union: UnionTime(time.Date(2010, 05, 23, 0, 0, 0, 0, time.UTC)),
555 },
556 },
557 "deeply-nested-struct,brackets": {
558 `--xxx
559 Content-Disposition: form-data; name="middle[middleNext][child]"
560
561 10
562 --xxx--
563 `,
564 MultipartMarshalerParent{
565 Middle: MultipartMarshalerMiddleNext{
566 MiddleNext: MultipartMarshalerMiddle{
567 Child: 10,
568 },
569 },
570 },
571 },
572 "deeply-nested-map,brackets": {
573 `--xxx
574 Content-Disposition: form-data; name="middle[middleNext][child]"
575
576 10
577 --xxx--
578 `,
579 map[string]any{"middle": map[string]any{"middleNext": map[string]any{"child": 10}}},
580 },
581 }
582
583 func TestEncode(t *testing.T) {
584 for name, test := range tests {
585 t.Run(name, func(t *testing.T) {
586 buf := bytes.NewBuffer(nil)
587 writer := multipart.NewWriter(buf)
588 err := writer.SetBoundary("xxx")
589 if err != nil {
590 t.Errorf("setting boundary for %v failed with error %v", test.val, err)
591 }
592
593 arrayFmt := "indices:dots"
594 if tags := strings.Split(name, ","); len(tags) > 1 {
595 arrayFmt = tags[1]
596 }
597
598 err = MarshalWithSettings(test.val, writer, arrayFmt)
599 if err != nil {
600 t.Errorf("serialization of %v failed with error %v", test.val, err)
601 }
602 err = writer.Close()
603 if err != nil {
604 t.Errorf("serialization of %v failed with error %v", test.val, err)
605 }
606 raw := buf.Bytes()
607 if string(raw) != strings.ReplaceAll(test.buf, "\n", "\r\n") {
608 t.Errorf("expected %+#v to serialize to '%s' but got '%s' (with format %s)", test.val, test.buf, string(raw), arrayFmt)
609 }
610 })
611 }
612 }