| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440 |
- package apiform
- import (
- "bytes"
- "mime/multipart"
- "strings"
- "testing"
- "time"
- )
- func P[T any](v T) *T { return &v }
- type Primitives struct {
- A bool `form:"a"`
- B int `form:"b"`
- C uint `form:"c"`
- D float64 `form:"d"`
- E float32 `form:"e"`
- F []int `form:"f"`
- }
- type PrimitivePointers struct {
- A *bool `form:"a"`
- B *int `form:"b"`
- C *uint `form:"c"`
- D *float64 `form:"d"`
- E *float32 `form:"e"`
- F *[]int `form:"f"`
- }
- type Slices struct {
- Slice []Primitives `form:"slices"`
- }
- type DateTime struct {
- Date time.Time `form:"date" format:"date"`
- DateTime time.Time `form:"date-time" format:"date-time"`
- }
- type AdditionalProperties struct {
- A bool `form:"a"`
- Extras map[string]interface{} `form:"-,extras"`
- }
- type TypedAdditionalProperties struct {
- A bool `form:"a"`
- Extras map[string]int `form:"-,extras"`
- }
- type EmbeddedStructs struct {
- AdditionalProperties
- A *int `form:"number2"`
- Extras map[string]interface{} `form:"-,extras"`
- }
- type Recursive struct {
- Name string `form:"name"`
- Child *Recursive `form:"child"`
- }
- type UnknownStruct struct {
- Unknown interface{} `form:"unknown"`
- }
- type UnionStruct struct {
- Union Union `form:"union" format:"date"`
- }
- type Union interface {
- union()
- }
- type UnionInteger int64
- func (UnionInteger) union() {}
- type UnionStructA struct {
- Type string `form:"type"`
- A string `form:"a"`
- B string `form:"b"`
- }
- func (UnionStructA) union() {}
- type UnionStructB struct {
- Type string `form:"type"`
- A string `form:"a"`
- }
- func (UnionStructB) union() {}
- type UnionTime time.Time
- func (UnionTime) union() {}
- type ReaderStruct struct {
- }
- var tests = map[string]struct {
- buf string
- val interface{}
- }{
- "map_string": {
- `--xxx
- Content-Disposition: form-data; name="foo"
- bar
- --xxx--
- `,
- map[string]string{"foo": "bar"},
- },
- "map_interface": {
- `--xxx
- Content-Disposition: form-data; name="a"
- 1
- --xxx
- Content-Disposition: form-data; name="b"
- str
- --xxx
- Content-Disposition: form-data; name="c"
- false
- --xxx--
- `,
- map[string]interface{}{"a": float64(1), "b": "str", "c": false},
- },
- "primitive_struct": {
- `--xxx
- Content-Disposition: form-data; name="a"
- false
- --xxx
- Content-Disposition: form-data; name="b"
- 237628372683
- --xxx
- Content-Disposition: form-data; name="c"
- 654
- --xxx
- Content-Disposition: form-data; name="d"
- 9999.43
- --xxx
- Content-Disposition: form-data; name="e"
- 43.76
- --xxx
- Content-Disposition: form-data; name="f.0"
- 1
- --xxx
- Content-Disposition: form-data; name="f.1"
- 2
- --xxx
- Content-Disposition: form-data; name="f.2"
- 3
- --xxx
- Content-Disposition: form-data; name="f.3"
- 4
- --xxx--
- `,
- Primitives{A: false, B: 237628372683, C: uint(654), D: 9999.43, E: 43.76, F: []int{1, 2, 3, 4}},
- },
- "slices": {
- `--xxx
- Content-Disposition: form-data; name="slices.0.a"
- false
- --xxx
- Content-Disposition: form-data; name="slices.0.b"
- 237628372683
- --xxx
- Content-Disposition: form-data; name="slices.0.c"
- 654
- --xxx
- Content-Disposition: form-data; name="slices.0.d"
- 9999.43
- --xxx
- Content-Disposition: form-data; name="slices.0.e"
- 43.76
- --xxx
- Content-Disposition: form-data; name="slices.0.f.0"
- 1
- --xxx
- Content-Disposition: form-data; name="slices.0.f.1"
- 2
- --xxx
- Content-Disposition: form-data; name="slices.0.f.2"
- 3
- --xxx
- Content-Disposition: form-data; name="slices.0.f.3"
- 4
- --xxx--
- `,
- Slices{
- Slice: []Primitives{{A: false, B: 237628372683, C: uint(654), D: 9999.43, E: 43.76, F: []int{1, 2, 3, 4}}},
- },
- },
- "primitive_pointer_struct": {
- `--xxx
- Content-Disposition: form-data; name="a"
- false
- --xxx
- Content-Disposition: form-data; name="b"
- 237628372683
- --xxx
- Content-Disposition: form-data; name="c"
- 654
- --xxx
- Content-Disposition: form-data; name="d"
- 9999.43
- --xxx
- Content-Disposition: form-data; name="e"
- 43.76
- --xxx
- Content-Disposition: form-data; name="f.0"
- 1
- --xxx
- Content-Disposition: form-data; name="f.1"
- 2
- --xxx
- Content-Disposition: form-data; name="f.2"
- 3
- --xxx
- Content-Disposition: form-data; name="f.3"
- 4
- --xxx
- Content-Disposition: form-data; name="f.4"
- 5
- --xxx--
- `,
- PrimitivePointers{
- A: P(false),
- B: P(237628372683),
- C: P(uint(654)),
- D: P(9999.43),
- E: P(float32(43.76)),
- F: &[]int{1, 2, 3, 4, 5},
- },
- },
- "datetime_struct": {
- `--xxx
- Content-Disposition: form-data; name="date"
- 2006-01-02
- --xxx
- Content-Disposition: form-data; name="date-time"
- 2006-01-02T15:04:05Z
- --xxx--
- `,
- DateTime{
- Date: time.Date(2006, time.January, 2, 0, 0, 0, 0, time.UTC),
- DateTime: time.Date(2006, time.January, 2, 15, 4, 5, 0, time.UTC),
- },
- },
- "additional_properties": {
- `--xxx
- Content-Disposition: form-data; name="a"
- true
- --xxx
- Content-Disposition: form-data; name="bar"
- value
- --xxx
- Content-Disposition: form-data; name="foo"
- true
- --xxx--
- `,
- AdditionalProperties{
- A: true,
- Extras: map[string]interface{}{
- "bar": "value",
- "foo": true,
- },
- },
- },
- "recursive_struct": {
- `--xxx
- Content-Disposition: form-data; name="child.name"
- Alex
- --xxx
- Content-Disposition: form-data; name="name"
- Robert
- --xxx--
- `,
- Recursive{Name: "Robert", Child: &Recursive{Name: "Alex"}},
- },
- "unknown_struct_number": {
- `--xxx
- Content-Disposition: form-data; name="unknown"
- 12
- --xxx--
- `,
- UnknownStruct{
- Unknown: 12.,
- },
- },
- "unknown_struct_map": {
- `--xxx
- Content-Disposition: form-data; name="unknown.foo"
- bar
- --xxx--
- `,
- UnknownStruct{
- Unknown: map[string]interface{}{
- "foo": "bar",
- },
- },
- },
- "union_integer": {
- `--xxx
- Content-Disposition: form-data; name="union"
- 12
- --xxx--
- `,
- UnionStruct{
- Union: UnionInteger(12),
- },
- },
- "union_struct_discriminated_a": {
- `--xxx
- Content-Disposition: form-data; name="union.a"
- foo
- --xxx
- Content-Disposition: form-data; name="union.b"
- bar
- --xxx
- Content-Disposition: form-data; name="union.type"
- typeA
- --xxx--
- `,
- UnionStruct{
- Union: UnionStructA{
- Type: "typeA",
- A: "foo",
- B: "bar",
- },
- },
- },
- "union_struct_discriminated_b": {
- `--xxx
- Content-Disposition: form-data; name="union.a"
- foo
- --xxx
- Content-Disposition: form-data; name="union.type"
- typeB
- --xxx--
- `,
- UnionStruct{
- Union: UnionStructB{
- Type: "typeB",
- A: "foo",
- },
- },
- },
- "union_struct_time": {
- `--xxx
- Content-Disposition: form-data; name="union"
- 2010-05-23
- --xxx--
- `,
- UnionStruct{
- Union: UnionTime(time.Date(2010, 05, 23, 0, 0, 0, 0, time.UTC)),
- },
- },
- }
- func TestEncode(t *testing.T) {
- for name, test := range tests {
- t.Run(name, func(t *testing.T) {
- buf := bytes.NewBuffer(nil)
- writer := multipart.NewWriter(buf)
- writer.SetBoundary("xxx")
- err := Marshal(test.val, writer)
- if err != nil {
- t.Errorf("serialization of %v failed with error %v", test.val, err)
- }
- err = writer.Close()
- if err != nil {
- t.Errorf("serialization of %v failed with error %v", test.val, err)
- }
- raw := buf.Bytes()
- if string(raw) != strings.ReplaceAll(test.buf, "\n", "\r\n") {
- t.Errorf("expected %+#v to serialize to '%s' but got '%s'", test.val, test.buf, string(raw))
- }
- })
- }
- }
|