2011-05-20 02:18:15 +02:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package multipart
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
2014-06-07 00:37:27 +02:00
|
|
|
"strings"
|
2011-05-20 02:18:15 +02:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestReadForm(t *testing.T) {
|
|
|
|
testBody := regexp.MustCompile("\n").ReplaceAllString(message, "\r\n")
|
2014-06-07 00:37:27 +02:00
|
|
|
b := strings.NewReader(testBody)
|
2011-05-20 02:18:15 +02:00
|
|
|
r := NewReader(b, boundary)
|
|
|
|
f, err := r.ReadForm(25)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("ReadForm:", err)
|
|
|
|
}
|
|
|
|
defer f.RemoveAll()
|
|
|
|
if g, e := f.Value["texta"][0], textaValue; g != e {
|
|
|
|
t.Errorf("texta value = %q, want %q", g, e)
|
|
|
|
}
|
|
|
|
if g, e := f.Value["textb"][0], textbValue; g != e {
|
|
|
|
t.Errorf("texta value = %q, want %q", g, e)
|
|
|
|
}
|
|
|
|
fd := testFile(t, f.File["filea"][0], "filea.txt", fileaContents)
|
|
|
|
if _, ok := fd.(*os.File); ok {
|
|
|
|
t.Error("file is *os.File, should not be")
|
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
fd.Close()
|
2011-05-20 02:18:15 +02:00
|
|
|
fd = testFile(t, f.File["fileb"][0], "fileb.txt", filebContents)
|
|
|
|
if _, ok := fd.(*os.File); !ok {
|
2011-09-16 17:47:21 +02:00
|
|
|
t.Errorf("file has unexpected underlying type %T", fd)
|
2011-05-20 02:18:15 +02:00
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
fd.Close()
|
2011-05-20 02:18:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func testFile(t *testing.T, fh *FileHeader, efn, econtent string) File {
|
|
|
|
if fh.Filename != efn {
|
|
|
|
t.Errorf("filename = %q, want %q", fh.Filename, efn)
|
|
|
|
}
|
|
|
|
f, err := fh.Open()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("opening file:", err)
|
|
|
|
}
|
|
|
|
b := new(bytes.Buffer)
|
|
|
|
_, err = io.Copy(b, f)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("copying contents:", err)
|
|
|
|
}
|
|
|
|
if g := b.String(); g != econtent {
|
|
|
|
t.Errorf("contents = %q, want %q", g, econtent)
|
|
|
|
}
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
fileaContents = "This is a test file."
|
|
|
|
filebContents = "Another test file."
|
|
|
|
textaValue = "foo"
|
|
|
|
textbValue = "bar"
|
|
|
|
boundary = `MyBoundary`
|
|
|
|
)
|
|
|
|
|
|
|
|
const message = `
|
|
|
|
--MyBoundary
|
|
|
|
Content-Disposition: form-data; name="filea"; filename="filea.txt"
|
|
|
|
Content-Type: text/plain
|
|
|
|
|
|
|
|
` + fileaContents + `
|
|
|
|
--MyBoundary
|
|
|
|
Content-Disposition: form-data; name="fileb"; filename="fileb.txt"
|
|
|
|
Content-Type: text/plain
|
|
|
|
|
|
|
|
` + filebContents + `
|
|
|
|
--MyBoundary
|
|
|
|
Content-Disposition: form-data; name="texta"
|
|
|
|
|
|
|
|
` + textaValue + `
|
|
|
|
--MyBoundary
|
|
|
|
Content-Disposition: form-data; name="textb"
|
|
|
|
|
|
|
|
` + textbValue + `
|
|
|
|
--MyBoundary--
|
|
|
|
`
|