OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package http_test | 5 package http_test |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 "fmt" | 9 "fmt" |
10 . "http" | 10 . "http" |
11 "http/httptest" | 11 "http/httptest" |
12 "io" | 12 "io" |
13 "io/ioutil" | 13 "io/ioutil" |
14 "mime/multipart" | 14 "mime/multipart" |
15 "os" | 15 "os" |
16 "reflect" | 16 "reflect" |
17 "regexp" | 17 "regexp" |
18 "strings" | 18 "strings" |
19 "testing" | 19 "testing" |
| 20 "url" |
20 ) | 21 ) |
21 | 22 |
22 type stringMultimap map[string][]string | 23 type stringMultimap map[string][]string |
23 | 24 |
24 type parseTest struct { | 25 type parseTest struct { |
25 query string | 26 query string |
26 out stringMultimap | 27 out stringMultimap |
27 } | 28 } |
28 | 29 |
29 var parseTests = []parseTest{ | 30 var parseTests = []parseTest{ |
30 { | 31 { |
31 query: "a=1&b=2", | 32 query: "a=1&b=2", |
32 out: stringMultimap{"a": []string{"1"}, "b": []string{"2"}}, | 33 out: stringMultimap{"a": []string{"1"}, "b": []string{"2"}}, |
33 }, | 34 }, |
34 { | 35 { |
35 query: "a=1&a=2&a=banana", | 36 query: "a=1&a=2&a=banana", |
36 out: stringMultimap{"a": []string{"1", "2", "banana"}}, | 37 out: stringMultimap{"a": []string{"1", "2", "banana"}}, |
37 }, | 38 }, |
38 { | 39 { |
39 query: "ascii=%3Ckey%3A+0x90%3E", | 40 query: "ascii=%3Ckey%3A+0x90%3E", |
40 out: stringMultimap{"ascii": []string{"<key: 0x90>"}}, | 41 out: stringMultimap{"ascii": []string{"<key: 0x90>"}}, |
41 }, | 42 }, |
42 } | 43 } |
43 | 44 |
44 func TestParseForm(t *testing.T) { | 45 func TestParseForm(t *testing.T) { |
45 for i, test := range parseTests { | 46 for i, test := range parseTests { |
46 » » form, err := ParseQuery(test.query) | 47 » » form, err := url.ParseQuery(test.query) |
47 if err != nil { | 48 if err != nil { |
48 t.Errorf("test %d: Unexpected error: %v", i, err) | 49 t.Errorf("test %d: Unexpected error: %v", i, err) |
49 continue | 50 continue |
50 } | 51 } |
51 if len(form) != len(test.out) { | 52 if len(form) != len(test.out) { |
52 t.Errorf("test %d: len(form) = %d, want %d", i, len(form
), len(test.out)) | 53 t.Errorf("test %d: len(form) = %d, want %d", i, len(form
), len(test.out)) |
53 } | 54 } |
54 for k, evs := range test.out { | 55 for k, evs := range test.out { |
55 vs, ok := form[k] | 56 vs, ok := form[k] |
56 if !ok { | 57 if !ok { |
57 t.Errorf("test %d: Missing key %q", i, k) | 58 t.Errorf("test %d: Missing key %q", i, k) |
58 continue | 59 continue |
59 } | 60 } |
60 if len(vs) != len(evs) { | 61 if len(vs) != len(evs) { |
61 t.Errorf("test %d: len(form[%q]) = %d, want %d",
i, k, len(vs), len(evs)) | 62 t.Errorf("test %d: len(form[%q]) = %d, want %d",
i, k, len(vs), len(evs)) |
62 continue | 63 continue |
63 } | 64 } |
64 for j, ev := range evs { | 65 for j, ev := range evs { |
65 if v := vs[j]; v != ev { | 66 if v := vs[j]; v != ev { |
66 t.Errorf("test %d: form[%q][%d] = %q, wa
nt %q", i, k, j, v, ev) | 67 t.Errorf("test %d: form[%q][%d] = %q, wa
nt %q", i, k, j, v, ev) |
67 } | 68 } |
68 } | 69 } |
69 } | 70 } |
70 } | 71 } |
71 } | 72 } |
72 | 73 |
73 func TestQuery(t *testing.T) { | 74 func TestQuery(t *testing.T) { |
74 req := &Request{Method: "GET"} | 75 req := &Request{Method: "GET"} |
75 » req.URL, _ = ParseURL("http://www.google.com/search?q=foo&q=bar") | 76 » req.URL, _ = url.Parse("http://www.google.com/search?q=foo&q=bar") |
76 if q := req.FormValue("q"); q != "foo" { | 77 if q := req.FormValue("q"); q != "foo" { |
77 t.Errorf(`req.FormValue("q") = %q, want "foo"`, q) | 78 t.Errorf(`req.FormValue("q") = %q, want "foo"`, q) |
78 } | 79 } |
79 } | 80 } |
80 | 81 |
81 func TestPostQuery(t *testing.T) { | 82 func TestPostQuery(t *testing.T) { |
82 req := &Request{Method: "POST"} | 83 req := &Request{Method: "POST"} |
83 » req.URL, _ = ParseURL("http://www.google.com/search?q=foo&q=bar&both=x") | 84 » req.URL, _ = url.Parse("http://www.google.com/search?q=foo&q=bar&both=x"
) |
84 req.Header = Header{ | 85 req.Header = Header{ |
85 "Content-Type": {"application/x-www-form-urlencoded; boo!"}, | 86 "Content-Type": {"application/x-www-form-urlencoded; boo!"}, |
86 } | 87 } |
87 req.Body = ioutil.NopCloser(strings.NewReader("z=post&both=y")) | 88 req.Body = ioutil.NopCloser(strings.NewReader("z=post&both=y")) |
88 if q := req.FormValue("q"); q != "foo" { | 89 if q := req.FormValue("q"); q != "foo" { |
89 t.Errorf(`req.FormValue("q") = %q, want "foo"`, q) | 90 t.Errorf(`req.FormValue("q") = %q, want "foo"`, q) |
90 } | 91 } |
91 if z := req.FormValue("z"); z != "post" { | 92 if z := req.FormValue("z"); z != "post" { |
92 t.Errorf(`req.FormValue("z") = %q, want "post"`, z) | 93 t.Errorf(`req.FormValue("z") = %q, want "post"`, z) |
93 } | 94 } |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
326 --MyBoundary | 327 --MyBoundary |
327 Content-Disposition: form-data; name="texta" | 328 Content-Disposition: form-data; name="texta" |
328 | 329 |
329 ` + textaValue + ` | 330 ` + textaValue + ` |
330 --MyBoundary | 331 --MyBoundary |
331 Content-Disposition: form-data; name="textb" | 332 Content-Disposition: form-data; name="textb" |
332 | 333 |
333 ` + textbValue + ` | 334 ` + textbValue + ` |
334 --MyBoundary-- | 335 --MyBoundary-- |
335 ` | 336 ` |
OLD | NEW |