OSDN Git Service

Add Go frontend, libgo library, and Go testsuite.
[pf3gnuchains/gcc-fork.git] / libgo / go / http / request_test.go
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.
4
5 package http
6
7 import (
8         "bytes"
9         "reflect"
10         "regexp"
11         "strings"
12         "testing"
13 )
14
15 type stringMultimap map[string][]string
16
17 type parseTest struct {
18         query string
19         out   stringMultimap
20 }
21
22 var parseTests = []parseTest{
23         {
24                 query: "a=1&b=2",
25                 out:   stringMultimap{"a": []string{"1"}, "b": []string{"2"}},
26         },
27         {
28                 query: "a=1&a=2&a=banana",
29                 out:   stringMultimap{"a": []string{"1", "2", "banana"}},
30         },
31         {
32                 query: "ascii=%3Ckey%3A+0x90%3E",
33                 out:   stringMultimap{"ascii": []string{"<key: 0x90>"}},
34         },
35 }
36
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
43                 }
44                 if len(form) != len(test.out) {
45                         t.Errorf("test %d: len(form) = %d, want %d", i, len(form), len(test.out))
46                 }
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
52                         }
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
56                         }
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)
60                                 }
61                         }
62                 }
63         }
64 }
65
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)
71         }
72 }
73
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)
81         }
82         if z := req.FormValue("z"); z != "post" {
83                 t.Errorf(`req.FormValue("z") = %q, want "post"`, z)
84         }
85         if both := req.Form["both"]; !reflect.DeepEqual(both, []string{"x", "y"}) {
86                 t.Errorf(`req.FormValue("both") = %q, want ["x", "y"]`, both)
87         }
88 }
89
90 type stringMap map[string]string
91 type parseContentTypeTest struct {
92         contentType stringMap
93         error       bool
94 }
95
96 var parseContentTypeTests = []parseContentTypeTest{
97         {contentType: stringMap{"Content-Type": "text/plain"}},
98         {contentType: stringMap{"Content-Type": ""}},
99         {contentType: stringMap{"Content-Type": "text/plain; boundary="}},
100         {
101                 contentType: stringMap{"Content-Type": "application/unknown"},
102                 error:       true,
103         },
104 }
105
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")},
112                 }
113                 err := req.ParseForm()
114                 if !test.error && err != nil {
115                         t.Errorf("test %d: Unexpected error: %v", i, err)
116                 }
117                 if test.error && err == nil {
118                         t.Errorf("test %d should have returned error", i)
119                 }
120         }
121 }
122
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)},
128         }
129         multipart, err := req.MultipartReader()
130         if multipart == nil {
131                 t.Errorf("expected multipart; error: %v", err)
132         }
133
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")
138         }
139 }
140
141 func TestRedirect(t *testing.T) {
142         const (
143                 start = "http://google.com/"
144                 endRe = "^http://www\\.google\\.[a-z.]+/$"
145         )
146         var end = regexp.MustCompile(endRe)
147         r, url, err := Get(start)
148         if err != nil {
149                 t.Fatal(err)
150         }
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)
154         }
155 }