form_test.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. package apiform
  2. import (
  3. "bytes"
  4. "mime/multipart"
  5. "strings"
  6. "testing"
  7. "time"
  8. )
  9. func P[T any](v T) *T { return &v }
  10. type Primitives struct {
  11. A bool `form:"a"`
  12. B int `form:"b"`
  13. C uint `form:"c"`
  14. D float64 `form:"d"`
  15. E float32 `form:"e"`
  16. F []int `form:"f"`
  17. }
  18. type PrimitivePointers struct {
  19. A *bool `form:"a"`
  20. B *int `form:"b"`
  21. C *uint `form:"c"`
  22. D *float64 `form:"d"`
  23. E *float32 `form:"e"`
  24. F *[]int `form:"f"`
  25. }
  26. type Slices struct {
  27. Slice []Primitives `form:"slices"`
  28. }
  29. type DateTime struct {
  30. Date time.Time `form:"date" format:"date"`
  31. DateTime time.Time `form:"date-time" format:"date-time"`
  32. }
  33. type AdditionalProperties struct {
  34. A bool `form:"a"`
  35. Extras map[string]interface{} `form:"-,extras"`
  36. }
  37. type TypedAdditionalProperties struct {
  38. A bool `form:"a"`
  39. Extras map[string]int `form:"-,extras"`
  40. }
  41. type EmbeddedStructs struct {
  42. AdditionalProperties
  43. A *int `form:"number2"`
  44. Extras map[string]interface{} `form:"-,extras"`
  45. }
  46. type Recursive struct {
  47. Name string `form:"name"`
  48. Child *Recursive `form:"child"`
  49. }
  50. type UnknownStruct struct {
  51. Unknown interface{} `form:"unknown"`
  52. }
  53. type UnionStruct struct {
  54. Union Union `form:"union" format:"date"`
  55. }
  56. type Union interface {
  57. union()
  58. }
  59. type UnionInteger int64
  60. func (UnionInteger) union() {}
  61. type UnionStructA struct {
  62. Type string `form:"type"`
  63. A string `form:"a"`
  64. B string `form:"b"`
  65. }
  66. func (UnionStructA) union() {}
  67. type UnionStructB struct {
  68. Type string `form:"type"`
  69. A string `form:"a"`
  70. }
  71. func (UnionStructB) union() {}
  72. type UnionTime time.Time
  73. func (UnionTime) union() {}
  74. type ReaderStruct struct {
  75. }
  76. var tests = map[string]struct {
  77. buf string
  78. val interface{}
  79. }{
  80. "map_string": {
  81. `--xxx
  82. Content-Disposition: form-data; name="foo"
  83. bar
  84. --xxx--
  85. `,
  86. map[string]string{"foo": "bar"},
  87. },
  88. "map_interface": {
  89. `--xxx
  90. Content-Disposition: form-data; name="a"
  91. 1
  92. --xxx
  93. Content-Disposition: form-data; name="b"
  94. str
  95. --xxx
  96. Content-Disposition: form-data; name="c"
  97. false
  98. --xxx--
  99. `,
  100. map[string]interface{}{"a": float64(1), "b": "str", "c": false},
  101. },
  102. "primitive_struct": {
  103. `--xxx
  104. Content-Disposition: form-data; name="a"
  105. false
  106. --xxx
  107. Content-Disposition: form-data; name="b"
  108. 237628372683
  109. --xxx
  110. Content-Disposition: form-data; name="c"
  111. 654
  112. --xxx
  113. Content-Disposition: form-data; name="d"
  114. 9999.43
  115. --xxx
  116. Content-Disposition: form-data; name="e"
  117. 43.76
  118. --xxx
  119. Content-Disposition: form-data; name="f.0"
  120. 1
  121. --xxx
  122. Content-Disposition: form-data; name="f.1"
  123. 2
  124. --xxx
  125. Content-Disposition: form-data; name="f.2"
  126. 3
  127. --xxx
  128. Content-Disposition: form-data; name="f.3"
  129. 4
  130. --xxx--
  131. `,
  132. Primitives{A: false, B: 237628372683, C: uint(654), D: 9999.43, E: 43.76, F: []int{1, 2, 3, 4}},
  133. },
  134. "slices": {
  135. `--xxx
  136. Content-Disposition: form-data; name="slices.0.a"
  137. false
  138. --xxx
  139. Content-Disposition: form-data; name="slices.0.b"
  140. 237628372683
  141. --xxx
  142. Content-Disposition: form-data; name="slices.0.c"
  143. 654
  144. --xxx
  145. Content-Disposition: form-data; name="slices.0.d"
  146. 9999.43
  147. --xxx
  148. Content-Disposition: form-data; name="slices.0.e"
  149. 43.76
  150. --xxx
  151. Content-Disposition: form-data; name="slices.0.f.0"
  152. 1
  153. --xxx
  154. Content-Disposition: form-data; name="slices.0.f.1"
  155. 2
  156. --xxx
  157. Content-Disposition: form-data; name="slices.0.f.2"
  158. 3
  159. --xxx
  160. Content-Disposition: form-data; name="slices.0.f.3"
  161. 4
  162. --xxx--
  163. `,
  164. Slices{
  165. Slice: []Primitives{{A: false, B: 237628372683, C: uint(654), D: 9999.43, E: 43.76, F: []int{1, 2, 3, 4}}},
  166. },
  167. },
  168. "primitive_pointer_struct": {
  169. `--xxx
  170. Content-Disposition: form-data; name="a"
  171. false
  172. --xxx
  173. Content-Disposition: form-data; name="b"
  174. 237628372683
  175. --xxx
  176. Content-Disposition: form-data; name="c"
  177. 654
  178. --xxx
  179. Content-Disposition: form-data; name="d"
  180. 9999.43
  181. --xxx
  182. Content-Disposition: form-data; name="e"
  183. 43.76
  184. --xxx
  185. Content-Disposition: form-data; name="f.0"
  186. 1
  187. --xxx
  188. Content-Disposition: form-data; name="f.1"
  189. 2
  190. --xxx
  191. Content-Disposition: form-data; name="f.2"
  192. 3
  193. --xxx
  194. Content-Disposition: form-data; name="f.3"
  195. 4
  196. --xxx
  197. Content-Disposition: form-data; name="f.4"
  198. 5
  199. --xxx--
  200. `,
  201. PrimitivePointers{
  202. A: P(false),
  203. B: P(237628372683),
  204. C: P(uint(654)),
  205. D: P(9999.43),
  206. E: P(float32(43.76)),
  207. F: &[]int{1, 2, 3, 4, 5},
  208. },
  209. },
  210. "datetime_struct": {
  211. `--xxx
  212. Content-Disposition: form-data; name="date"
  213. 2006-01-02
  214. --xxx
  215. Content-Disposition: form-data; name="date-time"
  216. 2006-01-02T15:04:05Z
  217. --xxx--
  218. `,
  219. DateTime{
  220. Date: time.Date(2006, time.January, 2, 0, 0, 0, 0, time.UTC),
  221. DateTime: time.Date(2006, time.January, 2, 15, 4, 5, 0, time.UTC),
  222. },
  223. },
  224. "additional_properties": {
  225. `--xxx
  226. Content-Disposition: form-data; name="a"
  227. true
  228. --xxx
  229. Content-Disposition: form-data; name="bar"
  230. value
  231. --xxx
  232. Content-Disposition: form-data; name="foo"
  233. true
  234. --xxx--
  235. `,
  236. AdditionalProperties{
  237. A: true,
  238. Extras: map[string]interface{}{
  239. "bar": "value",
  240. "foo": true,
  241. },
  242. },
  243. },
  244. "recursive_struct": {
  245. `--xxx
  246. Content-Disposition: form-data; name="child.name"
  247. Alex
  248. --xxx
  249. Content-Disposition: form-data; name="name"
  250. Robert
  251. --xxx--
  252. `,
  253. Recursive{Name: "Robert", Child: &Recursive{Name: "Alex"}},
  254. },
  255. "unknown_struct_number": {
  256. `--xxx
  257. Content-Disposition: form-data; name="unknown"
  258. 12
  259. --xxx--
  260. `,
  261. UnknownStruct{
  262. Unknown: 12.,
  263. },
  264. },
  265. "unknown_struct_map": {
  266. `--xxx
  267. Content-Disposition: form-data; name="unknown.foo"
  268. bar
  269. --xxx--
  270. `,
  271. UnknownStruct{
  272. Unknown: map[string]interface{}{
  273. "foo": "bar",
  274. },
  275. },
  276. },
  277. "union_integer": {
  278. `--xxx
  279. Content-Disposition: form-data; name="union"
  280. 12
  281. --xxx--
  282. `,
  283. UnionStruct{
  284. Union: UnionInteger(12),
  285. },
  286. },
  287. "union_struct_discriminated_a": {
  288. `--xxx
  289. Content-Disposition: form-data; name="union.a"
  290. foo
  291. --xxx
  292. Content-Disposition: form-data; name="union.b"
  293. bar
  294. --xxx
  295. Content-Disposition: form-data; name="union.type"
  296. typeA
  297. --xxx--
  298. `,
  299. UnionStruct{
  300. Union: UnionStructA{
  301. Type: "typeA",
  302. A: "foo",
  303. B: "bar",
  304. },
  305. },
  306. },
  307. "union_struct_discriminated_b": {
  308. `--xxx
  309. Content-Disposition: form-data; name="union.a"
  310. foo
  311. --xxx
  312. Content-Disposition: form-data; name="union.type"
  313. typeB
  314. --xxx--
  315. `,
  316. UnionStruct{
  317. Union: UnionStructB{
  318. Type: "typeB",
  319. A: "foo",
  320. },
  321. },
  322. },
  323. "union_struct_time": {
  324. `--xxx
  325. Content-Disposition: form-data; name="union"
  326. 2010-05-23
  327. --xxx--
  328. `,
  329. UnionStruct{
  330. Union: UnionTime(time.Date(2010, 05, 23, 0, 0, 0, 0, time.UTC)),
  331. },
  332. },
  333. }
  334. func TestEncode(t *testing.T) {
  335. for name, test := range tests {
  336. t.Run(name, func(t *testing.T) {
  337. buf := bytes.NewBuffer(nil)
  338. writer := multipart.NewWriter(buf)
  339. writer.SetBoundary("xxx")
  340. err := Marshal(test.val, writer)
  341. if err != nil {
  342. t.Errorf("serialization of %v failed with error %v", test.val, err)
  343. }
  344. err = writer.Close()
  345. if err != nil {
  346. t.Errorf("serialization of %v failed with error %v", test.val, err)
  347. }
  348. raw := buf.Bytes()
  349. if string(raw) != strings.ReplaceAll(test.buf, "\n", "\r\n") {
  350. t.Errorf("expected %+#v to serialize to '%s' but got '%s'", test.val, test.buf, string(raw))
  351. }
  352. })
  353. }
  354. }