summaryrefslogtreecommitdiff
path: root/lib/http/multipart_form_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'lib/http/multipart_form_test.go')
-rw-r--r--lib/http/multipart_form_test.go84
1 files changed, 84 insertions, 0 deletions
diff --git a/lib/http/multipart_form_test.go b/lib/http/multipart_form_test.go
new file mode 100644
index 00000000..8562acc7
--- /dev/null
+++ b/lib/http/multipart_form_test.go
@@ -0,0 +1,84 @@
+// Copyright 2024, Shulhan <ms@kilabit.info>. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package http
+
+import (
+ "crypto/rand"
+ "mime/multipart"
+ "strings"
+ "testing"
+
+ "git.sr.ht/~shulhan/pakakeh.go/lib/test"
+ "git.sr.ht/~shulhan/pakakeh.go/lib/test/mock"
+)
+
+func TestGenerateFormData(t *testing.T) {
+ type testcase struct {
+ form multipart.Form
+ tagOutput string
+ field2filename map[string]string
+ }
+
+ rand.Reader = mock.NewRandReader([]byte(`randomseed`))
+
+ var (
+ tdata *test.Data
+ err error
+ )
+ tdata, err = test.LoadData(`testdata/GenerateFormData_test.txt`)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ var listcase = []testcase{{
+ form: multipart.Form{
+ Value: map[string][]string{
+ `field1`: []string{`value1`, `value1.1`},
+ },
+ File: map[string][]*multipart.FileHeader{},
+ },
+ field2filename: map[string]string{
+ `field0`: `file0`,
+ },
+ tagOutput: `file0`,
+ }}
+
+ var (
+ tcase testcase
+ listFH []*multipart.FileHeader
+ fh *multipart.FileHeader
+
+ fieldname string
+ filename string
+ gotContentType string
+ gotBody string
+ tag string
+ )
+
+ for _, tcase = range listcase {
+ for fieldname, filename = range tcase.field2filename {
+ fh, err = CreateMultipartFileHeader(filename, tdata.Input[filename])
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ listFH = tcase.form.File[fieldname]
+ listFH = append(listFH, fh)
+ tcase.form.File[fieldname] = listFH
+ }
+
+ gotContentType, gotBody, err = GenerateFormData(&tcase.form)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ tag = tcase.tagOutput + `.ContentType`
+ test.Assert(t, tag, string(tdata.Output[tag]), gotContentType)
+
+ gotBody = strings.ReplaceAll(gotBody, "\r\n", "\n")
+ tag = tcase.tagOutput + `.Body`
+ test.Assert(t, tag, string(tdata.Output[tag]), gotBody)
+ }
+}