diff options
author | Ondřej Surý <ondrej@sury.org> | 2011-01-17 12:40:45 +0100 |
---|---|---|
committer | Ondřej Surý <ondrej@sury.org> | 2011-01-17 12:40:45 +0100 |
commit | 3e45412327a2654a77944249962b3652e6142299 (patch) | |
tree | bc3bf69452afa055423cbe0c5cfa8ca357df6ccf /src/pkg/http/request_test.go | |
parent | c533680039762cacbc37db8dc7eed074c3e497be (diff) | |
download | golang-upstream/2011.01.12.tar.gz |
Imported Upstream version 2011.01.12upstream/2011.01.12
Diffstat (limited to 'src/pkg/http/request_test.go')
-rw-r--r-- | src/pkg/http/request_test.go | 60 |
1 files changed, 49 insertions, 11 deletions
diff --git a/src/pkg/http/request_test.go b/src/pkg/http/request_test.go index 98d5342bb..d25e5e5e7 100644 --- a/src/pkg/http/request_test.go +++ b/src/pkg/http/request_test.go @@ -6,6 +6,9 @@ package http import ( "bytes" + "reflect" + "regexp" + "strings" "testing" ) @@ -17,15 +20,15 @@ type parseTest struct { } var parseTests = []parseTest{ - parseTest{ + { query: "a=1&b=2", out: stringMultimap{"a": []string{"1"}, "b": []string{"2"}}, }, - parseTest{ + { query: "a=1&a=2&a=banana", out: stringMultimap{"a": []string{"1", "2", "banana"}}, }, - parseTest{ + { query: "ascii=%3Ckey%3A+0x90%3E", out: stringMultimap{"ascii": []string{"<key: 0x90>"}}, }, @@ -68,6 +71,22 @@ func TestQuery(t *testing.T) { } } +func TestPostQuery(t *testing.T) { + req := &Request{Method: "POST"} + req.URL, _ = ParseURL("http://www.google.com/search?q=foo&q=bar&both=x") + req.Header = map[string]string{"Content-Type": "application/x-www-form-urlencoded; boo!"} + req.Body = nopCloser{strings.NewReader("z=post&both=y")} + if q := req.FormValue("q"); q != "foo" { + t.Errorf(`req.FormValue("q") = %q, want "foo"`, q) + } + if z := req.FormValue("z"); z != "post" { + t.Errorf(`req.FormValue("z") = %q, want "post"`, z) + } + if both := req.Form["both"]; !reflect.DeepEqual(both, []string{"x", "y"}) { + t.Errorf(`req.FormValue("both") = %q, want ["x", "y"]`, both) + } +} + type stringMap map[string]string type parseContentTypeTest struct { contentType stringMap @@ -75,10 +94,10 @@ type parseContentTypeTest struct { } var parseContentTypeTests = []parseContentTypeTest{ - parseContentTypeTest{contentType: stringMap{"Content-Type": "text/plain"}}, - parseContentTypeTest{contentType: stringMap{"Content-Type": ""}}, - parseContentTypeTest{contentType: stringMap{"Content-Type": "text/plain; boundary="}}, - parseContentTypeTest{ + {contentType: stringMap{"Content-Type": "text/plain"}}, + {contentType: stringMap{"Content-Type": ""}}, + {contentType: stringMap{"Content-Type": "text/plain; boundary="}}, + { contentType: stringMap{"Content-Type": "application/unknown"}, error: true, }, @@ -101,17 +120,36 @@ func TestPostContentTypeParsing(t *testing.T) { } } +func TestMultipartReader(t *testing.T) { + req := &Request{ + Method: "POST", + Header: stringMap{"Content-Type": `multipart/form-data; boundary="foo123"`}, + Body: nopCloser{new(bytes.Buffer)}, + } + multipart, err := req.MultipartReader() + if multipart == nil { + t.Errorf("expected multipart; error: %v", err) + } + + req.Header = stringMap{"Content-Type": "text/plain"} + multipart, err = req.MultipartReader() + if multipart != nil { + t.Errorf("unexpected multipart for text/plain") + } +} + func TestRedirect(t *testing.T) { const ( - start = "http://codesearch.google.com/" - end = "http://www.google.com/codesearch" + start = "http://google.com/" + endRe = "^http://www\\.google\\.[a-z.]+/$" ) + var end = regexp.MustCompile(endRe) r, url, err := Get(start) if err != nil { t.Fatal(err) } r.Body.Close() - if r.StatusCode != 200 || url != end { - t.Fatalf("Get(%s) got status %d at %s, want 200 at %s", start, r.StatusCode, url, end) + if r.StatusCode != 200 || !end.MatchString(url) { + t.Fatalf("Get(%s) got status %d at %q, want 200 matching %q", start, r.StatusCode, url, endRe) } } |