Merge from mainline (167278:168000).
[official-gcc/graphite-test-results.git] / libgo / go / http / request_test.go
blobd25e5e5e7e106fe19e89c1231474fba8f0563e43
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 package http
7 import (
8 "bytes"
9 "reflect"
10 "regexp"
11 "strings"
12 "testing"
15 type stringMultimap map[string][]string
17 type parseTest struct {
18 query string
19 out stringMultimap
22 var parseTests = []parseTest{
24 query: "a=1&b=2",
25 out: stringMultimap{"a": []string{"1"}, "b": []string{"2"}},
28 query: "a=1&a=2&a=banana",
29 out: stringMultimap{"a": []string{"1", "2", "banana"}},
32 query: "ascii=%3Ckey%3A+0x90%3E",
33 out: stringMultimap{"ascii": []string{"<key: 0x90>"}},
37 func TestParseForm(t *testing.T) {
38 for i, test := range parseTests {
39 form, err := ParseQuery(test.query)
40 if err != nil {
41 t.Errorf("test %d: Unexpected error: %v", i, err)
42 continue
44 if len(form) != len(test.out) {
45 t.Errorf("test %d: len(form) = %d, want %d", i, len(form), len(test.out))
47 for k, evs := range test.out {
48 vs, ok := form[k]
49 if !ok {
50 t.Errorf("test %d: Missing key %q", i, k)
51 continue
53 if len(vs) != len(evs) {
54 t.Errorf("test %d: len(form[%q]) = %d, want %d", i, k, len(vs), len(evs))
55 continue
57 for j, ev := range evs {
58 if v := vs[j]; v != ev {
59 t.Errorf("test %d: form[%q][%d] = %q, want %q", i, k, j, v, ev)
66 func TestQuery(t *testing.T) {
67 req := &Request{Method: "GET"}
68 req.URL, _ = ParseURL("http://www.google.com/search?q=foo&q=bar")
69 if q := req.FormValue("q"); q != "foo" {
70 t.Errorf(`req.FormValue("q") = %q, want "foo"`, q)
74 func TestPostQuery(t *testing.T) {
75 req := &Request{Method: "POST"}
76 req.URL, _ = ParseURL("http://www.google.com/search?q=foo&q=bar&both=x")
77 req.Header = map[string]string{"Content-Type": "application/x-www-form-urlencoded; boo!"}
78 req.Body = nopCloser{strings.NewReader("z=post&both=y")}
79 if q := req.FormValue("q"); q != "foo" {
80 t.Errorf(`req.FormValue("q") = %q, want "foo"`, q)
82 if z := req.FormValue("z"); z != "post" {
83 t.Errorf(`req.FormValue("z") = %q, want "post"`, z)
85 if both := req.Form["both"]; !reflect.DeepEqual(both, []string{"x", "y"}) {
86 t.Errorf(`req.FormValue("both") = %q, want ["x", "y"]`, both)
90 type stringMap map[string]string
91 type parseContentTypeTest struct {
92 contentType stringMap
93 error bool
96 var parseContentTypeTests = []parseContentTypeTest{
97 {contentType: stringMap{"Content-Type": "text/plain"}},
98 {contentType: stringMap{"Content-Type": ""}},
99 {contentType: stringMap{"Content-Type": "text/plain; boundary="}},
101 contentType: stringMap{"Content-Type": "application/unknown"},
102 error: true,
106 func TestPostContentTypeParsing(t *testing.T) {
107 for i, test := range parseContentTypeTests {
108 req := &Request{
109 Method: "POST",
110 Header: test.contentType,
111 Body: nopCloser{bytes.NewBufferString("body")},
113 err := req.ParseForm()
114 if !test.error && err != nil {
115 t.Errorf("test %d: Unexpected error: %v", i, err)
117 if test.error && err == nil {
118 t.Errorf("test %d should have returned error", i)
123 func TestMultipartReader(t *testing.T) {
124 req := &Request{
125 Method: "POST",
126 Header: stringMap{"Content-Type": `multipart/form-data; boundary="foo123"`},
127 Body: nopCloser{new(bytes.Buffer)},
129 multipart, err := req.MultipartReader()
130 if multipart == nil {
131 t.Errorf("expected multipart; error: %v", err)
134 req.Header = stringMap{"Content-Type": "text/plain"}
135 multipart, err = req.MultipartReader()
136 if multipart != nil {
137 t.Errorf("unexpected multipart for text/plain")
141 func TestRedirect(t *testing.T) {
142 const (
143 start = "http://google.com/"
144 endRe = "^http://www\\.google\\.[a-z.]+/$"
146 var end = regexp.MustCompile(endRe)
147 r, url, err := Get(start)
148 if err != nil {
149 t.Fatal(err)
151 r.Body.Close()
152 if r.StatusCode != 200 || !end.MatchString(url) {
153 t.Fatalf("Get(%s) got status %d at %q, want 200 matching %q", start, r.StatusCode, url, endRe)