2009-06-19 19:02:15 -06:00
|
|
|
// Copyright 2009 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.
|
|
|
|
|
2011-03-10 11:19:11 -07:00
|
|
|
package http_test
|
2009-06-19 19:02:15 -06:00
|
|
|
|
2009-10-03 12:09:01 -06:00
|
|
|
import (
|
2009-12-15 16:35:38 -07:00
|
|
|
"bytes"
|
2011-03-10 11:19:11 -07:00
|
|
|
"fmt"
|
|
|
|
. "http"
|
|
|
|
"http/httptest"
|
|
|
|
"io"
|
|
|
|
"os"
|
2010-08-17 18:01:50 -06:00
|
|
|
"reflect"
|
2010-09-22 18:40:07 -06:00
|
|
|
"regexp"
|
2010-08-17 18:01:50 -06:00
|
|
|
"strings"
|
2009-12-15 16:35:38 -07:00
|
|
|
"testing"
|
2009-10-03 12:09:01 -06:00
|
|
|
)
|
2009-06-19 19:02:15 -06:00
|
|
|
|
2009-10-06 20:41:51 -06:00
|
|
|
type stringMultimap map[string][]string
|
2009-06-19 19:02:15 -06:00
|
|
|
|
|
|
|
type parseTest struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
query string
|
|
|
|
out stringMultimap
|
2009-06-19 19:02:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
var parseTests = []parseTest{
|
2010-10-22 11:06:33 -06:00
|
|
|
{
|
2009-06-25 22:05:44 -06:00
|
|
|
query: "a=1&b=2",
|
2010-03-02 14:46:51 -07:00
|
|
|
out: stringMultimap{"a": []string{"1"}, "b": []string{"2"}},
|
2009-06-19 19:02:15 -06:00
|
|
|
},
|
2010-10-22 11:06:33 -06:00
|
|
|
{
|
2009-06-25 22:05:44 -06:00
|
|
|
query: "a=1&a=2&a=banana",
|
2010-03-02 14:46:51 -07:00
|
|
|
out: stringMultimap{"a": []string{"1", "2", "banana"}},
|
2009-06-19 19:02:15 -06:00
|
|
|
},
|
2010-10-22 11:06:33 -06:00
|
|
|
{
|
2009-06-25 22:05:44 -06:00
|
|
|
query: "ascii=%3Ckey%3A+0x90%3E",
|
2010-03-02 14:46:51 -07:00
|
|
|
out: stringMultimap{"ascii": []string{"<key: 0x90>"}},
|
2009-06-19 19:02:15 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseForm(t *testing.T) {
|
|
|
|
for i, test := range parseTests {
|
2010-03-16 14:57:16 -06:00
|
|
|
form, err := ParseQuery(test.query)
|
2009-06-19 19:02:15 -06:00
|
|
|
if err != nil {
|
2009-12-15 16:35:38 -07:00
|
|
|
t.Errorf("test %d: Unexpected error: %v", i, err)
|
|
|
|
continue
|
2009-06-19 19:02:15 -06:00
|
|
|
}
|
2009-06-25 22:05:44 -06:00
|
|
|
if len(form) != len(test.out) {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("test %d: len(form) = %d, want %d", i, len(form), len(test.out))
|
2009-06-19 19:02:15 -06:00
|
|
|
}
|
2009-06-25 22:05:44 -06:00
|
|
|
for k, evs := range test.out {
|
2009-12-15 16:35:38 -07:00
|
|
|
vs, ok := form[k]
|
2009-06-19 19:02:15 -06:00
|
|
|
if !ok {
|
2009-12-15 16:35:38 -07:00
|
|
|
t.Errorf("test %d: Missing key %q", i, k)
|
|
|
|
continue
|
2009-06-19 19:02:15 -06:00
|
|
|
}
|
2009-06-25 22:05:44 -06:00
|
|
|
if len(vs) != len(evs) {
|
2009-12-15 16:35:38 -07:00
|
|
|
t.Errorf("test %d: len(form[%q]) = %d, want %d", i, k, len(vs), len(evs))
|
|
|
|
continue
|
2009-06-19 19:02:15 -06:00
|
|
|
}
|
2009-06-25 22:05:44 -06:00
|
|
|
for j, ev := range evs {
|
|
|
|
if v := vs[j]; v != ev {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("test %d: form[%q][%d] = %q, want %q", i, k, j, v, ev)
|
2009-06-19 19:02:15 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-06-25 22:05:44 -06:00
|
|
|
|
|
|
|
func TestQuery(t *testing.T) {
|
2009-12-15 16:35:38 -07:00
|
|
|
req := &Request{Method: "GET"}
|
|
|
|
req.URL, _ = ParseURL("http://www.google.com/search?q=foo&q=bar")
|
2009-06-25 22:05:44 -06:00
|
|
|
if q := req.FormValue("q"); q != "foo" {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf(`req.FormValue("q") = %q, want "foo"`, q)
|
2009-06-25 22:05:44 -06:00
|
|
|
}
|
|
|
|
}
|
2009-10-03 12:09:01 -06:00
|
|
|
|
2010-08-17 18:01:50 -06:00
|
|
|
func TestPostQuery(t *testing.T) {
|
|
|
|
req := &Request{Method: "POST"}
|
|
|
|
req.URL, _ = ParseURL("http://www.google.com/search?q=foo&q=bar&both=x")
|
2011-02-22 22:39:25 -07:00
|
|
|
req.Header = Header{
|
|
|
|
"Content-Type": {"application/x-www-form-urlencoded; boo!"},
|
|
|
|
}
|
2010-08-17 18:01:50 -06:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-22 22:39:25 -07:00
|
|
|
type stringMap map[string][]string
|
2009-10-03 12:09:01 -06:00
|
|
|
type parseContentTypeTest struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
contentType stringMap
|
|
|
|
error bool
|
2009-10-03 12:09:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
var parseContentTypeTests = []parseContentTypeTest{
|
2011-02-22 22:39:25 -07:00
|
|
|
{contentType: stringMap{"Content-Type": {"text/plain"}}},
|
|
|
|
{contentType: stringMap{}}, // Non-existent keys are not placed. The value nil is illegal.
|
|
|
|
{contentType: stringMap{"Content-Type": {"text/plain; boundary="}}},
|
2010-10-22 11:06:33 -06:00
|
|
|
{
|
2011-02-22 22:39:25 -07:00
|
|
|
contentType: stringMap{"Content-Type": {"application/unknown"}},
|
2010-03-04 18:37:15 -07:00
|
|
|
error: true,
|
2009-10-03 12:09:01 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostContentTypeParsing(t *testing.T) {
|
|
|
|
for i, test := range parseContentTypeTests {
|
|
|
|
req := &Request{
|
2009-10-06 20:41:51 -06:00
|
|
|
Method: "POST",
|
2011-02-22 22:39:25 -07:00
|
|
|
Header: Header(test.contentType),
|
2010-03-02 14:46:51 -07:00
|
|
|
Body: nopCloser{bytes.NewBufferString("body")},
|
2009-12-15 16:35:38 -07:00
|
|
|
}
|
|
|
|
err := req.ParseForm()
|
2009-10-03 12:09:01 -06:00
|
|
|
if !test.error && err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("test %d: Unexpected error: %v", i, err)
|
2009-10-03 12:09:01 -06:00
|
|
|
}
|
|
|
|
if test.error && err == nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("test %d should have returned error", i)
|
2009-10-03 12:09:01 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-10-19 13:10:14 -06:00
|
|
|
|
2010-07-14 18:26:14 -06:00
|
|
|
func TestMultipartReader(t *testing.T) {
|
|
|
|
req := &Request{
|
|
|
|
Method: "POST",
|
2011-02-22 22:39:25 -07:00
|
|
|
Header: Header{"Content-Type": {`multipart/form-data; boundary="foo123"`}},
|
2010-07-14 18:26:14 -06:00
|
|
|
Body: nopCloser{new(bytes.Buffer)},
|
|
|
|
}
|
|
|
|
multipart, err := req.MultipartReader()
|
|
|
|
if multipart == nil {
|
|
|
|
t.Errorf("expected multipart; error: %v", err)
|
|
|
|
}
|
|
|
|
|
2011-02-22 22:39:25 -07:00
|
|
|
req.Header = Header{"Content-Type": {"text/plain"}}
|
2010-07-14 18:26:14 -06:00
|
|
|
multipart, err = req.MultipartReader()
|
|
|
|
if multipart != nil {
|
|
|
|
t.Errorf("unexpected multipart for text/plain")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-19 13:10:14 -06:00
|
|
|
func TestRedirect(t *testing.T) {
|
2011-03-10 11:19:11 -07:00
|
|
|
ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
|
|
|
|
switch r.URL.Path {
|
|
|
|
case "/":
|
|
|
|
w.Header().Set("Location", "/foo/")
|
|
|
|
w.WriteHeader(StatusSeeOther)
|
|
|
|
case "/foo/":
|
|
|
|
fmt.Fprintf(w, "foo")
|
|
|
|
default:
|
|
|
|
w.WriteHeader(StatusBadRequest)
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
var end = regexp.MustCompile("/foo/$")
|
|
|
|
r, url, err := Get(ts.URL)
|
2009-10-19 13:10:14 -06:00
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Fatal(err)
|
2009-10-19 13:10:14 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
r.Body.Close()
|
2010-09-22 18:40:07 -06:00
|
|
|
if r.StatusCode != 200 || !end.MatchString(url) {
|
2011-03-10 11:19:11 -07:00
|
|
|
t.Fatalf("Get got status %d at %q, want 200 matching /foo/$", r.StatusCode, url)
|
2009-10-19 13:10:14 -06:00
|
|
|
}
|
|
|
|
}
|
2011-03-10 11:19:11 -07:00
|
|
|
|
|
|
|
// TODO: stop copy/pasting this around. move to io/ioutil?
|
|
|
|
type nopCloser struct {
|
|
|
|
io.Reader
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nopCloser) Close() os.Error { return nil }
|