2009-06-19 18:02:15 -07: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.
|
|
|
|
|
|
|
|
|
|
package http
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt";
|
2009-06-25 21:05:44 -07:00
|
|
|
"os";
|
2009-06-19 18:02:15 -07:00
|
|
|
"testing";
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type stringMultimap map[string] []string
|
|
|
|
|
|
|
|
|
|
type parseTest struct {
|
2009-06-25 21:05:44 -07:00
|
|
|
query string;
|
2009-06-19 18:02:15 -07:00
|
|
|
out stringMultimap;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var parseTests = []parseTest{
|
|
|
|
|
parseTest{
|
2009-06-25 21:05:44 -07:00
|
|
|
query: "a=1&b=2",
|
2009-06-19 18:02:15 -07:00
|
|
|
out: stringMultimap{ "a": []string{ "1" }, "b": []string{ "2" } },
|
|
|
|
|
},
|
|
|
|
|
parseTest{
|
2009-06-25 21:05:44 -07:00
|
|
|
query: "a=1&a=2&a=banana",
|
2009-06-19 18:02:15 -07:00
|
|
|
out: stringMultimap{ "a": []string{ "1", "2", "banana" } },
|
|
|
|
|
},
|
|
|
|
|
parseTest{
|
2009-06-25 21:05:44 -07:00
|
|
|
query: "ascii=%3Ckey%3A+0x90%3E",
|
2009-06-19 18:02:15 -07:00
|
|
|
out: stringMultimap{ "ascii": []string{ "<key: 0x90>" } },
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestParseForm(t *testing.T) {
|
|
|
|
|
for i, test := range parseTests {
|
2009-06-25 21:05:44 -07:00
|
|
|
form, err := parseForm(test.query);
|
2009-06-19 18:02:15 -07:00
|
|
|
if err != nil {
|
|
|
|
|
t.Errorf("test %d: Unexpected error: %v", i, err);
|
|
|
|
|
continue
|
|
|
|
|
}
|
2009-06-25 21:05:44 -07:00
|
|
|
if len(form) != len(test.out) {
|
|
|
|
|
t.Errorf("test %d: len(form) = %d, want %d", i, len(form), len(test.out));
|
2009-06-19 18:02:15 -07:00
|
|
|
}
|
2009-06-25 21:05:44 -07:00
|
|
|
for k, evs := range test.out {
|
|
|
|
|
vs, ok := form[k];
|
2009-06-19 18:02:15 -07:00
|
|
|
if !ok {
|
|
|
|
|
t.Errorf("test %d: Missing key %q", i, k);
|
|
|
|
|
continue
|
|
|
|
|
}
|
2009-06-25 21:05:44 -07:00
|
|
|
if len(vs) != len(evs) {
|
|
|
|
|
t.Errorf("test %d: len(form[%q]) = %d, want %d", i, k, len(vs), len(evs));
|
2009-06-19 18:02:15 -07:00
|
|
|
continue
|
|
|
|
|
}
|
2009-06-25 21:05:44 -07:00
|
|
|
for j, ev := range evs {
|
|
|
|
|
if v := vs[j]; v != ev {
|
|
|
|
|
t.Errorf("test %d: form[%q][%d] = %q, want %q", i, k, j, v, ev);
|
2009-06-19 18:02:15 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-06-25 21:05:44 -07:00
|
|
|
|
|
|
|
|
func TestQuery(t *testing.T) {
|
|
|
|
|
var err os.Error;
|
|
|
|
|
req := &Request{ Method: "GET" };
|
|
|
|
|
req.Url, err = ParseURL("http://www.google.com/search?q=foo&q=bar");
|
|
|
|
|
if q := req.FormValue("q"); q != "foo" {
|
|
|
|
|
t.Errorf(`req.FormValue("q") = %q, want "foo"`, q);
|
|
|
|
|
}
|
|
|
|
|
}
|