summaryrefslogtreecommitdiff
path: root/src/pkg/mime/multipart/multipart_test.go
diff options
context:
space:
mode:
authorOndřej Surý <ondrej@sury.org>2011-05-04 15:46:02 +0200
committerOndřej Surý <ondrej@sury.org>2011-05-04 15:46:02 +0200
commitad811fbb8897a9a3063274e927133915941f1dca (patch)
tree3df18657e50a0313ed6defcda30e4474cb28a467 /src/pkg/mime/multipart/multipart_test.go
parent14cda8f405d55947c0a3fae0852b04af8405eae0 (diff)
downloadgolang-ad811fbb8897a9a3063274e927133915941f1dca.tar.gz
Imported Upstream version 2011.04.27upstream-weekly/2011.04.27
Diffstat (limited to 'src/pkg/mime/multipart/multipart_test.go')
-rw-r--r--src/pkg/mime/multipart/multipart_test.go125
1 files changed, 23 insertions, 102 deletions
diff --git a/src/pkg/mime/multipart/multipart_test.go b/src/pkg/mime/multipart/multipart_test.go
index 16249146c..f8f10f3e1 100644
--- a/src/pkg/mime/multipart/multipart_test.go
+++ b/src/pkg/mime/multipart/multipart_test.go
@@ -8,37 +8,38 @@ import (
"bytes"
"fmt"
"io"
- "io/ioutil"
"json"
"os"
+ "regexp"
"strings"
"testing"
)
func TestHorizontalWhitespace(t *testing.T) {
- if !onlyHorizontalWhitespace([]byte(" \t")) {
+ if !onlyHorizontalWhitespace(" \t") {
t.Error("expected pass")
}
- if onlyHorizontalWhitespace([]byte("foo bar")) {
+ if onlyHorizontalWhitespace("foo bar") {
t.Error("expected failure")
}
}
func TestBoundaryLine(t *testing.T) {
- mr := NewReader(strings.NewReader(""), "myBoundary").(*multiReader)
- if !mr.isBoundaryDelimiterLine([]byte("--myBoundary\r\n")) {
+ boundary := "myBoundary"
+ prefix := "--" + boundary
+ if !isBoundaryDelimiterLine("--myBoundary\r\n", prefix) {
t.Error("expected")
}
- if !mr.isBoundaryDelimiterLine([]byte("--myBoundary \r\n")) {
+ if !isBoundaryDelimiterLine("--myBoundary \r\n", prefix) {
t.Error("expected")
}
- if !mr.isBoundaryDelimiterLine([]byte("--myBoundary \n")) {
+ if !isBoundaryDelimiterLine("--myBoundary \n", prefix) {
t.Error("expected")
}
- if mr.isBoundaryDelimiterLine([]byte("--myBoundary bogus \n")) {
+ if isBoundaryDelimiterLine("--myBoundary bogus \n", prefix) {
t.Error("expected fail")
}
- if mr.isBoundaryDelimiterLine([]byte("--myBoundary bogus--")) {
+ if isBoundaryDelimiterLine("--myBoundary bogus--", prefix) {
t.Error("expected fail")
}
}
@@ -78,9 +79,7 @@ func TestFormName(t *testing.T) {
}
}
-var longLine = strings.Repeat("\n\n\r\r\r\n\r\000", (1<<20)/8)
-
-func testMultipartBody() string {
+func TestMultipart(t *testing.T) {
testBody := `
This is a multi-part message. This line is ignored.
--MyBoundary
@@ -91,10 +90,6 @@ foo-bar: baz
My value
The end.
--MyBoundary
-name: bigsection
-
-[longline]
---MyBoundary
Header1: value1b
HEADER2: value2b
foo-bar: bazb
@@ -107,26 +102,11 @@ Line 3 ends in a newline, but just one.
never read data
--MyBoundary--
-
-
-useless trailer
`
- testBody = strings.Replace(testBody, "\n", "\r\n", -1)
- return strings.Replace(testBody, "[longline]", longLine, 1)
-}
-
-func TestMultipart(t *testing.T) {
- bodyReader := strings.NewReader(testMultipartBody())
- testMultipart(t, bodyReader)
-}
-
-func TestMultipartSlowInput(t *testing.T) {
- bodyReader := strings.NewReader(testMultipartBody())
- testMultipart(t, &slowReader{bodyReader})
-}
+ testBody = regexp.MustCompile("\n").ReplaceAllString(testBody, "\r\n")
+ bodyReader := strings.NewReader(testBody)
-func testMultipart(t *testing.T, r io.Reader) {
- reader := NewReader(r, "MyBoundary")
+ reader := NewReader(bodyReader, "MyBoundary")
buf := new(bytes.Buffer)
// Part1
@@ -145,64 +125,38 @@ func testMultipart(t *testing.T, r io.Reader) {
t.Error("Expected Foo-Bar: baz")
}
buf.Reset()
- if _, err := io.Copy(buf, part); err != nil {
- t.Errorf("part 1 copy: %v", err)
- }
+ io.Copy(buf, part)
expectEq(t, "My value\r\nThe end.",
buf.String(), "Value of first part")
// Part2
part, err = reader.NextPart()
- if err != nil {
- t.Fatalf("Expected part2; got: %v", err)
- return
- }
- if e, g := "bigsection", part.Header.Get("name"); e != g {
- t.Errorf("part2's name header: expected %q, got %q", e, g)
- }
- buf.Reset()
- if _, err := io.Copy(buf, part); err != nil {
- t.Errorf("part 2 copy: %v", err)
- }
- s := buf.String()
- if len(s) != len(longLine) {
- t.Errorf("part2 body expected long line of length %d; got length %d",
- len(longLine), len(s))
- }
- if s != longLine {
- t.Errorf("part2 long body didn't match")
- }
-
- // Part3
- part, err = reader.NextPart()
if part == nil || err != nil {
- t.Error("Expected part3")
+ t.Error("Expected part2")
return
}
if part.Header.Get("foo-bar") != "bazb" {
t.Error("Expected foo-bar: bazb")
}
buf.Reset()
- if _, err := io.Copy(buf, part); err != nil {
- t.Errorf("part 3 copy: %v", err)
- }
+ io.Copy(buf, part)
expectEq(t, "Line 1\r\nLine 2\r\nLine 3 ends in a newline, but just one.\r\n",
- buf.String(), "body of part 3")
+ buf.String(), "Value of second part")
- // Part4
+ // Part3
part, err = reader.NextPart()
if part == nil || err != nil {
- t.Error("Expected part 4 without errors")
+ t.Error("Expected part3 without errors")
return
}
- // Non-existent part5
+ // Non-existent part4
part, err = reader.NextPart()
if part != nil {
- t.Error("Didn't expect a fifth part.")
+ t.Error("Didn't expect a third part.")
}
if err != nil {
- t.Errorf("Unexpected error getting fifth part: %v", err)
+ t.Errorf("Unexpected error getting third part: %v", err)
}
}
@@ -283,36 +237,3 @@ func TestLineLimit(t *testing.T) {
t.Errorf("expected to read < %d bytes; read %d", maxReadThreshold, mr.n)
}
}
-
-func TestMultipartTruncated(t *testing.T) {
- testBody := `
-This is a multi-part message. This line is ignored.
---MyBoundary
-foo-bar: baz
-
-Oh no, premature EOF!
-`
- body := strings.Replace(testBody, "\n", "\r\n", -1)
- bodyReader := strings.NewReader(body)
- r := NewReader(bodyReader, "MyBoundary")
-
- part, err := r.NextPart()
- if err != nil {
- t.Fatalf("didn't get a part")
- }
- _, err = io.Copy(ioutil.Discard, part)
- if err != io.ErrUnexpectedEOF {
- t.Fatalf("expected error io.ErrUnexpectedEOF; got %v", err)
- }
-}
-
-type slowReader struct {
- r io.Reader
-}
-
-func (s *slowReader) Read(p []byte) (int, os.Error) {
- if len(p) == 0 {
- return s.r.Read(p)
- }
- return s.r.Read(p[:1])
-}