aicra/internal/multipart/reader_test.go

318 lines
6.7 KiB
Go
Raw Normal View History

2018-09-25 19:22:25 +00:00
package multipart
import (
"bytes"
"fmt"
2018-09-25 19:22:25 +00:00
"testing"
)
func TestSimple(t *testing.T) {
2019-05-04 08:11:47 +00:00
t.Parallel()
2018-09-25 19:22:25 +00:00
test := struct {
Input []byte
Boundary string
}{
Input: []byte(`--BoUnDaRy
Content-Disposition: form-data; name="somevar"
google.com
--BoUnDaRy
Content-Disposition: form-data; name="somefile"; filename="somefilename.pdf"
Content-Type: application/pdf
facebook.com
--BoUnDaRy--`),
Boundary: "BoUnDaRy",
}
mpr, err := NewReader(bytes.NewReader(test.Input), test.Boundary)
if err != nil {
t.Fatalf("unexpected error: %s", err)
2018-09-25 19:22:25 +00:00
}
if err = mpr.Parse(); err != nil {
t.Fatalf("unexpected error: %s", err)
}
// 1. Check var
somevar := mpr.Get("somevar")
if somevar == nil {
t.Fatalf("expected data %q to exist", "somevar")
}
if somevar.ContentType != "raw" {
t.Fatalf("expected ContentType to be %q, got %q", "raw", somevar.ContentType)
}
if string(somevar.Data) != "google.com" {
t.Fatalf("expected data to be %q, got %q", "google.com", somevar.Data)
}
// 2. Check file
somefile := mpr.Get("somefile")
if somefile == nil {
t.Fatalf("expected data %q to exist", "somefile")
}
const fileCT = "application/pdf"
if somefile.ContentType != fileCT {
t.Fatalf("expected ContentType to be %q, got %q", fileCT, somevar.ContentType)
}
const fileData = "facebook.com"
if string(somefile.Data) != fileData {
t.Fatalf("expected data to be %q, got %q", fileData, somefile.Data)
}
const fileHeader = "somefilename.pdf"
filename := somefile.GetHeader("filename")
if len(filename) < 1 {
t.Fatalf("expected data to have header 'filename'")
}
if filename != fileHeader {
t.Fatalf("expected filename to be %q, got %q", fileHeader, filename)
}
}
func TestSimpleWithCRLF(t *testing.T) {
2019-05-04 08:11:47 +00:00
t.Parallel()
type tcase struct {
Input []byte
Boundary string
}
_test := tcase{
Input: []byte(`--BoUnDaRy
Content-Disposition: form-data; name="somevar"
google.com
--BoUnDaRy
Content-Disposition: form-data; name="somefile"; filename="somefilename.pdf"
Content-Type: application/pdf
facebook.com
--BoUnDaRy--`),
Boundary: "BoUnDaRy",
}
test := tcase{
Input: make([]byte, 0),
Boundary: _test.Boundary,
}
// replace all \n with \r\n
for _, char := range _test.Input {
if char == '\n' {
test.Input = append(test.Input, []byte("\r\n")...)
continue
}
test.Input = append(test.Input, char)
}
mpr, err := NewReader(bytes.NewReader(test.Input), test.Boundary)
if err != nil {
t.Fatalf("unexpected error <%s>", err)
}
if err = mpr.Parse(); err != nil {
t.Fatalf("unexpected error <%s>", err)
2018-09-25 19:22:25 +00:00
}
// 1. Check var
somevar := mpr.Get("somevar")
if somevar == nil {
t.Fatalf("expected data %q to exist", "somevar")
2018-09-25 19:22:25 +00:00
}
if somevar.ContentType != "raw" {
t.Fatalf("expected ContentType to be %q, got %q", "raw", somevar.ContentType)
2018-09-25 19:22:25 +00:00
}
if string(somevar.Data) != "google.com" {
t.Fatalf("expected data to be %q, got %q", "google.com", somevar.Data)
2018-09-25 19:22:25 +00:00
}
// 2. Check file
const fileCT = "application/pdf"
2018-09-25 19:22:25 +00:00
somefile := mpr.Get("somefile")
if somefile == nil {
t.Fatalf("expected data %q to exist", "somefile")
2018-09-25 19:22:25 +00:00
}
if somefile.ContentType != fileCT {
t.Fatalf("expected ContentType to be %q, got %q", fileCT, somevar.ContentType)
2018-09-25 19:22:25 +00:00
}
const fileData = "facebook.com"
if string(somefile.Data) != fileData {
t.Fatalf("expected data to be %q, got %q", fileData, somefile.Data)
2018-09-25 19:22:25 +00:00
}
const fileHeader = "somefilename.pdf"
2018-09-25 19:22:25 +00:00
filename := somefile.GetHeader("filename")
if len(filename) < 1 {
t.Fatalf("expected data to have header 'filename'")
2018-09-25 19:22:25 +00:00
}
if filename != fileHeader {
t.Fatalf("expected filename to be %q, got %q", fileHeader, filename)
2018-09-25 19:22:25 +00:00
}
}
func TestNoName(t *testing.T) {
2019-05-04 08:11:47 +00:00
t.Parallel()
2018-09-25 19:22:25 +00:00
tests := []struct {
Input []byte
Boundary string
Length int
}{
{
Input: []byte("--BoUnDaRy\nContent-Disposition: form-data; xname=\"somevar\"\n\ngoogle.com\n--BoUnDaRy--"),
Boundary: "BoUnDaRy",
},
{
Input: []byte("--BoUnDaRy\nContent-Disposition: form-data; name=\"\"\n\ngoogle.com\n--BoUnDaRy--"),
Boundary: "BoUnDaRy",
},
{
Input: []byte("--BoUnDaRy\nContent-Disposition: form-data; name=\n\ngoogle.com\n--BoUnDaRy--"),
Boundary: "BoUnDaRy",
},
{
Input: []byte("--BoUnDaRy\nContent-Disposition: form-data; name\n\ngoogle.com\n--BoUnDaRy--"),
Boundary: "BoUnDaRy",
},
}
for i, test := range tests {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
2018-09-25 19:22:25 +00:00
mpr, err := NewReader(bytes.NewReader(test.Input), test.Boundary)
2018-09-25 19:22:25 +00:00
if err != nil {
t.Errorf("unexpected error <%s>", err)
return
}
if err = mpr.Parse(); err != errMissingDataName {
t.Errorf("expected the error <%s>, got <%s>", errMissingDataName, err)
return
}
})
2018-09-25 19:22:25 +00:00
}
}
func TestNoHeader(t *testing.T) {
2019-05-04 08:11:47 +00:00
t.Parallel()
2018-09-25 19:22:25 +00:00
tests := []struct {
Input []byte
Boundary string
Length int
}{
{
Input: []byte("--BoUnDaRy\n\ngoogle.com\n--BoUnDaRy--"),
Boundary: "BoUnDaRy",
},
{
Input: []byte("--BoUnDaRy\nContent-Disposition: false;\n\ngoogle.com\n--BoUnDaRy--"),
Boundary: "BoUnDaRy",
},
{
Input: []byte("--BoUnDaRy\nContent-Disposition: form-data;\n\ngoogle.com\n--BoUnDaRy--"),
Boundary: "BoUnDaRy",
},
}
for i, test := range tests {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
2018-09-25 19:22:25 +00:00
mpr, err := NewReader(bytes.NewReader(test.Input), test.Boundary)
2018-09-25 19:22:25 +00:00
if err != nil {
t.Errorf("unexpected error <%s>", err)
return
}
2018-09-25 19:22:25 +00:00
if err = mpr.Parse(); err != errNoHeader {
t.Errorf("expected the error <%s>, got <%s>", errNoHeader, err)
return
}
})
2018-09-25 19:22:25 +00:00
}
}
func TestNameConflict(t *testing.T) {
2019-05-04 08:11:47 +00:00
t.Parallel()
2018-09-25 19:22:25 +00:00
test := struct {
Input []byte
Boundary string
Length int
}{
Input: []byte(`--BoUnDaRy
Content-Disposition: form-data; name="var1"
google.com
--BoUnDaRy
Content-Disposition: form-data; name="var1"
facebook.com
--BoUnDaRy--`),
Boundary: "BoUnDaRy",
}
mpr, err := NewReader(bytes.NewReader(test.Input), test.Boundary)
if err != nil {
t.Fatalf("unexpected error <%s>", err)
2018-09-25 19:22:25 +00:00
}
if err = mpr.Parse(); err != errDataNameConflict {
t.Fatalf("expected the error <%s>, got <%s>", errDataNameConflict, err)
2018-09-25 19:22:25 +00:00
}
}
func TestGetterNil(t *testing.T) {
2019-05-04 08:11:47 +00:00
t.Parallel()
2018-09-25 19:22:25 +00:00
test := struct {
Input []byte
Boundary string
Length int
}{
Input: []byte(`--BoUnDaRy
Content-Disposition: form-data; name="var1"
google.com
--BoUnDaRy
Content-Disposition: form-data; name="var2"
facebook.com
--BoUnDaRy--`),
Boundary: "BoUnDaRy",
}
mpr, err := NewReader(bytes.NewReader(test.Input), test.Boundary)
if err != nil {
t.Fatalf("unexpected error <%s>", err)
2018-09-25 19:22:25 +00:00
}
if err = mpr.Parse(); err != nil {
t.Fatalf("unexpected error <%s>", err)
2018-09-25 19:22:25 +00:00
}
if mpr.Get("unknown_key") != nil {
t.Fatalf("expected 'unknown_key' not to exist, got <%v>", mpr.Get("unknown_key"))
2018-09-25 19:22:25 +00:00
}
}